【类 型】:fix

【主	题】:修改 每次打开主页时 进行用户身份判断 及进行 无感登录 登录完整后 进行mqtt主题的 订阅
【描	述】:
	[原因]:
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
szdot 2024-05-31 17:22:52 +08:00
parent d9b8a7fb23
commit 398110e764
5 changed files with 78 additions and 28 deletions

19
App.vue
View File

@ -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: {},
}

View File

@ -50,6 +50,13 @@
"style": {
"navigationBarTitleText": "测试"
}
},
{
"path" : "pages/main/login",
"style" :
{
"navigationBarTitleText" : ""
}
}
],
"uniIdRouter": {}

View File

@ -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)
}
}
}

View File

@ -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)

View File

@ -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('新订单')
}
})
}