|
|
@@ -9,6 +9,7 @@ import {
|
|
|
logout as _logout,
|
|
|
refreshToken as _refreshToken,
|
|
|
wxLogin as _wxLogin,
|
|
|
+ addInviteConversion,
|
|
|
getUserProfile,
|
|
|
getWxCode,
|
|
|
} from '@/api/login'
|
|
|
@@ -34,6 +35,11 @@ export const useTokenStore = defineStore(
|
|
|
() => {
|
|
|
// 定义用户信息
|
|
|
const tokenInfo = ref<IAuthLoginRes>({ ...tokenInfoState })
|
|
|
+
|
|
|
+ const isShareEnter = computed(() => {
|
|
|
+ return uni.getStorageSync('shareParams') !== ''
|
|
|
+ })
|
|
|
+
|
|
|
// 设置用户信息
|
|
|
const setTokenInfo = (val: IAuthLoginRes) => {
|
|
|
tokenInfo.value = val
|
|
|
@@ -55,8 +61,8 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断token是否过期
|
|
|
- */
|
|
|
+ * 判断token是否过期
|
|
|
+ */
|
|
|
const isTokenExpired = computed(() => {
|
|
|
if (!tokenInfo.value) {
|
|
|
return true
|
|
|
@@ -71,8 +77,8 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 判断refreshToken是否过期
|
|
|
- */
|
|
|
+ * 判断refreshToken是否过期
|
|
|
+ */
|
|
|
const isRefreshTokenExpired = computed(() => {
|
|
|
if (!isDoubleTokenMode)
|
|
|
return true
|
|
|
@@ -86,9 +92,9 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 登录成功后处理逻辑
|
|
|
- * @param tokenInfo 登录返回的token信息
|
|
|
- */
|
|
|
+ * 登录成功后处理逻辑
|
|
|
+ * @param tokenInfo 登录返回的token信息
|
|
|
+ */
|
|
|
async function _postLogin(tokenInfo: IAuthLoginRes) {
|
|
|
setTokenInfo(tokenInfo)
|
|
|
// const userStore = useUserStore()
|
|
|
@@ -96,12 +102,12 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 用户登录
|
|
|
- * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
- * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
- * @param loginForm 登录参数
|
|
|
- * @returns 登录结果
|
|
|
- */
|
|
|
+ * 用户登录
|
|
|
+ * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
+ * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
+ * @param loginForm 登录参数
|
|
|
+ * @returns 登录结果
|
|
|
+ */
|
|
|
const login = async (loginForm: ILoginForm) => {
|
|
|
try {
|
|
|
const res = await _login(loginForm)
|
|
|
@@ -124,11 +130,11 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 微信登录
|
|
|
- * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
- * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
- * @returns 登录结果
|
|
|
- */
|
|
|
+ * 微信登录
|
|
|
+ * 有的时候后端会用一个接口返回token和用户信息,有的时候会分开2个接口,一个获取token,一个获取用户信息
|
|
|
+ * (各有利弊,看业务场景和系统复杂度),这里使用2个接口返回的来模拟
|
|
|
+ * @returns 登录结果
|
|
|
+ */
|
|
|
const wxLogin = async () => {
|
|
|
try {
|
|
|
// 获取用户信息
|
|
|
@@ -138,6 +144,9 @@ export const useTokenStore = defineStore(
|
|
|
const code = await getWxCode()
|
|
|
console.log('微信登录-code: ', code.code)
|
|
|
const res = await _wxLogin(code.code)
|
|
|
+ if (isShareEnter.value) {
|
|
|
+ await addInviteConversion(JSON.parse(uni.getStorageSync('shareParams')))
|
|
|
+ }
|
|
|
console.log('微信登录-res: ', res)
|
|
|
await _postLogin(res)
|
|
|
uni.showToast({
|
|
|
@@ -157,8 +166,8 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 退出登录 并 删除用户信息
|
|
|
- */
|
|
|
+ * 退出登录 并 删除用户信息
|
|
|
+ */
|
|
|
const logout = async () => {
|
|
|
try {
|
|
|
// TODO 实现自己的退出登录逻辑
|
|
|
@@ -189,9 +198,9 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 刷新token
|
|
|
- * @returns 刷新结果
|
|
|
- */
|
|
|
+ * 刷新token
|
|
|
+ * @returns 刷新结果
|
|
|
+ */
|
|
|
const refreshToken = async () => {
|
|
|
if (!isDoubleTokenMode) {
|
|
|
console.error('单token模式不支持刷新token')
|
|
|
@@ -217,10 +226,10 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取有效的token
|
|
|
- * 注意:在computed中不直接调用异步函数,只做状态判断
|
|
|
- * 实际的刷新操作应由调用方处理
|
|
|
- */
|
|
|
+ * 获取有效的token
|
|
|
+ * 注意:在computed中不直接调用异步函数,只做状态判断
|
|
|
+ * 实际的刷新操作应由调用方处理
|
|
|
+ */
|
|
|
const getValidToken = computed(() => {
|
|
|
// token已过期,返回空
|
|
|
if (isTokenExpired.value) {
|
|
|
@@ -236,8 +245,8 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 检查是否有登录信息(不考虑token是否过期)
|
|
|
- */
|
|
|
+ * 检查是否有登录信息(不考虑token是否过期)
|
|
|
+ */
|
|
|
const hasLoginInfo = computed(() => {
|
|
|
if (!tokenInfo.value) {
|
|
|
return false
|
|
|
@@ -251,17 +260,17 @@ export const useTokenStore = defineStore(
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 检查是否已登录且token有效
|
|
|
- */
|
|
|
+ * 检查是否已登录且token有效
|
|
|
+ */
|
|
|
const hasValidLogin = computed(() => {
|
|
|
console.log('hasValidLogin', hasLoginInfo.value && !isTokenExpired.value, hasLoginInfo.value, !isTokenExpired.value)
|
|
|
return hasLoginInfo.value && !isTokenExpired.value
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
- * 尝试获取有效的token,如果过期且可刷新,则刷新token
|
|
|
- * @returns 有效的token或空字符串
|
|
|
- */
|
|
|
+ * 尝试获取有效的token,如果过期且可刷新,则刷新token
|
|
|
+ * @returns 有效的token或空字符串
|
|
|
+ */
|
|
|
const tryGetValidToken = async (): Promise<string> => {
|
|
|
if (!getValidToken.value && isDoubleTokenMode && !isRefreshTokenExpired.value) {
|
|
|
try {
|
|
|
@@ -275,6 +284,10 @@ export const useTokenStore = defineStore(
|
|
|
}
|
|
|
return getValidToken.value
|
|
|
}
|
|
|
+ // 分享进入首页时,缓存分享参数
|
|
|
+ const cacheShareParams = (params: Record<string, string>) => {
|
|
|
+ uni.setStorageSync('shareParams', JSON.stringify(params))
|
|
|
+ }
|
|
|
|
|
|
return {
|
|
|
// 核心API方法
|
|
|
@@ -293,7 +306,8 @@ export const useTokenStore = defineStore(
|
|
|
// 调试或特殊场景可能需要直接访问的信息
|
|
|
tokenInfo,
|
|
|
setTokenInfo,
|
|
|
- cleanToken
|
|
|
+ cleanToken,
|
|
|
+ cacheShareParams
|
|
|
}
|
|
|
},
|
|
|
{
|