From 2e4184429a4187d6d69201ce49acc6ef5ad4a7fb Mon Sep 17 00:00:00 2001 From: tk Date: Wed, 5 Jun 2024 17:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=B1=BB=09=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Arefactor=20=E3=80=90=E4=B8=BB=09=E9=A2=98=E3=80=91?= =?UTF-8?q?=EF=BC=9A=E6=97=A0=E6=84=9F=E7=99=BB=E5=BD=95=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20=E3=80=90=E6=8F=8F=09=E8=BF=B0=E3=80=91=EF=BC=9A=20=09[?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0]=EF=BC=9A=E8=BF=87=E6=9C=9F=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=E9=9C=80=E8=A6=81=E5=BC=BA=E5=88=B6=E5=88=B7=E6=96=B0?= =?UTF-8?q?=20=E6=89=8D=E8=83=BD=E8=BF=9B=E8=A1=8C=E6=97=A0=E6=84=9F?= =?UTF-8?q?=E7=99=BB=E5=BD=95=20=09[=E8=BF=87=E7=A8=8B]=EF=BC=9A=E5=9C=A8?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=A3=80=E6=9F=A5=E5=86=85=E5=AD=98token=20?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E4=BB=8E=E6=9C=AC=E5=9C=B0=E6=8B=BF=20?= =?UTF-8?q?=E9=83=BD=E6=B2=A1=E6=9C=89=E8=BF=9B=E8=A1=8C=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E7=99=BB=E5=BD=95=20=E7=84=B6=E5=90=8E=20=E9=80=9A=E8=BF=87api?= =?UTF-8?q?=E9=AA=8C=E8=AF=81token=E5=90=88=E6=B3=95=E6=80=A7=20=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=80=9A=E8=BF=87=20=E9=A1=BA=E4=BE=BF=E8=AE=A2?= =?UTF-8?q?=E9=98=85mqtt=20=E5=A6=82=E6=9E=9C=E5=86=85=E5=AD=98=E4=B8=AD?= =?UTF-8?q?=E6=B2=A1=E6=9C=89token=E4=BF=A1=E6=81=AF=E7=9B=B4=E6=8E=A5=20?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=B9=B6=E9=AA=8C=E8=AF=81=20=E8=AE=A2?= =?UTF-8?q?=E9=98=85mqtt=20=09[=E5=BD=B1=E5=93=8D]=EF=BC=9A=20=E3=80=90?= =?UTF-8?q?=E7=BB=93=09=E6=9D=9F=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动 --- pages/shop/order.vue | 4 ++- store/index.js | 5 ++- utils/index.js | 86 ++++++++++++++++++++++---------------------- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/pages/shop/order.vue b/pages/shop/order.vue index 0a5a449..c139621 100644 --- a/pages/shop/order.vue +++ b/pages/shop/order.vue @@ -132,7 +132,9 @@ calculateTotal, //计算spu单价 formatPrice, //格式化价格 }, - onLoad() {} + onShow() { + + } } diff --git a/store/index.js b/store/index.js index 8832d2b..35241d2 100644 --- a/store/index.js +++ b/store/index.js @@ -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){ diff --git a/utils/index.js b/utils/index.js index 40d748d..8427993 100644 --- a/utils/index.js +++ b/utils/index.js @@ -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 // 订阅游客下单频道