本次修改: 1. 新增分销绑定 API 接口与路由 2. 老用户不再自动绑定推荐人,需手动输入邀请码绑定 3. 新增绑定推荐人 UI 弹层与展示组件 4. 新增防环校验与关系补全逻辑 5. 调整邀请码存储逻辑,仅未登录用户自动存储
87 行
2.7 KiB
JavaScript
87 行
2.7 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(() => {
|
||
if (state.userInfo && (state.userInfo.isMerchant || state.userInfo.merchantStatus === 1)) {
|
||
return true
|
||
}
|
||
return state.userType === config.constants.USER_TYPE.BUSINESS
|
||
})
|
||
const isMember = computed(() => !!(state.userInfo && state.userInfo.isMember))
|
||
const userInfo = computed(() => state.userInfo)
|
||
|
||
/** 微信登录 */
|
||
async function login(payload = {}) {
|
||
const inviteCode = storage.get('fc_invite_code', '') || ''
|
||
const inviterId = storage.get('fc_inviter_id', 0) || 0
|
||
const finalPayload = { ...payload }
|
||
if (inviteCode && !finalPayload.inviteCode) finalPayload.inviteCode = inviteCode
|
||
if (inviterId && !finalPayload.inviterId) finalPayload.inviterId = inviterId
|
||
|
||
const res = await authApi.wxLogin(finalPayload)
|
||
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)
|
||
storage.remove('fc_invite_code')
|
||
storage.remove('fc_inviter_id')
|
||
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,
|
||
userInfo,
|
||
isLogin,
|
||
isBusiness,
|
||
isMember,
|
||
login,
|
||
switchType,
|
||
fetchProfile,
|
||
logout,
|
||
}
|
||
}
|
||
|
||
export default useUserStore
|