【类 型】:refactor
【主 题】:无感登录逻辑 【描 述】: [原因]:过期之后需要强制刷新 才能进行无感登录 [过程]:在首页检查内存token 没有从本地拿 都没有进行重新登录 然后 通过api验证token合法性 验证通过 顺便订阅mqtt 如果内存中没有token信息直接 登录并验证 订阅mqtt [影响]: 【结 束】 # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动
This commit is contained in:
parent
3f29f106ca
commit
2e4184429a
@ -132,7 +132,9 @@
|
||||
calculateTotal, //计算spu单价
|
||||
formatPrice, //格式化价格
|
||||
},
|
||||
onLoad() {}
|
||||
onShow() {
|
||||
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
@ -40,13 +40,12 @@ const store = new Vuex.Store({
|
||||
tabbarCurrent: 0, //tabbar的当前激活项
|
||||
cartShow: false, //列表页 购物车折叠显隐
|
||||
mqttState: false, //标记mqtt服务器是否已经连接成功
|
||||
loginState: false,//标记 用户的登录状态
|
||||
},
|
||||
//修改状态
|
||||
mutations: {
|
||||
//设置 标记用户登录状态
|
||||
setLoginState(state,val){
|
||||
Vue.set(state,'loginState', val)
|
||||
setMqttState(state,val){
|
||||
Vue.set(state,'mqttState', val)
|
||||
},
|
||||
//设置tabbar当前激活像
|
||||
setTabbarCurrent(state,val){
|
||||
|
@ -1,4 +1,5 @@
|
||||
import mqtt from './mqtt.js'
|
||||
import store from '../store/index.js'
|
||||
/**
|
||||
* Parse the time to string
|
||||
* @param {(Object|string|number)} time
|
||||
@ -132,33 +133,54 @@ export function totalPrice(cartList) {
|
||||
}
|
||||
/**
|
||||
* 检查用户信息登录 信息
|
||||
* @param {obj} store vex $store对象
|
||||
*/
|
||||
export function checkUserInfo(store) {
|
||||
export function checkUserInfo() {
|
||||
//检查是否已经标记登录了
|
||||
if (!store.state.loginState) { //没有标记
|
||||
if (store.state.userInfo.token === null) {
|
||||
//先从storage里调取token
|
||||
uni.getStorage({
|
||||
key: 'userInfo',
|
||||
success: (res) => {
|
||||
store.commit('setUserInfo', res.data) //token拿到内存当中
|
||||
//验证token
|
||||
isTokenValid().then((isValid) => {
|
||||
if (isValid.data.status === -1) { //token验证失败或过期
|
||||
//清除内存和本地的token
|
||||
store.commit('clearUserInfo')
|
||||
uni.removeStorage({
|
||||
key: 'userInfo'
|
||||
})
|
||||
//重新登录并且把token写入本地和内存 登录失败跳转到登陆页面
|
||||
wxLogin()
|
||||
} else { //token验证成功之后
|
||||
if (!store.state.mqttState) { //判断是否已经订阅了mqtt
|
||||
initMqtt() //连接mqtt 并订阅 和执行回调逻辑
|
||||
store.commit('setMqttState', true) //标记订阅
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: (err) => { //storage没有token进行登录
|
||||
wxLogin(store)
|
||||
//重新登录并且把token写入本地和内存 登录失败跳转到登陆页面
|
||||
wxLogin()
|
||||
}
|
||||
})
|
||||
//再进行检测
|
||||
isTokenValid(store).then((isValid) => { //请求判断token是否有效
|
||||
if (isValid) { //如果token有效
|
||||
store.commit('setLoginState', true) //标记用户已成功登录
|
||||
initMqtt(store) //mqtt初始化 连接 并 订阅
|
||||
console.log('hi')
|
||||
} else { //仍然无效 则最终跳转到 登陆页面
|
||||
store.commit('clearUserInfo') //清除内存中的token
|
||||
wxLogin(store)
|
||||
uni.redirectTo({
|
||||
url: '/pages/main/login'
|
||||
} else {
|
||||
//验证token
|
||||
isTokenValid().then((isValid) => {
|
||||
if (isValid.data.status === -1) { //token验证失败或过期
|
||||
//清除内存和本地的token
|
||||
store.commit('clearUserInfo')
|
||||
uni.removeStorage({
|
||||
key: 'userInfo'
|
||||
})
|
||||
//重新登录并且把token写入本地和内存 登录失败跳转到登陆页面
|
||||
wxLogin()
|
||||
} else { //token验证成功之后
|
||||
if (!store.state.mqttState) { //判断是否已经订阅了mqtt
|
||||
initMqtt() //连接mqtt 并订阅 和执行回调逻辑
|
||||
store.commit('setMqttState', true) //标记订阅
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -166,28 +188,19 @@ export function checkUserInfo(store) {
|
||||
|
||||
/**
|
||||
* 微信登录 ps;请求接口 判断token是否有效
|
||||
* @param {obj} store vex $store对象
|
||||
* @return true有效 false无效
|
||||
*/
|
||||
async function isTokenValid(store) {
|
||||
const res = await uni.$u.http.post('/Api/Check/index', {}, {
|
||||
async function isTokenValid() {
|
||||
const res = await uni.$u.http.get('/Api/Check/index', {
|
||||
header: {
|
||||
'Token': store.state.userInfo.token,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
})
|
||||
// 根据响应状态判断 token 是否有效
|
||||
if (res.status === -1) {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return res
|
||||
}
|
||||
/**
|
||||
* 微信登录 ps;无感登录
|
||||
* @param {obj} store vex $store对象
|
||||
*/
|
||||
export function wxLogin(store) {
|
||||
export function wxLogin() {
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: res => {
|
||||
@ -201,11 +214,6 @@ export function wxLogin(store) {
|
||||
}).then(res => {
|
||||
// 登录接口访问成功后
|
||||
if (res.data.status === 0) {
|
||||
uni.showToast({
|
||||
title: res.data.msg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
@ -222,22 +230,12 @@ export function wxLogin(store) {
|
||||
}
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '登录态失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
uni.showToast({
|
||||
title: '登录态失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
@ -248,7 +246,7 @@ export function wxLogin(store) {
|
||||
* 微信登录 ps;订阅主题
|
||||
* @param {obj} store vex $store对象
|
||||
*/
|
||||
function initMqtt(store) {
|
||||
function initMqtt() {
|
||||
/* mqtt */
|
||||
mqtt.mqttConf() // 连接mqtt
|
||||
// 订阅游客下单频道
|
||||
|
Loading…
Reference in New Issue
Block a user