本次提交完成多模块真实数据对接,移除前端硬编码假数据: - 新增后端接口:区域列表、未读消息数、详情页评价列表、最低提现金额 - 前端对应修改:移除首页/列表/详情页硬编码区域选项、未读消息数、评价内容,分销页最低提现金额 - 修复登录页微信授权不伪造code、营业执照不伪造文件名,避免绕过校验 - 新增集成测试用例,验证真实数据对接正确性
72 行
2.1 KiB
JavaScript
72 行
2.1 KiB
JavaScript
/**
|
||
* 轻量全局状态(Vue3 reactive,无需引入 Pinia)
|
||
* 负责登录态、用户信息、用户类型的集中管理。
|
||
*/
|
||
import { reactive, computed } from 'vue'
|
||
import config from '../config/index.js'
|
||
import storage from '../utils/storage.js'
|
||
import authApi from '../api/auth.js'
|
||
|
||
const state = reactive({
|
||
token: storage.get(config.storageKeys.token, ''),
|
||
userInfo: storage.get(config.storageKeys.userInfo, null),
|
||
userType: Number(storage.get(config.storageKeys.userType, config.constants.USER_TYPE.PERSON)),
|
||
// 未读消息数由 /message/unread-count 实时拉取,不在此写死
|
||
unread: 0,
|
||
})
|
||
|
||
export const useUserStore = () => {
|
||
const isLogin = computed(() => !!state.token)
|
||
const isBusiness = computed(() => state.userType === config.constants.USER_TYPE.BUSINESS)
|
||
const isMember = computed(() => !!(state.userInfo && state.userInfo.isMember))
|
||
|
||
/** 微信登录 */
|
||
async function login(payload) {
|
||
const res = await authApi.wxLogin(payload)
|
||
state.token = res.token
|
||
state.userInfo = res.userInfo
|
||
state.userType = Number(res.userInfo.userType || config.constants.USER_TYPE.PERSON)
|
||
storage.set(config.storageKeys.token, state.token)
|
||
storage.set(config.storageKeys.userInfo, state.userInfo)
|
||
storage.set(config.storageKeys.userType, state.userType)
|
||
return res
|
||
}
|
||
|
||
/** 切换用户类型 */
|
||
async function switchType(userType, license) {
|
||
const res = await authApi.switchType({ userType, license })
|
||
state.userType = res.userType
|
||
storage.set(config.storageKeys.userType, state.userType)
|
||
return res
|
||
}
|
||
|
||
/** 拉取资料 */
|
||
async function fetchProfile() {
|
||
const info = await authApi.profile()
|
||
state.userInfo = info
|
||
storage.set(config.storageKeys.userInfo, info)
|
||
return info
|
||
}
|
||
|
||
/** 退出登录 */
|
||
function logout() {
|
||
state.token = ''
|
||
state.userInfo = null
|
||
storage.remove(config.storageKeys.token)
|
||
storage.remove(config.storageKeys.userInfo)
|
||
}
|
||
|
||
return {
|
||
state,
|
||
isLogin,
|
||
isBusiness,
|
||
isMember,
|
||
login,
|
||
switchType,
|
||
fetchProfile,
|
||
logout,
|
||
}
|
||
}
|
||
|
||
export default useUserStore
|