diff --git a/components/CouponWindow.vue b/components/CouponWindow.vue index 2517467..94ae022 100644 --- a/components/CouponWindow.vue +++ b/components/CouponWindow.vue @@ -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); diff --git a/libs/login.js b/libs/login.js deleted file mode 100644 index ffafcbb..0000000 --- a/libs/login.js +++ /dev/null @@ -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); -} diff --git a/pages/authorization/index.vue b/pages/authorization/index.vue index 5b0689b..af7b6dd 100644 --- a/pages/authorization/index.vue +++ b/pages/authorization/index.vue @@ -2,7 +2,7 @@ - + 您还未允许微信登录授权,请点击下方按钮允许微信授权登录。 @@ -11,12 +11,17 @@ - + 请先登录 + + + 登录中 + + @@ -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; + // }); + } }; diff --git a/pages/user/User/index.vue b/pages/user/User/index.vue index aac0cc0..fb60075 100644 --- a/pages/user/User/index.vue +++ b/pages/user/User/index.vue @@ -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(); diff --git a/store/index.js b/store/index.js index bef10b8..405ffd3 100644 --- a/store/index.js +++ b/store/index.js @@ -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') diff --git a/utils/index.js b/utils/index.js index 014bfe8..230f9d6 100644 --- a/utils/index.js +++ b/utils/index.js @@ -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('检验授权____授权成功') - resolve('获取授权成功') + console.log('检验授权', `scope.${authorizeStr}`) + uni.getSetting({ + success(res) { + console.log(res.authSetting) + if (res.authSetting[`scope.${authorizeStr}`]) { + resolve('获取授权成功') + } else { + reject('获取授权失败') + } }, fail() { - console.log('检验授权____授权失败') - // switchTab({ - // path: '/pages/home/index', - // // query - // }); - reject('获取授权失败') + 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') }