Files
pj_h5_cloud/src/pages/tabbar/my.vue
T
2025-03-26 16:55:39 +08:00

288 lines
8.4 KiB
Vue

<template>
<crud-layout bg="/static/page/blue-bg.png">
<view class="h-20rpx" />
<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-20rpx 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 { userApi } from "@@/api"
import isDevEnv from "@/sheep/utils/isDevEnv"
import { usePayStore } from "@/sheep/stores/pay"
// import dd from "gdt-jsapi"
interface MenuItem {
text: string
icon: string
router: string
}
const state = reactive<{ wallet: any; userInfo: any; menuList: MenuItem[] }>({
wallet: [],
userInfo: {},
menuList: [
{ text: "扫一扫", icon: "/static/icon/scan.png", router: "" },
{ 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 !== 0) return false
// const userInfo = await (window as any).CP2.getUserInfo()
// const { accountId, sessionId } = userInfo.data
// return doLogin(1, null, accountId, sessionId)
return false
},
// 浙政钉登录
zhengdingLogin() {
const params = new URL(window.location.href).searchParams
const token = params.get("uabaseToken")
if (token) {
return doLogin(2, token, null, null)
}
return false
},
}
// 检查应用环境并登录
const checkAppInfo = async () => {
try {
// 先尝试诗画登录
const shihuaResult = await loginMethods.shihuaLogin()
if (shihuaResult) return
// 尝试浙政钉登录
const zhengdingResult = loginMethods.zhengdingLogin()
if (zhengdingResult) return
uni.showToast({ title: "当前登录环境未知", icon: "none" })
} catch (error) {
console.error("登录失败:", error)
uni.showToast({ title: "登录失败", icon: "none" })
}
}
// 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)
switch (loginType) {
case 1:
// 诗画
// 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" })
}
}
break
default:
uni.showToast({ title: "当前登录环境未知", icon: "none" })
}
}
const doLogout = async () => {
const isLogin = await hooks.useCheckLogin()
if (!isLogin) return
uni.showLoading({
title: "退出登录中...",
mask: true,
})
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
uni.removeStorageSync(tokenKey)
uni.removeStorageSync(userInfoKey)
state.userInfo = {}
uni.hideLoading()
uni.showToast({ title: "退出登录成功", icon: "none" })
}
// 扫一扫
const doScanCode = async () => {
usePayStore().setPayData({})
try {
let decodedString: string | undefined = ""
if (isDevEnv()) {
decodedString = decodeBase64("eyJpZCI6IjE5MDQ3MDUzMDEwNTQ2NzY5OTMiLCJuYW1lIjoi5paw5Y6/5bqc6LaF5biCIn0=")
uni.showToast({ title: "开发环境不是浙政钉环境,扫码仅供测试,收款商家为新县府超市", icon: "none" })
} else {
const res: any = await (window as any).dd.scan({ type: "all" })
decodedString = decodeBase64(res.text)
}
if (decodedString) {
const decodedJson = JSON.parse(decodedString)
if (decodedJson?.id && decodedJson?.name) {
usePayStore().setPayData(decodedJson)
router.jump("/pages/pay/scanPay")
} else {
uni.showToast({
title: "无效的二维码",
icon: "none",
})
}
} else {
uni.showToast({
title: "无效的二维码",
icon: "none",
})
}
} catch (error: any) {
if (error) {
uni.showToast({
title: error,
icon: "none",
})
}
}
}
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) {
if (v.text === "扫一扫") {
await doScanCode()
} else {
router.jump(v.router)
}
}
}
// 解码base64
const decodeBase64 = (base64String: string) => {
try {
const decodedBinaryString = atob(base64String)
const uint8Array = new Uint8Array(decodedBinaryString.length)
for (let i = 0; i < decodedBinaryString.length; i++) {
uint8Array[i] = decodedBinaryString.charCodeAt(i)
}
const decoder = new TextDecoder("utf-8")
const decodedString = decoder.decode(uint8Array)
console.log("解码结果", decodedString)
return decodedString
} catch (error) {
console.error("Base64 解码失败:", error)
uni.showToast({
title: "解码失败,无效的二维码",
icon: "none",
})
}
}
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(() => {
const app = getApp()
app._initTabbar()
isDevEnv()
? state.menuList.push({
text: "测试登录",
icon: "/static/icon/login.png",
router: "/pages/test/login",
})
: ""
// checkAppInfo()
})
onShow(() => {
getStorageUserInfo()
})
</script>
<style lang="scss" scoped>
.service_item:not(:last-child) {
padding-bottom: 40rpx;
border-bottom: 1px solid #fafafa;
margin-bottom: 40rpx;
}
</style>