【类 型】:fix
【主 题】:修改 每次打开主页时 进行用户身份判断 及进行 无感登录 登录完整后 进行mqtt主题的 订阅 【描 述】: [原因]: [过程]: [影响]: 【结 束】 # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动
This commit is contained in:
parent
d9b8a7fb23
commit
398110e764
19
App.vue
19
App.vue
@ -1,5 +1,4 @@
|
||||
<script>
|
||||
import mqtt from '@/utils/mqtt.js'
|
||||
export default {
|
||||
// 数据初始化
|
||||
onLaunch: function() {
|
||||
@ -32,25 +31,11 @@
|
||||
})
|
||||
},
|
||||
// 当应用程序被带到前台或对用户可见时调用
|
||||
onShow: function() {
|
||||
setTimeout(() => {
|
||||
this.initMqtt()
|
||||
}, 3000);
|
||||
},
|
||||
onShow: function() {},
|
||||
// 当应用程序被隐藏或最小化时调用
|
||||
onHide: function() {},
|
||||
methods: {
|
||||
initMqtt() {
|
||||
/* mqtt */
|
||||
mqtt.mqttConf() // 连接mqtt
|
||||
// 订阅游客下单频道
|
||||
const topic = `refreshQuestList/${this.$store.state.userInfo.topic_prefix}`
|
||||
mqtt.doSubscribe(topic, (res) => {
|
||||
if (res.topic.indexOf(topic) > -1) {
|
||||
console.log('新订单')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
},
|
||||
watch: {},
|
||||
}
|
||||
|
@ -50,6 +50,13 @@
|
||||
"style": {
|
||||
"navigationBarTitleText": "测试"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path" : "pages/main/login",
|
||||
"style" :
|
||||
{
|
||||
"navigationBarTitleText" : ""
|
||||
}
|
||||
}
|
||||
],
|
||||
"uniIdRouter": {}
|
||||
|
@ -58,10 +58,11 @@
|
||||
}
|
||||
},
|
||||
onReady() {
|
||||
//检查token 没有则进行无感登录
|
||||
this.checkUserInfo()
|
||||
|
||||
},
|
||||
onShow() {
|
||||
//检查token 没有则进行无感登录
|
||||
this.checkUserInfo()
|
||||
// 当页面显示时,设置tabber的激活项
|
||||
this.$store.commit('setTabbarCurrent', 0)
|
||||
},
|
||||
@ -70,10 +71,6 @@
|
||||
// 检查用户信息
|
||||
checkUserInfo() {
|
||||
return checkUserInfo(this.$store)
|
||||
},
|
||||
//微信登录 ps;无感登录
|
||||
wxLogin() {
|
||||
return wxLogin(this.$store)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,9 +40,14 @@ const store = new Vuex.Store({
|
||||
tabbarCurrent: 0, //tabbar的当前激活项
|
||||
cartShow: false, //列表页 购物车折叠显隐
|
||||
mqttState: false, //标记mqtt服务器是否已经连接成功
|
||||
loginState: false,//标记 用户的登录状态
|
||||
},
|
||||
//修改状态
|
||||
mutations: {
|
||||
//设置 标记用户登录状态
|
||||
setLoginState(state,val){
|
||||
Vue.set(state,'loginState', val)
|
||||
},
|
||||
//设置tabbar当前激活像
|
||||
setTabbarCurrent(state,val){
|
||||
Vue.set(state,'tabbarCurrent', val)
|
||||
|
@ -1,3 +1,4 @@
|
||||
import mqtt from './mqtt.js'
|
||||
/**
|
||||
* Parse the time to string
|
||||
* @param {(Object|string|number)} time
|
||||
@ -134,20 +135,51 @@ export function totalPrice(cartList) {
|
||||
* @param {obj} store vex $store对象
|
||||
*/
|
||||
export function checkUserInfo(store) {
|
||||
//用户信息 从本地缓存 提取到内存
|
||||
if (!store.state.userInfo.token) {
|
||||
//检查是否已经标记登录了
|
||||
if (!store.state.loginState) { //没有标记
|
||||
//先从storage里调取token
|
||||
uni.getStorage({
|
||||
key: 'userInfo',
|
||||
success: (res) => {
|
||||
store.commit('setUserInfo', res.data)
|
||||
store.commit('setUserInfo', res.data) //token拿到内存当中
|
||||
},
|
||||
fail: (err) => {
|
||||
fail: (err) => { //storage没有token进行登录
|
||||
wxLogin(store)
|
||||
}
|
||||
});
|
||||
})
|
||||
//再进行检测
|
||||
isTokenValid(store).then((isValid) => { //请求判断token是否有效
|
||||
if (isValid) { //如果token有效
|
||||
store.commit('setLoginState', true) //标记用户已成功登录
|
||||
initMqtt(store)//mqtt初始化 连接 并 订阅
|
||||
} else { //仍然无效 则最终跳转到 登陆页面
|
||||
uni.redirectTo({
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信登录 ps;请求接口 判断token是否有效
|
||||
* @param {obj} store vex $store对象
|
||||
* @return true有效 false无效
|
||||
*/
|
||||
async function isTokenValid(store) {
|
||||
const res = await uni.$u.http.post('/Api/Check', {}, {
|
||||
header: {
|
||||
'Token': store.state.userInfo.token,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
})
|
||||
// 根据响应状态判断 token 是否有效
|
||||
if (res.status === -1) {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 微信登录 ps;无感登录
|
||||
* @param {obj} store vex $store对象
|
||||
@ -171,6 +203,9 @@ export function wxLogin(store) {
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
} else if (res.data.status === 1) {
|
||||
//写入 用户信息缓存
|
||||
uni.setStorage({
|
||||
@ -189,6 +224,9 @@ export function wxLogin(store) {
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
@ -197,6 +235,24 @@ export function wxLogin(store) {
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
uni.redirectTo({ //无感登录失败 跳转到登陆页面
|
||||
url: '/pages/main/login'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 微信登录 ps;订阅主题
|
||||
* @param {obj} store vex $store对象
|
||||
*/
|
||||
function initMqtt(store) {
|
||||
/* mqtt */
|
||||
mqtt.mqttConf() // 连接mqtt
|
||||
// 订阅游客下单频道
|
||||
const topic = `refreshQuestList/${store.state.userInfo.topic_prefix}`
|
||||
mqtt.doSubscribe(topic, (res) => {
|
||||
if (res.topic.indexOf(topic) > -1) {
|
||||
console.log('新订单')
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user