From 7cbb226416a3fbdd6e17d60b314bea31ef03bb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lemon=E6=A9=AA?= <17633066053@163.com> Date: Tue, 8 Jun 2021 18:46:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9jwt=E5=88=A4=E6=96=ADtoken=20?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E8=AE=A2=E5=8D=95=E9=87=91=E9=A2=9D=E4=B8=BA?= =?UTF-8?q?0=E6=97=B6=E5=89=8D=E5=8F=B0=20=E8=B7=B3=E8=BD=AC=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/order/fillorder.vue | 26 ++++++++----- pages/tabbar/cart/cartList.vue | 9 +---- utils/request.js | 70 +++++++++++++++++++++++----------- 3 files changed, 64 insertions(+), 41 deletions(-) diff --git a/pages/order/fillorder.vue b/pages/order/fillorder.vue index 9237efb..003e9fe 100644 --- a/pages/order/fillorder.vue +++ b/pages/order/fillorder.vue @@ -448,17 +448,23 @@ export default { duration: 2000, icon: "none", }); + // 如果当前价格为0跳转到订单列表 + if (this.orderMessage.priceDetailDTO.billPrice == 0) { + uni.redirectTo({ + url: "/pages/order/myOrder?status=0", + }); + } else { + // #ifdef MP-WEIXIN + // 微信小程序中点击创建订单直接开始支付 + this.pay(res.data.result.sn); + // #endif - // #ifdef MP-WEIXIN - // 微信小程序中点击创建订单直接开始支付 - this.pay(res.data.result.sn); - // #endif - - // #ifndef MP-WEIXIN - this.navigateTo( - `/pages/cart/payment/payOrder?trade_sn=${res.data.result.sn}` - ); - // #endif + // #ifndef MP-WEIXIN + this.navigateTo( + `/pages/cart/payment/payOrder?trade_sn=${res.data.result.sn}` + ); + // #endif + } } else { uni.showToast({ title: "创建订单有误!请稍后重试", diff --git a/pages/tabbar/cart/cartList.vue b/pages/tabbar/cart/cartList.vue index 8409684..83b744a 100644 --- a/pages/tabbar/cart/cartList.vue +++ b/pages/tabbar/cart/cartList.vue @@ -164,7 +164,7 @@ import * as API_Trade from "@/api/trade"; export default { data() { return { - lightColor:this.$lightColor, + lightColor: this.$lightColor, discountDetailsFlag: false, //优惠明细开关 // 商品栏右侧滑动按钮 options: [ @@ -499,16 +499,9 @@ export default { } uni.stopPullDownRefresh(); uni.hideLoading(); - } else { - uni.showToast({ - title: result.data.message, - duration: 2000, - icon: "none", - }); } }) .catch((err) => { - uni.hideLoading(); }); } else { diff --git a/utils/request.js b/utils/request.js index 8998393..116705a 100644 --- a/utils/request.js +++ b/utils/request.js @@ -120,6 +120,25 @@ http.interceptors.request.use( config.params = params; config.header.accessToken = accessToken; + console.log(accessToken); + /** + * jwt 因为安卓以及ios没有window的属性 + * window.atob()这个函数 base64编码的使用方法就是btoa(),而用于解码的使用方法是atob(), + * 所以使用手写 base-64 编码的字符串数据。 + */ + // + const atob = (str) => Buffer.from(str, "base64").toString("binary"); + // 判断如果过期时间小于我的当前时间,在请求上重新刷新token + if (accessToken.split(".").length <= 1) { + refresh(); + } else { + if ( + JSON.parse(atob(accessToken.split(".")[1])).exp < + new Date().getTime() / 1000 + ) { + refresh(); + } + } } config.header = { ...config.header, @@ -132,6 +151,33 @@ http.interceptors.request.use( } ); +async function refresh() { + // 本地储存的是过期token了,重新获取 + const getTokenResult = await refreshToken(); + // if (getTokenResult === "success") { + // // 获取新的token成功 刷新当前页面 + + // let routes = getCurrentPages(); // 获取当前打开过的页面路由数组 + // let curRoute = routes[routes.length - 1].route; //获取当前页面路由 + // let curParam = routes[routes.length - 1].options; //获取路由参数 + // // 拼接参数 + // let param = ""; + // for (let key in curParam) { + // param += "&" + key + "=" + curParam[key]; + // } + // // 判断当前路径 + // if (curRoute.indexOf("pages/tabbar") == 1) { + // uni.switchTab({ + // url: "/" + curRoute + param.replace("&", "?"), + // }); + // } + + // uni.redirectTo({ + // url: "/" + curRoute + param.replace("&", "?"), + // }); + // } +} + // 必须使用异步函数,注意 http.interceptors.response.use( async (response) => { @@ -143,33 +189,11 @@ http.interceptors.response.use( (token && response.statusCode === 403) || response.data.status === 403 ) { - expireToken.includes(token) ? cleanStorage() : ""; // jwt token 过期了 expireToken.push(token); // 把过期token 储存 const currentToken = storage.getAccessToken(); if (expireToken.includes(currentToken)) { - // 本地储存的是过期token了,重新获取 - const getTokenResult = await refreshToken(); - if (getTokenResult === "success") { - // 获取新的token成功 刷新当前页面 - - let routes = getCurrentPages(); // 获取当前打开过的页面路由数组 - let curRoute = routes[routes.length - 1].route; //获取当前页面路由 - let curParam = routes[routes.length - 1].options; //获取路由参数 - // 拼接参数 - let param = ""; - for (let key in curParam) { - param += "&" + key + "=" + curParam[key]; - } - - uni.redirectTo({ - url: "/" + curRoute + param.replace("&", "?"), - }); - } else { - cleanStorage(); - } - } else { - cleanStorage(); + refresh(); } // 如果当前返回没登录 } else if (