优化token失效重新登录
This commit is contained in:
@ -29,7 +29,7 @@
|
||||
import {
|
||||
mapGetters
|
||||
} from "vuex";
|
||||
import toLogin from "@/libs/login";
|
||||
import {handleLoginFailure} from "@/utils";
|
||||
import {
|
||||
couponReceiveBatch
|
||||
} from "@/api/user";
|
||||
@ -52,7 +52,7 @@
|
||||
methods: {
|
||||
checked() {
|
||||
const isLogin = this.isLogin;
|
||||
if (!isLogin) return toLogin();
|
||||
if (!isLogin) return handleLoginFailure();
|
||||
|
||||
const ids = this.couponList.reduce((initial, coupon) => {
|
||||
initial.push(coupon.id);
|
||||
|
@ -1,52 +0,0 @@
|
||||
// import router from "../router";
|
||||
import store from "../store";
|
||||
import cookie from "@/utils/store/cookie";
|
||||
import {
|
||||
isWeixin,
|
||||
login,
|
||||
getCurrentPageUrl,
|
||||
getCurrentPageUrlWithArgs,
|
||||
parseQuery,
|
||||
replace,
|
||||
routerPermissions,
|
||||
handleQrCode
|
||||
} from "@/utils";
|
||||
|
||||
export default function toLogin(push, backUrl) {
|
||||
console.log('token失效,需要重新登录')
|
||||
store.commit("LOGOUT");
|
||||
// 如果没有授权,走登录的接口重新授权
|
||||
// 授权获取失败会跳转到授权的接口
|
||||
console.log(store.getters.isAuthorization, '是否有登陆权限')
|
||||
if (store.getters.isAuthorization) {
|
||||
login()
|
||||
return
|
||||
}
|
||||
console.log(store.getters.isAuthorizationPage, '判断是不是登录页面或者授权页面')
|
||||
// 判断当前是不是已经在登录页面或者授权页,防止二次跳转
|
||||
if (store.getters.isAuthorizationPage || getCurrentPageUrl() == '/pages/user/Login/index') {
|
||||
console.log('已经是登录页面或者授权页面,跳出方法')
|
||||
return
|
||||
}
|
||||
|
||||
// 判断是不是扫描的砍价海报进来的
|
||||
if (getCurrentPageUrl() == 'pages/activity/DargainDetails/index' && handleQrCode()) {
|
||||
console.log('是扫描的砍价海报进来的')
|
||||
let url = handleQrCode();
|
||||
if (url) {
|
||||
replaceLogin({
|
||||
redirect: `/${getCurrentPageUrl()}`,
|
||||
id: url.bargainId,
|
||||
partake: url.uid
|
||||
})
|
||||
} else {
|
||||
console.log('是扫描的砍价海报进来的,但是没有获取到参数')
|
||||
routerPermissions()
|
||||
}
|
||||
} else {
|
||||
console.log('无特殊情况,重新登录')
|
||||
routerPermissions()
|
||||
}
|
||||
store.commit("UPDATE_AUTHORIZATION", false);
|
||||
store.commit("UPDATE_AUTHORIZATIONPAGE", true);
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
<view class="container">
|
||||
<view v-if="!token">
|
||||
<!-- 小程序 -->
|
||||
<view v-if="$deviceType == 'routine'&&!authorize">
|
||||
<view v-if="$deviceType == 'routine'&&authorize">
|
||||
<view class="getUserInfo">
|
||||
<text>您还未允许微信登录授权,请点击下方按钮允许微信授权登录。</text>
|
||||
<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">允许微信登录授权</button>
|
||||
@ -11,12 +11,17 @@
|
||||
</view>
|
||||
</view>
|
||||
<!-- app -->
|
||||
<view v-if="$deviceType == 'app'&&!authorize">
|
||||
<view v-if="$deviceType == 'app'&&authorize">
|
||||
<view class="getUserInfo">
|
||||
<text>请先登录</text>
|
||||
<button type="primary" @click="this.toLogin">去登录</button>
|
||||
</view>
|
||||
</view>
|
||||
<view v-if="!authorize">
|
||||
<view class="getUserInfo">
|
||||
<text>登录中</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
@ -33,7 +38,7 @@ import { login, authorize } from "@/utils";
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
authorize: true
|
||||
authorize: false
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
@ -41,19 +46,25 @@ export default {
|
||||
},
|
||||
onShow() {
|
||||
this.UPDATE_AUTHORIZATIONPAGE(true);
|
||||
// 先校验用户是否授权,如果没有授权,显示授权按钮
|
||||
// // 先校验用户是否授权,如果没有授权,显示授权按钮
|
||||
console.log("先校验用户是否授权,如果没有授权,显示授权按钮");
|
||||
authorize("userInfo")
|
||||
.then(() => {})
|
||||
.then(res => {
|
||||
console.log(res);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
// 用户未授权,显示授权按钮
|
||||
this.authorize = true;
|
||||
});
|
||||
},
|
||||
onHide() {
|
||||
console.log("离开授权页面,11111");
|
||||
this.UPDATE_AUTHORIZATIONPAGE(false);
|
||||
this.changeAuthorization(false);
|
||||
},
|
||||
onUnload() {
|
||||
console.log("离开授权页面,11111");
|
||||
this.UPDATE_AUTHORIZATIONPAGE(false);
|
||||
this.changeAuthorization(false);
|
||||
},
|
||||
@ -85,7 +96,7 @@ export default {
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
uni.showToast({
|
||||
title: "登录失败",
|
||||
title: error,
|
||||
icon: "none",
|
||||
duration: 2000
|
||||
});
|
||||
@ -98,8 +109,27 @@ export default {
|
||||
});
|
||||
}
|
||||
},
|
||||
onUnload() {},
|
||||
mounted() {}
|
||||
onUnload() {
|
||||
console.log("离开授权页面,11111");
|
||||
this.UPDATE_AUTHORIZATIONPAGE(false);
|
||||
this.changeAuthorization(false);
|
||||
},
|
||||
mounted() {
|
||||
// console.log("当前是授权页面,11111");
|
||||
// this.UPDATE_AUTHORIZATIONPAGE(true);
|
||||
// // 先校验用户是否授权,如果没有授权,显示授权按钮
|
||||
// console.log("先校验用户是否授权,如果没有授权,显示授权按钮");
|
||||
// authorize("userInfo")
|
||||
// .then(res => {
|
||||
// console.log(res);
|
||||
// this.authorize = false;
|
||||
// })
|
||||
// .catch(error => {
|
||||
// console.log(error);
|
||||
// // 用户未授权,显示授权按钮
|
||||
// this.authorize = true;
|
||||
// });
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
|
@ -297,6 +297,7 @@ export default {
|
||||
that.MyMenus = res.data.routine_my_menus;
|
||||
})
|
||||
.catch(error => {
|
||||
uni.hideLoading();
|
||||
console.log(error);
|
||||
});
|
||||
},
|
||||
@ -342,7 +343,7 @@ export default {
|
||||
if (this.$store.getters.token) {
|
||||
//
|
||||
uni.showLoading({
|
||||
title: "绑定中"
|
||||
title: "加载中"
|
||||
});
|
||||
this.$store.dispatch("getUser", true);
|
||||
this.MenuUser();
|
||||
|
@ -51,6 +51,7 @@ const vuexStore = new Vuex.Store({
|
||||
cookie.set(LOGIN_KEY, token, expires_time);
|
||||
},
|
||||
LOGOUT(state) {
|
||||
console.log('清除数据')
|
||||
state.token = null;
|
||||
state.userInfo = null
|
||||
let spread = cookie.get('spread')
|
||||
|
@ -132,22 +132,21 @@ export const getProvider = (service) => {
|
||||
|
||||
export const authorize = (authorizeStr) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log('检验授权')
|
||||
uni.authorize({
|
||||
scope: `scope.${authorizeStr}`,
|
||||
success() {
|
||||
console.log('检验授权____授权成功')
|
||||
console.log('检验授权', `scope.${authorizeStr}`)
|
||||
uni.getSetting({
|
||||
success(res) {
|
||||
console.log(res.authSetting)
|
||||
if (res.authSetting[`scope.${authorizeStr}`]) {
|
||||
resolve('获取授权成功')
|
||||
},
|
||||
fail() {
|
||||
console.log('检验授权____授权失败')
|
||||
// switchTab({
|
||||
// path: '/pages/home/index',
|
||||
// // query
|
||||
// });
|
||||
} else {
|
||||
reject('获取授权失败')
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
reject('获取设置失败')
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -383,6 +382,13 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (isAuth) {
|
||||
// 有token
|
||||
if (path == '/pages/home/index' || path == '/pages/shop/GoodsClass/index' || path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') {
|
||||
// switchTab({
|
||||
// path: parseUrl(location),
|
||||
// })
|
||||
// return
|
||||
}
|
||||
|
||||
resolve({
|
||||
url: parseUrl(location),
|
||||
complete,
|
||||
@ -404,6 +410,7 @@ export const handleLoginStatus = (location, complete, fail, success) => {
|
||||
// }
|
||||
|
||||
export function routerPermissions(url, type) {
|
||||
console.log('routerPermissions', url)
|
||||
let path = url
|
||||
if (!path) {
|
||||
path = getCurrentPageUrlWithArgs()
|
||||
@ -416,6 +423,11 @@ export function routerPermissions(url, type) {
|
||||
// 自动登录
|
||||
login().then(res => {
|
||||
// 登录成功,跳转到需要跳转的页面
|
||||
console.log('登录成功,跳转页面')
|
||||
store.commit("UPDATE_AUTHORIZATIONPAGE", false);
|
||||
if (path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') {
|
||||
return
|
||||
}
|
||||
if (type == 'reLaunch') {
|
||||
reLaunch({
|
||||
path,
|
||||
@ -438,6 +450,13 @@ export function routerPermissions(url, type) {
|
||||
})
|
||||
}).catch(error => {
|
||||
// 跳转到登录页面或者授权页面
|
||||
if (path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') {
|
||||
switchTab({
|
||||
path,
|
||||
})
|
||||
store.commit("UPDATE_AUTHORIZATIONPAGE", false);
|
||||
return
|
||||
}
|
||||
reLaunch({
|
||||
path: '/pages/authorization/index',
|
||||
})
|
||||
@ -664,6 +683,8 @@ export const PosterCanvas = (store, successCallBack) => {
|
||||
|
||||
|
||||
export const handleLoginFailure = () => {
|
||||
store.commit("LOGOUT");
|
||||
store.commit("UPDATE_AUTHORIZATION", false);
|
||||
|
||||
// token 失效
|
||||
// 判断当前是不是已经在登录页面或者授权页,防止二次跳转
|
||||
@ -672,8 +693,8 @@ export const handleLoginFailure = () => {
|
||||
return
|
||||
}
|
||||
|
||||
store.commit("LOGOUT");
|
||||
store.commit("UPDATE_AUTHORIZATION", false);
|
||||
console.log('当前是授权页面')
|
||||
console.log(store.getters)
|
||||
store.commit("UPDATE_AUTHORIZATIONPAGE", true);
|
||||
|
||||
let path = getCurrentPageUrlWithArgs()
|
||||
@ -735,6 +756,8 @@ export const handleLoginFailure = () => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
routerPermissions(path, 'reLaunch')
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user