feat(auth): 重构授权重定向逻辑并添加分享页面处理
将授权跳转和返回逻辑抽取到独立工具函数中 添加分享页面授权处理逻辑,优化用户体验
Showing
5 changed files
with
179 additions
and
26 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2022-09-19 14:11:06 | 2 | * @Date: 2022-09-19 14:11:06 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-04 12:09:01 | 4 | + * @LastEditTime: 2025-09-04 15:34:50 |
| 5 | * @FilePath: /lls_program/src/pages/ActivitiesCover/index.vue | 5 | * @FilePath: /lls_program/src/pages/ActivitiesCover/index.vue |
| 6 | * @Description: 活动海报页面 - 展示活动信息并处理定位授权 | 6 | * @Description: 活动海报页面 - 展示活动信息并处理定位授权 |
| 7 | --> | 7 | --> |
| ... | @@ -82,13 +82,14 @@ | ... | @@ -82,13 +82,14 @@ |
| 82 | <script setup> | 82 | <script setup> |
| 83 | import '@tarojs/taro/html.css' | 83 | import '@tarojs/taro/html.css' |
| 84 | import { ref, onMounted } from "vue" | 84 | import { ref, onMounted } from "vue" |
| 85 | -import Taro from '@tarojs/taro' | 85 | +import Taro, { useLoad } from '@tarojs/taro' |
| 86 | import "./index.less" | 86 | import "./index.less" |
| 87 | import BottomNav from '../../components/BottomNav.vue' | 87 | import BottomNav from '../../components/BottomNav.vue' |
| 88 | import PosterBuilder from '../../components/PosterBuilder/index.vue' | 88 | import PosterBuilder from '../../components/PosterBuilder/index.vue' |
| 89 | import ShareButton from '../../components/ShareButton/index.vue' | 89 | import ShareButton from '../../components/ShareButton/index.vue' |
| 90 | // 接口信息 | 90 | // 接口信息 |
| 91 | import { getMyFamiliesAPI } from '@/api/family' | 91 | import { getMyFamiliesAPI } from '@/api/family' |
| 92 | +import { handleSharePageAuth, addShareFlag } from '@/utils/authRedirect' | ||
| 92 | 93 | ||
| 93 | // 默认海报图 | 94 | // 默认海报图 |
| 94 | const defaultPoster = 'https://cdn.ipadbiz.cn/lls_prog/images/welcome_1.png'; | 95 | const defaultPoster = 'https://cdn.ipadbiz.cn/lls_prog/images/welcome_1.png'; |
| ... | @@ -282,12 +283,12 @@ const onShareActivity = () => { | ... | @@ -282,12 +283,12 @@ const onShareActivity = () => { |
| 282 | const onShareAppMessage = () => { | 283 | const onShareAppMessage = () => { |
| 283 | return { | 284 | return { |
| 284 | title: '分享活动海报', | 285 | title: '分享活动海报', |
| 285 | - path: '/pages/ActivitiesCover/index', | 286 | + path: addShareFlag('/pages/ActivitiesCover/index'), |
| 286 | success: (res) => { | 287 | success: (res) => { |
| 287 | - console.log('分享成功', res) | 288 | + // 分享成功 |
| 288 | }, | 289 | }, |
| 289 | fail: (err) => { | 290 | fail: (err) => { |
| 290 | - console.error('分享失败', err) | 291 | + // 分享失败 |
| 291 | } | 292 | } |
| 292 | } | 293 | } |
| 293 | } | 294 | } |
| ... | @@ -605,8 +606,10 @@ const savePoster = () => { | ... | @@ -605,8 +606,10 @@ const savePoster = () => { |
| 605 | }) | 606 | }) |
| 606 | } | 607 | } |
| 607 | 608 | ||
| 608 | -// 页面挂载时检查定位授权状态 | 609 | +/** |
| 609 | -onMounted(async () => { | 610 | + * 初始化页面数据 |
| 611 | + */ | ||
| 612 | +const initPageData = async () => { | ||
| 610 | // 获取用户是否加入家庭 | 613 | // 获取用户是否加入家庭 |
| 611 | const { code, data } = await getMyFamiliesAPI() | 614 | const { code, data } = await getMyFamiliesAPI() |
| 612 | if (code) { | 615 | if (code) { |
| ... | @@ -617,6 +620,19 @@ onMounted(async () => { | ... | @@ -617,6 +620,19 @@ onMounted(async () => { |
| 617 | } | 620 | } |
| 618 | // 检查定位授权弹窗 | 621 | // 检查定位授权弹窗 |
| 619 | checkLocationAuth() | 622 | checkLocationAuth() |
| 623 | +} | ||
| 624 | + | ||
| 625 | +// 处理页面加载时的授权检查 | ||
| 626 | +useLoad((options) => { | ||
| 627 | + // 处理分享页面的授权逻辑 | ||
| 628 | + handleSharePageAuth(options, () => { | ||
| 629 | + initPageData(); | ||
| 630 | + }); | ||
| 631 | +}); | ||
| 632 | + | ||
| 633 | +// 页面挂载时检查定位授权状态 | ||
| 634 | +onMounted(async () => { | ||
| 635 | + initPageData() | ||
| 620 | }) | 636 | }) |
| 621 | </script> | 637 | </script> |
| 622 | 638 | ... | ... |
| 1 | <template> | 1 | <template> |
| 2 | <view class="min-h-screen bg-white pb-24"> | 2 | <view class="min-h-screen bg-white pb-24"> |
| 3 | <!-- 分享按钮 --> | 3 | <!-- 分享按钮 --> |
| 4 | - <button id="share" data-name="shareBtn" open-type="share" style="font-size: 1.05rem; padding: 0; position: fixed; right: 40rpx; top: 40rpx; z-index: 1000;color: white; width: 80rpx; height: 80rpx; border-radius: 50%; background: rgba(0, 0, 0, 0.6);">分享</button> | 4 | + <button id="share" data-name="shareBtn" open-type="share" style="font-size: 0.9rem; padding: 0; position: absolute; right: 40rpx; top: 40rpx; z-index: 1000;color: white; width: 70rpx; height: 70rpx; border-radius: 50%; background: rgba(0, 0, 0, 0.6);">分享</button> |
| 5 | <!-- Top Image --> | 5 | <!-- Top Image --> |
| 6 | <view class="w-full h-48"> | 6 | <view class="w-full h-48"> |
| 7 | <image :src="reward.image" class="w-full h-full object-cover" /> | 7 | <image :src="reward.image" class="w-full h-full object-cover" /> |
| ... | @@ -65,9 +65,10 @@ | ... | @@ -65,9 +65,10 @@ |
| 65 | 65 | ||
| 66 | <script setup> | 66 | <script setup> |
| 67 | import { ref } from 'vue'; | 67 | import { ref } from 'vue'; |
| 68 | -import Taro, { useDidShow } from '@tarojs/taro'; | 68 | +import Taro, { useDidShow, useLoad } from '@tarojs/taro'; |
| 69 | import AppHeader from '../../components/AppHeader.vue'; | 69 | import AppHeader from '../../components/AppHeader.vue'; |
| 70 | import { getUserProfileAPI } from '@/api/user'; | 70 | import { getUserProfileAPI } from '@/api/user'; |
| 71 | +import { handleSharePageAuth, addShareFlag } from '@/utils/authRedirect'; | ||
| 71 | 72 | ||
| 72 | const isCreator = ref(false); | 73 | const isCreator = ref(false); |
| 73 | 74 | ||
| ... | @@ -138,6 +139,13 @@ const initData = async () => { | ... | @@ -138,6 +139,13 @@ const initData = async () => { |
| 138 | } | 139 | } |
| 139 | }; | 140 | }; |
| 140 | 141 | ||
| 142 | +useLoad((options) => { | ||
| 143 | + // 处理分享页面的授权逻辑 | ||
| 144 | + handleSharePageAuth(options, () => { | ||
| 145 | + initData(); | ||
| 146 | + }); | ||
| 147 | +}); | ||
| 148 | + | ||
| 141 | useDidShow(() => { | 149 | useDidShow(() => { |
| 142 | initData(); | 150 | initData(); |
| 143 | }); | 151 | }); |
| ... | @@ -170,7 +178,7 @@ const shareBtnClick = () => { | ... | @@ -170,7 +178,7 @@ const shareBtnClick = () => { |
| 170 | const onShareAppMessage = () => { | 178 | const onShareAppMessage = () => { |
| 171 | return { | 179 | return { |
| 172 | title: `${reward.value.title} - 仅需${reward.value.points}积分`, | 180 | title: `${reward.value.title} - 仅需${reward.value.points}积分`, |
| 173 | - path: `/pages/RewardDetail/index?id=${reward.value.id}`, | 181 | + path: addShareFlag(`/pages/RewardDetail/index?id=${reward.value.id}`), |
| 174 | imageUrl: reward.value.image | 182 | imageUrl: reward.value.image |
| 175 | } | 183 | } |
| 176 | } | 184 | } | ... | ... |
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2022-09-19 14:11:06 | 2 | * @Date: 2022-09-19 14:11:06 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-03 21:05:25 | 4 | + * @LastEditTime: 2025-09-04 09:20:14 |
| 5 | * @FilePath: /lls_program/src/pages/auth/index.vue | 5 | * @FilePath: /lls_program/src/pages/auth/index.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -22,6 +22,7 @@ import request from '@/utils/request'; | ... | @@ -22,6 +22,7 @@ import request from '@/utils/request'; |
| 22 | <script> | 22 | <script> |
| 23 | import "./index.less"; | 23 | import "./index.less"; |
| 24 | import { getCurrentPageParam } from "@/utils/weapp"; | 24 | import { getCurrentPageParam } from "@/utils/weapp"; |
| 25 | +import { returnToOriginalPage } from "@/utils/authRedirect"; | ||
| 25 | 26 | ||
| 26 | export default { | 27 | export default { |
| 27 | name: "authPage", | 28 | name: "authPage", |
| ... | @@ -41,14 +42,14 @@ export default { | ... | @@ -41,14 +42,14 @@ export default { |
| 41 | 42 | ||
| 42 | // 测试环境下传递openid,正式环境不传递 | 43 | // 测试环境下传递openid,正式环境不传递 |
| 43 | if (process.env.NODE_ENV === 'development') { | 44 | if (process.env.NODE_ENV === 'development') { |
| 44 | - // requestData.openid = 'h-008'; | 45 | + requestData.openid = 'h-008'; |
| 45 | // requestData.openid = 'h-009'; | 46 | // requestData.openid = 'h-009'; |
| 46 | // requestData.openid = 'h-010'; | 47 | // requestData.openid = 'h-010'; |
| 47 | // requestData.openid = 'h-011'; | 48 | // requestData.openid = 'h-011'; |
| 48 | // requestData.openid = 'h-012'; | 49 | // requestData.openid = 'h-012'; |
| 49 | // requestData.openid = 'h-013'; | 50 | // requestData.openid = 'h-013'; |
| 50 | // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; | 51 | // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; |
| 51 | - requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; | 52 | + // requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | request.post('/srv/?a=openid', requestData) | 55 | request.post('/srv/?a=openid', requestData) |
| ... | @@ -69,11 +70,8 @@ export default { | ... | @@ -69,11 +70,8 @@ export default { |
| 69 | // url: '../apxUserInfo/index' | 70 | // url: '../apxUserInfo/index' |
| 70 | // }) | 71 | // }) |
| 71 | // } | 72 | // } |
| 72 | - // TAG:处理分享跳转问题 | 73 | + // TAG:处理分享跳转问题 - 使用新的重定向逻辑 |
| 73 | - const params = getCurrentPageParam(); | 74 | + returnToOriginalPage(); |
| 74 | - Taro.reLaunch({ | ||
| 75 | - url: `/pages/Dashboard/index` | ||
| 76 | - }) | ||
| 77 | Taro.hideLoading(); | 75 | Taro.hideLoading(); |
| 78 | } | 76 | } |
| 79 | } else { | 77 | } else { | ... | ... |
src/utils/authRedirect.js
0 → 100644
| 1 | +/* | ||
| 2 | + * @Date: 2025-01-25 10:00:00 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2025-09-04 15:33:00 | ||
| 5 | + * @FilePath: /lls_program/src/utils/authRedirect.js | ||
| 6 | + * @Description: 授权重定向处理工具函数 | ||
| 7 | + */ | ||
| 8 | +import Taro from '@tarojs/taro' | ||
| 9 | +import { routerStore } from '@/stores/router' | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * 获取当前页面完整路径(包含参数) | ||
| 13 | + * @returns {string} 完整的页面路径 | ||
| 14 | + */ | ||
| 15 | +export const getCurrentPageFullPath = () => { | ||
| 16 | + const pages = getCurrentPages() | ||
| 17 | + if (pages.length === 0) return '' | ||
| 18 | + | ||
| 19 | + const currentPage = pages[pages.length - 1] | ||
| 20 | + const route = currentPage.route | ||
| 21 | + const options = currentPage.options | ||
| 22 | + | ||
| 23 | + // 构建查询参数字符串 | ||
| 24 | + const queryParams = Object.keys(options) | ||
| 25 | + .map(key => `${key}=${encodeURIComponent(options[key])}`) | ||
| 26 | + .join('&') | ||
| 27 | + | ||
| 28 | + return queryParams ? `${route}?${queryParams}` : route | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +/** | ||
| 32 | + * 保存当前页面路径到路由存储 | ||
| 33 | + * @param {string} customPath - 自定义路径,如果不提供则使用当前页面路径 | ||
| 34 | + */ | ||
| 35 | +export const saveCurrentPagePath = (customPath) => { | ||
| 36 | + const router = routerStore() | ||
| 37 | + const path = customPath || getCurrentPageFullPath() | ||
| 38 | + router.add(path) | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +/** | ||
| 42 | + * 跳转到授权页面 | ||
| 43 | + * @param {string} returnPath - 授权完成后要返回的页面路径 | ||
| 44 | + */ | ||
| 45 | +export const navigateToAuth = (returnPath) => { | ||
| 46 | + // 保存返回路径 | ||
| 47 | + if (returnPath) { | ||
| 48 | + saveCurrentPagePath(returnPath) | ||
| 49 | + } else { | ||
| 50 | + saveCurrentPagePath() | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + // 跳转到授权页面 | ||
| 54 | + Taro.navigateTo({ | ||
| 55 | + url: '/pages/auth/index' | ||
| 56 | + }) | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +/** | ||
| 60 | + * 授权完成后返回原页面 | ||
| 61 | + * @param {string} defaultPath - 默认返回路径,如果没有保存的路径则使用此路径 | ||
| 62 | + */ | ||
| 63 | +export const returnToOriginalPage = (defaultPath = '/pages/Dashboard/index') => { | ||
| 64 | + const router = routerStore() | ||
| 65 | + const savedPath = router.url | ||
| 66 | + | ||
| 67 | + if (savedPath && savedPath !== '') { | ||
| 68 | + // 清除保存的路径 | ||
| 69 | + router.remove() | ||
| 70 | + | ||
| 71 | + // 判断是否是首页,如果是首页使用reLaunch,否则使用redirectTo | ||
| 72 | + if (savedPath.includes('Dashboard/index') || savedPath === 'pages/Dashboard/index') { | ||
| 73 | + Taro.reLaunch({ | ||
| 74 | + url: `/${savedPath}` | ||
| 75 | + }) | ||
| 76 | + } else { | ||
| 77 | + Taro.redirectTo({ | ||
| 78 | + url: `/${savedPath}` | ||
| 79 | + }) | ||
| 80 | + } | ||
| 81 | + } else { | ||
| 82 | + Taro.reLaunch({ | ||
| 83 | + url: defaultPath | ||
| 84 | + }) | ||
| 85 | + } | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +/** | ||
| 89 | + * 检查页面是否来自分享 | ||
| 90 | + * @param {Object} options - 页面参数 | ||
| 91 | + * @returns {boolean} 是否来自分享 | ||
| 92 | + */ | ||
| 93 | +export const isFromShare = (options) => { | ||
| 94 | + // 检查是否有分享相关的参数或标识 | ||
| 95 | + return options && (options.from_share === '1' || options.scene) | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +/** | ||
| 99 | + * 处理分享页面的授权逻辑 | ||
| 100 | + * @param {Object} options - 页面参数 | ||
| 101 | + * @param {Function} callback - 授权成功后的回调函数 | ||
| 102 | + */ | ||
| 103 | +export const handleSharePageAuth = async (options, callback) => { | ||
| 104 | + const sessionid = wx.getStorageSync('sessionid') | ||
| 105 | + | ||
| 106 | + if (!sessionid) { | ||
| 107 | + // 没有授权,需要先授权 | ||
| 108 | + if (isFromShare(options)) { | ||
| 109 | + // 来自分享,保存当前页面路径用于授权后返回 | ||
| 110 | + saveCurrentPagePath() | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + // 跳转到授权页面 | ||
| 114 | + Taro.navigateTo({ | ||
| 115 | + url: '/pages/auth/index' | ||
| 116 | + }) | ||
| 117 | + return false | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + // 已授权,执行回调 | ||
| 121 | + if (callback && typeof callback === 'function') { | ||
| 122 | + callback() | ||
| 123 | + } | ||
| 124 | + return true | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +/** | ||
| 128 | + * 为分享链接添加分享标识参数 | ||
| 129 | + * @param {string} path - 原始路径 | ||
| 130 | + * @returns {string} 添加分享标识后的路径 | ||
| 131 | + */ | ||
| 132 | +export const addShareFlag = (path) => { | ||
| 133 | + const separator = path.includes('?') ? '&' : '?' | ||
| 134 | + return `${path}${separator}from_share=1` | ||
| 135 | +} |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2022-09-19 14:11:06 | 2 | * @Date: 2022-09-19 14:11:06 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-03 20:57:33 | 4 | + * @LastEditTime: 2025-09-04 15:33:42 |
| 5 | * @FilePath: /lls_program/src/utils/request.js | 5 | * @FilePath: /lls_program/src/utils/request.js |
| 6 | * @Description: 简单axios封装,后续按实际处理 | 6 | * @Description: 简单axios封装,后续按实际处理 |
| 7 | */ | 7 | */ |
| 8 | // import axios from 'axios' | 8 | // import axios from 'axios' |
| 9 | import axios from 'axios-miniprogram'; | 9 | import axios from 'axios-miniprogram'; |
| 10 | -import Taro from '@tarojs/taro' | 10 | +// // import qs from 'Qs' |
| 11 | -// import { strExist } from './tools' | 11 | +import { navigateToAuth } from '@/utils/authRedirect' |
| 12 | -// import qs from 'Qs' | ||
| 13 | -import { routerStore } from '@/stores/router' | ||
| 14 | 12 | ||
| 15 | // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; | 13 | // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; |
| 16 | // import store from '@/store' | 14 | // import store from '@/store' |
| ... | @@ -159,9 +157,7 @@ service.interceptors.response.use( | ... | @@ -159,9 +157,7 @@ service.interceptors.response.use( |
| 159 | * 授权完成后 返回当前页面 | 157 | * 授权完成后 返回当前页面 |
| 160 | */ | 158 | */ |
| 161 | setTimeout(() => { | 159 | setTimeout(() => { |
| 162 | - Taro.navigateTo({ | 160 | + navigateToAuth(); |
| 163 | - url: '/pages/auth/index?url=' + routerStore().url | ||
| 164 | - }); | ||
| 165 | }, 1000); | 161 | }, 1000); |
| 166 | } | 162 | } |
| 167 | return response | 163 | return response | ... | ... |
-
Please register or login to post a comment