feat: 跳转页面功能增加登录校验功能+401展示登录提示

This commit is contained in:
zc 2025-12-05 21:44:58 +08:00
parent 6c56c7f55a
commit c9de557000
18 changed files with 69 additions and 62 deletions

View File

@ -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}

View File

@ -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

View File

@ -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) {

View File

@ -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' })
}
},

View File

@ -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)
},

View File

@ -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))
},

View File

@ -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)
}

View File

@ -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

View File

@ -97,7 +97,7 @@
store.dispatch('LoginMpWx', { code })
.then(result => {
//
app.$toast(result.message)
app.$toast('登录成功')
// :
uni.$emit('syncRefresh', true)
//

View File

@ -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) {

View File

@ -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

View File

@ -163,7 +163,7 @@
//
handleTargetDetail(orderRefundId) {
this.$navTo('pages/refund/detail', { orderRefundId })
this.$goPageByToken('pages/refund/detail', { orderRefundId })
},
}

View File

@ -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)
}
},
/**

View File

@ -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')
}
}

View File

@ -320,7 +320,7 @@
setTimeout(() => uni.navigateBack(), 1000)
} else {
setTimeout(() => {
this.$navTo('pages/wallet/index', {}, 'redirectTo')
this.$goPageByToken('pages/wallet/index', {}, 'redirectTo')
}, 1200)
}
},

View File

@ -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()

View File

@ -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'

View File

@ -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状态码-数据处理