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}` }) uni.switchTab({ url: `/${url}` })
return true return true
} }
// 生成query参数 goPageByToken(url, query, modo, false)
const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
// 普通页面, 使用navigateTo
modo === 'navigateTo' && uni.navigateTo({
url: `/${url}${queryStr}`
})
// 特殊指定, 使用redirectTo
modo === 'redirectTo' && uni.redirectTo({
url: `/${url}${queryStr}`
})
return true 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} * @param {object}

View File

@ -4,7 +4,7 @@ import store from './store'
import bootstrap from './core/bootstrap' import bootstrap from './core/bootstrap'
import mixin from './core/mixins/app' import mixin from './core/mixins/app'
import uView from './uni_modules/vk-uview-ui' 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 // 不能修改createApp方法名不能修改从Vue中导入的createSSRApp
export function createApp() { export function createApp() {
@ -16,6 +16,7 @@ export function createApp() {
app.config.globalProperties.$success = showSuccess app.config.globalProperties.$success = showSuccess
app.config.globalProperties.$error = showError app.config.globalProperties.$error = showError
app.config.globalProperties.$navTo = navTo app.config.globalProperties.$navTo = navTo
app.config.globalProperties.$goPageByToken = goPageByToken
app.config.globalProperties.$getShareUrlParams = getShareUrlParams app.config.globalProperties.$getShareUrlParams = getShareUrlParams
app.config.globalProperties.$checkModule = checkModuleKey app.config.globalProperties.$checkModule = checkModuleKey
app.config.globalProperties.$checkModules = checkModules app.config.globalProperties.$checkModules = checkModules

View File

@ -93,12 +93,12 @@
}, },
// //
handleCreate() { handleCreate() {
this.$navTo('pages/address/create') this.$goPageByToken('pages/address/create')
}, },
// //
handleUpdate(item) { handleUpdate(item) {
const data = encodeURIComponent(JSON.stringify(item)) const data = encodeURIComponent(JSON.stringify(item))
this.$navTo('pages/address/update', { data }) this.$goPageByToken('pages/address/update', { data })
}, },
// //
handleRemove(addressId) { handleRemove(addressId) {

View File

@ -247,7 +247,7 @@
skuInfo: item.skuInfo skuInfo: item.skuInfo
})) }))
storage.set('goods', arr) 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)) { if (lastPage && inArray(lastPage.route, backRoutes)) {
uni.navigateBack() uni.navigateBack()
} else { } else {
this.$navTo('pages/order/index', {}, 'redirectTo') this.$goPageByToken('pages/order/index', {}, 'redirectTo')
} }
}, 1200) }, 1200)
}, },

View File

@ -329,7 +329,7 @@ import storage from '@/utils/storage'
methods: { methods: {
// //
onSelectAddress() { 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) { if (!res.data.package) {
app.showToast(result.message, 1500) app.showToast(result.message, 1500)
setTimeout(() => app.$navTo('pages/order/index', {}, 'redirectTo'), 1500) setTimeout(() => app.$goPageByToken('pages/order/index', {}, 'redirectTo'), 1500)
} else { } 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)) }).finally(() => setTimeout(() => app.disabled = false, 1600))
}, },

View File

@ -154,7 +154,7 @@
price: selectShop.price, price: selectShop.price,
buy_num: selectShop.buy_num buy_num: selectShop.buy_num
}]) }])
this.$navTo('pages/checkout/index', { mode: 'buyNow' }) this.$goPageByToken('pages/checkout/index', { mode: 'buyNow' })
// //
this.onChangeValue(false) this.onChangeValue(false)
} }

View File

@ -44,13 +44,10 @@ export default {
async getPageData(callback) { async getPageData(callback) {
const app = this; const app = this;
app.setPageBar() app.setPageBar()
console.warn('----- my data is 22: ', 22)
app.items = await this.handleGetHomeData().finally(() => callback && callback()); app.items = await this.handleGetHomeData().finally(() => callback && callback());
}, },
async handleGetHomeData() { async handleGetHomeData() {
console.warn('----- my data is 33: ', 33)
const res = await apiGetCommodityList({}); const res = await apiGetCommodityList({});
console.warn('----- my data is res.data: ', res.data)
return mockData.map(item => { return mockData.map(item => {
if (item.type === 'goods') { if (item.type === 'goods') {
item.data = res.data.rows item.data = res.data.rows

View File

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

View File

@ -296,7 +296,7 @@
// //
handleTargetExpress(trackNumber) { handleTargetExpress(trackNumber) {
this.$navTo('pages/order/express/index', { trackNumber }) this.$goPageByToken('pages/order/express/index', { trackNumber })
}, },
// //
@ -306,7 +306,7 @@
// //
handleApplyRefund(orderGoodsId) { handleApplyRefund(orderGoodsId) {
this.$navTo('pages/refund/apply', { orderGoodsId }) this.$goPageByToken('pages/refund/apply', { orderGoodsId })
}, },
// //
@ -437,11 +437,11 @@
}, },
// //
onPay(orderId) { onPay(orderId) {
this.$navTo('pages/checkout/cashier/index', { orderId }) this.$goPageByToken('pages/checkout/cashier/index', { orderId })
}, },
// //
handleTargetComment(orderId) { handleTargetComment(orderId) {
this.$navTo('pages/order/comment/index', { orderId }) this.$goPageByToken('pages/order/comment/index', { orderId })
}, },
}, },
onClickBtn(orderId, action) { onClickBtn(orderId, action) {

View File

@ -330,17 +330,17 @@
}, },
// //
onPay(orderId) { onPay(orderId) {
this.$navTo('pages/checkout/cashier/index', { tradeOrderId: orderId }) this.$goPageByToken('pages/checkout/cashier/index', { tradeOrderId: orderId })
}, },
// //
handleTargetDetail(orderId) { handleTargetDetail(orderId) {
this.$navTo('pages/order/detail', { tradeOrderId: orderId }) this.$goPageByToken('pages/order/detail', { tradeOrderId: orderId })
}, },
// //
handleTargetComment(orderId) { handleTargetComment(orderId) {
this.$navTo('pages/order/comment/index', { tradeOrderId: orderId }) this.$goPageByToken('pages/order/comment/index', { tradeOrderId: orderId })
}, },
onReceipt(orderId, url) { onReceipt(orderId, url) {
const app = this const app = this

View File

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

View File

@ -221,12 +221,12 @@
// //
handleBindMobile() { handleBindMobile() {
this.$navTo('pages/user/bind/index') this.$goPageByToken('pages/user/bind/index')
}, },
// //
/* handlePersonal() { /* handlePersonal() {
this.$navTo('pages/user/personal/index') this.$goPageByToken('pages/user/personal/index')
}, */ }, */
// 退 // 退
@ -257,23 +257,23 @@
}, },
// //
onTargetWallet() { onTargetWallet() {
this.$navTo('pages/wallet/index') this.$goPageByToken('pages/wallet/index')
}, },
// //
onTargetOrder(item) { onTargetOrder(item) {
this.$navTo('pages/order/index', { dataType: item.id }) this.$goPageByToken('pages/order/index', { dataType: item.id })
}, },
// //
onTargetPoints() { onTargetPoints() {
this.$navTo('pages/points/log') this.$goPageByToken('pages/points/log')
}, },
// //
onTargetMyCoupon() { onTargetMyCoupon() {
this.$navTo('pages/my-coupon/index') this.$goPageByToken('pages/my-coupon/index')
}, },
// //
handleService({ url }) { handleService({ url }) {
this.$navTo(url) this.$goPageByToken(url)
} }
}, },
/** /**

View File

@ -90,17 +90,17 @@
// //
onTargetRecharge() { onTargetRecharge() {
this.$navTo('pages/wallet/recharge/index') this.$goPageByToken('pages/wallet/recharge/index')
}, },
// //
onTargetRechargeOrder() { onTargetRechargeOrder() {
this.$navTo('pages/wallet/recharge/order') this.$goPageByToken('pages/wallet/recharge/order')
}, },
// //
onTargetBalanceLog() { onTargetBalanceLog() {
this.$navTo('pages/wallet/balance/log') this.$goPageByToken('pages/wallet/balance/log')
} }
} }

View File

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

View File

@ -1,4 +1,3 @@
import { ACCESS_TOKEN, USER_ID, FLAG } from '@/store/mutation-types'
import storage from '@/utils/storage' import storage from '@/utils/storage'
import * as LoginApi from '@/api/login' import * as LoginApi from '@/api/login'
@ -65,7 +64,7 @@ const user = {
Logout({ commit }, data) { Logout({ commit }, data) {
const store = this const store = this
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (store.getters.userId > 0) { if (store.getters.buyerId > 0) {
storage.remove('user_info') storage.remove('user_info')
commit('SET_USER_INFO', { token: '', flag: '', buyerId: '' }) commit('SET_USER_INFO', { token: '', flag: '', buyerId: '' })
resolve() resolve()

View File

@ -1,5 +1,4 @@
export const ACCESS_TOKEN = 'AccessToken' export const ACCESS_TOKEN = 'AccessToken'
export const USER_ID = 'userId'
export const FLAG = 'flag' export const FLAG = 'flag'
export const PLATFORM = 'platform' export const PLATFORM = 'platform'
export const APP_THEME = 'appTheme' export const APP_THEME = 'appTheme'

View File

@ -23,6 +23,7 @@ export const HttpMethod = {
class HttpRequest { class HttpRequest {
static instance = null; static instance = null;
static loginModal = false;
constructor() {} constructor() {}
static getInstance() { static getInstance() {
if (!this.instance) { if (!this.instance) {
@ -109,25 +110,30 @@ class HttpRequest {
resolve(data); resolve(data);
} }
} else if (code === 401) { } else if (code === 401) {
// 未授权 // 防止重复弹窗
!requestConfig.noShowMsg && if (!HttpRequest.loginModal) {
uni.showModal({ HttpRequest.loginModal = true
title: "登录失效", // 弹窗告诉用户去登录
content: "登录失效,请重新登录", uni.showModal({
success: (resModa) => { title: '温馨提示',
if (resModa.confirm) { content: '此时此刻需要您登录喔~',
// showCancel: false,
confirmText: "去登录",
cancelText: "再逛会",
success: res => {
if (res.confirm) {
uni.removeStorageSync("user_info"); uni.removeStorageSync("user_info");
uni.navigateTo({ url: "/pages/login/index" }); uni.navigateTo({
} else if (resModa.cancel) { url: "/pages/login/index"
try { })
uni.removeStorageSync("user_info");
uni.reLaunch({ url: "/pages/index/index" });
} catch (e) {
console.log("登录失效catch", e);
} }
} if (res.cancel && getCurrentPages().length > 1) {
}, uni.navigateBack()
}); }
HttpRequest.loginModal = false
}
})
}
reject({ code, msg: "未登录", data: data }); reject({ code, msg: "未登录", data: data });
} else { } else {
//非200及401状态码-数据处理 //非200及401状态码-数据处理