Browse Source

Merge branch 'wlogin'

master
gaoxs 5 years ago
parent
commit
840b52e59f
  1. 4
      components/CouponWindow.vue
  2. 52
      libs/login.js
  3. 15
      pages/Loading/index.vue
  4. 82
      pages/authorization/index.vue
  5. 27
      pages/order/MyOrder/index.vue
  6. 6
      pages/order/OrderSubmission/index.vue
  7. 7
      pages/shop/GoodsCon/index.vue
  8. 22
      pages/shop/GoodsEvaluate/index.vue
  9. 21
      pages/user/User/index.vue
  10. 6
      pages/user/promotion/Poster/index.vue
  11. 1
      store/index.js
  12. 292
      utils/index.js
  13. 10
      utils/request.js

4
components/CouponWindow.vue

@ -29,7 +29,7 @@
import { import {
mapGetters mapGetters
} from "vuex"; } from "vuex";
import toLogin from "@/libs/login"; import {handleLoginFailure} from "@/utils";
import { import {
couponReceiveBatch couponReceiveBatch
} from "@/api/user"; } from "@/api/user";
@ -52,7 +52,7 @@
methods: { methods: {
checked() { checked() {
const isLogin = this.isLogin; const isLogin = this.isLogin;
if (!isLogin) return toLogin(); if (!isLogin) return handleLoginFailure();
const ids = this.couponList.reduce((initial, coupon) => { const ids = this.couponList.reduce((initial, coupon) => {
initial.push(coupon.id); initial.push(coupon.id);

52
libs/login.js

@ -1,52 +0,0 @@
// import router from "../router";
import store from "../store";
import cookie from "@/utils/store/cookie";
import {
isWeixin,
login,
replaceLogin,
getCurrentPageUrl,
getCurrentPageUrlWithArgs,
parseQuery,
replace,
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('是扫描的砍价海报进来的,但是没有获取到参数')
replaceLogin()
}
} else {
console.log('无特殊情况,重新登录')
replaceLogin()
}
store.commit("UPDATE_AUTHORIZATION", false);
store.commit("UPDATE_AUTHORIZATIONPAGE", true);
}

15
pages/Loading/index.vue

@ -43,22 +43,23 @@ export default {
} }
cookie.get("spread"); cookie.get("spread");
// this.toLaunch(); // this.toLaunch();
if (this.$deviceType == 'app') { if (this.$deviceType == "app") {
// this.toLaunch(); // this.toLaunch();
this.$yrouter.switchTab({ this.$yrouter.switchTab({
path: '/pages/home/index', path: "/pages/home/index"
}); });
return return;
}
login({
fail: () => {
this.toLaunch();
} }
login().finally(() => {
this.$yrouter.switchTab({
path: "/pages/home/index"
});
}); });
}, },
methods: { methods: {
...mapActions(["changeAuthorization", "setUserInfo"]), ...mapActions(["changeAuthorization", "setUserInfo"]),
toLaunch() { toLaunch() {
console.log("loading home");
this.changeAuthorization(false); this.changeAuthorization(false);
this.$yrouter.switchTab({ this.$yrouter.switchTab({
path: "/pages/home/index" path: "/pages/home/index"

82
pages/authorization/index.vue

@ -2,7 +2,7 @@
<view class="container"> <view class="container">
<view v-if="!token"> <view v-if="!token">
<!-- 小程序 --> <!-- 小程序 -->
<view v-if="$deviceType == 'routine'"> <view v-if="$deviceType == 'routine'&&authorize">
<view class="getUserInfo"> <view class="getUserInfo">
<text>您还未允许微信登录授权请点击下方按钮允许微信授权登录</text> <text>您还未允许微信登录授权请点击下方按钮允许微信授权登录</text>
<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">允许微信登录授权</button> <button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">允许微信登录授权</button>
@ -11,78 +11,122 @@
</view> </view>
</view> </view>
<!-- app --> <!-- app -->
<view v-if="$deviceType == 'app'"> <view v-if="$deviceType == 'app'&&authorize">
<view class="getUserInfo"> <view class="getUserInfo">
<text>请先登录</text> <text>请先登录</text>
<button type="primary" @click="this.toLogin">去登录</button> <button type="primary" @click="this.toLogin">去登录</button>
</view> </view>
</view> </view>
<view v-if="!authorize">
<view class="getUserInfo">
<text>登录中</text>
</view>
</view>
</view> </view>
</view> </view>
</template> </template>
<script> <script>
import { mapState, mapMutations, mapActions } from 'vuex'; import { mapState, mapMutations, mapActions } from "vuex";
// //
// import request from "@//api/request"; // import request from "@//api/request";
import { wxappAuth, getUser } from '@/api/user'; import { wxappAuth, getUser } from "@/api/user";
import dayjs from 'dayjs'; import dayjs from "dayjs";
import cookie from '@/utils/store/cookie'; import cookie from "@/utils/store/cookie";
import { login } from '@/utils'; import { login, authorize } from "@/utils";
export default { export default {
data() { data() {
return { return {
authorize: false
}; };
}, },
computed: { computed: {
...mapState(['isAuthorization','$deviceType','token']) ...mapState(["isAuthorization", "$deviceType", "token"])
}, },
onShow() { onShow() {
this.UPDATE_AUTHORIZATIONPAGE(true); this.UPDATE_AUTHORIZATIONPAGE(true);
// //
console.log("先校验用户是否授权,如果没有授权,显示授权按钮");
authorize("userInfo")
.then(res => {
console.log(res);
})
.catch(error => {
console.log(error);
//
this.authorize = true;
});
}, },
onHide() { onHide() {
console.log("离开授权页面,11111");
this.UPDATE_AUTHORIZATIONPAGE(false); this.UPDATE_AUTHORIZATIONPAGE(false);
this.changeAuthorization(false); this.changeAuthorization(false);
}, },
onUnload() { onUnload() {
console.log("离开授权页面,11111");
this.UPDATE_AUTHORIZATIONPAGE(false); this.UPDATE_AUTHORIZATIONPAGE(false);
this.changeAuthorization(false); this.changeAuthorization(false);
}, },
methods: { methods: {
...mapActions(['changeAuthorization', 'setUserInfo']), ...mapActions(["changeAuthorization", "setUserInfo"]),
...mapMutations(['UPDATE_AUTHORIZATIONPAGE', 'CHANGE_TABTAR']), ...mapMutations(["UPDATE_AUTHORIZATIONPAGE", "CHANGE_TABTAR"]),
back() { back() {
this.$yrouter.switchTab({ this.$yrouter.switchTab({
path: '/pages/home/index', path: "/pages/home/index",
query: {} query: {}
}); });
}, },
getUserInfo(data) { getUserInfo(data) {
if (data.detail.errMsg == 'getUserInfo:fail auth deny') { if (data.detail.errMsg == "getUserInfo:fail auth deny") {
uni.showToast({ uni.showToast({
title: '取消授权', title: "取消授权",
icon: 'none', icon: "none",
duration: 2000 duration: 2000
}); });
return; return;
} }
uni.showLoading({ uni.showLoading({
title: '登录中' title: "登录中"
});
login()
.then(res => {
this.$yrouter.replace({ path: cookie.get("redirect") });
})
.catch(error => {
console.log(error);
uni.showToast({
title: error,
icon: "none",
duration: 2000
}); });
login({
success: () => {}
}); });
}, },
toLogin() { toLogin() {
this.$yrouter.push({ this.$yrouter.push({
path: '/pages/user/Login/index', path: "/pages/user/Login/index",
query: {} query: {}
}); });
} }
}, },
onUnload() {}, onUnload() {
console.log("离开授权页面,11111");
this.UPDATE_AUTHORIZATIONPAGE(false);
this.changeAuthorization(false);
},
mounted() { mounted() {
this.UPDATE_AUTHORIZATIONPAGE(true);
// //
console.log("先校验用户是否授权,如果没有授权,显示授权按钮");
authorize("userInfo")
.then(res => {
console.log(res);
})
.catch(error => {
console.log(error);
//
this.authorize = true;
});
} }
}; };
</script> </script>

27
pages/order/MyOrder/index.vue

@ -199,17 +199,19 @@ export default {
DataFormat DataFormat
}, },
computed: mapGetters(["userInfo"]), computed: mapGetters(["userInfo"]),
watch: { onShow: function() {
$yroute(n) { console.log(this)
if (n.name === NAME) { this.type = parseInt(this.$yroute.query.type) || 0;
const type = parseInt(this.$yroute.query.type) || 0; this.changeType(this.type);
if (this.type !== type) {
this.changeType(type);
}
this.getOrderData(); this.getOrderData();
} this.getOrderList();
}, },
type() {} onHide: function() {
this.orderList = [];
this.page = 1;
this.limit = 20;
this.loaded = false;
this.loading = false;
}, },
methods: { methods: {
goLogistics(order) { goLogistics(order) {
@ -308,11 +310,7 @@ export default {
}, },
toPay() {} toPay() {}
}, },
mounted() { mounted() {},
this.type = parseInt(this.$yroute.query.type) || 0;
this.getOrderData();
this.getOrderList();
},
onReachBottom() { onReachBottom() {
!this.loading && this.getOrderList(); !this.loading && this.getOrderList();
} }
@ -320,7 +318,6 @@ export default {
</script> </script>
<style scoped lang="less"> <style scoped lang="less">
.noCart { .noCart {
margin-top: 0.17 * 100rpx; margin-top: 0.17 * 100rpx;
padding-top: 0.1 * 100rpx; padding-top: 0.1 * 100rpx;

6
pages/order/OrderSubmission/index.vue

@ -482,9 +482,9 @@
uni.showLoading({ uni.showLoading({
title: "生成订单中" title: "生成订单中"
}); });
let form = {} let from = {}
if (this.$deviceType == 'app') { if (this.$deviceType == 'app') {
form.form = 'app' from.from = 'app'
} }
createOrder(this.orderGroupInfo.orderKey, { createOrder(this.orderGroupInfo.orderKey, {
realName: this.contacts, realName: this.contacts,
@ -500,7 +500,7 @@
from: this.from, from: this.from,
mark: this.mark || "", mark: this.mark || "",
shippingType: parseInt(shipping_type) + 1, shippingType: parseInt(shipping_type) + 1,
...form ...from
}) })
.then(res => { .then(res => {
uni.hideLoading(); uni.hideLoading();

7
pages/shop/GoodsCon/index.vue

@ -266,6 +266,7 @@ export default {
console.log(url); console.log(url);
if (url && url.productId) { if (url && url.productId) {
this.id = url.productId; this.id = url.productId;
} else { } else {
this.id = this._route.query.id; this.id = this._route.query.id;
} }
@ -339,12 +340,12 @@ export default {
// //
productCon: function() { productCon: function() {
let that = this; let that = this;
let form = {}; let from = {};
if (this.$deviceType == "app") { if (this.$deviceType == "app") {
form.form = "app"; from.from = "app";
} }
uni.showLoading({ title: "加载中", mask: true }); uni.showLoading({ title: "加载中", mask: true });
getProductDetail(that.id, form) getProductDetail(that.id, from)
.then(res => { .then(res => {
that.$set(that, "storeInfo", res.data.storeInfo); that.$set(that, "storeInfo", res.data.storeInfo);
// attr attr // attr attr

22
pages/shop/GoodsEvaluate/index.vue

@ -196,12 +196,22 @@
icon: "success", icon: "success",
duration: 2000 duration: 2000
}); });
this.$yrouter.push({
path: "/pages/order/OrderDetails/index", // ?
query: { this.$yrouter.back()
id: this.orderCon.orderId
} // ?
}); // this.$yrouter.push({
// path: "/pages/order/OrderDetails/index",
// query: {
// id: this.orderCon.orderId
// }
// });
// ?
// this.$yrouter.reLaunch({
// path: "/pages/home/index",
// });
}) })
.catch(err => { .catch(err => {
uni.showToast({ uni.showToast({

21
pages/user/User/index.vue

@ -164,11 +164,11 @@
</view> </view>
</template> </template>
<script> <script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
import { getUser, getMenuUser, bindingPhone } from "@/api/user"; import { getUser, getMenuUser, bindingPhone } from "@/api/user";
import { isWeixin, VUE_APP_RESOURCES_URL } from "@/utils"; import { isWeixin, VUE_APP_RESOURCES_URL } from "@/utils";
import SwitchWindow from "@/components/SwitchWindow"; import SwitchWindow from "@/components/SwitchWindow";
import Authorization from "@/pages/authorization/index"; import Authorization from "@/pages/authorization/index";
import { mapGetters } from "vuex";
const NAME = "User"; const NAME = "User";
@ -188,6 +188,7 @@ export default {
}, },
computed: mapGetters(["userInfo"]), computed: mapGetters(["userInfo"]),
methods: { methods: {
...mapMutations(["UPDATE_AUTHORIZATIONPAGE", "CHANGE_TABTAR"]),
goReturnList() { goReturnList() {
this.$yrouter.push("/pages/order/ReturnList/index"); this.$yrouter.push("/pages/order/ReturnList/index");
}, },
@ -291,8 +292,14 @@ export default {
}, },
MenuUser: function() { MenuUser: function() {
let that = this; let that = this;
getMenuUser().then(res => { getMenuUser()
.then(res => {
uni.hideLoading();
that.MyMenus = res.data.routine_my_menus; that.MyMenus = res.data.routine_my_menus;
})
.catch(error => {
uni.hideLoading();
console.log(error);
}); });
}, },
goPages: function(index) { goPages: function(index) {
@ -336,11 +343,17 @@ export default {
onShow() { onShow() {
if (this.$store.getters.token) { if (this.$store.getters.token) {
// //
this.$store.dispatch('getUser', true) uni.showLoading({
title: "加载中"
});
this.$store.dispatch("getUser", true);
this.MenuUser(); this.MenuUser();
this.isWeixin = isWeixin(); this.isWeixin = isWeixin();
} }
},
onHide() {
console.log('离开用户中心')
this.UPDATE_AUTHORIZATIONPAGE(false);
} }
}; };
</script> </script>

6
pages/user/promotion/Poster/index.vue

@ -64,11 +64,11 @@ export default {
methods: { methods: {
getIndex: function() { getIndex: function() {
let that = this; let that = this;
let form = {}; let from = {};
if (this.$deviceType == "app") { if (this.$deviceType == "app") {
form.form = "app"; from.from = "app";
} }
getSpreadImg(form).then( getSpreadImg(from).then(
res => { res => {
that.info = res.data; that.info = res.data;
}, },

1
store/index.js

@ -51,6 +51,7 @@ const vuexStore = new Vuex.Store({
cookie.set(LOGIN_KEY, token, expires_time); cookie.set(LOGIN_KEY, token, expires_time);
}, },
LOGOUT(state) { LOGOUT(state) {
console.log('清除数据')
state.token = null; state.token = null;
state.userInfo = null state.userInfo = null
let spread = cookie.get('spread') let spread = cookie.get('spread')

292
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) => { export const getProvider = (service) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 获取当前环境的服务商 // 获取当前环境的服务商
@ -172,27 +132,25 @@ export const getProvider = (service) => {
export const authorize = (authorizeStr) => { export const authorize = (authorizeStr) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
uni.authorize({ console.log('检验授权', `scope.${authorizeStr}`)
scope: `scope.${authorizeStr}`, uni.getSetting({
success() { success(res) {
console.log(res.authSetting)
if (res.authSetting[`scope.${authorizeStr}`]) {
resolve('获取授权成功') resolve('获取授权成功')
} else {
reject('获取授权失败')
}
}, },
fail() { fail() {
reject('获取设置失败')
console.log('授权失败跳转首页')
switchTab({
path: '/pages/home/index',
// query
});
reject('获取授权失败')
} }
}) })
}).catch(error => {
console.log(error)
}) })
} }
export const login = (option) => { export const login = () => {
console.log('开始登录 ————————————————————') console.log('开始登录 ————————————————————')
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log('获取环境商') console.log('获取环境商')
@ -215,62 +173,48 @@ export const login = (option) => {
success: function (user) { success: function (user) {
console.log('获取用户信息成功') console.log('获取用户信息成功')
console.log('开始调用登录接口') console.log('开始调用登录接口')
if (Vue.prototype.$deviceType == 'routine') {
wxappAuth({ wxappAuth({
encryptedData: user.encryptedData, encryptedData: user.encryptedData,
iv: user.iv, iv: user.iv,
code: code, code: code,
spread: cookie.get("spread") spread: cookie.get("spread")
}).then(({ }).then(({ data }) => {
data
}) => {
console.log('登录接口调用成功') console.log('登录接口调用成功')
console.log('开始处理登录信息保存,并获取用户详情') console.log('开始处理登录信息保存,并获取用户详情')
resolve(data)
uni.hideLoading(); uni.hideLoading();
store.commit("LOGIN", data.token, dayjs(data.expires_time)); store.commit("LOGIN", data.token, dayjs(data.expires_time));
store.dispatch('USERINFO', true) store.dispatch('USERINFO', true)
console.log(store) getUser().then(user => {
handleGetUserInfo() console.log('获取用户信息成功')
store.dispatch('setUserInfo', user.data)
resolve(user)
}).catch(error => {
console.log('获取用户信息失败')
reject('获取用户信息失败')
});
}).catch(error => { }).catch(error => {
console.log('登录接口调用失败') console.log('登录接口调用失败')
reject() reject('登录接口调用失败')
console.log(error)
handleFail(option, '微信登录失败')
}); });
}
}, },
fail() { fail() {
console.log('获取用户信息失败') console.log('获取用户信息失败')
// 获取用户信息失败 reject('获取用户信息失败')
reject()
handleFail(option, '获取用户信息失败')
} }
}); });
}).catch(error => { }).catch(error => {
console.log('用户未授权') console.log('用户未授权')
reject() reject('用户未授权')
console.log(error)
handleFail(option, '用户未授权')
}) })
}, },
fail() { fail() {
console.log('调用登录接口失败') console.log('调用登录接口失败')
// 调用登录接口失败 reject('调用登录接口失败')
reject()
handleFail(option, '登录失败')
} }
}); });
}).catch(error => { }).catch(error => {
handleFail(option, '获取环境服务商失败') reject('获取环境服务商失败')
reject()
console.log(error)
handleFail(option, '获取环境服务商失败')
}) })
}).catch(error => {
handleFail(option, '登录失败')
console.log(error)
handleFail(option, '登录失败')
}) })
} }
@ -343,18 +287,13 @@ export const handleGetUserInfo = () => {
}) })
} }
const handleFail = (option, msg) => {
// 此处是处理登录失效的问题的
option && option.fail ? option.fail() : replaceLogin('登录失败,请重新登录')
}
export function parseUrl(location) { export function parseUrl(location) {
if (typeof location === 'string') return location if (typeof location === 'string') return location
const { const {
path, path,
query query
} = location } = location
const queryStr = stringify(query) const queryStr = stringify(query)
if (!queryStr) { if (!queryStr) {
@ -419,6 +358,8 @@ export const handleLoginStatus = (location, complete, fail, success) => {
// 是否可以访问 // 是否可以访问
let isAuth = false let isAuth = false
console.log('即将跳转', location, parseUrl(location))
// 从 location 中获取当前url,location typeof string || object // 从 location 中获取当前url,location typeof string || object
let path = '' let path = ''
if (typeof location === 'string') { if (typeof location === 'string') {
@ -427,6 +368,7 @@ export const handleLoginStatus = (location, complete, fail, success) => {
path = location.path path = location.path
} }
// 判断用户是否有token
if (!auth()) { if (!auth()) {
page.map((item) => { page.map((item) => {
if (item.path == path) { if (item.path == path) {
@ -439,7 +381,14 @@ export const handleLoginStatus = (location, complete, fail, success) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (isAuth) { 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({ resolve({
url: parseUrl(location), url: parseUrl(location),
complete, complete,
@ -447,8 +396,8 @@ export const handleLoginStatus = (location, complete, fail, success) => {
success success
}) })
} else { } else {
// 未登录没有权限 // 没有token,先校验用户是否授权,如果授权了,进行自动登录
replaceLogin() routerPermissions(parseUrl(location))
reject() reject()
} }
}).catch(error => { }).catch(error => {
@ -456,6 +405,74 @@ export const handleLoginStatus = (location, complete, fail, success) => {
}) })
} }
// export function checkPermissions(){
// }
export function routerPermissions(url, type) {
console.log('routerPermissions', url)
let path = url
if (!path) {
path = getCurrentPageUrlWithArgs()
}
if (Vue.prototype.$deviceType == 'routine') {
console.log('当前是微信小程序,开始处理小程序登录方法')
// 如果是微信小程序,跳转到授权页
// 先校验用户是否授权,如果授权了,进行自动登录
authorize('userInfo').then(() => {
// 自动登录
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,
})
} else {
push({
path,
})
}
}).catch(error => {
uni.showToast({
title: error,
icon: "none",
duration: 2000
});
reLaunch({
path: '/pages/authorization/index',
})
cookie.set('redirect', path)
})
}).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',
})
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) { export function push(location, complete, fail, success) {
handleLoginStatus(location, complete, fail, success).then(params => { handleLoginStatus(location, complete, fail, success).then(params => {
uni.navigateTo(params) uni.navigateTo(params)
@ -662,3 +679,98 @@ export const PosterCanvas = (store, successCallBack) => {
// } // }
// }) // })
} }
export const handleLoginFailure = () => {
store.commit("LOGOUT");
store.commit("UPDATE_AUTHORIZATION", false);
// token 失效
// 判断当前是不是已经在登录页面或者授权页,防止二次跳转
if (store.getters.isAuthorizationPage || getCurrentPageUrl() == '/pages/user/Login/index') {
console.log('已经是登录页面或者授权页面,跳出方法')
return
}
console.log('当前是授权页面')
console.log(store.getters)
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('是拼团进来的,但是没有获取到参数')
handleNoParameters()
}
}
// 判断是不是扫描的砍价海报进来的
if (getCurrentPageUrl() == 'pages/activity/DargainDetails/index' && handleQrCode()) {
console.log('是扫描的砍价海报进来的')
let url = handleQrCode();
if (url) {
path = parseUrl({
path: `/${getCurrentPageUrl()}`,
query: {
id: url.bargainId,
partake: url.uid
}
})
// cookie.set("spread", url.spread || 0);
} else {
handleNoParameters()
console.log('是扫描的砍价海报进来的,但是没有获取到参数')
}
}
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 {
handleNoParameters()
console.log('是扫描的商品详情进来的,但是没有获取到参数')
}
}
routerPermissions(path, 'reLaunch')
}
const handleNoParameters = () => {
uni.showToast({
title: '未获取到必要参数,即将跳转首页',
icon: 'success',
duration: 2000
})
setTimeout(() => {
clearTimeout()
switchTab({
path: '/pages/home/index',
});
}, 1500)
}

10
utils/request.js

@ -1,6 +1,6 @@
import Fly from "flyio/dist/npm/wx"; import Fly from "flyio/dist/npm/wx";
import $store from "../store"; import $store from "../store";
import toLogin from "@/libs/login"; import { handleLoginFailure } from "@/utils";
import { VUE_APP_API_URL } from "@/config"; import { VUE_APP_API_URL } from "@/config";
import cookie from "@/utils/store/cookie"; import cookie from "@/utils/store/cookie";
@ -16,11 +16,11 @@ fly.interceptors.response.use(
}, },
error => { error => {
if (error.toString() == 'Error: Network Error') { if (error.toString() == 'Error: Network Error') {
toLogin(); handleLoginFailure();
return Promise.reject({ msg: "未登录", toLogin: true }); return Promise.reject({ msg: "未登录", toLogin: true });
} }
if (error.status == 401) { if (error.status == 401) {
toLogin(); handleLoginFailure();
return Promise.reject({ msg: "未登录", toLogin: true }); return Promise.reject({ msg: "未登录", toLogin: true });
} }
return Promise.reject(error); return Promise.reject(error);
@ -44,7 +44,7 @@ function baseRequest(options) {
// 如果需要登录才可访问的接口没有拿到 token 视为登录失效 // 如果需要登录才可访问的接口没有拿到 token 视为登录失效
if (options.login === true && !token) { if (options.login === true && !token) {
// 跳转到登录或授权页面 // 跳转到登录或授权页面
toLogin(); handleLoginFailure();
// 提示错误信息 // 提示错误信息
return Promise.reject({ msg: "未登录", toLogin: true }); return Promise.reject({ msg: "未登录", toLogin: true });
} }
@ -60,7 +60,7 @@ function baseRequest(options) {
return Promise.reject({ msg: "请求失败", res, data }); return Promise.reject({ msg: "请求失败", res, data });
} }
if ([410000, 410001, 410002].indexOf(data.status) !== -1) { if ([410000, 410001, 410002].indexOf(data.status) !== -1) {
toLogin(); handleLoginFailure();
return Promise.reject({ msg: res.data.msg, res, data, toLogin: true }); return Promise.reject({ msg: res.data.msg, res, data, toLogin: true });
} else if (data.status === 200) { } else if (data.status === 200) {
return Promise.resolve(data, res); return Promise.resolve(data, res);

Loading…
Cancel
Save