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