Browse Source

fix(登录): 修复登录流程并优化用户信息获取

- 在用户未登录时调用getUserInfo
- 添加微信登录功能到store模块
- 优化请求拦截器中401错误的处理逻辑,自动触发登录
- 修复直辖市地址解析逻辑,添加调试日志
master
前端-胡立永 16 hours ago
parent
commit
372021a7fe
4 changed files with 126 additions and 69 deletions
  1. +2
    -1
      pages/index.vue
  2. +7
    -2
      pages/newOrder/address.vue
  3. +40
    -2
      store/modules/user.js
  4. +77
    -64
      utils/request.js

+ 2
- 1
pages/index.vue View File

@ -885,6 +885,7 @@ import { bindCode } from '@/api/order/order.js'
this.login() this.login()
}else { }else {
this.getCouponListAuth() this.getCouponListAuth()
this.$store.commit('getUserInfo')
} }
if(this.$globalData.mainSku.length < 1 || !this.$globalData.mainSku[0].price){ 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.companionLevel = this.$globalData.newOrderData.companionLevel
} }
this.$store.commit('getUserInfo')
// //
this.getCompanionList(); this.getCompanionList();


+ 7
- 2
pages/newOrder/address.vue View File

@ -310,6 +310,9 @@
selectAddress: '' selectAddress: ''
}; };
} }
console.log(address);
// //
const municipalities = ['北京市', '上海市', '天津市', '重庆市']; const municipalities = ['北京市', '上海市', '天津市', '重庆市'];
@ -334,12 +337,14 @@
city = municipality; city = municipality;
remainingAddress = address.substring(address.indexOf(municipality) + municipality.length); remainingAddress = address.substring(address.indexOf(municipality) + municipality.length);
//
const districtMatch = remainingAddress.match(districtReg);
//
//
const districtMatch = remainingAddress.match(/^(.*?(?:区|县|市|旗|自治县|自治旗))/);
if (districtMatch) { if (districtMatch) {
district = districtMatch[1]; district = districtMatch[1];
detailAddress = remainingAddress.substring(districtMatch[0].length).trim(); detailAddress = remainingAddress.substring(districtMatch[0].length).trim();
} else { } else {
//
detailAddress = remainingAddress.trim(); detailAddress = remainingAddress.trim();
} }
} else { } else {


+ 40
- 2
store/modules/user.js View File

@ -9,9 +9,15 @@ import {
import { import {
getToken, getToken,
setToken, setToken,
removeToken
removeToken,
getOpenIdKey,
setOpenIdKey,
} from '@/utils/auth' } from '@/utils/auth'
import {
getOpenId,
} from "@/api/system/user"
const baseUrl = config.baseUrl const baseUrl = config.baseUrl
const user = { const user = {
@ -109,7 +115,39 @@ const user = {
reject(error) 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('授权失败,请授权后再试')
}
});
})
},
} }
} }

+ 77
- 64
utils/request.js View File

@ -1,72 +1,85 @@
import store from '@/store' import store from '@/store'
import { getToken } from '@/utils/auth'
import {
getToken
} from '@/utils/auth'
import errorCode from '@/utils/errorCode' 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 let timeout = 10000
const baseUrl = currentUrl const baseUrl = currentUrl
const request = config => { 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

Loading…
Cancel
Save