“我的”页面修改
This commit is contained in:
1 parent
a2ee2f402c
commit
fcd311ec49
2 files changed
+251
-193
No files matched your search
+247
-190
@@ -57,13 +57,16 @@ 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[]; appInfo: string }>({
|
||||
const state = reactive<{
|
||||
wallet: any[]
|
||||
userInfo: any
|
||||
menuList: {
|
||||
text: string
|
||||
icon: string
|
||||
router: string
|
||||
}[]
|
||||
appInfo: string
|
||||
}>({
|
||||
wallet: [],
|
||||
userInfo: {},
|
||||
menuList: [
|
||||
@@ -76,219 +79,286 @@ const state = reactive<{ wallet: any; userInfo: any; menuList: MenuItem[]; appIn
|
||||
appInfo: "",
|
||||
})
|
||||
|
||||
const onTabLogin = () => (isDevEnv() ? router.jump("/pages/test/login") : checkAppInfo())
|
||||
|
||||
// 检查应用环境并登录
|
||||
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" })
|
||||
}
|
||||
// 常量
|
||||
const STORAGE_KEYS = {
|
||||
TOKEN: constant.storageConstant.TOKEN,
|
||||
USER_INFO: constant.storageConstant.USERINFO,
|
||||
USER_ROLE: constant.storageConstant.USERROLE,
|
||||
}
|
||||
|
||||
// 登录相关方法
|
||||
const loginMethods = {
|
||||
const storage = {
|
||||
// 获取存储的用户信息
|
||||
getUserInfo: () => {
|
||||
const tokenStorage = uni.getStorageSync(STORAGE_KEYS.TOKEN)
|
||||
const userStorage = uni.getStorageSync(STORAGE_KEYS.USER_INFO)
|
||||
|
||||
if (!isEmpty(userStorage) && !isEmpty(tokenStorage)) {
|
||||
state.userInfo = userStorage
|
||||
} else {
|
||||
state.userInfo = {}
|
||||
}
|
||||
},
|
||||
|
||||
// 保存用户信息到存储
|
||||
saveUserInfo: (userData: any) => {
|
||||
uni.setStorageSync(STORAGE_KEYS.USER_INFO, userData)
|
||||
storage.getUserInfo()
|
||||
},
|
||||
|
||||
// 保存token到存储
|
||||
saveToken: (tokenData: any) => {
|
||||
uni.setStorageSync(STORAGE_KEYS.TOKEN, tokenData)
|
||||
},
|
||||
|
||||
// 清除用户登录相关的存储
|
||||
clearUserStorage: () => {
|
||||
uni.removeStorageSync(STORAGE_KEYS.TOKEN)
|
||||
uni.removeStorageSync(STORAGE_KEYS.USER_INFO)
|
||||
state.userInfo = {}
|
||||
},
|
||||
|
||||
// 清除用户角色存储
|
||||
clearUserRole: () => {
|
||||
uni.removeStorageSync(STORAGE_KEYS.USER_ROLE)
|
||||
},
|
||||
}
|
||||
|
||||
const login = {
|
||||
// 检查应用环境并登录
|
||||
checkAppInfo: async () => {
|
||||
try {
|
||||
// 先尝试诗画登录
|
||||
const shihuaResult = await login.shihuaLogin()
|
||||
if (shihuaResult) return
|
||||
|
||||
// 尝试浙政钉登录
|
||||
const zhengdingResult = login.zhengdingLogin()
|
||||
if (zhengdingResult) return
|
||||
|
||||
uni.showToast({ title: "当前登录环境未知" })
|
||||
} catch (error) {
|
||||
console.error("登录失败:", error)
|
||||
uni.showToast({ title: "登录失败" })
|
||||
}
|
||||
},
|
||||
|
||||
// 诗画环境登录
|
||||
async shihuaLogin() {
|
||||
shihuaLogin: async () => {
|
||||
const appInfo = await (window as any).CP2.getAppInfo()
|
||||
if (appInfo.code == 0) return false
|
||||
|
||||
const userInfo = await (window as any).CP2.getUserInfo()
|
||||
if (userInfo.code == 0) {
|
||||
return uni.showToast({ title: "获取诗画用户信息失败", icon: "none" })
|
||||
uni.showToast({ title: "获取诗画用户信息失败" })
|
||||
return false
|
||||
} else {
|
||||
const { accountId, sessionId } = userInfo.data
|
||||
return doLogin(1, "", accountId, sessionId)
|
||||
// return doLogin(1, "", "67186e49d63fea141aad6772", "67186e49d63fea141aad6776")
|
||||
return login.doLogin(1, "", accountId, sessionId)
|
||||
// return login.doLogin(1, "", "67186e49d63fea141aad6772", "67186e49d63fea141aad6776")
|
||||
}
|
||||
},
|
||||
|
||||
// 浙政钉登录
|
||||
zhengdingLogin() {
|
||||
zhengdingLogin: () => {
|
||||
const params = new URL(window.location.href).searchParams
|
||||
const token = params.get("uabaseToken")
|
||||
if (token) {
|
||||
return doLogin(2, token, "", "")
|
||||
return login.doLogin(2, token, "", "")
|
||||
}
|
||||
return false
|
||||
},
|
||||
}
|
||||
|
||||
// loginType 1:诗画登录 2:浙政钉登录
|
||||
const doLogin = async (loginType: number, token: string, accountId: string, sessionId: string) => {
|
||||
const tokenKey = constant.storageConstant.TOKEN
|
||||
const userInfoKey = constant.storageConstant.USERINFO
|
||||
const tokenStorage = uni.getStorageSync(tokenKey)
|
||||
const userStorage = uni.getStorageSync(userInfoKey)
|
||||
switch (loginType) {
|
||||
case 1:
|
||||
// 诗画
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const response = await userApi.authsh({ accountId, sessionId })
|
||||
if (response.code == 0) {
|
||||
uni.setStorageSync(tokenKey, { accessToken: response.data.accessToken })
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "登录成功", icon: "none" })
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
uni.setStorageSync(userInfoKey, userData)
|
||||
getStorageUserInfo()
|
||||
} else {
|
||||
uni.hideLoading()
|
||||
uni.removeStorageSync(tokenKey)
|
||||
uni.removeStorageSync(userInfoKey)
|
||||
uni.showToast({ title: "登录失败", icon: "none" })
|
||||
}
|
||||
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.removeStorageSync(tokenKey)
|
||||
uni.removeStorageSync(userInfoKey)
|
||||
if (res.msg == "token已失效") {
|
||||
uni.showToast({ title: "用户凭证已失效,请退出并重新进入智慧后勤应用", icon: "none", duration: 6000 })
|
||||
} else {
|
||||
uni.showToast({ title: res.msg || "登录失败", icon: "none" })
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "登录错误", icon: "none" })
|
||||
} finally {
|
||||
// 执行登录操作 - loginType 1:诗画登录 2:浙政钉登录
|
||||
doLogin: async (loginType: number, token: string, accountId: string, sessionId: string) => {
|
||||
const tokenStorage = uni.getStorageSync(STORAGE_KEYS.TOKEN)
|
||||
const userStorage = uni.getStorageSync(STORAGE_KEYS.USER_INFO)
|
||||
|
||||
switch (loginType) {
|
||||
case 1: // 诗画登录
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const response = await userApi.authsh({ accountId, sessionId })
|
||||
|
||||
if (response.code == 0) {
|
||||
storage.saveToken({ accessToken: response.data.accessToken })
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "登录成功" })
|
||||
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
storage.saveUserInfo(userData)
|
||||
} else {
|
||||
uni.hideLoading()
|
||||
storage.clearUserStorage()
|
||||
uni.showToast({ title: "登录失败" })
|
||||
}
|
||||
}
|
||||
break
|
||||
default:
|
||||
uni.showToast({ title: "当前登录环境未知", icon: "none" })
|
||||
}
|
||||
}
|
||||
break
|
||||
|
||||
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" })
|
||||
}
|
||||
case 2: // 浙政钉登录
|
||||
if (isEmpty(tokenStorage) || isEmpty(userStorage)) {
|
||||
try {
|
||||
uni.showLoading({ title: "登录中...", mask: true })
|
||||
const res = await userApi.auth(token)
|
||||
|
||||
// 切换角色
|
||||
const doChangeRole = () => {
|
||||
uni.removeStorageSync("userRole")
|
||||
router.jump("/pages/user/userRole")
|
||||
}
|
||||
if (res.code == 0) {
|
||||
storage.saveToken(res.data)
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "登录成功" })
|
||||
|
||||
// 扫一扫
|
||||
const doScanCode = async () => {
|
||||
usePayStore().setPayData({})
|
||||
try {
|
||||
let decodedString: string | undefined = ""
|
||||
if (isDevEnv()) {
|
||||
// decodedString = decodeBase64("eyJpZCI6IjE4OTYxNTcyMDc2NDQwNTM1MDYiLCJuYW1lIjoi5rWL6K+V5ZWG5oi3In0=") // 线上-测试超市-收款码
|
||||
decodedString = decodeBase64("eyJpZCI6IjE5MDU4MTY3NTEwNDM0NTI5MzAiLCJuYW1lIjoi5paw5Y6/5bqc6LaF5biCIn0=") // 测试环境-邓本地-新县府超市-收款码
|
||||
uni.showToast({ title: "开发环境不是浙政钉环境,扫码仅供测试,收款商家为测试商户超市", icon: "none" })
|
||||
} else {
|
||||
const res: any = await (window as any).dd.scan({ type: "all" })
|
||||
decodedString = decodeBase64(res.text)
|
||||
const { data: userData } = await userApi.userInfo()
|
||||
storage.saveUserInfo(userData)
|
||||
} else {
|
||||
storage.clearUserStorage()
|
||||
if (res.msg == "token已失效") {
|
||||
uni.showToast({ title: "用户凭证已失效,请退出并重新进入智慧后勤应用", duration: 6000 })
|
||||
} else {
|
||||
uni.showToast(res.msg || "登录失败")
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "登录错误" })
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
break
|
||||
|
||||
default:
|
||||
uni.showToast({ title: "当前登录环境未知" })
|
||||
}
|
||||
},
|
||||
|
||||
if (decodedString) {
|
||||
// 退出登录
|
||||
logout: async () => {
|
||||
const isLogin = await hooks.useCheckLogin()
|
||||
if (!isLogin) return
|
||||
|
||||
uni.showLoading({ title: "退出登录中...", mask: true })
|
||||
storage.clearUserStorage()
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: "退出登录成功" })
|
||||
},
|
||||
|
||||
// 切换角色
|
||||
changeRole: () => {
|
||||
storage.clearUserRole()
|
||||
router.jump("/pages/user/userRole")
|
||||
},
|
||||
}
|
||||
|
||||
const scanCode = {
|
||||
// 扫一扫
|
||||
scan: async () => {
|
||||
usePayStore().setPayData({})
|
||||
try {
|
||||
const decodedString = await scanCode.getScanResult()
|
||||
|
||||
if (decodedString) {
|
||||
scanCode.processScanResult(decodedString)
|
||||
} else {
|
||||
uni.showToast({ title: "无效的二维码" })
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error) {
|
||||
uni.showToast({ title: error })
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 获取扫码结果
|
||||
getScanResult: async (): Promise<string | undefined> => {
|
||||
if (isDevEnv()) {
|
||||
// 开发环境模拟扫码
|
||||
// const testQrCode = "eyJpZCI6IjE4OTYxNTcyMDc2NDQwNTM1MDYiLCJuYW1lIjoi5rWL6K+V5ZWG5oi3In0=" // 线上-测试超市-收款码
|
||||
const testQrCode = "eyJpZCI6IjE5MDU4MTY3NTEwNDM0NTI5MzAiLCJuYW1lIjoi5paw5Y6/5bqc6LaF5biCIn0=" // 测试环境-邓本地-新县府超市-收款码
|
||||
uni.showToast({ title: "开发环境不是浙政钉环境,扫码仅供测试,收款商家为测试商户超市" })
|
||||
return scanCode.decodeBase64(testQrCode)
|
||||
} else {
|
||||
// 实际环境扫码
|
||||
const res: any = await (window as any).dd.scan({ type: "all" })
|
||||
return scanCode.decodeBase64(res.text)
|
||||
}
|
||||
},
|
||||
|
||||
// 处理扫码结果
|
||||
processScanResult: (decodedString: string) => {
|
||||
try {
|
||||
const decodedJson = JSON.parse(decodedString)
|
||||
if (decodedJson?.id && decodedJson?.name) {
|
||||
usePayStore().setPayData(decodedJson)
|
||||
router.jump("/pages/pay/scanPay")
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: "无效的二维码",
|
||||
icon: "none",
|
||||
})
|
||||
uni.showToast({ title: "无效的二维码" })
|
||||
}
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: "无效的二维码",
|
||||
icon: "none",
|
||||
})
|
||||
} catch (error) {
|
||||
uni.showToast({ title: "无效的二维码内容" })
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error) {
|
||||
uni.showToast({
|
||||
title: error,
|
||||
icon: "none",
|
||||
})
|
||||
},
|
||||
|
||||
// 解码base64
|
||||
decodeBase64: (base64String: string): string | undefined => {
|
||||
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: "解码失败,无效的二维码" })
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const onTabMenu = async (v: MenuItem, i: number) => {
|
||||
if (i == state.menuList.length - 1) {
|
||||
router.jump(v.router)
|
||||
const onTabLogin = () => (isDevEnv() ? router.jump("/pages/test/login") : login.checkAppInfo())
|
||||
|
||||
const onTabMenu = async (item: any, index: number) => {
|
||||
if (index == state.menuList.length - 1) {
|
||||
router.jump(item.router)
|
||||
return
|
||||
}
|
||||
|
||||
const isLogin = await hooks.useCheckLogin()
|
||||
if (isLogin) {
|
||||
if (v.text === "扫一扫") {
|
||||
await doScanCode()
|
||||
if (item.text === "扫一扫") {
|
||||
await scanCode.scan()
|
||||
} else {
|
||||
router.jump(v.router)
|
||||
router.jump(item.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 doLogout = async () => {
|
||||
await login.logout()
|
||||
}
|
||||
|
||||
// 切换角色
|
||||
const doChangeRole = () => {
|
||||
login.changeRole()
|
||||
}
|
||||
|
||||
// 初始化配置菜单
|
||||
const initMenuConfig = () => {
|
||||
if (isDevEnv()) {
|
||||
state.menuList.push({
|
||||
text: "测试登录",
|
||||
icon: "/static/icon/login.png",
|
||||
router: "/pages/test/login",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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 = {}
|
||||
// 根据启动参数添加扫一扫菜单项
|
||||
const launchParam = new URL(window.location.href).searchParams.get("launchParam")
|
||||
if (launchParam === "selfMerchant") {
|
||||
state.menuList.unshift({
|
||||
text: "扫一扫",
|
||||
icon: "/static/icon/scan.png",
|
||||
router: "",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,29 +366,16 @@ onLoad(async () => {
|
||||
const app = getApp()
|
||||
app._initTabbar()
|
||||
|
||||
isDevEnv()
|
||||
? state.menuList.push({
|
||||
text: "测试登录",
|
||||
icon: "/static/icon/login.png",
|
||||
router: "/pages/test/login",
|
||||
})
|
||||
: ""
|
||||
// checkAppInfo()
|
||||
|
||||
new URL(window.location.href).searchParams.get("launchParam") == "canteen"
|
||||
? ""
|
||||
: new URL(window.location.href).searchParams.get("launchParam") == "selfMerchant"
|
||||
? state.menuList.unshift({
|
||||
text: "扫一扫",
|
||||
icon: "/static/icon/scan.png",
|
||||
router: "",
|
||||
})
|
||||
: ""
|
||||
// 初始化配置菜单
|
||||
initMenuConfig()
|
||||
|
||||
// 获取应用信息
|
||||
state.appInfo = await getAppInfo()
|
||||
})
|
||||
|
||||
onShow(() => {
|
||||
getStorageUserInfo()
|
||||
// 获取存储中的用户信息
|
||||
storage.getUserInfo()
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export default {
|
||||
TOKEN: "token",
|
||||
USERINFO: "user_info",
|
||||
};
|
||||
TOKEN: "token",
|
||||
USERINFO: "user_info",
|
||||
USERROLE: "userRole",
|
||||
}
|
||||
Reference in new issue
Block a user