From 4a5e422031aeae10519f018dd5bdca74f5a11dd3 Mon Sep 17 00:00:00 2001 From: zpc Date: Tue, 22 Apr 2025 01:43:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E7=BB=91=E5=AE=9A=E8=B7=B3=E8=BD=AC=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E3=80=82=E5=A2=9E=E5=8A=A0=E8=B7=AF=E7=94=B1=E5=AE=88=E5=8D=AB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/common.js | 122 +++++------ common/config.js | 29 +++ common/gotopage.js | 47 +++-- common/request.js | 72 ++++--- common/router.js | 253 +++++++++++++++++++++++ components/nav-header/nav-header.vue | 27 +-- main.js | 3 + pages/DrawCard/index.vue | 4 +- pages/infinite/benefit.vue | 4 +- pages/infinite/bonus_house_details.vue | 4 +- pages/infinite/index.vue | 272 +++++++++++++------------ pages/other/order_list.vue | 4 +- pages/shop/detail.vue | 8 +- pages/shop/index.vue | 13 +- pages/shouye/huanxiang.vue | 27 +-- pages/shouye/index.vue | 20 +- pages/shouye/rili.vue | 4 +- pages/user/bangding.vue | 33 ++- pages/user/change.vue | 4 +- pages/user/coupon.vue | 4 +- pages/user/fhjl.vue | 9 +- pages/user/index.vue | 14 +- pages/user/login.vue | 148 ++++++++------ 23 files changed, 703 insertions(+), 422 deletions(-) create mode 100644 common/router.js diff --git a/common/common.js b/common/common.js index e940acd..1d9636d 100644 --- a/common/common.js +++ b/common/common.js @@ -5,6 +5,7 @@ */ import Vue from 'vue' import config from '@/common/config.js' +import { navigateTo as routerNavigateTo, navigateBack as routerNavigateBack } from '@/common/router.js' // 防止处理多次点击 function noMultipleClicks(methods, info) { @@ -444,27 +445,35 @@ export default { return } - if (Object.keys(query).length > 0) { - url += this.qs(query) + // 确定跳转类型 + let navigationType = 'navigateTo'; + switch (type) { + case 2: + navigationType = 'redirectTo'; + break; + case 3: + navigationType = 'switchTab'; + break; + case 4: + navigationType = 'reLaunch'; + break; + default: + navigationType = 'navigateTo'; } - - const opt = { - url, - success, - fail, - complete - } - - const dic = { - 1: uni.navigateTo, - 2: uni.redirectTo, - 3: uni.switchTab, - 4: uni.reLaunch - } - - const action = dic[type] - - action(opt) + + // 使用导入的路由方法而不是this.$router + return routerNavigateTo(url, query, navigationType) + .then(res => { + if (typeof success === 'function') success(res); + return res; + }) + .catch(err => { + if (typeof fail === 'function') fail(err); + throw err; + }) + .finally(() => { + if (typeof complete === 'function') complete(); + }); }, /** @@ -474,32 +483,17 @@ export default { */ back(e) { /* 判断传入的是否是数字 */ - let pages = getCurrentPages() - console.log('pgaes', pages) - if (pages.length >= 2) { - if (typeof e === 'number' && !isNaN(e)) { - /* 如果要跳转的长度小于页面栈数量 */ - if (e < pages.length) { - uni.navigateBack({ - delta: e - }) - /* 否则回首页 */ - } else { - uni.switchTab({ - url: '/pages/shouye/index' - }) - } - /* 默认返回上一级 */ - } else { - uni.navigateBack({ - delta: 1 - }) - } - } else { - uni.switchTab({ - url: '/pages/shouye/index' - }) + let delta = 1; + if (typeof e === 'number' && !isNaN(e)) { + delta = e; } + + // 使用导入的routerNavigateBack而不是this.$router + routerNavigateBack(delta) + .catch(() => { + // 如果返回失败,则回到首页 + routerNavigateTo('/pages/shouye/index', {}, 'switchTab'); + }); }, /** @@ -519,14 +513,11 @@ export default { $navBack(num = 1, time = 0) { // 页面返回 setTimeout(() => { - uni.navigateBack({ - delta: num, - fail: () => { - uni.reLaunch({ - url: '/pages/shouye/index' - }) - } - }) + routerNavigateBack(num) + .catch(() => { + // 如果返回失败,则回到首页 + routerNavigateTo('/pages/shouye/index', {}, 'reLaunch'); + }); }, time * 1000) }, /** @@ -617,14 +608,27 @@ export default { // #endif }, nav(url) { - uni.navigateTo({ - url: url, - fail: () => { - uni.switchTab({ - url: url + // 解析URL和参数 + let urlPath = url; + let params = {}; + + if (url.includes('?')) { + const parts = url.split('?'); + urlPath = parts[0]; + + // 解析参数 + if (parts[1]) { + parts[1].split('&').forEach(param => { + const keyValue = param.split('='); + if (keyValue.length === 2) { + params[keyValue[0]] = decodeURIComponent(keyValue[1]); + } }); } - }) + } + + // 使用导入的路由方法 + return routerNavigateTo(urlPath, params); }, red(url) { uni.redirectTo({ diff --git a/common/config.js b/common/config.js index 51fc8de..83c2e07 100644 --- a/common/config.js +++ b/common/config.js @@ -9,6 +9,35 @@ let configData = null; let isLoading = false; let loadPromise = null; const wx_version = "104"; + +// 白名单页面(不需要登录即可访问) +export const whiteList = [ + "pages/shouye/index", // 首页 + "pages/shouye/detail", // 详情页 + "pages/shouye/huanxiang", // 换箱页面 + "pages/mall/index", // 商城首页 + "pages/shouye/detail_wuxian", // 无限详情页 + "pages/sangdai/sangdai", // 盒柜页面 + "pages/infinite/index", // 福利首页 + "pages/user/index", // 用户中心 + "pages/infinite/daily_check_in", // 每日签到 + "pages/infinite/bonus_house", // 福利屋 + "pages/other/prize_draw", // 每日奖品抽取 + "pages/shouye/danye", // 单页 + "pages/guize/guize", // 规则页面 + "pages/shouye/dada_ranking", // 达达排行榜 + "pages/shouye/yaoqing_ranking", // 邀请排行榜 + "pages/user/login" // 登录页面 +]; + +// API白名单(不需要登录即可访问的API) +export const apiWhiteList = [ + 'login_record', + 'api/warehouse_index', + 'api/user', + 'api/warehouse_send_record' +]; + const defaultConfig = { "good_type": [{ "value": 0, "sort_order": 0, "is_show": 1, "name": "全部", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 1, "sort_order": 1, "is_show": 0, "name": "一番赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 2, "sort_order": 2, "is_show": 1, "name": "无限赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 3, "sort_order": 3, "is_show": 0, "name": "擂台赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 5, "sort_order": 4, "is_show": 0, "name": "积分赏", "pay_wechat": 0, "pay_balance": 0, "pay_currency": 0, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 0 }, { "value": 6, "sort_order": 5, "is_show": 1, "name": "限时活动", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 8, "sort_order": 6, "is_show": 1, "name": "领主赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 9, "sort_order": 7, "is_show": 0, "name": "连击赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 10, "sort_order": 8, "is_show": 0, "name": "商城赏", "pay_wechat": 1, "pay_balance": 0, "pay_currency": 0, "pay_currency2": 1, "pay_coupon": 0, "is_deduction": 0 }, { "value": 11, "sort_order": 9, "is_show": 0, "name": "自制赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 15, "sort_order": 15, "is_show": 0, "name": "福利屋", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 0, "pay_currency2": 0, "pay_coupon": 0, "is_deduction": 0 }, { "value": 16, "sort_order": 16, "is_show": 1, "name": "翻倍赏", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 1, "pay_currency2": 1, "pay_coupon": 1, "is_deduction": 1 }, { "value": 17, "sort_order": 17, "is_show": 0, "name": "外卖盒子", "pay_wechat": 1, "pay_balance": 1, "pay_currency": 0, "pay_currency2": 0, "pay_coupon": 0, "is_deduction": 0 }], "app_setting": { diff --git a/common/gotopage.js b/common/gotopage.js index 93232e6..6f83803 100644 --- a/common/gotopage.js +++ b/common/gotopage.js @@ -1,4 +1,7 @@ // import config from '../config.js' +// 导入路由模块 +import { navigateTo as routerNavigateTo } from '@/common/router.js'; + /*导航菜单白名单*/ const tabBarLinks = [ '/pages/index/index', @@ -28,27 +31,33 @@ export const gotopage = (url, type) => { return false; } + // 从URL中提取路径和参数 + let path = url; + let params = {}; - // tabBar页面 - if (type == 'switch') { - uni.reLaunch({ - url: url - }); - } else { + if (url.includes('?')) { + const [basePath, queryString] = url.split('?'); + path = basePath; - if(type == 'redirect'){ - uni.redirectTo({ - url: url - }); - } - if(type == 'reLaunch'){ - uni.reLaunch({ - url: url - }); - } - // 普通页面 - uni.navigateTo({ - url: url + // 解析参数 + queryString.split('&').forEach(param => { + const [key, value] = param.split('='); + if (key && value) { + params[key] = decodeURIComponent(value); + } }); } + + // 使用新的路由方法进行导航 + let navigationType = 'navigateTo'; + if (type === 'switch') { + navigationType = 'reLaunch'; + } else if (type === 'redirect') { + navigationType = 'redirectTo'; + } else if (type === 'reLaunch') { + navigationType = 'reLaunch'; + } + + // 使用导入的路由方法 + return routerNavigateTo(path, params, navigationType); } diff --git a/common/request.js b/common/request.js index a2885de..016cc5b 100644 --- a/common/request.js +++ b/common/request.js @@ -6,7 +6,8 @@ import EnvConfig from '@/common/env.js' import md5 from 'js-md5' import ConfigManager from '@/common/config.js' - +import { apiWhiteList } from '@/common/config.js' +import RouterManager from '@/common/router.js' class RequestManager { /** * 判断URL是否在白名单中 @@ -14,26 +15,16 @@ class RequestManager { * @returns {Boolean} 是否在白名单中 */ static isUrlInWhitelist(url) { - // 白名单URL列表 - const whitelistUrls = [ - 'login_record', - 'api/warehouse_index', - 'api/user', - 'api/warehouse_send_record' - ] - - - // 白名单URL列表 - const whitelistUrls2 = [ - 'login_record', - ] - let iswx = ConfigManager.GetVersion(); + // let iswx = ConfigManager.GetVersion(); + + // // 根据版本使用不同的白名单 + // if (!iswx) { + // // 非微信版本只允许 login_record + // return apiWhiteList.includes('login_record') && url.indexOf('login_record') > -1; + // } - if (!iswx) { - return whitelistUrls2.some(whiteItem => url.indexOf(whiteItem) > -1) - } // 检查URL是否包含白名单中的任一项 - return whitelistUrls.some(whiteItem => url.indexOf(whiteItem) > -1) + return apiWhiteList.some(whiteItem => url.indexOf(whiteItem) > -1); } /** @@ -208,6 +199,7 @@ class RequestManager { data: data, success: res => { console.log("res.data.status", res.data.status) + var pages = getCurrentPages() if (res.data.status == 1) { // 请求成功 resolve(res.data) @@ -245,19 +237,37 @@ class RequestManager { icon: 'none' }) }, 100) + reject(res.data) } else if (res.data.status < 0) { var pages = getCurrentPages() - for (var a = 0; a < pages.length; a++) { - console.log(pages[a].route) - if (pages[a].route == 'pages/user/index') { - uni.setStorageSync('lgurl', pages[a].route) - uni.setStorageSync('lgurldata', JSON.stringify(pages[a].options)) + // 获取当前页面路径和参数 + var currentPage = pages[pages.length - 1]; + if (currentPage) { + var currentRoute = currentPage.route; + var currentParams = currentPage.options || {}; + + // 只有非登录页面才保存重定向信息 + if (currentRoute && currentRoute !== 'pages/user/login') { + // 构建完整的重定向URL + var redirectPath = '/' + currentRoute; + + // 如果有参数,拼接参数 + if (Object.keys(currentParams).length > 0) { + var paramString = Object.keys(currentParams) + .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(currentParams[key])}`) + .join('&'); + redirectPath += '?' + paramString; + } + + // 保存重定向URL到缓存 + console.log('保存重定向URL:', redirectPath); + uni.setStorageSync('redirect', redirectPath); } } + console.log(requestUrl); if (RequestManager.isUrlInWhitelist(requestUrl)) { - reject(res.data) return; } @@ -267,11 +277,13 @@ class RequestManager { icon: 'none' }) }, 100) - uni.redirectTo({ - url: '/pages/user/login', - // animationType: 'pop-in', - // animationDuration: 500 - }) + + // 使用新的路由守卫方法进行跳转 + RouterManager.navigateTo('/pages/user/login', {}, 'navigateTo') + .catch(err => { + console.error('登录页面跳转失败:', err); + }); + reject(res.data) } else { reject(res.data) diff --git a/common/router.js b/common/router.js new file mode 100644 index 0000000..c6a1b9e --- /dev/null +++ b/common/router.js @@ -0,0 +1,253 @@ +/** + * 通用路由守卫 + */ + +import { whiteList } from '@/common/config.js' + +/** + * 判断用户是否已登录 + * @returns {Boolean} true表示已登录,false表示未登录 + */ +function isLogin() { + // 从本地存储获取用户token或其他登录凭证 + const token = uni.getStorageSync('token'); + return !!token; +} + +/** + * 判断URL是否在白名单中 + * @param {String} url 需要检查的URL + * @returns {Boolean} 是否在白名单中 + */ +function isInWhiteList(url) { + if (!url) return false; + + // 去除URL开头的斜杠,以便与白名单匹配 + const cleanUrl = url.startsWith('/') ? url.substring(1) : url; + + // 提取路径部分(不含参数) + const pathPart = cleanUrl.split('?')[0]; + + // 检查是否在白名单中 + return whiteList.some(item => { + // 确保白名单项目不带/开头,以实现一致的比较 + const cleanItem = item.startsWith('/') ? item.substring(1) : item; + return pathPart === cleanItem || pathPart.startsWith(cleanItem + '/') || pathPart.startsWith(cleanItem + '?'); + }); +} + +/** + * 返回上一页,如果上一页是登录页则跳过它 + * @returns {Promise} 返回操作结果的Promise + */ +export function navigateBack(delta = 1) { + return new Promise((resolve, reject) => { + const pages = getCurrentPages(); + + // 如果页面栈不足,直接返回首页 + if (pages.length <= 1) { + return navigateTo('/pages/shouye/index', {}, 'reLaunch') + .then(resolve) + .catch(reject); + } + + // 查看前一个页面是否是登录页 + if (pages.length > 1 && pages[pages.length - 2].route === 'pages/user/login') { + // 如果前一个页面是登录页,则返回两步 + uni.navigateBack({ + delta: 2, + success: resolve, + fail: reject + }); + } else { + // 正常返回 + uni.navigateBack({ + delta, + success: resolve, + fail: reject + }); + } + }); +} + +/** + * 通用路由跳转方法 + * @param {Object} options 跳转配置 + * @param {String} options.url 跳转地址 + * @param {Object} options.params 请求参数,可选 + * @param {String} options.type 跳转方式,可选:navigateTo(默认)、switchTab、redirectTo、reLaunch + * @returns {Promise} 返回一个Promise对象 + */ +export function routerTo(options) { + return new Promise((resolve, reject) => { + // 处理参数 + let { url, params, type } = options; + + // 判断url是否存在 + if (!url || url.length === 0) { + reject('跳转地址不能为空'); + return; + } + + // 拼接参数 + if (params && Object.keys(params).length > 0) { + const queryString = Object.keys(params) + .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) + .join('&'); + + url += (url.indexOf('?') === -1 ? '?' : '&') + queryString; + } + + // 检查是否需要登录 + const needLogin = !isInWhiteList(url); + + if (needLogin && !isLogin()) { + // 需要登录但未登录,跳转到登录页面 + // 先保存当前URL用于登录后重定向 + uni.setStorageSync('redirect', url); + + // 使用navigateTo而非redirectTo,保留页面栈 + uni.navigateTo({ + url: '/pages/user/login', + success: () => { + console.log('跳转到登录页面成功,保存的重定向地址:', url); + }, + fail: (err) => { + console.error('跳转到登录页面失败:', err); + reject(err); + } + }); + + // 拒绝当前的导航请求 + return reject(new Error('需要登录')); + } + + // 根据type选择跳转方式 + if (type) { + // 指定了跳转方式,直接使用 + switch (type) { + case 'switchTab': + uni.switchTab({ + url, + success: resolve, + fail: reject + }); + break; + case 'redirectTo': + uni.redirectTo({ + url, + success: resolve, + fail: reject + }); + break; + case 'reLaunch': + uni.reLaunch({ + url, + success: resolve, + fail: reject + }); + break; + case 'navigateTo': + default: + uni.navigateTo({ + url, + success: resolve, + fail: reject + }); + break; + } + } else { + // 未指定跳转方式,先尝试navigateTo,失败后尝试switchTab + uni.navigateTo({ + url, + success: resolve, + fail: (err) => { + // navigateTo失败,尝试switchTab + uni.switchTab({ + url, + success: resolve, + fail: (switchErr) => { + // 两种方式都失败,返回错误 + reject(switchErr); + } + }); + } + }); + } + }); +} + +/** + * 简化版路由跳转方法 + * @param {String} url 跳转地址 + * @param {Object} params 请求参数,可选 + * @param {String} type 跳转方式,可选 + * @returns {Promise} 返回Promise对象 + */ +export function navigateTo(url, params = {}, type = '') { + return routerTo({ + url, + params, + type + }); +} + +/** + * 收集白名单路径 + * @param {String} path 需要添加到白名单的路径 + * @returns {Array} 返回更新后的白名单数组 + */ +export function collectWhitePath(path) { + // 缓存的键名 + const WHITELIST_CACHE_KEY = 'APP_WHITELIST_PATHS'; + + // 从缓存中获取现有白名单 + let cachedWhitelist = uni.getStorageSync(WHITELIST_CACHE_KEY) || []; + + // 如果不是数组,则初始化为空数组 + if (!Array.isArray(cachedWhitelist)) { + cachedWhitelist = []; + } + + // 检查路径是否已存在于白名单中 + if (path && path.trim() && !cachedWhitelist.includes(path)) { + // 添加新路径到白名单 + cachedWhitelist.push(path); + + // 保存更新后的白名单到缓存 + uni.setStorageSync(WHITELIST_CACHE_KEY, cachedWhitelist); + + console.log('添加白名单路径成功:', path); + } + + return cachedWhitelist; +} + +/** + * 获取已收集的白名单路径 + * @returns {Array} 返回缓存中的白名单数组 + */ +export function getCollectedWhitePaths() { + const WHITELIST_CACHE_KEY = 'APP_WHITELIST_PATHS'; + return uni.getStorageSync(WHITELIST_CACHE_KEY) || []; +} + +/** + * 清空已收集的白名单路径 + * @returns {Boolean} 返回操作结果 + */ +export function clearCollectedWhitePaths() { + const WHITELIST_CACHE_KEY = 'APP_WHITELIST_PATHS'; + uni.setStorageSync(WHITELIST_CACHE_KEY, []); + return true; +} + +// 更新默认导出 +export default { + routerTo, + navigateTo, + navigateBack, + collectWhitePath, + getCollectedWhitePaths, + clearCollectedWhitePaths +}; \ No newline at end of file diff --git a/components/nav-header/nav-header.vue b/components/nav-header/nav-header.vue index e3200ca..7e10ab6 100644 --- a/components/nav-header/nav-header.vue +++ b/components/nav-header/nav-header.vue @@ -64,25 +64,14 @@ export default { goBack() { if (this.backUrl) { // 如果指定了返回页面路径,则跳转到指定页面 - uni.redirectTo({ - url: this.backUrl, - success: () => { - - }, - fail: (err) => { + this.$router.navigateTo(this.backUrl, {}, 'redirectTo') + .catch(err => { console.log('返回页面失败', err); - uni.switchTab({ - url: this.backUrl, - success: () => { - - }, - fail: (err) => { + this.$router.navigateTo(this.backUrl, {}, 'switchTab') + .catch(err => { console.log('返回页面失败1', err); - } - }); - } - }); - + }); + }); } else { var pages = getCurrentPages(); if (pages.length > 1) { @@ -93,9 +82,7 @@ export default { } }); } else { - uni.switchTab({ - url: '/pages/shouye/index', - }); + this.$router.navigateTo('/pages/shouye/index', {}, 'switchTab'); } } } diff --git a/main.js b/main.js index cf6c50f..38d3216 100644 --- a/main.js +++ b/main.js @@ -3,6 +3,7 @@ import App from './App' import Mixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js' import common from '@/common/common.js' import { gotopage } from '@/common/gotopage.js' +import router from '@/common/router.js' import ConfigManager from '@/common/config.js' import RequestManager from '@/common/request.js' import EnvConfig from '@/common/env.js' @@ -24,6 +25,8 @@ Vue.prototype.$wxloginPage = EnvConfig.wxLoginUrl // 公共方法 Vue.prototype.gotoPage = gotopage +Vue.prototype.$router = router +Vue.prototype.$navigateTo = router.navigateTo Vue.prototype.$noMultipleClicks = common.noMultipleClicks Vue.prototype.$c = common // 全局配置管理器 diff --git a/pages/DrawCard/index.vue b/pages/DrawCard/index.vue index 0887dc9..47fbd71 100644 --- a/pages/DrawCard/index.vue +++ b/pages/DrawCard/index.vue @@ -181,9 +181,7 @@ }) }, todetails(e) { - uni.navigateTo({ - url: 'cardDetail?goods_id=' + e.id - }) + this.$router.navigateTo('cardDetail', { goods_id: e.id }); }, getlist(v) { this.show = v; diff --git a/pages/infinite/benefit.vue b/pages/infinite/benefit.vue index 1eb8a9e..468d0ef 100644 --- a/pages/infinite/benefit.vue +++ b/pages/infinite/benefit.vue @@ -60,9 +60,7 @@ export default { getlist(index) { this.current = index if (index == 1) { - uni.navigateTo({ - url: 'UnlimitedOrder' - }) + this.$router.navigateTo('UnlimitedOrder'); } } }, diff --git a/pages/infinite/bonus_house_details.vue b/pages/infinite/bonus_house_details.vue index dc5205a..d2f2324 100644 --- a/pages/infinite/bonus_house_details.vue +++ b/pages/infinite/bonus_house_details.vue @@ -691,9 +691,7 @@ export default { }, toHome() { - uni.reLaunch({ - url: '/pages/shouye/index' - }); + this.$router.navigateTo('/pages/shouye/index', {}, 'reLaunch'); }, applyPageTransitions() { diff --git a/pages/infinite/index.vue b/pages/infinite/index.vue index dcc37f7..4c69f34 100644 --- a/pages/infinite/index.vue +++ b/pages/infinite/index.vue @@ -4,7 +4,8 @@ - + {{ item.name }} @@ -13,161 +14,166 @@ \ No newline at end of file diff --git a/pages/other/order_list.vue b/pages/other/order_list.vue index 64769a4..0294861 100644 --- a/pages/other/order_list.vue +++ b/pages/other/order_list.vue @@ -89,9 +89,7 @@ export default { }, goOrderDetail(item) { - uni.navigateTo({ - url: `/pages/other/order_info?order_num=${item.order_num}` - }); + this.$router.navigateTo('/pages/other/order_info', { order_num: item.order_num }); } } } diff --git a/pages/shop/detail.vue b/pages/shop/detail.vue index b682952..6a8bf83 100644 --- a/pages/shop/detail.vue +++ b/pages/shop/detail.vue @@ -229,9 +229,7 @@ setTimeout(() => { that.$refs.order_show.close() //去订单 - uni.redirectTo({ - url: '/pages/miaosha/shop_order' - }) + that.$router.navigateTo('/pages/miaosha/shop_order', {}, 'redirectTo'); }, 1500) } } @@ -245,9 +243,7 @@ setTimeout(() => { that.$refs.order_show.close() //去订单 - uni.redirectTo({ - url: '/pages/miaosha/shop_order' - }) + that.$router.navigateTo('/pages/miaosha/shop_order', {}, 'redirectTo'); }, 1500) } } else { diff --git a/pages/shop/index.vue b/pages/shop/index.vue index 7ab99bc..778be16 100644 --- a/pages/shop/index.vue +++ b/pages/shop/index.vue @@ -100,21 +100,14 @@ }, openUrl(e) { if (e.target == 1) { - uni.navigateTo({ - url: '../shouye/detail?id=' + e.goods_id - }) + this.$router.navigateTo('../shouye/detail', { id: e.goods_id }); } if (e.target == 2) { - uni.navigateTo({ - url: 'detail?goods_id=' + e.product_id - }) + this.$router.navigateTo('detail', { goods_id: e.product_id }); } if (e.target == 3) { - uni.navigateTo({ - url: '../web-view?url=' + e.url - }) + this.$router.navigateTo('../web-view', { url: e.url }); } - }, diff --git a/pages/shouye/huanxiang.vue b/pages/shouye/huanxiang.vue index c728872..07e42b2 100644 --- a/pages/shouye/huanxiang.vue +++ b/pages/shouye/huanxiang.vue @@ -107,25 +107,18 @@ todetail(e) { if (this.type == '3') { - uni.redirectTo({ - url: '/package/index/leitai?goods_id=' + - this.id + - '&goods_num=' + - e + - '&type_text=' + - this.type_text - }) + this.$router.navigateTo('/package/index/leitai', { + goods_id: this.id, + goods_num: e, + type_text: this.type_text + }, 'redirectTo'); } else { - uni.redirectTo({ - url: 'detail?goods_id=' + - this.id + - '&goods_num=' + - e + - '&type_text=' + - this.type_text - }) + this.$router.navigateTo('detail', { + goods_id: this.id, + goods_num: e, + type_text: this.type_text + }, 'redirectTo'); } - }, change_saixuan(a, b) { this.saixuan_index = a diff --git a/pages/shouye/index.vue b/pages/shouye/index.vue index ac92cb0..300d6aa 100644 --- a/pages/shouye/index.vue +++ b/pages/shouye/index.vue @@ -396,6 +396,10 @@ export default { todetails(e) { // 无限赏 let url = "/pages/shouye/detail"; + let params = { + goods_id: e.id, + type_text: e.type_text + }; if (e.type == 2 || e.type == 8 || e.type == 16) { url = "/pages/shouye/detail_wuxian"; @@ -408,13 +412,7 @@ export default { url = "/package/index/leitai"; } - this.$c.to({ - url, - query: { - goods_id: e.id, - type_text: e.type_text, - }, - }); + this.$router.navigateTo(url, params); }, getlist(v) { this.tabCur = v; @@ -426,14 +424,10 @@ export default { }, toRanking() { - uni.navigateTo({ - url: "/pages/shouye/ranking", - }); + this.$router.navigateTo("/pages/shouye/ranking"); }, toyaqingRanking() { - uni.navigateTo({ - url: "/pages/shouye/yaoqing_ranking", - }); + this.$router.navigateTo("/pages/shouye/yaoqing_ranking"); }, }, }; diff --git a/pages/shouye/rili.vue b/pages/shouye/rili.vue index 1c1f64a..7aefade 100644 --- a/pages/shouye/rili.vue +++ b/pages/shouye/rili.vue @@ -40,9 +40,7 @@ // }) // } else { if (e.goods_id !== 0) { - uni.navigateTo({ - url: 'detail?goods_id=' + e.goods_id - }) + this.$router.navigateTo('detail', { goods_id: e.goods_id }); } // } diff --git a/pages/user/bangding.vue b/pages/user/bangding.vue index 651f06e..7bb1970 100644 --- a/pages/user/bangding.vue +++ b/pages/user/bangding.vue @@ -39,10 +39,14 @@ export default { }, methods: { back() { - // uni.navigateBack() - uni.reLaunch({ - url: 'index' - }) + // 尝试返回上一页 + let that = this; + uni.navigateBack({ + fail: function() { + // 如果返回失败,表示没有上一页,则跳转到首页 + that.$router.navigateTo('/pages/user/index', {}, 'reLaunch'); + } + }); }, getPhoneNumber(e) { // console.log(e) @@ -69,20 +73,13 @@ export default { // var pageurl = uni.getStorageSync( // 'page'); setTimeout(() => { - uni.reLaunch({ - url: 'index' - }) - // console.log(pageurl); - // if (pageurl != null && - // pageurl != '') { - // console.log(pageurl + - // "12321312"); - // // uni.reLaunch({ - // // url: pageurl - // // }) - // } else { - // uni.navigateBack() - // } + // 返回上一页而不是跳转到首页 + uni.navigateBack({ + fail: function() { + // 如果返回失败,表示没有上一页,则跳转到首页 + that.$router.navigateTo('/pages/user/index', {}, 'reLaunch'); + } + }); }, 1000) } }) diff --git a/pages/user/change.vue b/pages/user/change.vue index 2dbd0ad..4942829 100644 --- a/pages/user/change.vue +++ b/pages/user/change.vue @@ -144,9 +144,7 @@ // 延迟返回 setTimeout(() => { - uni.redirectTo({ - url: "./index", - }); + this.$router.navigateTo("./index", {}, 'redirectTo'); }, 1000); } else { uni.showToast({ diff --git a/pages/user/coupon.vue b/pages/user/coupon.vue index 3d2f80d..1183478 100644 --- a/pages/user/coupon.vue +++ b/pages/user/coupon.vue @@ -106,9 +106,7 @@ methods: { toUse(item) { if (this.pageType == 1) { - uni.switchTab({ - url: '/pages/shouye/index' - }) + this.$router.navigateTo('/pages/shouye/index', {}, 'switchTab'); } if (this.pageType == 2) { diff --git a/pages/user/fhjl.vue b/pages/user/fhjl.vue index b77a7ab..31d5020 100644 --- a/pages/user/fhjl.vue +++ b/pages/user/fhjl.vue @@ -219,14 +219,13 @@ export default { }) }, toorder(e) { - uni.navigateTo({ - url: 'order?id=' + e + this.$router.navigateTo('/pages/shouye/detail', { + goods_id: e, + goods_num: 1 }) }, towuliu(e) { - uni.navigateTo({ - url: 'wuliu_detail?id=' + e - }) + this.$router.navigateTo('/pages/shouye/wuliu_detail', { id: e }) }, getlist(v) { this.show = v diff --git a/pages/user/index.vue b/pages/user/index.vue index 798f922..f2effe6 100644 --- a/pages/user/index.vue +++ b/pages/user/index.vue @@ -560,16 +560,12 @@ export default { }, todetail(a) { if (a.type == 2) { - uni.navigateTo({ - url: '/pages/shouye/detail_wuxian?goods_id=' + a.goods_id - }) + this.$router.navigateTo('/pages/shouye/detail_wuxian', { goods_id: a.goods_id }); } else { - uni.navigateTo({ - url: '/pages/shouye/detail?goods_id=' + - a.goods_id + - '&goods_num=' + - a.num - }) + this.$router.navigateTo('/pages/shouye/detail', { + goods_id: a.goods_id, + goods_num: a.num + }); } }, diff --git a/pages/user/login.vue b/pages/user/login.vue index aecc8ff..a3ddd32 100644 --- a/pages/user/login.vue +++ b/pages/user/login.vue @@ -19,18 +19,18 @@ 我已阅读并同意 - + 《用户协议》 和 - + 《隐私政策》 - + 拒绝 @@ -80,9 +80,10 @@ export default { //第一授权获取用户信息===》按钮触发 getUserProfile(e) { if (!this.isAgree) { - return this.$c.toast({ - title: '请阅读并同意《用户协议》和《隐私政策》' - }) + return uni.showToast({ + title: '请阅读并同意《用户协议》和《隐私政策》', + icon: 'none' + }); } // #ifdef MP-WEIXIN @@ -90,6 +91,56 @@ export default { // #endif + }, + /** + * 处理登录成功后的重定向逻辑 + * @param {String} redirectUrl 重定向URL + */ + handleRedirect(redirectUrl) { + let that = this; + + // 获取当前页面栈 + const pages = getCurrentPages(); + + // 检查是否有上一页,以及redirect URL是否与上一页相同 + if (pages.length > 1) { + const prevPage = pages[pages.length - 2]; + // 构建完整的上一页路径(带参数) + let prevPageUrl = '/' + prevPage.route; + if (prevPage.options && Object.keys(prevPage.options).length > 0) { + const paramStr = Object.keys(prevPage.options) + .map(key => `${key}=${prevPage.options[key]}`) + .join('&'); + prevPageUrl += '?' + paramStr; + } + + // 比较redirect URL和上一页URL + const redirectUrlPath = redirectUrl.split('?')[0]; + const prevPageUrlPath = prevPageUrl.split('?')[0]; + + if (redirectUrlPath === prevPageUrlPath) { + // 如果路径相同,直接返回上一页 + console.log('重定向URL与上一页相同,直接返回'); + setTimeout(() => { + uni.navigateBack(); + }, 1000); + return true; + } + } + + // 重定向到指定URL + setTimeout(() => { + that.$router.routerTo({ + url: redirectUrl, + type: 'navigateTo' + }).catch(err => { + console.error('重定向跳转失败:', err) + // 失败时跳转到首页 + that.$router.navigateTo('/pages/user/index', {}, 'navigateTo') + }) + }, 1000); + + return true; }, wxUserProfile() { var that = this @@ -112,35 +163,21 @@ export default { if (res.status == 1) { uni.setStorageSync('token', res.data) that.$c.msg("登录成功") - var lgurl = uni.getStorageSync('lgurl') //获取页面路径 - var lgdata = uni.getStorageSync('lgurldata') //获取页面参数 - if (lgurl) { - lgdata = JSON.parse(lgdata) - // 拼接参数 - let param = '' - for (let key in lgdata) { - param += '&' + key + '=' + lgdata[ - key] - } - param = '/' + lgurl + param.replace( - '&', '?') - uni.setStorageSync('lgurl', '') - uni.setStorageSync('lgurldata', '') - console.log(param) - - setTimeout(() => { - uni.reLaunch({ - url: param - }) - }, 1000) + + // 检查重定向URL + const redirectUrl = uni.getStorageSync('redirect') + + if (redirectUrl) { + // 清除重定向信息 + uni.removeStorageSync('redirect') + + // 使用公共方法处理重定向 + that.handleRedirect(redirectUrl); } else { + // 无重定向页面,检查是否需要绑定手机号 setTimeout(() => { that.getdata(); }, 1000) - - // uni.reLaunch({ - // url: 'index' - // }) } } else { that.$c.msg("登录失败!" + res.msg) @@ -180,32 +217,21 @@ export default { success: function (res) { if (res.status == 1) { uni.setStorageSync('token', res.data) - var lgurl = uni.getStorageSync( - 'lgurl') //获取页面路径 - var lgdata = uni.getStorageSync( - 'lgurldata') //获取页面参数 - if (lgurl) { - lgdata = JSON.parse(lgdata) - // 拼接参数 - let param = '' - for (let key in lgdata) { - param += '&' + key + '=' + lgdata[ - key] - } - param = '/' + lgurl + param.replace( - '&', '?') - uni.setStorageSync('lgurl', '') - uni.setStorageSync('lgurldata', '') - console.log(param) - setTimeout(() => { - uni.reLaunch({ - url: param - }) - }, 1000) + + // 检查重定向URL + const redirectUrl = uni.getStorageSync('redirect') + + if (redirectUrl) { + // 清除重定向信息 + uni.removeStorageSync('redirect') + + // 使用公共方法处理重定向 + that.handleRedirect(redirectUrl); } else { - uni.reLaunch({ - url: 'index' - }) + // 无重定向页面,检查是否需要绑定手机号 + setTimeout(() => { + that.getdata(); + }, 1000) } } } @@ -224,13 +250,11 @@ export default { success(res) { if (res.status == 1 && res.data.userinfo != null && res.data.userinfo.mobile_is == 0) { console.log("判断用户有没有手机号", res.data.userinfo.mobile_is); - uni.reLaunch({ - url: 'bangding' - }) + // 使用新的路由守卫方法跳转到绑定页面 + that.$router.navigateTo('/pages/user/bangding', {}, 'navigateTo'); } else { - uni.reLaunch({ - url: 'index' - }) + // 使用新的路由守卫方法跳转到用户首页 + that.$router.navigateTo('/pages/user/index', {}, 'navigateTo'); } } }) From da23c1066d2d1de1d87ddeeacba76f008da84b7d Mon Sep 17 00:00:00 2001 From: zpc Date: Tue, 22 Apr 2025 02:42:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=BC=E8=88=AA?= =?UTF-8?q?=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/nav-header/nav-header.vue | 307 +++++++++++++++++-- components/page-container/page-container.vue | 130 +++++++- pages/shouye/detail_wuxian.vue | 4 +- 3 files changed, 417 insertions(+), 24 deletions(-) diff --git a/components/nav-header/nav-header.vue b/components/nav-header/nav-header.vue index 7e10ab6..ebc29a7 100644 --- a/components/nav-header/nav-header.vue +++ b/components/nav-header/nav-header.vue @@ -1,16 +1,51 @@ @@ -18,16 +53,46 @@ export default { name: 'NavHeader', props: { + // 是否暗黑模式 + dark: { + type: Boolean, + default: false + }, // 标题文字 title: { type: String, default: '' }, + // 左侧文字 + leftText: { + type: String, + default: '' + }, + // 右侧文字 + rightText: { + type: String, + default: '' + }, + // 左侧图标 + leftIcon: { + type: String, + default: '' + }, + // 右侧图标 + rightIcon: { + type: String, + default: '' + }, // 是否显示返回按钮 showBack: { type: Boolean, default: false }, + // 是否显示返回文字 + showBackText: { + type: Boolean, + default: false + }, // 返回页面路径,不传则返回上一页 backUrl: { type: String, @@ -36,12 +101,12 @@ export default { // 文字颜色 color: { type: String, - default: '#000000' + default: '' }, // 背景颜色 backgroundColor: { type: String, - default: 'transparent' + default: '' }, // 是否固定在顶部 fixed: { @@ -53,15 +118,87 @@ export default { type: Boolean, default: true }, - // 是否显示底部边框 - border: { + // 是否显示阴影 + shadow: { type: Boolean, default: false + }, + // 导航栏高度 + height: { + type: [Number, String], + default: 44 + }, + // 左侧宽度 + leftWidth: { + type: [Number, String], + default: 60 + }, + // 右侧宽度 + rightWidth: { + type: [Number, String], + default: 60 + } + }, + data() { + return { + statusBarHeight: 20, // 默认状态栏高度 + innerTitle: '', // 内部使用的标题 + navbarHeight: 44 // 导航栏高度 + } + }, + computed: { + // 导航栏主题背景色 + themeBgColor() { + if (this.dark) { + if (this.backgroundColor) { + return this.backgroundColor; + } else { + return this.dark ? '#333' : '#FFF'; + } + } + return this.backgroundColor || '#FFF'; + }, + // 导航栏主题文字色 + themeColor() { + if (this.dark) { + if (this.color) { + return this.color; + } else { + return this.dark ? '#fff' : '#333'; + } + } + return this.color || '#333'; + } + }, + created() { + this.getStatusBarHeight(); + this.innerTitle = this.title; + + // 转换导航栏高度 + this.navbarHeight = typeof this.height === 'number' ? this.height : parseInt(this.height); + }, + watch: { + title(newVal) { + this.innerTitle = newVal; } }, methods: { + // 获取状态栏高度 + getStatusBarHeight() { + uni.getSystemInfo({ + success: (res) => { + this.statusBarHeight = res.statusBarHeight || 20; + } + }); + }, // 返回页面 goBack() { + this.onClickLeft(); + }, + // 点击左侧按钮 + onClickLeft() { + this.$emit('clickLeft'); + if (this.backUrl) { // 如果指定了返回页面路径,则跳转到指定页面 this.$router.navigateTo(this.backUrl, {}, 'redirectTo') @@ -85,18 +222,152 @@ export default { this.$router.navigateTo('/pages/shouye/index', {}, 'switchTab'); } } + }, + // 点击右侧按钮 + onClickRight() { + this.$emit('clickRight'); + }, + // 点击标题 + onClickTitle() { + this.$emit('clickTitle'); } } } \ No newline at end of file diff --git a/components/page-container/page-container.vue b/components/page-container/page-container.vue index 7474b3c..8e41a11 100644 --- a/components/page-container/page-container.vue +++ b/components/page-container/page-container.vue @@ -1,9 +1,28 @@