From 0f344a4d7e19e80e2a79879706b9b2ee3f20afae Mon Sep 17 00:00:00 2001 From: Gao xiaosong <704041637@qq.com> Date: Tue, 2 Jun 2020 02:34:01 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/login.js | 6 +- pages/Loading/index.vue | 21 +- pages/authorization/index.vue | 351 +++++++++++++++++----------------- utils/index.js | 186 +++++++++--------- 4 files changed, 282 insertions(+), 282 deletions(-) diff --git a/libs/login.js b/libs/login.js index e7c681a..ffafcbb 100644 --- a/libs/login.js +++ b/libs/login.js @@ -4,11 +4,11 @@ import cookie from "@/utils/store/cookie"; import { isWeixin, login, - replaceLogin, getCurrentPageUrl, getCurrentPageUrlWithArgs, parseQuery, replace, + routerPermissions, handleQrCode } from "@/utils"; @@ -41,11 +41,11 @@ export default function toLogin(push, backUrl) { }) } else { console.log('是扫描的砍价海报进来的,但是没有获取到参数') - replaceLogin() + routerPermissions() } } else { console.log('无特殊情况,重新登录') - replaceLogin() + routerPermissions() } store.commit("UPDATE_AUTHORIZATION", false); store.commit("UPDATE_AUTHORIZATIONPAGE", true); diff --git a/pages/Loading/index.vue b/pages/Loading/index.vue index 943f58f..42dc004 100644 --- a/pages/Loading/index.vue +++ b/pages/Loading/index.vue @@ -43,22 +43,23 @@ export default { } cookie.get("spread"); // this.toLaunch(); - if (this.$deviceType == 'app') { - // this.toLaunch(); - this.$yrouter.switchTab({ - path: '/pages/home/index', - }); - return + if (this.$deviceType == "app") { + // this.toLaunch(); + this.$yrouter.switchTab({ + path: "/pages/home/index" + }); + return; } - login({ - fail: () => { - this.toLaunch(); - } + login().finally(() => { + this.$yrouter.switchTab({ + path: "/pages/home/index" + }); }); }, methods: { ...mapActions(["changeAuthorization", "setUserInfo"]), toLaunch() { + console.log("loading home"); this.changeAuthorization(false); this.$yrouter.switchTab({ path: "/pages/home/index" diff --git a/pages/authorization/index.vue b/pages/authorization/index.vue index 880dc6d..0479a04 100644 --- a/pages/authorization/index.vue +++ b/pages/authorization/index.vue @@ -1,211 +1,218 @@ diff --git a/utils/index.js b/utils/index.js index 0e9dac6..d44e845 100644 --- a/utils/index.js +++ b/utils/index.js @@ -109,46 +109,6 @@ export const copyClipboard = (data) => { }) } - -export const replaceLogin = (msg) => { - uni.hideLoading(); - console.log('尝试开始重新登录') - uni.showToast({ - title: '重新登录中...', - icon: 'none', - duration: 2000 - }); - // 这里代表已经失去登录状态以及401强制推出登录了 - store.commit('LOGOUT') - if (Vue.prototype.$deviceType == 'routine') { - console.log('当前是微信小程序,开始调用登录方法') - // 如果是微信小程序,跳转到授权页 - login({ - fail: () => { - console.log('自动登录失败,重定向授权页面') - reLaunch({ - path: '/pages/authorization/index', - query: { - redirect: `/${getCurrentPageUrl()}`, - ...parseQuery() - } - }) - } - }) - - } else { - // 如果不是小程序跳转到登录页 - console.log('当前是app,跳转到登录页面') - push({ - path: '/pages/user/Login/index', - query: { - redirect: `/${getCurrentPageUrl()}`, - ...parseQuery() - } - }) - } -} - export const getProvider = (service) => { return new Promise((resolve, reject) => { // 获取当前环境的服务商 @@ -172,27 +132,26 @@ export const getProvider = (service) => { export const authorize = (authorizeStr) => { return new Promise((resolve, reject) => { + console.log('检验授权') uni.authorize({ scope: `scope.${authorizeStr}`, success() { + console.log('检验授权____授权成功') resolve('获取授权成功') }, fail() { - - console.log('授权失败跳转首页') - switchTab({ - path: '/pages/home/index', - // query - }); + console.log('检验授权____授权失败') + // switchTab({ + // path: '/pages/home/index', + // // query + // }); reject('获取授权失败') } }) - }).catch(error => { - console.log(error) }) } -export const login = (option) => { +export const login = () => { console.log('开始登录 ————————————————————') return new Promise((resolve, reject) => { console.log('获取环境商') @@ -215,62 +174,48 @@ export const login = (option) => { success: function (user) { console.log('获取用户信息成功') console.log('开始调用登录接口') - if (Vue.prototype.$deviceType == 'routine') { - wxappAuth({ - encryptedData: user.encryptedData, - iv: user.iv, - code: code, - spread: cookie.get("spread") - }).then(({ - data - }) => { - console.log('登录接口调用成功') - console.log('开始处理登录信息保存,并获取用户详情') - resolve(data) - uni.hideLoading(); - store.commit("LOGIN", data.token, dayjs(data.expires_time)); - store.dispatch('USERINFO', true) - console.log(store) - handleGetUserInfo() + wxappAuth({ + encryptedData: user.encryptedData, + iv: user.iv, + code: code, + spread: cookie.get("spread") + }).then(({ data }) => { + console.log('登录接口调用成功') + console.log('开始处理登录信息保存,并获取用户详情') + uni.hideLoading(); + store.commit("LOGIN", data.token, dayjs(data.expires_time)); + store.dispatch('USERINFO', true) + getUser().then(user => { + console.log('获取用户信息成功') + store.dispatch('setUserInfo', user.data) + resolve(user) }).catch(error => { - console.log('登录接口调用失败') - reject() - console.log(error) - handleFail(option, '微信登录失败') + console.log('获取用户信息失败') + reject('获取用户信息失败') }); - } + }).catch(error => { + console.log('登录接口调用失败') + reject('登录接口调用失败') + }); }, fail() { console.log('获取用户信息失败') - // 获取用户信息失败 - reject() - handleFail(option, '获取用户信息失败') + reject('获取用户信息失败') } }); }).catch(error => { console.log('用户未授权') - reject() - console.log(error) - handleFail(option, '用户未授权') + reject('用户未授权') }) }, fail() { console.log('调用登录接口失败') - // 调用登录接口失败 - reject() - handleFail(option, '登录失败') + reject('调用登录接口失败') } }); }).catch(error => { - handleFail(option, '获取环境服务商失败') - reject() - console.log(error) - handleFail(option, '获取环境服务商失败') + reject('获取环境服务商失败') }) - }).catch(error => { - handleFail(option, '登录失败') - console.log(error) - handleFail(option, '登录失败') }) } @@ -343,18 +288,13 @@ export const handleGetUserInfo = () => { }) } - -const handleFail = (option, msg) => { - // 此处是处理登录失效的问题的 - option && option.fail ? option.fail() : replaceLogin('登录失败,请重新登录') -} - export function parseUrl(location) { if (typeof location === 'string') return location const { path, query } = location + const queryStr = stringify(query) if (!queryStr) { @@ -419,6 +359,8 @@ export const handleLoginStatus = (location, complete, fail, success) => { // 是否可以访问 let isAuth = false + console.log('即将跳转', location, parseUrl(location)) + // 从 location 中获取当前url,location typeof string || object let path = '' if (typeof location === 'string') { @@ -427,6 +369,7 @@ export const handleLoginStatus = (location, complete, fail, success) => { path = location.path } + // 判断用户是否有token if (!auth()) { page.map((item) => { if (item.path == path) { @@ -439,7 +382,7 @@ export const handleLoginStatus = (location, complete, fail, success) => { return new Promise((resolve, reject) => { if (isAuth) { - // 登录了有权限 + // 有token resolve({ url: parseUrl(location), complete, @@ -447,8 +390,8 @@ export const handleLoginStatus = (location, complete, fail, success) => { success }) } else { - // 未登录没有权限 - replaceLogin() + // 没有token,先校验用户是否授权,如果授权了,进行自动登录 + routerPermissions(parseUrl(location)) reject() } }).catch(error => { @@ -456,6 +399,55 @@ export const handleLoginStatus = (location, complete, fail, success) => { }) } +// export function checkPermissions(){ + +// } + +export function routerPermissions(url) { + let path = url + if (!path) { + path = getCurrentPageUrlWithArgs() + } + if (Vue.prototype.$deviceType == 'routine') { + console.log('当前是微信小程序,开始处理小程序登录方法') + // 如果是微信小程序,跳转到授权页 + // 先校验用户是否授权,如果授权了,进行自动登录 + authorize('userInfo').then(() => { + // 自动登录 + login().then(res => { + // 登录成功,跳转到需要跳转的页面 + push({ + path, + }) + }).catch(error => { + uni.showToast({ + title: error, + icon: "none", + duration: 2000 + }); + reLaunch({ + path: '/pages/authorization/index', + }) + cookie.set('redirect', path) + }) + }).catch(error => { + // 跳转到登录页面或者授权页面 + reLaunch({ + path: '/pages/authorization/index', + }) + cookie.set('redirect', path) + }) + } else { + // 如果不是小程序跳转到登录页 + console.log('当前是app,开始处理app登录方法') + push({ + path: '/pages/user/Login/index', + }) + cookie.set('redirect', path) + } + +} + export function push(location, complete, fail, success) { handleLoginStatus(location, complete, fail, success).then(params => { uni.navigateTo(params) From 614d0607fd51eec5c276338b38aa59bb1e6e0bcb Mon Sep 17 00:00:00 2001 From: Gao xiaosong <704041637@qq.com> Date: Tue, 2 Jun 2020 02:34:11 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/shop/GoodsCon/index.vue | 1 + 1 file changed, 1 insertion(+) diff --git a/pages/shop/GoodsCon/index.vue b/pages/shop/GoodsCon/index.vue index 98a3137..14d3919 100644 --- a/pages/shop/GoodsCon/index.vue +++ b/pages/shop/GoodsCon/index.vue @@ -266,6 +266,7 @@ export default { console.log(url); if (url && url.productId) { this.id = url.productId; + } else { this.id = this._route.query.id; } From 15694002e8f46be2aabf4fd6a842c191bd29717f Mon Sep 17 00:00:00 2001 From: gaoxs Date: Tue, 2 Jun 2020 20:09:02 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/index.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ utils/request.js | 10 +++--- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/utils/index.js b/utils/index.js index d44e845..d37b122 100644 --- a/utils/index.js +++ b/utils/index.js @@ -654,3 +654,94 @@ export const PosterCanvas = (store, successCallBack) => { // } // }) } + + + +export const handleLoginFailure = () => { + + // token 失效 + // 判断当前是不是已经在登录页面或者授权页,防止二次跳转 + if (store.getters.isAuthorizationPage || getCurrentPageUrl() == '/pages/user/Login/index') { + console.log('已经是登录页面或者授权页面,跳出方法') + return + } + + store.commit("LOGOUT"); + store.commit("UPDATE_AUTHORIZATION", false); + store.commit("UPDATE_AUTHORIZATIONPAGE", true); + + let path = getCurrentPageUrlWithArgs() + + // 判断是不是拼团进来的 + if (getCurrentPageUrl() == 'pages/activity/GroupRule/index' && handleQrCode()) { + console.log('是拼团进来的') + let url = handleQrCode(); + console.log(url) + if (url) { + path = parseUrl({ + path: `/${getCurrentPageUrl()}`, + query: { + id: url.pinkId, + } + }) + // cookie.set("spread", url.spread || 0); + } else { + console.log('是拼团进来的,但是没有获取到参数') + switchTab({ + path: '/pages/home/index', + }); + } + } + + // 判断是不是扫描的砍价海报进来的 + if (getCurrentPageUrl() == 'pages/activity/DargainDetails/index' && handleQrCode()) { + console.log('是扫描的砍价海报进来的') + let url = handleQrCode(); + console.log(url) + // bargainId: "36" +// bargainUserUid: 1404 + +// bargainId: "36" +// codeType: "routine" +// pageType: "dargain" +// spread: "1404" +// uid: "1404" + if (url) { + path = parseUrl({ + path: `/${getCurrentPageUrl()}`, + query: { + id: url.bargainId, + partake: url.uid + } + }) + // cookie.set("spread", url.spread || 0); + } else { + console.log('是扫描的砍价海报进来的,但是没有获取到参数') + switchTab({ + path: '/pages/home/index', + }); + } + } + + if (getCurrentPageUrl() == 'pages/shop/GoodsCon/index' && handleQrCode()) { + console.log('是扫描的商品详情') + let url = handleQrCode(); + console.log(url) + if (url) { + path = parseUrl({ + path: `/${getCurrentPageUrl()}`, + query: { + id: url.productId, + } + }) + cookie.set("spread", url.spread || 0); + } else { + switchTab({ + path: '/pages/home/index', + }); + console.log('是扫描的商品详情进来的,但是没有获取到参数') + } + } + + routerPermissions(path) +} diff --git a/utils/request.js b/utils/request.js index d55f9bd..8c4e980 100644 --- a/utils/request.js +++ b/utils/request.js @@ -1,6 +1,6 @@ import Fly from "flyio/dist/npm/wx"; import $store from "../store"; -import toLogin from "@/libs/login"; +import { handleLoginFailure } from "@/utils"; import { VUE_APP_API_URL } from "@/config"; import cookie from "@/utils/store/cookie"; @@ -16,11 +16,11 @@ fly.interceptors.response.use( }, error => { if (error.toString() == 'Error: Network Error') { - toLogin(); + handleLoginFailure(); return Promise.reject({ msg: "未登录", toLogin: true }); } if (error.status == 401) { - toLogin(); + handleLoginFailure(); return Promise.reject({ msg: "未登录", toLogin: true }); } return Promise.reject(error); @@ -44,7 +44,7 @@ function baseRequest(options) { // 如果需要登录才可访问的接口没有拿到 token 视为登录失效 if (options.login === true && !token) { // 跳转到登录或授权页面 - toLogin(); + handleLoginFailure(); // 提示错误信息 return Promise.reject({ msg: "未登录", toLogin: true }); } @@ -60,7 +60,7 @@ function baseRequest(options) { return Promise.reject({ msg: "请求失败", res, data }); } if ([410000, 410001, 410002].indexOf(data.status) !== -1) { - toLogin(); + handleLoginFailure(); return Promise.reject({ msg: res.data.msg, res, data, toLogin: true }); } else if (data.status === 200) { return Promise.resolve(data, res); From d7e08753815556c2b3ffe6af48904e14724ccd0c Mon Sep 17 00:00:00 2001 From: gaoxs Date: Wed, 3 Jun 2020 10:58:31 +0800 Subject: [PATCH 4/6] =?UTF-8?q?1.=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=B5=81=E7=A8=8B=202.uniapp=E8=AF=84=E4=BB=B7=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=90=8E=E8=A6=81=E8=BF=9E=E9=9C=803-4=E6=AC=A1?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=89=8D=E8=83=BD=E5=88=B0=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=EF=BC=88=E4=BD=93=E9=AA=8C=E4=B8=8D=E6=98=AF=E5=BE=88=E5=A5=BD?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/authorization/index.vue | 17 ++++++--- pages/order/MyOrder/index.vue | 31 ++++++++-------- pages/order/OrderSubmission/index.vue | 6 ++-- pages/shop/GoodsCon/index.vue | 6 ++-- pages/shop/GoodsEvaluate/index.vue | 22 ++++++++---- pages/user/User/index.vue | 19 ++++++---- pages/user/promotion/Poster/index.vue | 6 ++-- utils/index.js | 52 +++++++++++++++------------ 8 files changed, 94 insertions(+), 65 deletions(-) diff --git a/pages/authorization/index.vue b/pages/authorization/index.vue index 0479a04..5b0689b 100644 --- a/pages/authorization/index.vue +++ b/pages/authorization/index.vue @@ -2,7 +2,7 @@ - + 您还未允许微信登录授权,请点击下方按钮允许微信授权登录。 @@ -11,7 +11,7 @@ - + 请先登录 @@ -28,17 +28,26 @@ import { mapState, mapMutations, mapActions } from "vuex"; import { wxappAuth, getUser } from "@/api/user"; import dayjs from "dayjs"; import cookie from "@/utils/store/cookie"; -import { login } from "@/utils"; +import { login, authorize } from "@/utils"; export default { data() { - return {}; + return { + authorize: true + }; }, computed: { ...mapState(["isAuthorization", "$deviceType", "token"]) }, onShow() { this.UPDATE_AUTHORIZATIONPAGE(true); + // 先校验用户是否授权,如果没有授权,显示授权按钮 + authorize("userInfo") + .then(() => {}) + .catch(error => { + // 用户未授权,显示授权按钮 + this.authorize = true; + }); }, onHide() { this.UPDATE_AUTHORIZATIONPAGE(false); diff --git a/pages/order/MyOrder/index.vue b/pages/order/MyOrder/index.vue index f6a233f..2895e56 100644 --- a/pages/order/MyOrder/index.vue +++ b/pages/order/MyOrder/index.vue @@ -199,17 +199,19 @@ export default { DataFormat }, computed: mapGetters(["userInfo"]), - watch: { - $yroute(n) { - if (n.name === NAME) { - const type = parseInt(this.$yroute.query.type) || 0; - if (this.type !== type) { - this.changeType(type); - } - this.getOrderData(); - } - }, - type() {} + onShow: function() { + console.log(this) + this.type = parseInt(this.$yroute.query.type) || 0; + this.changeType(this.type); + this.getOrderData(); + this.getOrderList(); + }, + onHide: function() { + this.orderList = []; + this.page = 1; + this.limit = 20; + this.loaded = false; + this.loading = false; }, methods: { goLogistics(order) { @@ -308,11 +310,7 @@ export default { }, toPay() {} }, - mounted() { - this.type = parseInt(this.$yroute.query.type) || 0; - this.getOrderData(); - this.getOrderList(); - }, + mounted() {}, onReachBottom() { !this.loading && this.getOrderList(); } @@ -320,7 +318,6 @@ export default {