diff --git a/.env b/.env index fdcdde2..b22752f 100644 --- a/.env +++ b/.env @@ -17,31 +17,32 @@ VITE_PWD_ENC_KEY='pigxpigxpigxpigx' VITE_OAUTH2_APP_CLIENT='mini:mini' # 图片访问基础地址 -VITE_IMAGE_BASE_PATH = http://localhost:9999 +#VITE_IMAGE_BASE_PATH = http://localhost:9999 # VITE_IMAGE_BASE_PATH = http://192.168.101.29:9999 # 邓-本地 # VITE_IMAGE_BASE_PATH = http://192.168.101.37:9999 # 邓-本地 # VITE_IMAGE_BASE_PATH = https://123.156.230.15:8089/api # 线上 -#VITE_IMAGE_BASE_PATH = http://220.191.229.171:8090/api # 线上 +# VITE_IMAGE_BASE_PATH = http://220.191.229.171:8090/api # 线上 # VITE_IMAGE_BASE_PATH = https://stgl.pj.gov.cn/api # 线上 # VITE_IMAGE_BASE_PATH = http://pjcloud.center.takin.cc # 张 -# VITE_IMAGE_BASE_PATH = http://59.202.170.152:81/api # 信创线上 +VITE_IMAGE_BASE_PATH = http://59.202.170.152:81/api # 信创线上 # ******************** start 开发环境 start ******************** -# VITE_APP_DEV_SERVICE = http://59.202.170.152:81/api # 信创线上 +VITE_APP_DEV_SERVICE = http://59.202.170.152:81/api # 信创线上 # VITE_APP_DEV_SERVICE = https://stgl.pj.gov.cn/app-api -# VITE_APP_DEV_SERVICE = http://192.168.101.29:9999 # 邓-本地 -# VITE_APP_DEV_SERVICE = http://192.168.101.37:9999 # 邓-本地 +# VITE_APP_DEV_SERVICE = http://192.168.101.50:9999 # 邓-本地 # VITE_APP_DEV_SERVICE = http://127.0.0.1:9999 # VITE_APP_DEV_SERVICE = http://123.156.230.15:8089/api/ # 线上 -#VITE_APP_DEV_SERVICE = http://220.191.229.171:8090/api # 线上 -VITE_APP_DEV_SERVICE = http://localhost:9999 +# VITE_APP_DEV_SERVICE = http://220.191.229.171:8090/api # 线上 +# VITE_APP_DEV_SERVICE = http://localhost:9999 # ******************** end 开发环境 end ******************** # ******************** start 生产环境 start **************** #VITE_APP_PROD_SERVICE = http://123.156.228.134:48080/app-api #VITE_APP_PROD_SERVICE = http://59.202.170.152:81/api # VITE_APP_PROD_SERVICE = https://stgl.pj.gov.cn/api -VITE_APP_PROD_SERVICE = http://220.191.229.171:8090/api +# VITE_APP_PROD_SERVICE = http://220.191.229.171:8090/api +VITE_APP_PROD_SERVICE = http://59.202.170.152:81/api # 信创线上 # ******************** end 生产环境 end ******************** + # VITE_DEV_ENV = dev -VITE_DEV_ENV = prod +VITE_DEV_ENV = prod \ No newline at end of file diff --git a/.prettierrc b/.prettierrc index 5e07d0e..ef9c5c7 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,8 +1,8 @@ { - "tabWidth": 2, - "useTabs": true, - "trailingComma": "all", - "semi": false, - "singleQuote": false, - "printWidth": 120 -} \ No newline at end of file + "tabWidth": 2, + "useTabs": true, + "trailingComma": "all", + "semi": false, + "singleQuote": false, + "printWidth": 140 +} diff --git a/src/pages.json b/src/pages.json index 91a538a..45db073 100644 --- a/src/pages.json +++ b/src/pages.json @@ -16,13 +16,13 @@ "disableScroll": true } }, - { - "path": "pages/tabbar/shop", - "style": { - "navigationBarTitleText": "超市", - "disableScroll": true - } - }, + // { + // "path": "pages/tabbar/shop", + // "style": { + // "navigationBarTitleText": "超市", + // "disableScroll": true + // } + // }, { "path": "pages/tabbar/order", "style": { @@ -273,12 +273,12 @@ "iconPath": "static/tabbar/home.png", "selectedIconPath": "static/tabbar/home-select.png" }, - { - "pagePath": "pages/tabbar/shop", - "text": "超市", - "iconPath": "static/tabbar/shop.png", - "selectedIconPath": "static/tabbar/shop-select.png" - }, + // { + // "pagePath": "pages/tabbar/shop", + // "text": "超市", + // "iconPath": "static/tabbar/shop.png", + // "selectedIconPath": "static/tabbar/shop-select.png" + // }, { "pagePath": "pages/tabbar/order", "text": "订单", diff --git a/src/pages/boxes/list.vue b/src/pages/boxes/list.vue index c2c63e3..b5581eb 100644 --- a/src/pages/boxes/list.vue +++ b/src/pages/boxes/list.vue @@ -1,6 +1,6 @@ - - \ No newline at end of file + diff --git a/src/pages/merchant/food.vue b/src/pages/merchant/food.vue index c9dc62f..5023010 100644 --- a/src/pages/merchant/food.vue +++ b/src/pages/merchant/food.vue @@ -107,7 +107,7 @@ const doProduct = (v: any) => { @scroll="pageHook.scroll" > - + - - diff --git a/src/pages/tabbar/my.vue b/src/pages/tabbar/my.vue index ad28c77..0d97c75 100644 --- a/src/pages/tabbar/my.vue +++ b/src/pages/tabbar/my.vue @@ -1,47 +1,85 @@ + + - - diff --git a/src/sheep/utils/isDevEnv.ts b/src/sheep/utils/isDevEnv.ts index 9e624c5..b5bdee1 100644 --- a/src/sheep/utils/isDevEnv.ts +++ b/src/sheep/utils/isDevEnv.ts @@ -1 +1,2 @@ export default () => import.meta.env.VITE_DEV_ENV === "dev" +// export default () => process.env.NODE_ENV === "development" diff --git a/src/sheep/utils/request.ts b/src/sheep/utils/request.ts index 80d4ed2..ce9ed7e 100644 --- a/src/sheep/utils/request.ts +++ b/src/sheep/utils/request.ts @@ -1,40 +1,15 @@ import Request from "luch-request" import { isEmpty } from "radash" - -import { userApi } from "@@/api" -import { router, constant } from "@@/index" import isDevEnv from "./isDevEnv" +import { router, constant } from "@@/index" +import { userApi } from "../api" // 定义常量 const TOKEN_KEY = constant.storageConstant.TOKEN const USER_INFO_KEY = constant.storageConstant.USERINFO -const REFRESH_TOKEN_URL = "/auth/oauth2/token?grant_type=refresh_token" - -// 定义类型 -interface LoadingInstance { - target: any - count: number -} - -// 默认请求配置 -const defaultOptions = { - showSuccess: false, - // 成功提醒 默认使用后端返回值 - successMsg: "", - // 显示失败消息 默认显示 - showError: true, - // 失败提醒 默认使用后端返回信息 - errorMsg: "", - // 显示请求时loading模态框 默认显示 - showLoading: true, - // loading提醒文字 - loadingMsg: "加载中", - // 需要授权才能请求 默认放开 - auth: false, -} // Loading 管理 -const LoadingInstance: LoadingInstance = { +const LoadingInstance: { target: any; count: number } = { target: null, count: 0, } @@ -55,7 +30,21 @@ const http = new Request({ "Content-Type": "application/json;charset=UTF-8", }, withCredentials: true, - custom: defaultOptions, + custom: { + showSuccess: false, + // 成功提醒 默认使用后端返回值 + successMsg: "", + // 显示失败消息 默认显示 + showError: true, + // 失败提醒 默认使用后端返回信息 + errorMsg: "", + // 显示请求时loading模态框 默认显示 + showLoading: true, + // loading提醒文字 + loadingMsg: "加载中", + // 需要授权才能请求 默认放开 + auth: false, + }, }) // 处理请求头 Authorization @@ -71,14 +60,14 @@ http.interceptors.request.use((config) => { const tokenStorage = uni.getStorageSync(TOKEN_KEY) const config_header: any = config?.header || {} - // 设置通用请求头 + // 设置租户请求头 config_header["TENANT-ID"] = "1" if (import.meta.env.VITE_IS_TOC === "true") { config_header["CLIENT-TOC"] = "Y" } // 设置认证头 - if (config.url !== "/sys/auth/refresh-token") { + if (config.url !== "/sys/auth/refresh-token" && config.url !== "/auth/oauth2/token") { const authHeader = getAuthHeader(tokenStorage) if (authHeader) { config_header.Authorization = authHeader @@ -131,7 +120,7 @@ const responseHelpers = { // 处理成功提示 handleSuccessMessage(response: any) { const { data, config } = response - if (data.code === 200 && data.message && config?.custom?.showSuccess) { + if (data.code === 0 && data.message && config?.custom?.showSuccess) { uni.showToast({ title: config.custom.successMsg || data.message, icon: "none", @@ -142,21 +131,67 @@ const responseHelpers = { // 处理登录过期 handleLoginExpired() { uni.removeStorageSync(TOKEN_KEY) + uni.removeStorageSync(USER_INFO_KEY) uni.showToast({ title: "登录已过期,请重新登录", icon: "none" }) - router.jump("/pages/test/login") + isDevEnv() ? router.jump("/pages/test/login") : router.jump("/pages/tabbar/my") }, // 检查并刷新token - async checkAndRefreshToken(tokenStorage: any) { - const { refresh_token, expiresTime } = tokenStorage - if (!refresh_token || !expiresTime) return + checkAndRefreshToken(tokenStorage: any) { + const { accessToken } = tokenStorage + if (!accessToken) return - const now = new Date().getTime() - const remain = expiresTime - now - if (remain <= 1000 * 60 * 20) { - const refreshData = await userApi.refreshToken({ refreshToken: refresh_token }) - if (!isEmpty(refreshData.data)) { - uni.setStorageSync(TOKEN_KEY, refreshData.data) + 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) + // } + try { + 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.hideLoading() + uni.showToast({ title: "错误", icon: "none" }) } } }, @@ -165,6 +200,8 @@ const responseHelpers = { // 响应拦截器 http.interceptors.response.use( async (response) => { + console.log("response", response) + // 关闭loading if (response.config?.custom?.showLoading) { closeLoading() @@ -195,19 +232,15 @@ http.interceptors.response.use( // Token 刷新检查 const tokenStorage = uni.getStorageSync(TOKEN_KEY) - if (!isEmpty(tokenStorage) && response.config.url !== "/sys/auth/refresh-token") { + if (!isEmpty(tokenStorage) && !response.config.url?.startsWith("/thirdparty/thirdPartyOAth2/zzdsso")) { await responseHelpers.checkAndRefreshToken(tokenStorage) } return Promise.resolve(response.data) }, - - // 错误拦截器 - async (error) => { + (error) => { // 处理 424 错误(token过期) if (error.statusCode === 424) { - const tokenStorage = uni.getStorageSync(TOKEN_KEY) - if (isDevEnv()) { // 开发环境直接清除token并跳转登录 uni.removeStorageSync(TOKEN_KEY) @@ -216,34 +249,6 @@ http.interceptors.response.use( router.jump("/pages/test/login") return false } - - // 生产环境尝试刷新token - if (tokenStorage?.refreshToken?.tokenValue) { - try { - const refreshData = await request({ - url: REFRESH_TOKEN_URL, - method: "POST", - data: JSON.stringify({ - scope: "server", - refresh_token: tokenStorage.refreshToken.tokenValue, - }), - header: { - Authorization: "Basic " + window.btoa(import.meta.env.VITE_OAUTH2_APP_CLIENT), - "content-type": "application/x-www-form-urlencoded", - }, - }) - - if (refreshData.code === 0) { - // 刷新成功,更新token并重试请求 - uni.setStorageSync(TOKEN_KEY, refreshData.data) - const config = error.config - config.header!.Authorization = `Bearer ${refreshData.data.accessToken.tokenValue}` - return http.request(config) - } - } catch (refreshError) { - console.error("Token refresh failed:", refreshError) - } - } } // 显示错误信息