feat: 跳转页面功能增加登录校验功能+401展示登录提示
This commit is contained in:
parent
6c56c7f55a
commit
c9de557000
25
core/app.js
25
core/app.js
@ -118,19 +118,24 @@ export const navTo = (url, query = {}, modo = 'navigateTo') => {
|
||||
uni.switchTab({ url: `/${url}` })
|
||||
return true
|
||||
}
|
||||
// 生成query参数
|
||||
const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
|
||||
// 普通页面, 使用navigateTo
|
||||
modo === 'navigateTo' && uni.navigateTo({
|
||||
url: `/${url}${queryStr}`
|
||||
})
|
||||
// 特殊指定, 使用redirectTo
|
||||
modo === 'redirectTo' && uni.redirectTo({
|
||||
url: `/${url}${queryStr}`
|
||||
})
|
||||
goPageByToken(url, query, modo, false)
|
||||
return true
|
||||
}
|
||||
|
||||
// 根据token跳转页面
|
||||
export const goPageByToken = (url, query, modo = 'navigateTo', needLogin = true) => {
|
||||
if (needLogin && !checkLogin()) {
|
||||
uni.navigateTo({
|
||||
url: "/pages/login/index"
|
||||
})
|
||||
return false
|
||||
}
|
||||
const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
|
||||
uni[modo]({
|
||||
url: `/${url}${queryStr}`
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前页面数据
|
||||
* @param {object}
|
||||
|
||||
3
main.js
3
main.js
@ -4,7 +4,7 @@ import store from './store'
|
||||
import bootstrap from './core/bootstrap'
|
||||
import mixin from './core/mixins/app'
|
||||
import uView from './uni_modules/vk-uview-ui'
|
||||
import { navTo, showToast, showSuccess, showError, getShareUrlParams, checkModuleKey, checkModules } from './core/app'
|
||||
import { navTo, goPageByToken, showToast, showSuccess, showError, getShareUrlParams, checkModuleKey, checkModules } from './core/app'
|
||||
|
||||
// 不能修改createApp方法名,不能修改从Vue中导入的createSSRApp
|
||||
export function createApp() {
|
||||
@ -16,6 +16,7 @@ export function createApp() {
|
||||
app.config.globalProperties.$success = showSuccess
|
||||
app.config.globalProperties.$error = showError
|
||||
app.config.globalProperties.$navTo = navTo
|
||||
app.config.globalProperties.$goPageByToken = goPageByToken
|
||||
app.config.globalProperties.$getShareUrlParams = getShareUrlParams
|
||||
app.config.globalProperties.$checkModule = checkModuleKey
|
||||
app.config.globalProperties.$checkModules = checkModules
|
||||
|
||||
@ -93,12 +93,12 @@
|
||||
},
|
||||
// 添加新地址
|
||||
handleCreate() {
|
||||
this.$navTo('pages/address/create')
|
||||
this.$goPageByToken('pages/address/create')
|
||||
},
|
||||
// 编辑地址
|
||||
handleUpdate(item) {
|
||||
const data = encodeURIComponent(JSON.stringify(item))
|
||||
this.$navTo('pages/address/update', { data })
|
||||
this.$goPageByToken('pages/address/update', { data })
|
||||
},
|
||||
// 删除收货地址
|
||||
handleRemove(addressId) {
|
||||
|
||||
@ -247,7 +247,7 @@
|
||||
skuInfo: item.skuInfo
|
||||
}))
|
||||
storage.set('goods', arr)
|
||||
app.$navTo('pages/checkout/index', { mode: 'cart' })
|
||||
app.$goPageByToken('pages/checkout/index', { mode: 'cart' })
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@ -302,7 +302,7 @@
|
||||
if (lastPage && inArray(lastPage.route, backRoutes)) {
|
||||
uni.navigateBack()
|
||||
} else {
|
||||
this.$navTo('pages/order/index', {}, 'redirectTo')
|
||||
this.$goPageByToken('pages/order/index', {}, 'redirectTo')
|
||||
}
|
||||
}, 1200)
|
||||
},
|
||||
|
||||
@ -329,7 +329,7 @@ import storage from '@/utils/storage'
|
||||
methods: {
|
||||
// 快递配送:选择收货地址
|
||||
onSelectAddress() {
|
||||
this.$navTo('pages/address/index', { from: 'checkout' })
|
||||
this.$goPageByToken('pages/address/index', { from: 'checkout' })
|
||||
},
|
||||
|
||||
// 跳转到商品详情页
|
||||
@ -359,10 +359,10 @@ import storage from '@/utils/storage'
|
||||
// 判断订单是否已支付: 已支付跳转订单列表 未支付跳转支付页
|
||||
if (!res.data.package) {
|
||||
app.showToast(result.message, 1500)
|
||||
setTimeout(() => app.$navTo('pages/order/index', {}, 'redirectTo'), 1500)
|
||||
setTimeout(() => app.$goPageByToken('pages/order/index', {}, 'redirectTo'), 1500)
|
||||
} else {
|
||||
// 订单未支付: 跳转到订单支付页
|
||||
setTimeout(() => app.$navTo('pages/checkout/cashier/index', res.data, 'redirectTo'), 100)
|
||||
setTimeout(() => app.$goPageByToken('pages/checkout/cashier/index', res.data, 'redirectTo'), 100)
|
||||
}
|
||||
}).finally(() => setTimeout(() => app.disabled = false, 1600))
|
||||
},
|
||||
|
||||
@ -154,7 +154,7 @@
|
||||
price: selectShop.price,
|
||||
buy_num: selectShop.buy_num
|
||||
}])
|
||||
this.$navTo('pages/checkout/index', { mode: 'buyNow' })
|
||||
this.$goPageByToken('pages/checkout/index', { mode: 'buyNow' })
|
||||
// 隐藏当前弹窗
|
||||
this.onChangeValue(false)
|
||||
}
|
||||
|
||||
@ -44,13 +44,10 @@ export default {
|
||||
async getPageData(callback) {
|
||||
const app = this;
|
||||
app.setPageBar()
|
||||
console.warn('----- my data is 22: ', 22)
|
||||
app.items = await this.handleGetHomeData().finally(() => callback && callback());
|
||||
},
|
||||
async handleGetHomeData() {
|
||||
console.warn('----- my data is 33: ', 33)
|
||||
const res = await apiGetCommodityList({});
|
||||
console.warn('----- my data is res.data: ', res.data)
|
||||
return mockData.map(item => {
|
||||
if (item.type === 'goods') {
|
||||
item.data = res.data.rows
|
||||
|
||||
@ -97,7 +97,7 @@
|
||||
store.dispatch('LoginMpWx', { code })
|
||||
.then(result => {
|
||||
// 一键登录成功
|
||||
app.$toast(result.message)
|
||||
app.$toast('登录成功')
|
||||
// 相应全局事件订阅: 刷新上级页面数据
|
||||
uni.$emit('syncRefresh', true)
|
||||
// 跳转回原页面
|
||||
|
||||
@ -296,7 +296,7 @@
|
||||
|
||||
// 跳转到物流跟踪页面
|
||||
handleTargetExpress(trackNumber) {
|
||||
this.$navTo('pages/order/express/index', { trackNumber })
|
||||
this.$goPageByToken('pages/order/express/index', { trackNumber })
|
||||
},
|
||||
|
||||
// 跳转到商品详情页面
|
||||
@ -306,7 +306,7 @@
|
||||
|
||||
// 跳转到申请售后页面
|
||||
handleApplyRefund(orderGoodsId) {
|
||||
this.$navTo('pages/refund/apply', { orderGoodsId })
|
||||
this.$goPageByToken('pages/refund/apply', { orderGoodsId })
|
||||
},
|
||||
|
||||
// 取消订单
|
||||
@ -437,11 +437,11 @@
|
||||
},
|
||||
// 点击去支付
|
||||
onPay(orderId) {
|
||||
this.$navTo('pages/checkout/cashier/index', { orderId })
|
||||
this.$goPageByToken('pages/checkout/cashier/index', { orderId })
|
||||
},
|
||||
// 跳转到订单评价页
|
||||
handleTargetComment(orderId) {
|
||||
this.$navTo('pages/order/comment/index', { orderId })
|
||||
this.$goPageByToken('pages/order/comment/index', { orderId })
|
||||
},
|
||||
},
|
||||
onClickBtn(orderId, action) {
|
||||
|
||||
@ -330,17 +330,17 @@
|
||||
},
|
||||
// 点击去支付
|
||||
onPay(orderId) {
|
||||
this.$navTo('pages/checkout/cashier/index', { tradeOrderId: orderId })
|
||||
this.$goPageByToken('pages/checkout/cashier/index', { tradeOrderId: orderId })
|
||||
},
|
||||
|
||||
// 跳转到订单详情页
|
||||
handleTargetDetail(orderId) {
|
||||
this.$navTo('pages/order/detail', { tradeOrderId: orderId })
|
||||
this.$goPageByToken('pages/order/detail', { tradeOrderId: orderId })
|
||||
},
|
||||
|
||||
// 跳转到订单评价页
|
||||
handleTargetComment(orderId) {
|
||||
this.$navTo('pages/order/comment/index', { tradeOrderId: orderId })
|
||||
this.$goPageByToken('pages/order/comment/index', { tradeOrderId: orderId })
|
||||
},
|
||||
onReceipt(orderId, url) {
|
||||
const app = this
|
||||
|
||||
@ -163,7 +163,7 @@
|
||||
|
||||
// 跳转到售后单详情页
|
||||
handleTargetDetail(orderRefundId) {
|
||||
this.$navTo('pages/refund/detail', { orderRefundId })
|
||||
this.$goPageByToken('pages/refund/detail', { orderRefundId })
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
@ -221,12 +221,12 @@
|
||||
|
||||
// 跳转到绑定手机号页面
|
||||
handleBindMobile() {
|
||||
this.$navTo('pages/user/bind/index')
|
||||
this.$goPageByToken('pages/user/bind/index')
|
||||
},
|
||||
|
||||
// 跳转到修改个人信息页
|
||||
/* handlePersonal() {
|
||||
this.$navTo('pages/user/personal/index')
|
||||
this.$goPageByToken('pages/user/personal/index')
|
||||
}, */
|
||||
|
||||
// 退出登录
|
||||
@ -257,23 +257,23 @@
|
||||
},
|
||||
// 跳转到钱包页面
|
||||
onTargetWallet() {
|
||||
this.$navTo('pages/wallet/index')
|
||||
this.$goPageByToken('pages/wallet/index')
|
||||
},
|
||||
// 跳转到订单页
|
||||
onTargetOrder(item) {
|
||||
this.$navTo('pages/order/index', { dataType: item.id })
|
||||
this.$goPageByToken('pages/order/index', { dataType: item.id })
|
||||
},
|
||||
// 跳转到我的积分页面
|
||||
onTargetPoints() {
|
||||
this.$navTo('pages/points/log')
|
||||
this.$goPageByToken('pages/points/log')
|
||||
},
|
||||
// 跳转到我的优惠券页
|
||||
onTargetMyCoupon() {
|
||||
this.$navTo('pages/my-coupon/index')
|
||||
this.$goPageByToken('pages/my-coupon/index')
|
||||
},
|
||||
// 跳转到服务页面
|
||||
handleService({ url }) {
|
||||
this.$navTo(url)
|
||||
this.$goPageByToken(url)
|
||||
}
|
||||
},
|
||||
/**
|
||||
|
||||
@ -90,17 +90,17 @@
|
||||
|
||||
// 跳转充值页面
|
||||
onTargetRecharge() {
|
||||
this.$navTo('pages/wallet/recharge/index')
|
||||
this.$goPageByToken('pages/wallet/recharge/index')
|
||||
},
|
||||
|
||||
// 跳转充值记录页面
|
||||
onTargetRechargeOrder() {
|
||||
this.$navTo('pages/wallet/recharge/order')
|
||||
this.$goPageByToken('pages/wallet/recharge/order')
|
||||
},
|
||||
|
||||
// 跳转账单详情页面
|
||||
onTargetBalanceLog() {
|
||||
this.$navTo('pages/wallet/balance/log')
|
||||
this.$goPageByToken('pages/wallet/balance/log')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -320,7 +320,7 @@
|
||||
setTimeout(() => uni.navigateBack(), 1000)
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
this.$navTo('pages/wallet/index', {}, 'redirectTo')
|
||||
this.$goPageByToken('pages/wallet/index', {}, 'redirectTo')
|
||||
}, 1200)
|
||||
}
|
||||
},
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { ACCESS_TOKEN, USER_ID, FLAG } from '@/store/mutation-types'
|
||||
import storage from '@/utils/storage'
|
||||
import * as LoginApi from '@/api/login'
|
||||
|
||||
@ -65,7 +64,7 @@ const user = {
|
||||
Logout({ commit }, data) {
|
||||
const store = this
|
||||
return new Promise((resolve, reject) => {
|
||||
if (store.getters.userId > 0) {
|
||||
if (store.getters.buyerId > 0) {
|
||||
storage.remove('user_info')
|
||||
commit('SET_USER_INFO', { token: '', flag: '', buyerId: '' })
|
||||
resolve()
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
export const ACCESS_TOKEN = 'AccessToken'
|
||||
export const USER_ID = 'userId'
|
||||
export const FLAG = 'flag'
|
||||
export const PLATFORM = 'platform'
|
||||
export const APP_THEME = 'appTheme'
|
||||
|
||||
@ -23,6 +23,7 @@ export const HttpMethod = {
|
||||
|
||||
class HttpRequest {
|
||||
static instance = null;
|
||||
static loginModal = false;
|
||||
constructor() {}
|
||||
static getInstance() {
|
||||
if (!this.instance) {
|
||||
@ -109,25 +110,30 @@ class HttpRequest {
|
||||
resolve(data);
|
||||
}
|
||||
} else if (code === 401) {
|
||||
// 未授权
|
||||
!requestConfig.noShowMsg &&
|
||||
uni.showModal({
|
||||
title: "登录失效",
|
||||
content: "登录失效,请重新登录",
|
||||
success: (resModa) => {
|
||||
if (resModa.confirm) {
|
||||
// 防止重复弹窗
|
||||
if (!HttpRequest.loginModal) {
|
||||
HttpRequest.loginModal = true
|
||||
// 弹窗告诉用户去登录
|
||||
uni.showModal({
|
||||
title: '温馨提示',
|
||||
content: '此时此刻需要您登录喔~',
|
||||
// showCancel: false,
|
||||
confirmText: "去登录",
|
||||
cancelText: "再逛会",
|
||||
success: res => {
|
||||
if (res.confirm) {
|
||||
uni.removeStorageSync("user_info");
|
||||
uni.navigateTo({ url: "/pages/login/index" });
|
||||
} else if (resModa.cancel) {
|
||||
try {
|
||||
uni.removeStorageSync("user_info");
|
||||
uni.reLaunch({ url: "/pages/index/index" });
|
||||
} catch (e) {
|
||||
console.log("登录失效catch:", e);
|
||||
uni.navigateTo({
|
||||
url: "/pages/login/index"
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
if (res.cancel && getCurrentPages().length > 1) {
|
||||
uni.navigateBack()
|
||||
}
|
||||
HttpRequest.loginModal = false
|
||||
}
|
||||
})
|
||||
}
|
||||
reject({ code, msg: "未登录", data: data });
|
||||
} else {
|
||||
//非200及401状态码-数据处理
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user