修改token刷新

This commit is contained in:
weiwei committed 2025-02-19 16:44:30 +08:00
1 parent 2adaae4151
commit e15e84aecf
4 files changed
+238 -257

No files matched your search

+148 -172
View File
@@ -1,44 +1,84 @@
<template>
<crud-layout title="我的" bg="/static/page/blue-bg.png">
<view class="flex-row items-center">
<image
v-if="state.userInfo?.sysUser?.avatar"
class="w-100rpx h-100rpx rounded-full"
:src="filePath(state.userInfo.sysUser.avatar)"
mode="aspectFill"
/>
<image v-else class="w-100rpx h-100rpx rounded-full" src="/static/icon/default-avatar.png" mode="aspectFill" />
<view class="m-l-30rpx" v-if="state.userInfo?.sysUser?.userId">
<view class="flex-row items-center">
<text class="sub-text-30">{{ state.userInfo?.sysUser?.name }}</text>
<text class="iconfont icon-youjiantou color-#666666 m-l-20rpx"></text>
</view>
<text class="sub-text-26">ID {{ state.userInfo?.sysUser?.userId }}</text>
</view>
<view v-else class="flex-row items-center m-l-30rpx" @tap="onTabLogin">
<text class="sub-text-32">请先登录</text>
<text class="iconfont icon-youjiantou" />
</view>
</view>
<view class="m-t-40rpx rounded-20rpx shadow-main p-30rpx p-b-40rpx bg-white!">
<view class="service_item flex-row items-center justify-between" v-for="(v, i) in state.menuList" :key="i" @tap="onTabMenu(v, i)">
<view class="flex-row-center">
<image class="w-40rpx h-40rpx" :src="v.icon" mode="aspectFill" />
<text class="sub-text-28 m-l-20rpx">{{ v.text }}</text>
</view>
<view class="iconfont icon-youjiantou" />
</view>
</view>
<view v-if="isDevEnv()" class="flex-col-center m-t-40rpx m-x-30rpx">
<view class="w-full text-center sub-text-28 p-y-20rpx rounded-40rpx border-solid border border-#0094ff" @tap="doLogout">
退出登录
</view>
</view>
</crud-layout>
</template>
<script setup lang="ts">
import { reactive } from "vue"
import { onLoad, onShow } from "@dcloudio/uni-app"
import { isEmpty } from "radash"
import { filePath } from "@@/utils/file"
import { hooks, router, constant } from "@@/index"
import { merchantApi, userApi, walletApi } from "@@/api"
import { userApi } from "@@/api"
import isDevEnv from "@/sheep/utils/isDevEnv"
// 定义类型
interface ServiceItem {
interface MenuItem {
text: string
icon: string
router: string
}
// 服务列表配置
const DEFAULT_SERVICES: ServiceItem[] = [
{ text: "钱包管理", icon: "/static/icon/service_3.png", router: "/pages/wallet/list" },
{ text: "包厢预定", icon: "/static/icon/service_4.png", router: "/pages/boxes/apply-list" },
{ text: "外来人员就餐", icon: "/static/icon/service_1.png", router: "/pages/apply/foreign-list" },
{ text: "跨单位就餐", icon: "/static/icon/service_1.png", router: "/pages/apply/stride-list" },
{ text: "人脸管理", icon: "/static/icon/service_5.png", router: "/pages/human-face/list" },
]
const state = reactive<any>({
const state = reactive<{ wallet: any; userInfo: any; menuList: MenuItem[] }>({
wallet: [],
userInfo: {},
serviceList: [...DEFAULT_SERVICES],
menuList: [
{ text: "钱包管理", icon: "/static/icon/service_3.png", router: "/pages/wallet/list" },
{ text: "包厢预定", icon: "/static/icon/service_4.png", router: "/pages/boxes/apply-list" },
{ text: "外来人员就餐", icon: "/static/icon/service_1.png", router: "/pages/apply/foreign-list" },
{ text: "跨单位就餐", icon: "/static/icon/service_1.png", router: "/pages/apply/stride-list" },
{ text: "人脸管理", icon: "/static/icon/service_5.png", router: "/pages/human-face/list" },
],
})
const onTabLogin = () => (isDevEnv() ? router.jump("/pages/test/login") : checkAppInfo())
// 登录相关方法
const loginMethods = {
// 诗画环境登录
async shihuaLogin() {
const appInfo = await (window as any).CP2.getAppInfo()
if (appInfo.code !== 1) return false
// const appInfo = await (window as any).CP2.getAppInfo()
// if (appInfo.code !== 0) return false
const userInfo = await (window as any).CP2.getUserInfo()
const { accountId, sessionId } = userInfo.data
return doLogin(1, null, accountId, sessionId)
// const userInfo = await (window as any).CP2.getUserInfo()
// const { accountId, sessionId } = userInfo.data
// return doLogin(1, null, accountId, sessionId)
return false
},
// 浙政钉登录
@@ -58,120 +98,60 @@ const checkAppInfo = async () => {
// 先尝试诗画登录
const shihuaResult = await loginMethods.shihuaLogin()
if (shihuaResult) return
// 尝试浙政钉登录
const zhengdingResult = await loginMethods.zhengdingLogin()
const zhengdingResult = loginMethods.zhengdingLogin()
if (zhengdingResult) return
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
const tokenStorage = uni.getStorageSync(tokenKey)
const userStorage = uni.getStorageSync(userInfoKey)
// 开发环境下跳转到测试登录页
if ((isDevEnv() && isEmpty(tokenStorage)) || isEmpty(userStorage)) {
router.jump("/pages/test/login")
}
uni.showToast({ title: "当前登录环境未知", icon: "none" })
} catch (error) {
console.error("登录失败:", error)
uni.showToast({ title: "登录失败", icon: "none" })
}
}
onLoad(() => {
if (isDevEnv()) {
state.serviceList.push({
text: "测试登录",
icon: "/static/icon/login.png",
router: "/pages/test/login",
})
}
checkAppInfo()
})
onShow(() => {
getUserInfo()
})
const getUserInfo = () => {
const userInfoKey = constant.storageConstant.USERINFO
const userStorage = uni.getStorageSync(userInfoKey)
if (!isEmpty(userStorage)) {
state.userInfo = userStorage
} else {
state.userInfo = {}
}
}
// {
// "accessToken": {
// "tokenValue": "06d43795-a3ef-4019-a70f-25830e7183a4",
// "issuedAt": "2025-01-24T11:10:30.423604599Z",
// "expiresAt": "2025-01-25T11:10:30.423604599Z",
// "tokenType": {
// "value": "Bearer"
// },
// "scopes": []
// },
// "refreshToken": {
// "tokenValue": "FmRVP4l3d7t3263h9LeKnV8hdU0lPasMuI1wank_EbylRQPNLm7XP4_OtUV-zH9AbMZc9f14Qk2IXhw-33fWMGGKipVY8crbvVyWV5DeAcfeU8fYyQuNDh9rw3Dq0w75",
// "issuedAt": "2025-01-24T11:10:30.423699280Z",
// "expiresAt": "2025-01-31T11:10:30.423699280Z"
// }
// }
const doLogin = async (loginType, token, accountId, sessionId) => {
// loginType 1:诗画登录 2:浙政钉登录
const doLogin = async (loginType: number, token: string, accountId: string | null, sessionId: string | null) => {
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
const tokenStorage = uni.getStorageSync(tokenKey)
const userStorage = uni.getStorageSync(userInfoKey)
if (isEmpty(tokenStorage) || isEmpty(userStorage)) {
uni.showLoading({
title: "登录中...",
mask: true,
})
let authData
if (loginType == 1) {
switch (loginType) {
case 1:
// 诗画
const params = {
accountId: accountId,
sessionId: sessionId,
// const params = {
// accountId: accountId,
// sessionId: sessionId,
// }
// const response = await userApi.authsh(params)
// authData = response.data
break
case 2:
if (isEmpty(tokenStorage) || isEmpty(userStorage)) {
try {
uni.showLoading({
title: "登录中...",
mask: true,
})
const res = await userApi.auth(token)
if (res?.code === 0) {
uni.setStorageSync(tokenKey, res.data)
uni.hideLoading()
uni.showToast({ title: "登录成功", icon: "none" })
const { data: userData } = await userApi.userInfo()
uni.setStorageSync(userInfoKey, userData)
getStorageUserInfo()
} else {
uni.hideLoading()
uni.showToast({ title: "获取浙政钉用户信息失败", icon: "none" })
}
} catch (error) {
uni.hideLoading()
uni.showToast({ title: "错误", icon: "none" })
}
}
const response = await userApi.authsh(params)
authData = response.data
} else if (loginType == 2) {
//浙政钉
const response: any = await userApi.auth(token)
if (!response) return
authData = response.data
} else {
//后期手机号+验证码登录
if (isDevEnv()) router.jump("/pages/test/login")
uni.hideLoading()
return
}
// const {data: authData} = await userApi.auth(token);
console.log("authData", authData)
uni.setStorageSync(tokenKey, authData)
try {
const { data: userData } = await userApi.userInfo()
uni.setStorageSync(userInfoKey, userData)
getUserInfo()
uni.hideLoading()
// uni.showToast({ title: "登录成功", icon: "none" })
} catch (error) {
uni.hideLoading()
uni.showToast({ title: "登录失败", icon: "none" })
}
}
}
const toService = async (v: any, i: number) => {
if (i == state.serviceList.length - 1) {
router.jump(v.router)
return
}
const isLogin = await hooks.useCheckLogin()
if (isLogin) {
router.jump(v.router)
break
default:
uni.showToast({ title: "当前登录环境未知", icon: "none" })
}
}
@@ -191,64 +171,60 @@ const doLogout = async () => {
uni.showToast({ title: "退出登录成功", icon: "none" })
}
const onTabLogin = () => {
if (isDevEnv()) {
router.jump("/pages/test/login")
} else {
checkAppInfo()
const onTabMenu = async (v: MenuItem, i: number) => {
if (i == state.menuList.length - 1) {
router.jump(v.router)
return
}
const isLogin = await hooks.useCheckLogin()
if (isLogin) {
router.jump(v.router)
}
}
const getStorageUserInfo = () => {
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
const tokenStorage = uni.getStorageSync(tokenKey)
const userStorage = uni.getStorageSync(userInfoKey)
if (!isEmpty(userStorage) && !isEmpty(tokenStorage)) {
state.userInfo = userStorage
} else {
state.userInfo = {}
}
}
onLoad(() => {
isDevEnv()
? state.menuList.push({
text: "测试登录",
icon: "/static/icon/login.png",
router: "/pages/test/login",
})
: checkAppInfo()
})
onShow(() => {
getStorageUserInfo()
})
// {
// "accessToken": {
// "tokenValue": "06d43795-a3ef-4019-a70f-25830e7183a4",
// "issuedAt": "2025-01-24T11:10:30.423604599Z",
// "expiresAt": "2025-01-25T11:10:30.423604599Z",
// "tokenType": {
// "value": "Bearer"
// },
// "scopes": []
// },
// "refreshToken": {
// "tokenValue": "FmRVP4l3d7t3263h9LeKnV8hdU0lPasMuI1wank_EbylRQPNLm7XP4_OtUV-zH9AbMZc9f14Qk2IXhw-33fWMGGKipVY8crbvVyWV5DeAcfeU8fYyQuNDh9rw3Dq0w75",
// "issuedAt": "2025-01-24T11:10:30.423699280Z",
// "expiresAt": "2025-01-31T11:10:30.423699280Z"
// }
// }
</script>
<template>
<crud-layout title="我的" bg="/static/page/blue-bg.png">
<view class="flex-row items-center">
<image
class="w-100rpx h-100rpx rounded-full"
v-if="state.userInfo?.sysUser?.avatar"
:src="filePath(state.userInfo.sysUser.avatar)"
mode="aspectFill"
/>
<image class="w-100rpx h-100rpx rounded-full" v-else src="/static/icon/default-avatar.png" mode="aspectFill" />
<view class="m-l-30rpx" v-if="state.userInfo?.sysUser?.userId">
<view class="flex-row items-center">
<text class="sub-text-30">{{ state.userInfo?.sysUser?.name }}</text>
<text class="iconfont icon-youjiantou color-#666666 m-l-20rpx"></text>
</view>
<text class="sub-text-26">ID {{ state.userInfo?.sysUser?.userId }}</text>
</view>
<view v-else class="flex-row items-center m-l-30rpx" @tap="onTabLogin">
<text class="sub-text-32">请先登录</text>
<text class="iconfont icon-youjiantou"></text>
</view>
</view>
<view class="m-t-40rpx rounded-20rpx shadow-main p-30rpx p-b-40rpx bg-white!">
<view
class="service_item flex-row items-center justify-between"
v-for="(v, i) in state.serviceList"
:key="i"
@tap="toService(v, i)"
>
<view class="flex-row-center">
<image class="w-40rpx h-40rpx" :src="v.icon" mode="aspectFill" />
<text class="sub-text-28 m-l-20rpx">{{ v.text }}</text>
</view>
<view class="iconfont icon-youjiantou"></view>
</view>
</view>
<view v-if="isDevEnv()" class="flex-col-center m-t-40rpx m-x-30rpx">
<view
class="w-full text-center sub-text-28 p-y-20rpx rounded-40rpx border-solid border border-#0094ff"
@tap="doLogout"
>
退出登录
</view>
</view>
</crud-layout>
</template>
<style lang="scss" scoped>
.service_item:not(:last-child) {
padding-bottom: 40rpx;