This commit is contained in:
zjf committed 2025-03-07 12:31:05 +08:00
commit 4d35d3daa9
6 files changed
+249 -189

No files matched your search

+3 -2
View File
@@ -12,7 +12,7 @@
<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>
<!-- <text class="iconfont icon-youjiantou color-#666666 m-l-20rpx"></text> -->
</view>
<text class="sub-text-26">ID {{ state.userInfo?.sysUser?.userId }}</text>
</view>
@@ -202,7 +202,8 @@ onLoad(() => {
icon: "/static/icon/login.png",
router: "/pages/test/login",
})
: checkAppInfo()
: ""
// checkAppInfo()
})
onShow(() => {
getStorageUserInfo()
+2 -2
View File
@@ -145,8 +145,8 @@ const doRefund = (v: any) => {
</view>
</view>
<view v-if="['5', '10'].includes(v.orderStatus)" class="flex-row items-center justify-end p-t-20rpx">
<view v-if="v.orderStatus == 5 && v.orderType == 2" class="botder_btn" @tap.stop="doRefund(v)">退款 </view>
<view v-if="v.orderStatus == 5 && v.orderType == 2" class="m-l-20rpx botder_btn" @tap.stop="doToDetail(v)"> 核销码 </view>
<!-- <view v-if="v.orderStatus == 5 && v.orderType == 2" class="botder_btn" @tap.stop="doRefund(v)">退款</view> -->
<!-- <view v-if="v.orderStatus == 5 && v.orderType == 2" class="m-l-20rpx botder_btn" @tap.stop="doToDetail(v)">核销码</view> -->
</view>
</view>
</z-paging>
@@ -1,77 +1,77 @@
<script setup>
import { defineProps, computed } from 'vue'
import { router } from '@@/index'
import { defineProps, computed } from "vue"
import { router } from "@@/index"
const props = defineProps({
title: {
type: String,
default: '标题'
},
titleColor: {
type: String,
default: '#333'
},
bgColor: {
type: String,
default: 'white'
},
showLeft: {
type: Boolean,
default: true
}
title: {
type: String,
default: "标题",
},
titleColor: {
type: String,
default: "#333",
},
bgColor: {
type: String,
default: "white",
},
showLeft: {
type: Boolean,
default: true,
},
})
const isHistiry = computed(() => {
return router.hasHistory()
return router.hasHistory()
})
const isTabbar = computed(() => {
return router.hasTabbar()
return router.hasTabbar()
})
function doBackClick() {
router.back()
router.back()
}
function getNavTop() {
const { statusBarHeight } = uni.getSystemInfoSync();
return `${statusBarHeight}px`
const { statusBarHeight } = uni.getSystemInfoSync()
return `${statusBarHeight}px`
}
</script>
<template>
<view class="navbar" :style="{ top: getNavTop() }">
<view class="navbar_center" v-if="showLeft && !isTabbar" @tap="doBackClick">
<text class="sicon-back icon" v-if="isHistiry" />
<text class="sicon-home icon" v-else />
</view>
</view>
<view v-if="false" class="navbar" :style="{ top: getNavTop() }">
<view class="navbar_center" v-if="showLeft && !isTabbar" @tap="doBackClick">
<text class="sicon-back icon" v-if="isHistiry" />
<text class="sicon-home icon" v-else />
</view>
</view>
</template>
<style lang="scss" scoped>
.navbar {
position: absolute;
left: 20rpx;
height: 44px;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
left: 20rpx;
height: 44px;
display: flex;
justify-content: center;
align-items: center;
&_center {
background: #fff;
box-shadow: 0px 0px 4rpx rgba(51, 51, 51, 0.08), 0px 4rpx 6rpx 2rpx rgba(102, 102, 102, 0.12);
border-radius: 28rpx;
width: 56rpx;
height: 56rpx;
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
&_center {
background: #fff;
box-shadow: 0px 0px 4rpx rgba(51, 51, 51, 0.08), 0px 4rpx 6rpx 2rpx rgba(102, 102, 102, 0.12);
border-radius: 28rpx;
width: 56rpx;
height: 56rpx;
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
&>.icon {
font-size: 32rpx;
color: #333;
}
}
& > .icon {
font-size: 32rpx;
color: #333;
}
}
}
</style>
@@ -1,24 +1,24 @@
<script setup>
import { defineProps, computed } from 'vue'
import { router } from '@@/index'
import { defineProps, computed } from "vue"
import { router } from "@@/index"
const props = defineProps({
title: {
type: String,
default: '标题'
default: "标题",
},
titleColor: {
type: String,
default: '#333'
default: "#333",
},
bgColor: {
type: String,
default: 'white'
default: "white",
},
showLeft: {
type: Boolean,
default: true
}
default: true,
},
})
const isHistiry = computed(() => {
@@ -32,11 +32,10 @@ const isTabbar = computed(() => {
function doBackClick() {
router.back()
}
</script>
<template>
<view class="navbar" :style="{ backgroundColor: bgColor, height: '44px' }">
<view v-if="false" class="navbar" :style="{ backgroundColor: bgColor, height: '44px' }">
<view class="navbar_left" v-if="showLeft && !isTabbar" @tap="doBackClick">
<text class="sicon-back icon" v-if="isHistiry" />
<text class="sicon-home icon" v-else />
+24 -18
View File
@@ -28,23 +28,29 @@ export const useStaticCheckLogin = () => {
export const useCheckLogin = async (): Promise<boolean> => {
const tokenKey = storageConstant.TOKEN
const tokenStorage = uni.getStorageSync(tokenKey)
if (tokenStorage) return true
uni.showModal({
title: "提示",
content: "您还未登录,请先登录",
confirmColor: "#0094ff",
cancelText: "取消",
confirmText: "去登录",
success: async (res) => {
if (res.confirm) {
isDevEnv() ? jump("/pages/test/login", {}, 3) : jump("/pages/tabbar/my", {}, 3)
if (isDevEnv()) {
if (tokenStorage) return true
uni.showModal({
title: "提示",
content: "您还未登录,请先登录",
confirmColor: "#0094ff",
cancelText: "取消",
confirmText: "去登录",
success: async (res) => {
if (res.confirm) {
jump("/pages/test/login", {}, 3)
return
}
if (!res.confirm) {
return
}
return
}
if (!res.confirm) {
return
}
return
},
})
return false
},
})
return false
} else {
if (tokenStorage) return true
uni.showToast({ title: "你还未登录", icon: "none" })
return false
}
}
+162 -108
View File
@@ -55,8 +55,64 @@ const getAuthHeader = (tokenStorage: any) => {
return tokenStorage?.accessToken?.tokenValue ? `Bearer ${tokenStorage.accessToken.tokenValue}` : null
}
// token过期刷新检查
const checkAndRefreshToken = async (tokenStorage: any) => {
const { accessToken } = tokenStorage
if (!accessToken) return
const expiresDate = new Date(accessToken.expiresAt).getTime()
console.log("现在的时间大于过期时间吗:", new Date().getTime() >= expiresDate)
if (new Date().getTime() >= expiresDate) {
// try {
// const refreshData: any = await request({
// url: "/auth/oauth2/token",
// method: "POST",
// params: { grant_type: "refresh_token" },
// data: JSON.stringify({
// scope: "server",
// refresh_token: tokenStorage.refreshToken.tokenValue,
// }),
// header: {
// skipToken: true,
// Authorization: "Basic " + window.btoa(import.meta.env.VITE_OAUTH2_APP_CLIENT),
// "content-type": "application/x-www-form-urlencoded",
// },
// })
// if (refreshData.code === 0 && !isEmpty(refreshData.data)) {
// console.log("refreshData.data", refreshData.data)
// // 刷新成功,更新token并重试请求
// uni.setStorageSync(TOKEN_KEY, refreshData.data)
// }
// } catch (refreshError) {
// console.error("Token refresh failed:", refreshError)
// }
try {
uni.showLoading({ title: "加载中...", mask: true })
const params = new URL(window.location.href).searchParams
const token = params.get("uabaseToken")
if (token) {
const res = await userApi.auth(token)
if (res?.code === 0) {
uni.setStorageSync(TOKEN_KEY, res.data)
} else {
uni.removeStorageSync(TOKEN_KEY)
uni.removeStorageSync(USER_INFO_KEY)
uni.showToast({ title: res.msg, icon: "none" })
}
}
} catch (error) {
uni.showToast({ title: "错误", icon: "none" })
} finally {
uni.hideLoading()
}
}
}
// 请求拦截器
http.interceptors.request.use((config) => {
http.interceptors.request.use(async (config) => {
const tokenStorage = uni.getStorageSync(TOKEN_KEY)
const config_header: any = config?.header || {}
@@ -66,14 +122,6 @@ http.interceptors.request.use((config) => {
config_header["CLIENT-TOC"] = "Y"
}
// 设置认证头
if (config.url !== "/sys/auth/refresh-token" && config.url !== "/auth/oauth2/token") {
const authHeader = getAuthHeader(tokenStorage)
if (authHeader) {
config_header.Authorization = authHeader
}
}
// 处理 loading
if (config.custom?.showLoading) {
LoadingInstance.count++
@@ -86,90 +134,67 @@ http.interceptors.request.use((config) => {
}
}
// 设置认证头
if (
!config.url?.startsWith("/thirdparty/thirdPartyOAth2/zzdsso") &&
config.url !== "/sys/auth/refresh-token" &&
config.url !== "/auth/oauth2/token"
) {
if (!isEmpty(tokenStorage)) {
// token过期刷新检查
checkAndRefreshToken(tokenStorage)
const authHeader = getAuthHeader(tokenStorage)
if (authHeader) {
config_header.Authorization = authHeader
}
} else {
// token不存在,则发起登录
const params = new URL(window.location.href).searchParams
const token = params.get("uabaseToken")
if (token) {
try {
uni.showLoading({ title: "加载中...", mask: true })
const res = await userApi.auth(token)
if (res?.code === 0) {
uni.setStorageSync(TOKEN_KEY, res.data)
const { data: userData } = await userApi.userInfo()
uni.setStorageSync(USER_INFO_KEY, userData)
const authHeader = getAuthHeader(tokenStorage)
if (authHeader) {
config_header.Authorization = authHeader
}
} else {
uni.showToast({ title: "获取浙政钉用户信息失败", icon: "none" })
}
} catch (error) {
uni.showToast({ title: "错误", icon: "none" })
} finally {
uni.hideLoading()
}
}
}
}
return config
}, Promise.reject)
// 定义响应处理的辅助函数
const responseHelpers = {
// 处理错误响应
handleErrorResponse(response: any) {
if (!response.config?.custom?.showError) return
// 处理错误响应
const handleErrorResponse = (response: any) => {
if (!response.config?.custom?.showError) return
switch (response.data.code) {
case 401:
this.handleUnauthorized()
break
default:
uni.showToast({
title: response.data.msg || "服务器开小差啦,请稍后再试~",
icon: "none",
mask: true,
})
}
},
// 处理未授权情况
handleUnauthorized() {
setTimeout(() => {
uni.removeStorageSync(TOKEN_KEY)
uni.removeStorageSync(USER_INFO_KEY)
router.jump("/pages/tabbar/my", {}, 2)
}, 500)
},
// 处理成功提示
handleSuccessMessage(response: any) {
const { data, config } = response
if (data.code === 0 && data.message && config?.custom?.showSuccess) {
uni.showToast({
title: config.custom.successMsg || data.message,
icon: "none",
})
}
},
// 处理登录过期
handleLoginExpired() {
uni.removeStorageSync(TOKEN_KEY)
uni.removeStorageSync(USER_INFO_KEY)
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" })
isDevEnv() ? router.jump("/pages/test/login") : router.jump("/pages/tabbar/my")
},
// 检查并刷新token
checkAndRefreshToken(tokenStorage: any) {
const { accessToken } = tokenStorage
if (!accessToken) return
const issuedDate = new Date(accessToken.issuedAt).getTime()
console.log(new Date().getTime() - issuedDate)
if (new Date().getTime() - issuedDate >= 1000 * 60 * 20) {
// try {
// const refreshData: any = await request({
// url: "/auth/oauth2/token",
// method: "POST",
// params: { grant_type: "refresh_token" },
// data: JSON.stringify({
// scope: "server",
// refresh_token: tokenStorage.refreshToken.tokenValue,
// }),
// header: {
// skipToken: true,
// Authorization: "Basic " + window.btoa(import.meta.env.VITE_OAUTH2_APP_CLIENT),
// "content-type": "application/x-www-form-urlencoded",
// },
// })
// if (refreshData.code === 0 && !isEmpty(refreshData.data)) {
// console.log("refreshData.data", refreshData.data)
// // 刷新成功,更新token并重试请求
// uni.setStorageSync(TOKEN_KEY, refreshData.data)
// }
// } catch (refreshError) {
// console.error("Token refresh failed:", refreshError)
// }
switch (response.data.code) {
case 401:
// 处理未授权情况
setTimeout(() => {
uni.removeStorageSync(TOKEN_KEY)
uni.removeStorageSync(USER_INFO_KEY)
console.log("未授权")
}, 500)
break
case 424:
try {
const params = new URL(window.location.href).searchParams
const token = params.get("uabaseToken")
@@ -193,15 +218,30 @@ const responseHelpers = {
uni.hideLoading()
uni.showToast({ title: "错误", icon: "none" })
}
}
},
break
default:
uni.showToast({
title: response.data.msg || "服务器开小差啦,请稍后再试~",
icon: "none",
mask: true,
})
}
}
// 处理成功提示
const handleSuccessMessage = (response: any) => {
const { data, config } = response
if (data.code === 0 && data.message && config?.custom?.showSuccess) {
uni.showToast({
title: config.custom.successMsg || data.message,
icon: "none",
})
}
}
// 响应拦截器
http.interceptors.response.use(
async (response) => {
console.log("response", response)
// 关闭loading
if (response.config?.custom?.showLoading) {
closeLoading()
@@ -216,25 +256,14 @@ http.interceptors.response.use(
})
}
// 错误处理
// 错误处理和错误提示
if (response.data.code !== 0) {
responseHelpers.handleErrorResponse(response)
handleErrorResponse(response)
return Promise.resolve(response.data)
}
// 成功提示
responseHelpers.handleSuccessMessage(response)
// 登录过期处理
if (response.data.code === 1) {
responseHelpers.handleLoginExpired()
}
// Token 刷新检查
const tokenStorage = uni.getStorageSync(TOKEN_KEY)
if (!isEmpty(tokenStorage) && !response.config.url?.startsWith("/thirdparty/thirdPartyOAth2/zzdsso")) {
await responseHelpers.checkAndRefreshToken(tokenStorage)
}
handleSuccessMessage(response)
return Promise.resolve(response.data)
},
@@ -248,6 +277,32 @@ http.interceptors.response.use(
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" })
router.jump("/pages/test/login")
return false
} else {
try {
uni.showLoading({ title: "加载中...", mask: true })
const params = new URL(window.location.href).searchParams
const token = params.get("uabaseToken")
if (token) {
userApi.auth(token).then((res) => {
if (res?.code === 0) {
uni.setStorageSync(TOKEN_KEY, res.data)
} else {
uni.removeStorageSync(TOKEN_KEY)
uni.removeStorageSync(USER_INFO_KEY)
// uni.showToast({ title: "登录已过期", icon: "none" })
return Promise.resolve({
code: 1,
data: "登录已过期",
msg: "登录已过期",
})
}
})
}
} catch (error) {
uni.showToast({ title: "错误", icon: "none" })
} finally {
uni.hideLoading()
}
}
}
@@ -259,7 +314,6 @@ http.interceptors.response.use(
mask: true,
})
}
return false
},
)