diff --git a/src/app.ts b/src/app.ts index d773185..a048510 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,11 +1,8 @@ import { Component, ReactNode } from 'react' -import Taro from '@tarojs/taro'; import './nutui-theme.scss' import './app.scss' import { useDictionaryStore } from './store/dictionaryStore' import { useGlobalStore } from './store/global' -import { check_login_status } from './services/loginService'; - // import { getNavbarHeight } from "@/utils/getNavbarHeight"; @@ -18,13 +15,6 @@ class App extends Component { onLaunch() { console.log('小程序启动,初始化逻辑写这里') - // 已经登录过就跳转到 列表页 - let is_login = check_login_status() - if (is_login) { - Taro.redirectTo({ - url: '/pages/list/index' - }) - } } componentDidMount() { diff --git a/src/components/Auth/index.tsx b/src/components/Auth/index.tsx new file mode 100644 index 0000000..a78c66b --- /dev/null +++ b/src/components/Auth/index.tsx @@ -0,0 +1,51 @@ +import React, { useEffect, useState } from 'react' +import Taro, { useDidShow } from '@tarojs/taro' +import { View } from '@tarojs/components' +import { check_login_status } from '@/services/loginService' + + + +export function getCurrentFullPath(): string { + const pages = Taro.getCurrentPages() + const currentPage = pages.at(-1) + + if (currentPage) { + const route = currentPage.route + const options = currentPage.options || {} + + const query = Object.keys(options) + .map(key => `${key}=${options[key]}`) + .join('&') + + return query ? `/${route}?${query}` : `/${route}` + } + return '' +} + +export default function withAuth

(WrappedComponent: React.ComponentType

) { + const ComponentWithAuth: React.FC

= (props: P) => { + const [authed, setAuthed] = useState(false) + + useEffect(() => { + const is_login = check_login_status() + setAuthed(is_login) + + if (!is_login) { + const currentPage = getCurrentFullPath() + Taro.redirectTo({ + url: `/pages/login/index/index${ + currentPage ? `?redirect=${currentPage}` : '' + }`, + }) + } + }, []) + + if (!authed) { + return // 空壳,避免 children 渲染出错 + } + + return + } + + return ComponentWithAuth +} \ No newline at end of file diff --git a/src/components/index.ts b/src/components/index.ts index 7b22a80..141a971 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -15,15 +15,16 @@ import CommonDialog from './CommonDialog' import PublishMenu from './PublishMenu/PublishMenu' import UploadCover from './UploadCover' import EditModal from './EditModal/index' +import withAuth from './Auth' - export { - ActivityTypeSwitch, - TextareaTag, + export { + ActivityTypeSwitch, + TextareaTag, FormSwitch, - ImageUpload, - Range, - NumberInterval, - TimeSelector, + ImageUpload, + Range, + NumberInterval, + TimeSelector, TitleTextarea, CommonPopup, DateTimePicker, @@ -33,6 +34,7 @@ import EditModal from './EditModal/index' CommonDialog, PublishMenu, UploadCover, - EditModal + EditModal, + withAuth, } diff --git a/src/pages/detail/index.tsx b/src/pages/detail/index.tsx index 4137f70..7746b91 100644 --- a/src/pages/detail/index.tsx +++ b/src/pages/detail/index.tsx @@ -1,30 +1,24 @@ -import React, { useState, useEffect, useRef, useImperativeHandle, forwardRef } from 'react' -import { View, Text, Button, Swiper, SwiperItem, Image, Map, ScrollView } from '@tarojs/components' -import { Cell, Avatar, Progress, Popover } from '@nutui/nutui-react-taro' +import React, { useState, useRef, useImperativeHandle, forwardRef } from 'react' +import { View, Text, Image, Map, ScrollView } from '@tarojs/components' +import { Avatar, Popover } from '@nutui/nutui-react-taro' import Taro, { useRouter, useShareAppMessage, useShareTimeline, useDidShow } from '@tarojs/taro' -import dayjs, { locale } from 'dayjs' +import dayjs from 'dayjs' import 'dayjs/locale/zh-cn' // 导入API服务 -import DetailService, { MATCH_STATUS} from '../../services/detailService' -import { updateUserProfile, get_user_info } from '../../services/loginService' -import { getCurrentLocation, calculateDistance } from '../../utils/locationUtils' +import { CommonPopup, withAuth } from '@/components' +import DetailService, { MATCH_STATUS} from '@/services/detailService' +import { getCurrentLocation, calculateDistance } from '@/utils/locationUtils' import { useUserInfo, useUserActions, -} from '../../store/userStore' -import img from '../../config/images' -import { getTextColorOnImage } from '../../utils' +} from '@/store/userStore' +import img from '@/config/images' +// import { getTextColorOnImage } from '../../utils' import './index.scss' -import { CommonPopup } from '@/components' dayjs.locale('zh-cn') -const images = [ - 'http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/1a35ebbf-2361-44da-b338-7608561d0b31.png', - 'http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/cf5a82ba-90af-4138-a1b3-9119adcde9e0.png', - 'http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/49d7cdf0-b03c-4a0f-91c6-e7778080cfcd.png' -] - +// 将·作为连接符插入到标签文本之间 function insertDotInTags(tags: string[]) { return tags.join('-·-').split('-') } @@ -39,35 +33,35 @@ const SharePopup = forwardRef(({ id, from }: { id: string, from: string }, ref) } })) - function handleShareToWechat() { - useShareAppMessage(() => { - return { - title: '分享', - path: `/pages/detail/index?id=${id}&from=${from}`, - } - }) - } + // function handleShareToWechat() { + // useShareAppMessage(() => { + // return { + // title: '分享', + // path: `/pages/detail/index?id=${id}&from=share`, + // } + // }) + // } - function handleShareToWechatMoments() { - useShareTimeline(() => { - return { - title: '分享', - path: `/pages/detail/index?id=${id}&from=${from}`, - } - }) - } + // function handleShareToWechatMoments() { + // useShareTimeline(() => { + // return { + // title: '分享', + // path: `/pages/detail/index?id=${id}&from=share`, + // } + // }) + // } - function handleSaveToLocal() { - Taro.saveImageToPhotosAlbum({ - filePath: images[0], - success: () => { - Taro.showToast({ title: '保存成功', icon: 'success' }) - }, - fail: () => { - Taro.showToast({ title: '保存失败', icon: 'none' }) - }, - }) - } + // function handleSaveToLocal() { + // Taro.saveImageToPhotosAlbum({ + // filePath: images[0], + // success: () => { + // Taro.showToast({ title: '保存成功', icon: 'success' }) + // }, + // fail: () => { + // Taro.showToast({ title: '保存失败', icon: 'none' }) + // }, + // }) + // } return ( - - - 分享到微信 - - - - 分享朋友圈 - - - - 保存到本地 - + 分享卡片 ) @@ -398,7 +381,7 @@ function Index() { - + {/* swiper */} @@ -649,4 +632,4 @@ function Index() { ) } -export default Index \ No newline at end of file +export default withAuth(Index) \ No newline at end of file diff --git a/src/pages/list/index.tsx b/src/pages/list/index.tsx index b76c3f0..4d33499 100644 --- a/src/pages/list/index.tsx +++ b/src/pages/list/index.tsx @@ -13,6 +13,7 @@ import InputCustomerBar from "@/container/inputCustomerNavbar"; import GuideBar from "@/components/GuideBar"; import ListContainer from "@/container/listContainer"; import DistanceQuickFilter from "@/components/DistanceQuickFilter"; +import { withAuth } from "@/components"; import img from "@/config/images"; const ListPage = () => { @@ -193,4 +194,4 @@ const ListPage = () => { ); }; -export default ListPage; +export default withAuth(ListPage); diff --git a/src/pages/login/index/index.tsx b/src/pages/login/index/index.tsx index 528932e..d00dbf0 100644 --- a/src/pages/login/index/index.tsx +++ b/src/pages/login/index/index.tsx @@ -1,6 +1,6 @@ import React, { useState } from 'react'; import { View, Text, Button, Image } from '@tarojs/components'; -import Taro from '@tarojs/taro'; +import Taro, { useRouter } from '@tarojs/taro'; import { wechat_auth_login, save_login_state, check_login_status } from '../../../services/loginService'; import './index.scss'; @@ -8,9 +8,11 @@ const LoginPage: React.FC = () => { const [is_loading, set_is_loading] = useState(false); const [agree_terms, set_agree_terms] = useState(false); const [show_terms_layer, set_show_terms_layer] = useState(false); + const { params: { redirect } } = useRouter(); + + - // 微信授权登录 const handle_wechat_login = async (e: any) => { @@ -42,7 +44,11 @@ const LoginPage: React.FC = () => { save_login_state(response.token!, response.user_info!); setTimeout(() => { - Taro.redirectTo({ url: '/pages/list/index' }); + if (redirect) { + Taro.redirectTo({ url: decodeURIComponent(redirect) }); + } else { + Taro.redirectTo({ url: '/pages/list/index' }); + } }, 200); } else { Taro.showToast({ @@ -76,7 +82,7 @@ const LoginPage: React.FC = () => { // 跳转到验证码页面 Taro.navigateTo({ - url: '/pages/login/verification/index' + url: `/pages/login/verification/index?redirect=${redirect}` }); }; diff --git a/src/pages/login/verification/index.tsx b/src/pages/login/verification/index.tsx index 3d977ea..b78e6a0 100644 --- a/src/pages/login/verification/index.tsx +++ b/src/pages/login/verification/index.tsx @@ -1,6 +1,6 @@ import React, { useState, useEffect } from 'react'; import { View, Text, Input, Button, Image } from '@tarojs/components'; -import Taro from '@tarojs/taro'; +import Taro, { useRouter } from '@tarojs/taro'; import { phone_auth_login, send_sms_code } from '../../../services/loginService'; import './index.scss'; @@ -12,6 +12,8 @@ const VerificationPage: React.FC = () => { const [is_loading, setIsLoading] = useState(false); const [code_input_focus, setCodeInputFocus] = useState(false); + const { params: { redirect } } = useRouter(); + // 计算登录按钮是否应该启用 const can_login = phone.length === 11 && verification_code.length === 6 && !is_loading; @@ -123,9 +125,13 @@ const VerificationPage: React.FC = () => { if (result.success) { setTimeout(() => { - Taro.redirectTo({ - url: '/pages/list/index' - }); + if (redirect) { + Taro.redirectTo({ url: decodeURIComponent(redirect) }); + } else { + Taro.redirectTo({ + url: '/pages/list/index' + }); + } }, 200); } else { Taro.showToast({ diff --git a/src/pages/message/index.tsx b/src/pages/message/index.tsx index 18f4130..5040708 100644 --- a/src/pages/message/index.tsx +++ b/src/pages/message/index.tsx @@ -3,10 +3,11 @@ import { View, Text, ScrollView } from '@tarojs/components' import { Avatar } from '@nutui/nutui-react-taro' import Taro from '@tarojs/taro' import GuideBar from '@/components/GuideBar' +import { withAuth } from '@/components' // import img from '@/config/images' import './index.scss' -const Personal = () => { +const Message = () => { const messageList = Array(10).fill(0).map((_, index) => ({ id: index + 1, title: `消息${index + 1}消息${index + 1}消息${index + 1}消息${index + 1}`, @@ -40,4 +41,4 @@ const Personal = () => { ) } -export default Personal \ No newline at end of file +export default withAuth(Message) \ No newline at end of file diff --git a/src/pages/orderCheck/index.tsx b/src/pages/orderCheck/index.tsx index 5f3c231..94c8903 100644 --- a/src/pages/orderCheck/index.tsx +++ b/src/pages/orderCheck/index.tsx @@ -4,6 +4,7 @@ import Taro, { useDidShow, useRouter } from '@tarojs/taro' import { delay } from '@/utils' import orderService from '@/services/orderService' import detailService, { GameDetail } from '@/services/detailService' +import { withAuth } from '@/components' const OrderCheck = () => { const { params } = useRouter() @@ -67,4 +68,4 @@ const OrderCheck = () => { ) } -export default OrderCheck \ No newline at end of file +export default withAuth(OrderCheck) \ No newline at end of file diff --git a/src/pages/publishBall/index.tsx b/src/pages/publishBall/index.tsx index f4c2bd8..2cb9fbf 100644 --- a/src/pages/publishBall/index.tsx +++ b/src/pages/publishBall/index.tsx @@ -4,6 +4,7 @@ import { Checkbox } from '@nutui/nutui-react-taro' import Taro from '@tarojs/taro' import { type ActivityType } from '../../components/ActivityTypeSwitch' import CommonDialog from '../../components/CommonDialog' +import { withAuth } from '@/components' import PublishForm from './publishForm' import { FormFieldConfig, publishBallFormSchema } from '../../config/formSchema/publishBallFormSchema'; import { PublishBallFormData } from '../../../types/publishBall'; @@ -87,13 +88,13 @@ const PublishBall: React.FC = () => { // 检查相邻两组数据是否相同 const checkAdjacentDataSame = (formDataArray: PublishBallFormData[]) => { if (formDataArray.length < 2) return false - + const lastIndex = formDataArray.length - 1 const secondLastIndex = formDataArray.length - 2 - + const lastData = formDataArray[lastIndex] const secondLastData = formDataArray[secondLastIndex] - + // 比较关键字段是否相同 return (JSON.stringify(lastData) === JSON.stringify(secondLastData)) } @@ -117,7 +118,7 @@ const PublishBall: React.FC = () => { } }]) } - + // 复制上一场数据 const handleCopyPrevious = (index: number) => { @@ -372,7 +373,7 @@ const PublishBall: React.FC = () => { {/* 活动类型切换 */} - {/* */} @@ -452,7 +453,7 @@ const PublishBall: React.FC = () => { { activityType === 'group' && ( - { ) } -export default PublishBall +export default withAuth(PublishBall) diff --git a/src/pages/search/index.tsx b/src/pages/search/index.tsx index 66b4299..60e98e1 100644 --- a/src/pages/search/index.tsx +++ b/src/pages/search/index.tsx @@ -4,6 +4,7 @@ import { Input } from "@nutui/nutui-react-taro"; import { useEffect, useMemo, useRef } from "react"; import { useListState } from "@/store/listStore"; import img from "@/config/images"; +import { withAuth } from "@/components"; import "./index.scss"; import Taro from "@tarojs/taro"; @@ -197,4 +198,4 @@ const ListSearch = () => { ); }; -export default ListSearch; +export default withAuth(ListSearch); diff --git a/src/pages/searchResult/index.tsx b/src/pages/searchResult/index.tsx index cc47c16..410c23f 100644 --- a/src/pages/searchResult/index.tsx +++ b/src/pages/searchResult/index.tsx @@ -5,6 +5,7 @@ import ListContainer from "@/container/listContainer"; import "./index.scss"; import DistanceQuickFilter from "@/components/DistanceQuickFilter"; +import { withAuth } from "@/components"; import { useEffect } from "react"; const SearchResult = () => { @@ -68,4 +69,4 @@ const SearchResult = () => { ); }; -export default SearchResult; \ No newline at end of file +export default withAuth(SearchResult); \ No newline at end of file diff --git a/src/pages/userInfo/myself/index.tsx b/src/pages/userInfo/myself/index.tsx index 83cd111..77380ea 100644 --- a/src/pages/userInfo/myself/index.tsx +++ b/src/pages/userInfo/myself/index.tsx @@ -7,6 +7,7 @@ import { UserInfoCard, UserInfo } from '@/components/UserInfo/index' import { UserService } from '@/services/userService' import ListContainer from '@/container/listContainer' import { TennisMatch } from '../../../../types/list/types' +import { withAuth } from '@/components'; const MyselfPage: React.FC = () => { // 获取页面参数 @@ -157,7 +158,7 @@ const MyselfPage: React.FC = () => { is_current_user={is_current_user} is_following={is_following} on_follow={handle_follow} - + /> )} {/* 球局订单和收藏功能 */} @@ -212,4 +213,4 @@ const MyselfPage: React.FC = () => { ); }; -export default MyselfPage; +export default withAuth(MyselfPage);