refactor(store): 将localStorage替换为uni存储API

feat(login): 添加微信登录功能并获取openId
fix(env): 关闭生产环境console删除功能
style(me): 移除未使用的hover和active样式
refactor(token): 修改getToken返回测试值
This commit is contained in:
dzq 2025-11-04 15:17:42 +08:00
parent 75de18ccfc
commit 70bf8121b1
6 changed files with 44 additions and 38 deletions

2
env/.env.production vendored
View File

@ -1,7 +1,7 @@
# 变量必须以 VITE_ 为前缀才能暴露给外部读取 # 变量必须以 VITE_ 为前缀才能暴露给外部读取
NODE_ENV = 'production' NODE_ENV = 'production'
# 是否去除console 和 debugger # 是否去除console 和 debugger
VITE_DELETE_CONSOLE = true VITE_DELETE_CONSOLE = false
# 是否开启sourcemap # 是否开启sourcemap
VITE_SHOW_SOURCEMAP = false VITE_SHOW_SOURCEMAP = false

View File

@ -8,12 +8,13 @@ import {isPageTabbar} from "@/tabbar/store";
import {tabbarList} from "@/tabbar/config"; import {tabbarList} from "@/tabbar/config";
import { useWxStore } from './pinia/stores/wx'; import { useWxStore } from './pinia/stores/wx';
import { mpCodeToOpenId } from './api/users';
const redirectUrl = ref('') const redirectUrl = ref('')
const wxStore = useWxStore(); const wxStore = useWxStore();
onLoad((options) => { onLoad(async (options) => {
console.log('login options: ', options) console.log('login options: ', options);
if (options.redirect) { if (options.redirect) {
redirectUrl.value = ensureDecodeURIComponent(options.redirect) redirectUrl.value = ensureDecodeURIComponent(options.redirect)
} }
@ -28,7 +29,6 @@ onLaunch( (options) => {
}) })
onMounted(async () => { onMounted(async () => {
await wxStore.fakeQyLogin()
}) })
onShow((options) => { onShow((options) => {

View File

@ -6,6 +6,7 @@ import { useCartStore } from '@/pinia/stores/cart'
import { getShopListApi } from '@/api/shop' import { getShopListApi } from '@/api/shop'
import type { ShopEntity } from '@/api/shop/types' import type { ShopEntity } from '@/api/shop/types'
import ProductContainer from './components/product-container.vue'; import ProductContainer from './components/product-container.vue';
import { mpCodeToOpenId } from '@/api/users'
definePage({ definePage({
style: { style: {
@ -25,6 +26,18 @@ const shopId = ref<number>(0)
// //
onMounted(async () => { 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) { if (showShopList.value) {
try { try {
// handleWxCallback // handleWxCallback

View File

@ -378,14 +378,6 @@ onMounted(() => {
transition: left 0.5s ease; 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 { wd-icon {
position: relative; position: relative;

View File

@ -1,6 +1,7 @@
import { pinia } from "@/pinia" import { pinia } from "@/pinia"
import { LoginData } from "@/api/ab98/types" import { LoginData } from "@/api/ab98/types"
import { defineStore } from "pinia" import { defineStore } from "pinia"
import { ref } from "vue"
// 本地存储键名常量 // 本地存储键名常量
const STORAGE_KEYS = { const STORAGE_KEYS = {
@ -20,24 +21,24 @@ const STORAGE_KEYS = {
*/ */
export const useAb98UserStore = defineStore("ab98User", () => { export const useAb98UserStore = defineStore("ab98User", () => {
// 用户面部图像URL // 用户面部图像URL
const storedFace = localStorage.getItem(STORAGE_KEYS.FACE) const storedFace = uni.getStorageSync(STORAGE_KEYS.FACE)
const face_img = ref<string>(storedFace ? decodeURIComponent(storedFace) : '') const face_img = ref<string>(storedFace ? decodeURIComponent(storedFace) : '')
// 用户性别(男/女) // 用户性别(男/女)
const storedSex = localStorage.getItem(STORAGE_KEYS.SEX) const storedSex = uni.getStorageSync(STORAGE_KEYS.SEX)
const sex = ref<string>(storedSex ? decodeURIComponent(storedSex) : '') const sex = ref<string>(storedSex ? decodeURIComponent(storedSex) : '')
// 用户真实姓名 // 用户真实姓名
const storedName = localStorage.getItem(STORAGE_KEYS.NAME) const storedName = uni.getStorageSync(STORAGE_KEYS.NAME)
const name = ref<string>(storedName ? decodeURIComponent(storedName) : '') const name = ref<string>(storedName ? decodeURIComponent(storedName) : '')
// AB98系统用户唯一标识 // AB98系统用户唯一标识
const storedUserId = localStorage.getItem(STORAGE_KEYS.USERID) const storedUserId = uni.getStorageSync(STORAGE_KEYS.USERID)
const userid = ref<string>(storedUserId ? decodeURIComponent(storedUserId) : "") const userid = ref<string>(storedUserId ? decodeURIComponent(storedUserId) : "")
// 是否已完成注册流程 // 是否已完成注册流程
const registered = ref<boolean>(JSON.parse(localStorage.getItem(STORAGE_KEYS.REGISTERED) || "false")) const registered = ref<boolean>(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<string>(storedTel ? decodeURIComponent(storedTel) : "") const tel = ref<string>(storedTel ? decodeURIComponent(storedTel) : "")
// 用户认证令牌 // 用户认证令牌
const storedToken = localStorage.getItem(STORAGE_KEYS.TOKEN) const storedToken = uni.getStorageSync(STORAGE_KEYS.TOKEN)
const token = ref<string>(storedToken ? decodeURIComponent(storedToken) : ""); const token = ref<string>(storedToken ? decodeURIComponent(storedToken) : "");
const tokenLogin = ref<string>(""); const tokenLogin = ref<string>("");
@ -45,7 +46,7 @@ export const useAb98UserStore = defineStore("ab98User", () => {
const loginCode = '1'; // 默认验证码 const loginCode = '1'; // 默认验证码
// 用户登录状态 // 用户登录状态
const isLogin = ref<boolean>(false); const isLogin = ref<boolean>(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; isLogin.value = tel.value && storedLoginCode === String(loginCode) ? true : false;
/** /**
@ -54,18 +55,18 @@ export const useAb98UserStore = defineStore("ab98User", () => {
*/ */
const setUserInfo = (data: LoginData) => { const setUserInfo = (data: LoginData) => {
face_img.value = data.face_img 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 sex.value = data.sex
localStorage.setItem(STORAGE_KEYS.SEX, encodeURIComponent(data.sex)) uni.setStorageSync(STORAGE_KEYS.SEX, encodeURIComponent(data.sex))
name.value = data.name name.value = data.name
localStorage.setItem(STORAGE_KEYS.NAME, encodeURIComponent(data.name)) uni.setStorageSync(STORAGE_KEYS.NAME, encodeURIComponent(data.name))
userid.value = data.userid userid.value = data.userid
localStorage.setItem(STORAGE_KEYS.USERID, encodeURIComponent(data.userid)) uni.setStorageSync(STORAGE_KEYS.USERID, encodeURIComponent(data.userid))
registered.value = data.registered 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 tel.value = data.tel
localStorage.setItem(STORAGE_KEYS.TEL, encodeURIComponent(data.tel)) uni.setStorageSync(STORAGE_KEYS.TEL, encodeURIComponent(data.tel))
localStorage.setItem(STORAGE_KEYS.LOGIN_CODE, encodeURIComponent(loginCode)) uni.setStorageSync(STORAGE_KEYS.LOGIN_CODE, encodeURIComponent(loginCode))
} }
/** /**
@ -74,19 +75,19 @@ export const useAb98UserStore = defineStore("ab98User", () => {
*/ */
const clearUserInfo = () => { const clearUserInfo = () => {
face_img.value = "" face_img.value = ""
localStorage.removeItem(STORAGE_KEYS.FACE) uni.removeStorageSync(STORAGE_KEYS.FACE)
sex.value = "" sex.value = ""
localStorage.removeItem(STORAGE_KEYS.SEX) uni.removeStorageSync(STORAGE_KEYS.SEX)
name.value = "" name.value = ""
localStorage.removeItem(STORAGE_KEYS.NAME) uni.removeStorageSync(STORAGE_KEYS.NAME)
userid.value = "" userid.value = ""
localStorage.removeItem(STORAGE_KEYS.USERID) uni.removeStorageSync(STORAGE_KEYS.USERID)
registered.value = false registered.value = false
localStorage.removeItem(STORAGE_KEYS.REGISTERED) uni.removeStorageSync(STORAGE_KEYS.REGISTERED)
tel.value = "" tel.value = ""
localStorage.removeItem(STORAGE_KEYS.TEL) uni.removeStorageSync(STORAGE_KEYS.TEL)
localStorage.removeItem(STORAGE_KEYS.TOKEN) uni.removeStorageSync(STORAGE_KEYS.TOKEN)
localStorage.removeItem(STORAGE_KEYS.LOGIN_CODE) uni.removeStorageSync(STORAGE_KEYS.LOGIN_CODE)
} }
/** /**
@ -94,12 +95,12 @@ export const useAb98UserStore = defineStore("ab98User", () => {
* @param value - JWT格式的认证令牌 * @param value - JWT格式的认证令牌
*/ */
const setToken = (value: string) => { const setToken = (value: string) => {
localStorage.setItem(STORAGE_KEYS.TOKEN, encodeURIComponent(value)) uni.setStorageSync(STORAGE_KEYS.TOKEN, encodeURIComponent(value))
token.value = value token.value = value
} }
const setTel = (value: string) => { const setTel = (value: string) => {
localStorage.setItem(STORAGE_KEYS.TEL, btoa(value)) uni.setStorageSync(STORAGE_KEYS.TEL, btoa(value))
tel.value = value tel.value = value
} }

View File

@ -7,7 +7,7 @@ import { TOKEN_CACHE_NAME,TOKEN_CACHE_NAME_UNIFY } from '@/config/setting';
* token * token
*/ */
export function getToken(): string | null { export function getToken(): string | null {
return null; return 'token';
const token = uni.getStorageSync(TOKEN_CACHE_NAME) const token = uni.getStorageSync(TOKEN_CACHE_NAME)
return token; return token;
} }