hookehuyr

feat(auth): 重构授权重定向逻辑并添加分享页面处理

将授权跳转和返回逻辑抽取到独立工具函数中
添加分享页面授权处理逻辑,优化用户体验
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-04 12:09:01
* @LastEditTime: 2025-09-04 15:34:50
* @FilePath: /lls_program/src/pages/ActivitiesCover/index.vue
* @Description: 活动海报页面 - 展示活动信息并处理定位授权
-->
......@@ -82,13 +82,14 @@
<script setup>
import '@tarojs/taro/html.css'
import { ref, onMounted } from "vue"
import Taro from '@tarojs/taro'
import Taro, { useLoad } from '@tarojs/taro'
import "./index.less"
import BottomNav from '../../components/BottomNav.vue'
import PosterBuilder from '../../components/PosterBuilder/index.vue'
import ShareButton from '../../components/ShareButton/index.vue'
// 接口信息
import { getMyFamiliesAPI } from '@/api/family'
import { handleSharePageAuth, addShareFlag } from '@/utils/authRedirect'
// 默认海报图
const defaultPoster = 'https://cdn.ipadbiz.cn/lls_prog/images/welcome_1.png';
......@@ -282,12 +283,12 @@ const onShareActivity = () => {
const onShareAppMessage = () => {
return {
title: '分享活动海报',
path: '/pages/ActivitiesCover/index',
path: addShareFlag('/pages/ActivitiesCover/index'),
success: (res) => {
console.log('分享成功', res)
// 分享成功
},
fail: (err) => {
console.error('分享失败', err)
// 分享失败
}
}
}
......@@ -605,8 +606,10 @@ const savePoster = () => {
})
}
// 页面挂载时检查定位授权状态
onMounted(async () => {
/**
* 初始化页面数据
*/
const initPageData = async () => {
// 获取用户是否加入家庭
const { code, data } = await getMyFamiliesAPI()
if (code) {
......@@ -617,6 +620,19 @@ onMounted(async () => {
}
// 检查定位授权弹窗
checkLocationAuth()
}
// 处理页面加载时的授权检查
useLoad((options) => {
// 处理分享页面的授权逻辑
handleSharePageAuth(options, () => {
initPageData();
});
});
// 页面挂载时检查定位授权状态
onMounted(async () => {
initPageData()
})
</script>
......
<template>
<view class="min-h-screen bg-white pb-24">
<!-- 分享按钮 -->
<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>
<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>
<!-- Top Image -->
<view class="w-full h-48">
<image :src="reward.image" class="w-full h-full object-cover" />
......@@ -65,9 +65,10 @@
<script setup>
import { ref } from 'vue';
import Taro, { useDidShow } from '@tarojs/taro';
import Taro, { useDidShow, useLoad } from '@tarojs/taro';
import AppHeader from '../../components/AppHeader.vue';
import { getUserProfileAPI } from '@/api/user';
import { handleSharePageAuth, addShareFlag } from '@/utils/authRedirect';
const isCreator = ref(false);
......@@ -138,6 +139,13 @@ const initData = async () => {
}
};
useLoad((options) => {
// 处理分享页面的授权逻辑
handleSharePageAuth(options, () => {
initData();
});
});
useDidShow(() => {
initData();
});
......@@ -170,7 +178,7 @@ const shareBtnClick = () => {
const onShareAppMessage = () => {
return {
title: `${reward.value.title} - 仅需${reward.value.points}积分`,
path: `/pages/RewardDetail/index?id=${reward.value.id}`,
path: addShareFlag(`/pages/RewardDetail/index?id=${reward.value.id}`),
imageUrl: reward.value.image
}
}
......
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-03 21:05:25
* @LastEditTime: 2025-09-04 09:20:14
* @FilePath: /lls_program/src/pages/auth/index.vue
* @Description: 文件描述
-->
......@@ -22,6 +22,7 @@ import request from '@/utils/request';
<script>
import "./index.less";
import { getCurrentPageParam } from "@/utils/weapp";
import { returnToOriginalPage } from "@/utils/authRedirect";
export default {
name: "authPage",
......@@ -41,14 +42,14 @@ export default {
// 测试环境下传递openid,正式环境不传递
if (process.env.NODE_ENV === 'development') {
// requestData.openid = 'h-008';
requestData.openid = 'h-008';
// requestData.openid = 'h-009';
// requestData.openid = 'h-010';
// requestData.openid = 'h-011';
// requestData.openid = 'h-012';
// requestData.openid = 'h-013';
// requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8';
requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo';
// requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo';
}
request.post('/srv/?a=openid', requestData)
......@@ -69,11 +70,8 @@ export default {
// url: '../apxUserInfo/index'
// })
// }
// TAG:处理分享跳转问题
const params = getCurrentPageParam();
Taro.reLaunch({
url: `/pages/Dashboard/index`
})
// TAG:处理分享跳转问题 - 使用新的重定向逻辑
returnToOriginalPage();
Taro.hideLoading();
}
} else {
......
/*
* @Date: 2025-01-25 10:00:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-04 15:33:00
* @FilePath: /lls_program/src/utils/authRedirect.js
* @Description: 授权重定向处理工具函数
*/
import Taro from '@tarojs/taro'
import { routerStore } from '@/stores/router'
/**
* 获取当前页面完整路径(包含参数)
* @returns {string} 完整的页面路径
*/
export const getCurrentPageFullPath = () => {
const pages = getCurrentPages()
if (pages.length === 0) return ''
const currentPage = pages[pages.length - 1]
const route = currentPage.route
const options = currentPage.options
// 构建查询参数字符串
const queryParams = Object.keys(options)
.map(key => `${key}=${encodeURIComponent(options[key])}`)
.join('&')
return queryParams ? `${route}?${queryParams}` : route
}
/**
* 保存当前页面路径到路由存储
* @param {string} customPath - 自定义路径,如果不提供则使用当前页面路径
*/
export const saveCurrentPagePath = (customPath) => {
const router = routerStore()
const path = customPath || getCurrentPageFullPath()
router.add(path)
}
/**
* 跳转到授权页面
* @param {string} returnPath - 授权完成后要返回的页面路径
*/
export const navigateToAuth = (returnPath) => {
// 保存返回路径
if (returnPath) {
saveCurrentPagePath(returnPath)
} else {
saveCurrentPagePath()
}
// 跳转到授权页面
Taro.navigateTo({
url: '/pages/auth/index'
})
}
/**
* 授权完成后返回原页面
* @param {string} defaultPath - 默认返回路径,如果没有保存的路径则使用此路径
*/
export const returnToOriginalPage = (defaultPath = '/pages/Dashboard/index') => {
const router = routerStore()
const savedPath = router.url
if (savedPath && savedPath !== '') {
// 清除保存的路径
router.remove()
// 判断是否是首页,如果是首页使用reLaunch,否则使用redirectTo
if (savedPath.includes('Dashboard/index') || savedPath === 'pages/Dashboard/index') {
Taro.reLaunch({
url: `/${savedPath}`
})
} else {
Taro.redirectTo({
url: `/${savedPath}`
})
}
} else {
Taro.reLaunch({
url: defaultPath
})
}
}
/**
* 检查页面是否来自分享
* @param {Object} options - 页面参数
* @returns {boolean} 是否来自分享
*/
export const isFromShare = (options) => {
// 检查是否有分享相关的参数或标识
return options && (options.from_share === '1' || options.scene)
}
/**
* 处理分享页面的授权逻辑
* @param {Object} options - 页面参数
* @param {Function} callback - 授权成功后的回调函数
*/
export const handleSharePageAuth = async (options, callback) => {
const sessionid = wx.getStorageSync('sessionid')
if (!sessionid) {
// 没有授权,需要先授权
if (isFromShare(options)) {
// 来自分享,保存当前页面路径用于授权后返回
saveCurrentPagePath()
}
// 跳转到授权页面
Taro.navigateTo({
url: '/pages/auth/index'
})
return false
}
// 已授权,执行回调
if (callback && typeof callback === 'function') {
callback()
}
return true
}
/**
* 为分享链接添加分享标识参数
* @param {string} path - 原始路径
* @returns {string} 添加分享标识后的路径
*/
export const addShareFlag = (path) => {
const separator = path.includes('?') ? '&' : '?'
return `${path}${separator}from_share=1`
}
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-03 20:57:33
* @LastEditTime: 2025-09-04 15:33:42
* @FilePath: /lls_program/src/utils/request.js
* @Description: 简单axios封装,后续按实际处理
*/
// import axios from 'axios'
import axios from 'axios-miniprogram';
import Taro from '@tarojs/taro'
// import { strExist } from './tools'
// import qs from 'Qs'
import { routerStore } from '@/stores/router'
// // import qs from 'Qs'
import { navigateToAuth } from '@/utils/authRedirect'
// import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress';
// import store from '@/store'
......@@ -159,9 +157,7 @@ service.interceptors.response.use(
* 授权完成后 返回当前页面
*/
setTimeout(() => {
Taro.navigateTo({
url: '/pages/auth/index?url=' + routerStore().url
});
navigateToAuth();
}, 1000);
}
return response
......