From 70bf8121b1dfbe4957b7076c242e7d3f4e06ca03 Mon Sep 17 00:00:00 2001 From: dzq Date: Tue, 4 Nov 2025 15:17:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(store):=20=E5=B0=86localStorage?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=BAuni=E5=AD=98=E5=82=A8API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat(login): 添加微信登录功能并获取openId fix(env): 关闭生产环境console删除功能 style(me): 移除未使用的hover和active样式 refactor(token): 修改getToken返回测试值 --- env/.env.production | 2 +- src/App.vue | 6 ++--- src/pages/index/index.vue | 13 +++++++++ src/pages/me/index.vue | 8 ------ src/pinia/stores/ab98-user.ts | 51 ++++++++++++++++++----------------- src/utils/token-util.ts | 2 +- 6 files changed, 44 insertions(+), 38 deletions(-) diff --git a/env/.env.production b/env/.env.production index 73d3a5b..6135d8d 100644 --- a/env/.env.production +++ b/env/.env.production @@ -1,7 +1,7 @@ # 变量必须以 VITE_ 为前缀才能暴露给外部读取 NODE_ENV = 'production' # 是否去除console 和 debugger -VITE_DELETE_CONSOLE = true +VITE_DELETE_CONSOLE = false # 是否开启sourcemap VITE_SHOW_SOURCEMAP = false diff --git a/src/App.vue b/src/App.vue index 9ebc095..4413537 100644 --- a/src/App.vue +++ b/src/App.vue @@ -8,12 +8,13 @@ import {isPageTabbar} from "@/tabbar/store"; import {tabbarList} from "@/tabbar/config"; import { useWxStore } from './pinia/stores/wx'; +import { mpCodeToOpenId } from './api/users'; const redirectUrl = ref('') const wxStore = useWxStore(); -onLoad((options) => { - console.log('login options: ', options) +onLoad(async (options) => { + console.log('login options: ', options); if (options.redirect) { redirectUrl.value = ensureDecodeURIComponent(options.redirect) } @@ -28,7 +29,6 @@ onLaunch( (options) => { }) onMounted(async () => { - await wxStore.fakeQyLogin() }) onShow((options) => { diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index cf11dc6..356afa3 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -6,6 +6,7 @@ import { useCartStore } from '@/pinia/stores/cart' import { getShopListApi } from '@/api/shop' import type { ShopEntity } from '@/api/shop/types' import ProductContainer from './components/product-container.vue'; +import { mpCodeToOpenId } from '@/api/users' definePage({ style: { @@ -25,6 +26,18 @@ const shopId = ref(0) // 页面加载 onMounted(async () => { + await wxStore.fakeQyLogin(); + uni.login({ + provider: 'weixin', //使用微信登录 + success: function (loginRes) { + mpCodeToOpenId(loginRes.code).then((openId) => { + console.log('openId:', openId) + }).catch((e) => { + console.error('mpCodeToOpenId error:', e) + }) + }, + }); + if (showShopList.value) { try { // 等待 handleWxCallback 完成 diff --git a/src/pages/me/index.vue b/src/pages/me/index.vue index 580c019..420f1d9 100644 --- a/src/pages/me/index.vue +++ b/src/pages/me/index.vue @@ -378,14 +378,6 @@ onMounted(() => { transition: left 0.5s ease; } - &:hover::before { - left: 100%; - } - - &:active { - transform: translateY(2px); - box-shadow: 0 2px 8px rgba(64, 158, 255, 0.4); - } wd-icon { position: relative; diff --git a/src/pinia/stores/ab98-user.ts b/src/pinia/stores/ab98-user.ts index 8609119..4b784fc 100644 --- a/src/pinia/stores/ab98-user.ts +++ b/src/pinia/stores/ab98-user.ts @@ -1,6 +1,7 @@ import { pinia } from "@/pinia" import { LoginData } from "@/api/ab98/types" import { defineStore } from "pinia" +import { ref } from "vue" // 本地存储键名常量 const STORAGE_KEYS = { @@ -20,24 +21,24 @@ const STORAGE_KEYS = { */ export const useAb98UserStore = defineStore("ab98User", () => { // 用户面部图像URL - const storedFace = localStorage.getItem(STORAGE_KEYS.FACE) + const storedFace = uni.getStorageSync(STORAGE_KEYS.FACE) const face_img = ref(storedFace ? decodeURIComponent(storedFace) : '') // 用户性别(男/女) - const storedSex = localStorage.getItem(STORAGE_KEYS.SEX) + const storedSex = uni.getStorageSync(STORAGE_KEYS.SEX) const sex = ref(storedSex ? decodeURIComponent(storedSex) : '') // 用户真实姓名 - const storedName = localStorage.getItem(STORAGE_KEYS.NAME) + const storedName = uni.getStorageSync(STORAGE_KEYS.NAME) const name = ref(storedName ? decodeURIComponent(storedName) : '') // AB98系统用户唯一标识 - const storedUserId = localStorage.getItem(STORAGE_KEYS.USERID) + const storedUserId = uni.getStorageSync(STORAGE_KEYS.USERID) const userid = ref(storedUserId ? decodeURIComponent(storedUserId) : "") // 是否已完成注册流程 - const registered = ref(JSON.parse(localStorage.getItem(STORAGE_KEYS.REGISTERED) || "false")) + const registered = ref(JSON.parse(uni.getStorageSync(STORAGE_KEYS.REGISTERED) || "false")) // 用户绑定手机号 - const storedTel = localStorage.getItem(STORAGE_KEYS.TEL) + const storedTel = uni.getStorageSync(STORAGE_KEYS.TEL) const tel = ref(storedTel ? decodeURIComponent(storedTel) : "") // 用户认证令牌 - const storedToken = localStorage.getItem(STORAGE_KEYS.TOKEN) + const storedToken = uni.getStorageSync(STORAGE_KEYS.TOKEN) const token = ref(storedToken ? decodeURIComponent(storedToken) : ""); const tokenLogin = ref(""); @@ -45,7 +46,7 @@ export const useAb98UserStore = defineStore("ab98User", () => { const loginCode = '1'; // 默认验证码 // 用户登录状态 const isLogin = ref(false); - const storedLoginCode = localStorage.getItem(STORAGE_KEYS.LOGIN_CODE); + const storedLoginCode = uni.getStorageSync(STORAGE_KEYS.LOGIN_CODE); isLogin.value = tel.value && storedLoginCode === String(loginCode) ? true : false; /** @@ -54,18 +55,18 @@ export const useAb98UserStore = defineStore("ab98User", () => { */ const setUserInfo = (data: LoginData) => { face_img.value = data.face_img - localStorage.setItem(STORAGE_KEYS.FACE, encodeURIComponent(data.face_img)) + uni.setStorageSync(STORAGE_KEYS.FACE, encodeURIComponent(data.face_img)) sex.value = data.sex - localStorage.setItem(STORAGE_KEYS.SEX, encodeURIComponent(data.sex)) + uni.setStorageSync(STORAGE_KEYS.SEX, encodeURIComponent(data.sex)) name.value = data.name - localStorage.setItem(STORAGE_KEYS.NAME, encodeURIComponent(data.name)) + uni.setStorageSync(STORAGE_KEYS.NAME, encodeURIComponent(data.name)) userid.value = data.userid - localStorage.setItem(STORAGE_KEYS.USERID, encodeURIComponent(data.userid)) + uni.setStorageSync(STORAGE_KEYS.USERID, encodeURIComponent(data.userid)) registered.value = data.registered - localStorage.setItem(STORAGE_KEYS.REGISTERED, JSON.stringify(data.registered)) + uni.setStorageSync(STORAGE_KEYS.REGISTERED, JSON.stringify(data.registered)) tel.value = data.tel - localStorage.setItem(STORAGE_KEYS.TEL, encodeURIComponent(data.tel)) - localStorage.setItem(STORAGE_KEYS.LOGIN_CODE, encodeURIComponent(loginCode)) + uni.setStorageSync(STORAGE_KEYS.TEL, encodeURIComponent(data.tel)) + uni.setStorageSync(STORAGE_KEYS.LOGIN_CODE, encodeURIComponent(loginCode)) } /** @@ -74,19 +75,19 @@ export const useAb98UserStore = defineStore("ab98User", () => { */ const clearUserInfo = () => { face_img.value = "" - localStorage.removeItem(STORAGE_KEYS.FACE) + uni.removeStorageSync(STORAGE_KEYS.FACE) sex.value = "" - localStorage.removeItem(STORAGE_KEYS.SEX) + uni.removeStorageSync(STORAGE_KEYS.SEX) name.value = "" - localStorage.removeItem(STORAGE_KEYS.NAME) + uni.removeStorageSync(STORAGE_KEYS.NAME) userid.value = "" - localStorage.removeItem(STORAGE_KEYS.USERID) + uni.removeStorageSync(STORAGE_KEYS.USERID) registered.value = false - localStorage.removeItem(STORAGE_KEYS.REGISTERED) + uni.removeStorageSync(STORAGE_KEYS.REGISTERED) tel.value = "" - localStorage.removeItem(STORAGE_KEYS.TEL) - localStorage.removeItem(STORAGE_KEYS.TOKEN) - localStorage.removeItem(STORAGE_KEYS.LOGIN_CODE) + uni.removeStorageSync(STORAGE_KEYS.TEL) + uni.removeStorageSync(STORAGE_KEYS.TOKEN) + uni.removeStorageSync(STORAGE_KEYS.LOGIN_CODE) } /** @@ -94,12 +95,12 @@ export const useAb98UserStore = defineStore("ab98User", () => { * @param value - JWT格式的认证令牌 */ const setToken = (value: string) => { - localStorage.setItem(STORAGE_KEYS.TOKEN, encodeURIComponent(value)) + uni.setStorageSync(STORAGE_KEYS.TOKEN, encodeURIComponent(value)) token.value = value } const setTel = (value: string) => { - localStorage.setItem(STORAGE_KEYS.TEL, btoa(value)) + uni.setStorageSync(STORAGE_KEYS.TEL, btoa(value)) tel.value = value } diff --git a/src/utils/token-util.ts b/src/utils/token-util.ts index ac17ad9..6ee36fe 100644 --- a/src/utils/token-util.ts +++ b/src/utils/token-util.ts @@ -7,7 +7,7 @@ import { TOKEN_CACHE_NAME,TOKEN_CACHE_NAME_UNIFY } from '@/config/setting'; * 获取缓存的token */ export function getToken(): string | null { - return null; + return 'token'; const token = uni.getStorageSync(TOKEN_CACHE_NAME) return token; }