From 372021a7fe79a27d6edcb201413d68c8a027c7c4 Mon Sep 17 00:00:00 2001 From: huliyong <2783385703@qq.com> Date: Fri, 12 Sep 2025 13:55:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=99=BB=E5=BD=95):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=B5=81=E7=A8=8B=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在用户未登录时调用getUserInfo - 添加微信登录功能到store模块 - 优化请求拦截器中401错误的处理逻辑,自动触发登录 - 修复直辖市地址解析逻辑,添加调试日志 --- pages/index.vue | 3 +- pages/newOrder/address.vue | 9 ++- store/modules/user.js | 42 +++++++++++++- utils/request.js | 141 +++++++++++++++++++++++++-------------------- 4 files changed, 126 insertions(+), 69 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 2c77b9e..5646f7b 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -885,6 +885,7 @@ import { bindCode } from '@/api/order/order.js' this.login() }else { this.getCouponListAuth() + this.$store.commit('getUserInfo') } if(this.$globalData.mainSku.length < 1 || !this.$globalData.mainSku[0].price){ // 获取主产品 @@ -904,7 +905,7 @@ import { bindCode } from '@/api/order/order.js' this.companionLevel = this.$globalData.newOrderData.companionLevel } - this.$store.commit('getUserInfo') + // 获取周边伴宠师数据 this.getCompanionList(); diff --git a/pages/newOrder/address.vue b/pages/newOrder/address.vue index faa9527..b3b0f24 100644 --- a/pages/newOrder/address.vue +++ b/pages/newOrder/address.vue @@ -310,6 +310,9 @@ selectAddress: '' }; } + + console.log(address); + // 直辖市列表 const municipalities = ['北京市', '上海市', '天津市', '重庆市']; @@ -334,12 +337,14 @@ city = municipality; remainingAddress = address.substring(address.indexOf(municipality) + municipality.length); - // 解析区县 - const districtMatch = remainingAddress.match(districtReg); + // 对于直辖市,需要特殊处理区县解析 + // 先尝试匹配区县名称 + const districtMatch = remainingAddress.match(/^(.*?(?:区|县|市|旗|自治县|自治旗))/); if (districtMatch) { district = districtMatch[1]; detailAddress = remainingAddress.substring(districtMatch[0].length).trim(); } else { + // 如果没有匹配到区县,整个剩余部分作为详细地址 detailAddress = remainingAddress.trim(); } } else { diff --git a/store/modules/user.js b/store/modules/user.js index 0c6b418..44f87c0 100644 --- a/store/modules/user.js +++ b/store/modules/user.js @@ -9,9 +9,15 @@ import { import { getToken, setToken, - removeToken + removeToken, + getOpenIdKey, + setOpenIdKey, } from '@/utils/auth' +import { + getOpenId, +} from "@/api/system/user" + const baseUrl = config.baseUrl const user = { @@ -109,7 +115,39 @@ const user = { reject(error) }) }) - } + }, + login() { + return new Promise((resolve, reject) => { + uni.showLoading({ + title: '登录中...', + zIndex : 9999999 + }) + uni.login({ + provider: 'weixin', + success: (loginRes) => { + getOpenId(loginRes.code).then(res => { + uni.hideLoading() + if (res.code == 200 && res.data) { + let resData = JSON.parse(res.data) + let token = resData.token; + let openId = resData.openId; + setOpenIdKey(openId) + if (token) { + setToken(token) + resolve(token) + } + } + }).catch(e => { + uni.hideLoading() + }) + }, + fail: function(error) { + reject(error) + uni.showToast('授权失败,请授权后再试') + } + }); + }) + }, } } diff --git a/utils/request.js b/utils/request.js index 02adf8b..8a64c49 100644 --- a/utils/request.js +++ b/utils/request.js @@ -1,72 +1,85 @@ import store from '@/store' -import { getToken } from '@/utils/auth' +import { + getToken +} from '@/utils/auth' import errorCode from '@/utils/errorCode' -import { toast, showConfirm, tansParams } from '@/utils/common' -import { currentUrl } from '@/utils/getUrl' +import { + toast, + showConfirm, + tansParams +} from '@/utils/common' +import { + currentUrl +} from '@/utils/getUrl' let timeout = 10000 const baseUrl = currentUrl const request = config => { - // 是否需要设置 token - const isToken = (config.headers || {}).isToken || false - config.header = config.header || {} - if (getToken() && isToken) { - config.header['Authorization'] = 'Bearer ' + getToken() - } - // get请求映射params参数 - if (config.params) { - let url = config.url + '?' + tansParams(config.params) - url = url.slice(0, -1) - config.url = url - } - return new Promise((resolve, reject) => { - uni.request({ - method: config.method || 'get', - timeout: config.timeout || timeout, - url: config.baseUrl || baseUrl + config.url, - data: config.data, - header: config.header, - dataType: 'json' - }).then(response => { - let [error, res] = response - if (error) { - toast('后端接口连接异常') - reject('后端接口连接异常') - return - } - const code = res.data.code || 200 - const msg = errorCode[code] || res.data.msg || errorCode['default'] - if (code === 401) { - showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => { - if (res.confirm) { - store.dispatch('LogOut').then(res => { - uni.reLaunch({ url: '/pages/index' }) - }) - } - }) - reject('无效的会话,或者会话已过期,请重新登录。') - } else if (code === 500) { - toast(msg) - reject('500') - } else if (code !== 200) { - toast(msg) - reject(code) - } - resolve(res.data) - }) - .catch(error => { - let { message } = error - if (message === 'Network Error') { - message = '后端接口连接异常' - } else if (message.includes('timeout')) { - message = '系统接口请求超时' - } else if (message.includes('Request failed with status code')) { - message = '系统接口' + message.substr(message.length - 3) + '异常' - } - toast(message) - reject(error) - }) - }) + // 是否需要设置 token + const isToken = (config.headers || {}).isToken || false + config.header = config.header || {} + if (getToken() && isToken) { + config.header['Authorization'] = 'Bearer ' + getToken() + } + // get请求映射params参数 + if (config.params) { + let url = config.url + '?' + tansParams(config.params) + url = url.slice(0, -1) + config.url = url + } + return new Promise((resolve, reject) => { + uni.request({ + method: config.method || 'get', + timeout: config.timeout || timeout, + url: config.baseUrl || baseUrl + config.url, + data: config.data, + header: config.header, + dataType: 'json' + }).then(response => { + let [error, res] = response + if (error) { + toast('后端接口连接异常') + reject('后端接口连接异常') + return + } + const code = res.data.code || 200 + const msg = errorCode[code] || res.data.msg || errorCode['default'] + if (code === 401) { + store.dispatch('LogOut') + showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => { + if (res.confirm) { + store.dispatch('login').then(res => { + uni.reLaunch({ + url: '/pages/index' + }) + }) + } + }) + reject('无效的会话,或者会话已过期,请重新登录。') + } else if (code === 500) { + toast(msg) + reject('500') + } else if (code !== 200) { + toast(msg) + reject(code) + } + resolve(res.data) + }) + .catch(error => { + let { + message + } = error + if (message === 'Network Error') { + message = '后端接口连接异常' + } else if (message.includes('timeout')) { + message = '系统接口请求超时' + } else if (message.includes('Request failed with status code')) { + message = '系统接口' + message.substr(message.length - 3) + '异常' + } + toast(message) + reject(error) + }) + }) } -export default request +export default request \ No newline at end of file