From ea60742d828f7ada0cd7cef63133b1b93e15667c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lemon=E6=A9=AA?= <17633066053@163.com> Date: Thu, 10 Mar 2022 17:15:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=83=A8=E5=88=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/order/afterSales/afterSales.vue | 199 ++++++++++---- utils/request.js | 375 +++++++++++++------------- utils/storage.js | 4 +- 3 files changed, 338 insertions(+), 240 deletions(-) diff --git a/pages/order/afterSales/afterSales.vue b/pages/order/afterSales/afterSales.vue index 0334375..f7e71ca 100644 --- a/pages/order/afterSales/afterSales.vue +++ b/pages/order/afterSales/afterSales.vue @@ -1,31 +1,53 @@ @@ -148,7 +229,7 @@ export default { pageNumber: 1, pageSize: 10, sort: "createTime", - flowPrice: 1, + flowPrice: 1, order: "desc", }, @@ -157,21 +238,27 @@ export default { pageSize: 10, }, status: "loadmore", + orderSn: "", // 搜索订单sn }; }, onLoad(options) { this.orderList = []; this.params.pageNumber = 1; if (options.orderSn) this.params.orderSn = options.orderSn; - this.getOrderList(this.current); + this.searchOrderList(this.current); }, onPullDownRefresh() { this.change(this.current); }, methods: { - handleGetOrderList(current) { + /** + * 点击搜索执行搜索 + */ + submitSearchOrderList(current) { + this.params.pageNumber = 1; + this.logParams.pageNumber = 1; this.orderList = []; - this.getOrderList(current); + this.searchOrderList(current); }, /** * 切换tab页时,初始化数据 @@ -184,8 +271,18 @@ export default { }; this.orderList = []; //如果是2 则读取售后申请记录列表 + this.searchOrderList(index); + uni.stopPullDownRefresh(); + }, + + /** + * 搜索初始化 + * 根据当前tab传值的索引进行更改 + */ + searchOrderList(index) { if (index == 0) { - this.getOrderList(index); + this.orderSn ? (this.params.orderSn = this.orderSn) : ""; + this.getOrderList(); } else { this.logParams = { pageNumber: 1, @@ -196,16 +293,16 @@ export default { if (index === 1) { this.logParams.serviceStatus = "APPLY"; } + this.orderSn ? (this.logParams.orderSn = this.orderSn) : ""; this.orderList = []; this.getAfterSaleLogList(); } - uni.stopPullDownRefresh(); }, /** * 获取订单列表 */ - getOrderList(index) { + getOrderList() { uni.showLoading({ title: "加载中", mask: true, @@ -245,7 +342,7 @@ export default { }); } this.orderList = []; - this.getOrderList(this.current); + this.searchOrderList(this.current); uni.hideLoading(); }, diff --git a/utils/request.js b/utils/request.js index b6dc4a7..54452de 100644 --- a/utils/request.js +++ b/utils/request.js @@ -1,187 +1,188 @@ -import Request from "@/lib/request/index.js"; -import { refreshTokenFn } from "@/api/login.js"; -import storage from "@/utils/storage.js"; -import { md5 } from "@/utils/md5.js"; -import Foundation from "@/utils/Foundation.js"; -import api from "@/config/api.js"; - -import uuid from "@/utils/uuid.modified.js"; - -function cleanStorage() { - uni.showToast({ - title: "你的登录状态已过期,请重新登录", - icon: "none", - duration: 1500, - }); - if (uni.showLoading()) { - uni.hideLoading(); - } - - storage.setHasLogin(false); - storage.setAccessToken(""); - storage.setRefreshToken(""); - console.log("清空token"); - storage.setUuid(""); - storage.setUserInfo({}); - - - - // 防抖处理跳转 - // #ifdef MP-WEIXIN - - uni.navigateTo({ - url: "/pages/passport/wechatMPLogin", - }); - - // #endif - - // #ifndef MP-WEIXIN - - uni.navigateTo({ - url: "/pages/passport/login", - }); - - // #endif -} - -let http = new Request(); - - -http.setConfig((config) => { - // 没有uuid创建 - if (!storage.getUuid()) { - storage.setUuid(uuid.v1()); - } - - /* 设置全局配置 */ - config.baseURL = api.buyer; - config.header = { - ...config.header, - }; - config.validateStatus = (statusCode) => { - // 不论什么状态,统一在正确中处理 - return true; - }; - return config; -}); - -http.interceptors.request.use( - (config) => { - /* 请求之前拦截器。可以使用async await 做异步操作 */ - let accessToken = storage.getAccessToken(); - if (accessToken) { - const nonce = Foundation.randomString(6); - const timestamp = parseInt(new Date().getTime() / 1000); - const sign = md5(nonce + timestamp + accessToken); - const _params = { - nonce, - timestamp, - sign, - }; - let params = config.params || {}; - params = { ...params, ..._params }; - - config.params = params; - config.header.accessToken = accessToken; - - - } - config.header = { - ...config.header, - uuid: storage.getUuid() || uuid.v1(), - }; - return config; - }, - (config) => { - return Promise.reject(config); - } -); - - -// 是否正在刷新的标记 -let isRefreshing = false; -//重试队列 -let requests = []; -// 必须使用异步函数,注意 -http.interceptors.response.use( - async (response) => { - /* 请求之后拦截器。可以使用async await 做异步操作 */ - // token存在并且token过期 - if (isRefreshing && response.statusCode === 403) { - cleanStorage(); - isRefreshing = false; - } - - let token = storage.getAccessToken(); - if ( - (token && response.statusCode === 403) || - response.data.status === 403 - ) { - if (!isRefreshing) { - isRefreshing = true; - //调用刷新token的接口 - return refreshTokenFn(storage.getRefreshToken()) - .then((res) => { - let { accessToken, refreshToken } = res.data.result; - storage.setAccessToken(accessToken); - storage.setRefreshToken(refreshToken); - - response.header.accessToken = `${accessToken}`; - // token 刷新后将数组的方法重新执行 - requests.forEach((cb) => cb(accessToken)); - requests = []; // 重新请求完清空 - return http.request(response.config); - }) - .catch((err) => { - cleanStorage(); - return Promise.reject(err); - }) - .finally(() => { - isRefreshing = false; - }); - } else { - // 返回未执行 resolve 的 Promise - return new Promise((resolve) => { - // 用函数形式将 resolve 存入,等待刷新后再执行 - requests.push((token) => { - response.header.accessToken = `${token}`; - resolve(http.request(response.config)); - }); - }); - } - - // 如果当前返回没登录 - } else if ( - (!token && response.statusCode === 403) || - response.data.code === 403 - ) { - cleanStorage(); - - // 如果当前状态码为正常但是success为不正常时 - } else if ( - (response.statusCode == 200 && !response.data.success) || - response.statusCode == 400 - ) { - if (response.data.message) { - uni.showToast({ - title: response.data.message, - icon: "none", - duration: 1500, - }); - } - } - return response; - }, - (error) => { - return error; - } -); - -export { http }; - -export const Method = { - GET: "GET", - POST: "POST", - PUT: "PUT", - DELETE: "DELETE", -}; +import Request from "@/lib/request/index.js"; +import { refreshTokenFn } from "@/api/login.js"; +import storage from "@/utils/storage.js"; +import { md5 } from "@/utils/md5.js"; +import Foundation from "@/utils/Foundation.js"; +import api from "@/config/api.js"; + +import uuid from "@/utils/uuid.modified.js"; + + +let isNavigateTo = false + +function cleanStorage() { + uni.showToast({ + title: "你的登录状态已过期,请重新登录", + icon: "none", + duration: 1500, + }); + if (uni.showLoading()) { + uni.hideLoading(); + } + + storage.setHasLogin(false); + storage.setAccessToken(""); + storage.setRefreshToken(""); + console.log("清空token"); + storage.setUuid(""); + storage.setUserInfo({}); + + + if(!isNavigateTo){ + isNavigateTo= true + // 防抖处理跳转 + // #ifdef MP-WEIXIN + uni.navigateTo({ + url: "/pages/passport/wechatMPLogin", + }); + // #endif + // #ifndef MP-WEIXIN + uni.navigateTo({ + url: "/pages/passport/login", + }); + // #endif + } +} + +let http = new Request(); + + +http.setConfig((config) => { + // 没有uuid创建 + if (!storage.getUuid()) { + storage.setUuid(uuid.v1()); + } + + /* 设置全局配置 */ + config.baseURL = api.buyer; + config.header = { + ...config.header, + }; + config.validateStatus = (statusCode) => { + // 不论什么状态,统一在正确中处理 + return true; + }; + return config; +}); + +http.interceptors.request.use( + (config) => { + /* 请求之前拦截器。可以使用async await 做异步操作 */ + let accessToken = storage.getAccessToken(); + if (accessToken) { + const nonce = Foundation.randomString(6); + const timestamp = parseInt(new Date().getTime() / 1000); + const sign = md5(nonce + timestamp + accessToken); + const _params = { + nonce, + timestamp, + sign, + }; + let params = config.params || {}; + params = { ...params, ..._params }; + + config.params = params; + config.header.accessToken = accessToken; + + + } + config.header = { + ...config.header, + uuid: storage.getUuid() || uuid.v1(), + }; + return config; + }, + (config) => { + return Promise.reject(config); + } +); + + +// 是否正在刷新的标记 +let isRefreshing = false; +//重试队列 +let requests = []; +// 必须使用异步函数,注意 +http.interceptors.response.use( + async (response) => { + isNavigateTo = false + /* 请求之后拦截器。可以使用async await 做异步操作 */ + // token存在并且token过期 + if (isRefreshing && response.statusCode === 403) { + cleanStorage(); + isRefreshing = false; + } + + let token = storage.getAccessToken(); + if ( + (token && response.statusCode === 403) || + response.data.status === 403 + ) { + if (!isRefreshing) { + isRefreshing = true; + //调用刷新token的接口 + return refreshTokenFn(storage.getRefreshToken()) + .then((res) => { + let { accessToken, refreshToken } = res.data.result; + storage.setAccessToken(accessToken); + storage.setRefreshToken(refreshToken); + + response.header.accessToken = `${accessToken}`; + // token 刷新后将数组的方法重新执行 + requests.forEach((cb) => cb(accessToken)); + requests = []; // 重新请求完清空 + return http.request(response.config); + }) + .catch((err) => { + cleanStorage(); + return Promise.reject(err); + }) + .finally(() => { + isRefreshing = false; + }); + } else { + // 返回未执行 resolve 的 Promise + return new Promise((resolve) => { + // 用函数形式将 resolve 存入,等待刷新后再执行 + requests.push((token) => { + response.header.accessToken = `${token}`; + resolve(http.request(response.config)); + }); + }); + } + + // 如果当前返回没登录 + } else if ( + (!token && response.statusCode === 403) || + response.data.code === 403 + ) { + cleanStorage(); + + // 如果当前状态码为正常但是success为不正常时 + } else if ( + (response.statusCode == 200 && !response.data.success) || + response.statusCode == 400 + ) { + if (response.data.message) { + uni.showToast({ + title: response.data.message, + icon: "none", + duration: 1500, + }); + } + } + return response; + }, + (error) => { + return error; + } +); + +export { http }; + +export const Method = { + GET: "GET", + POST: "POST", + PUT: "PUT", + DELETE: "DELETE", +}; diff --git a/utils/storage.js b/utils/storage.js index 9723e14..9fefeb3 100644 --- a/utils/storage.js +++ b/utils/storage.js @@ -92,11 +92,11 @@ export default { removeRefreshToken() { uni.removeStorageSync(REFRESH_TOKEN); }, - // 写入刷新token + setAfterSaleData(val) { uni.setStorageSync(AFTERSALE_DATA, val); }, - // 获取刷新token + getAfterSaleData() { return uni.getStorageSync(AFTERSALE_DATA); },