feat: auth
This commit is contained in:
10
src/app.ts
10
src/app.ts
@@ -1,11 +1,8 @@
|
|||||||
import { Component, ReactNode } from 'react'
|
import { Component, ReactNode } from 'react'
|
||||||
import Taro from '@tarojs/taro';
|
|
||||||
import './nutui-theme.scss'
|
import './nutui-theme.scss'
|
||||||
import './app.scss'
|
import './app.scss'
|
||||||
import { useDictionaryStore } from './store/dictionaryStore'
|
import { useDictionaryStore } from './store/dictionaryStore'
|
||||||
import { useGlobalStore } from './store/global'
|
import { useGlobalStore } from './store/global'
|
||||||
import { check_login_status } from './services/loginService';
|
|
||||||
|
|
||||||
|
|
||||||
// import { getNavbarHeight } from "@/utils/getNavbarHeight";
|
// import { getNavbarHeight } from "@/utils/getNavbarHeight";
|
||||||
|
|
||||||
@@ -18,13 +15,6 @@ class App extends Component<AppProps> {
|
|||||||
|
|
||||||
onLaunch() {
|
onLaunch() {
|
||||||
console.log('小程序启动,初始化逻辑写这里')
|
console.log('小程序启动,初始化逻辑写这里')
|
||||||
// 已经登录过就跳转到 列表页
|
|
||||||
let is_login = check_login_status()
|
|
||||||
if (is_login) {
|
|
||||||
Taro.redirectTo({
|
|
||||||
url: '/pages/list/index'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
|||||||
51
src/components/Auth/index.tsx
Normal file
51
src/components/Auth/index.tsx
Normal file
@@ -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<P extends object>(WrappedComponent: React.ComponentType<P>) {
|
||||||
|
const ComponentWithAuth: React.FC<P> = (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 <View /> // 空壳,避免 children 渲染出错
|
||||||
|
}
|
||||||
|
|
||||||
|
return <WrappedComponent {...props} />
|
||||||
|
}
|
||||||
|
|
||||||
|
return ComponentWithAuth
|
||||||
|
}
|
||||||
@@ -15,6 +15,7 @@ import CommonDialog from './CommonDialog'
|
|||||||
import PublishMenu from './PublishMenu/PublishMenu'
|
import PublishMenu from './PublishMenu/PublishMenu'
|
||||||
import UploadCover from './UploadCover'
|
import UploadCover from './UploadCover'
|
||||||
import EditModal from './EditModal/index'
|
import EditModal from './EditModal/index'
|
||||||
|
import withAuth from './Auth'
|
||||||
|
|
||||||
export {
|
export {
|
||||||
ActivityTypeSwitch,
|
ActivityTypeSwitch,
|
||||||
@@ -33,6 +34,7 @@ import EditModal from './EditModal/index'
|
|||||||
CommonDialog,
|
CommonDialog,
|
||||||
PublishMenu,
|
PublishMenu,
|
||||||
UploadCover,
|
UploadCover,
|
||||||
EditModal
|
EditModal,
|
||||||
|
withAuth,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,24 @@
|
|||||||
import React, { useState, useEffect, useRef, useImperativeHandle, forwardRef } from 'react'
|
import React, { useState, useRef, useImperativeHandle, forwardRef } from 'react'
|
||||||
import { View, Text, Button, Swiper, SwiperItem, Image, Map, ScrollView } from '@tarojs/components'
|
import { View, Text, Image, Map, ScrollView } from '@tarojs/components'
|
||||||
import { Cell, Avatar, Progress, Popover } from '@nutui/nutui-react-taro'
|
import { Avatar, Popover } from '@nutui/nutui-react-taro'
|
||||||
import Taro, { useRouter, useShareAppMessage, useShareTimeline, useDidShow } from '@tarojs/taro'
|
import Taro, { useRouter, useShareAppMessage, useShareTimeline, useDidShow } from '@tarojs/taro'
|
||||||
import dayjs, { locale } from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import 'dayjs/locale/zh-cn'
|
import 'dayjs/locale/zh-cn'
|
||||||
// 导入API服务
|
// 导入API服务
|
||||||
import DetailService, { MATCH_STATUS} from '../../services/detailService'
|
import { CommonPopup, withAuth } from '@/components'
|
||||||
import { updateUserProfile, get_user_info } from '../../services/loginService'
|
import DetailService, { MATCH_STATUS} from '@/services/detailService'
|
||||||
import { getCurrentLocation, calculateDistance } from '../../utils/locationUtils'
|
import { getCurrentLocation, calculateDistance } from '@/utils/locationUtils'
|
||||||
import {
|
import {
|
||||||
useUserInfo,
|
useUserInfo,
|
||||||
useUserActions,
|
useUserActions,
|
||||||
} from '../../store/userStore'
|
} from '@/store/userStore'
|
||||||
import img from '../../config/images'
|
import img from '@/config/images'
|
||||||
import { getTextColorOnImage } from '../../utils'
|
// import { getTextColorOnImage } from '../../utils'
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
import { CommonPopup } from '@/components'
|
|
||||||
|
|
||||||
dayjs.locale('zh-cn')
|
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[]) {
|
function insertDotInTags(tags: string[]) {
|
||||||
return tags.join('-·-').split('-')
|
return tags.join('-·-').split('-')
|
||||||
}
|
}
|
||||||
@@ -39,35 +33,35 @@ const SharePopup = forwardRef(({ id, from }: { id: string, from: string }, ref)
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
function handleShareToWechat() {
|
// function handleShareToWechat() {
|
||||||
useShareAppMessage(() => {
|
// useShareAppMessage(() => {
|
||||||
return {
|
// return {
|
||||||
title: '分享',
|
// title: '分享',
|
||||||
path: `/pages/detail/index?id=${id}&from=${from}`,
|
// path: `/pages/detail/index?id=${id}&from=share`,
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
function handleShareToWechatMoments() {
|
// function handleShareToWechatMoments() {
|
||||||
useShareTimeline(() => {
|
// useShareTimeline(() => {
|
||||||
return {
|
// return {
|
||||||
title: '分享',
|
// title: '分享',
|
||||||
path: `/pages/detail/index?id=${id}&from=${from}`,
|
// path: `/pages/detail/index?id=${id}&from=share`,
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
function handleSaveToLocal() {
|
// function handleSaveToLocal() {
|
||||||
Taro.saveImageToPhotosAlbum({
|
// Taro.saveImageToPhotosAlbum({
|
||||||
filePath: images[0],
|
// filePath: images[0],
|
||||||
success: () => {
|
// success: () => {
|
||||||
Taro.showToast({ title: '保存成功', icon: 'success' })
|
// Taro.showToast({ title: '保存成功', icon: 'success' })
|
||||||
},
|
// },
|
||||||
fail: () => {
|
// fail: () => {
|
||||||
Taro.showToast({ title: '保存失败', icon: 'none' })
|
// Taro.showToast({ title: '保存失败', icon: 'none' })
|
||||||
},
|
// },
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CommonPopup
|
<CommonPopup
|
||||||
@@ -78,18 +72,7 @@ const SharePopup = forwardRef(({ id, from }: { id: string, from: string }, ref)
|
|||||||
style={{ minHeight: '100px' }}
|
style={{ minHeight: '100px' }}
|
||||||
>
|
>
|
||||||
<View catchMove className='share-popup-content'>
|
<View catchMove className='share-popup-content'>
|
||||||
<View onClick={handleShareToWechat}>
|
分享卡片
|
||||||
<Image src={img.ICON_DETAIL_SHARE} />
|
|
||||||
<Text>分享到微信</Text>
|
|
||||||
</View>
|
|
||||||
<View onClick={handleShareToWechatMoments}>
|
|
||||||
<Image src={img.ICON_DETAIL_SHARE} />
|
|
||||||
<Text>分享朋友圈</Text>
|
|
||||||
</View>
|
|
||||||
<View onClick={handleSaveToLocal}>
|
|
||||||
<Image src={img.ICON_DETAIL_SHARE} />
|
|
||||||
<Text>保存到本地</Text>
|
|
||||||
</View>
|
|
||||||
</View>
|
</View>
|
||||||
</CommonPopup>
|
</CommonPopup>
|
||||||
)
|
)
|
||||||
@@ -398,7 +381,7 @@ function Index() {
|
|||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</view>
|
</view>
|
||||||
<View className='detail-page-bg' style={{ backgroundImage: `url(${images[0]})` }} />
|
<View className='detail-page-bg' style={detail?.image_list?.[0] ? { backgroundImage: `url(${detail?.image_list?.[0]})` } : {}} />
|
||||||
<View className='detail-page-bg-text' />
|
<View className='detail-page-bg-text' />
|
||||||
{/* swiper */}
|
{/* swiper */}
|
||||||
<View className="detail-swiper-container">
|
<View className="detail-swiper-container">
|
||||||
@@ -649,4 +632,4 @@ function Index() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Index
|
export default withAuth(Index)
|
||||||
@@ -13,6 +13,7 @@ import InputCustomerBar from "@/container/inputCustomerNavbar";
|
|||||||
import GuideBar from "@/components/GuideBar";
|
import GuideBar from "@/components/GuideBar";
|
||||||
import ListContainer from "@/container/listContainer";
|
import ListContainer from "@/container/listContainer";
|
||||||
import DistanceQuickFilter from "@/components/DistanceQuickFilter";
|
import DistanceQuickFilter from "@/components/DistanceQuickFilter";
|
||||||
|
import { withAuth } from "@/components";
|
||||||
import img from "@/config/images";
|
import img from "@/config/images";
|
||||||
|
|
||||||
const ListPage = () => {
|
const ListPage = () => {
|
||||||
@@ -193,4 +194,4 @@ const ListPage = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ListPage;
|
export default withAuth(ListPage);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { View, Text, Button, Image } from '@tarojs/components';
|
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 { wechat_auth_login, save_login_state, check_login_status } from '../../../services/loginService';
|
||||||
import './index.scss';
|
import './index.scss';
|
||||||
|
|
||||||
@@ -8,6 +8,8 @@ const LoginPage: React.FC = () => {
|
|||||||
const [is_loading, set_is_loading] = useState(false);
|
const [is_loading, set_is_loading] = useState(false);
|
||||||
const [agree_terms, set_agree_terms] = useState(false);
|
const [agree_terms, set_agree_terms] = useState(false);
|
||||||
const [show_terms_layer, set_show_terms_layer] = useState(false);
|
const [show_terms_layer, set_show_terms_layer] = useState(false);
|
||||||
|
const { params: { redirect } } = useRouter();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -42,7 +44,11 @@ const LoginPage: React.FC = () => {
|
|||||||
save_login_state(response.token!, response.user_info!);
|
save_login_state(response.token!, response.user_info!);
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
if (redirect) {
|
||||||
|
Taro.redirectTo({ url: decodeURIComponent(redirect) });
|
||||||
|
} else {
|
||||||
Taro.redirectTo({ url: '/pages/list/index' });
|
Taro.redirectTo({ url: '/pages/list/index' });
|
||||||
|
}
|
||||||
}, 200);
|
}, 200);
|
||||||
} else {
|
} else {
|
||||||
Taro.showToast({
|
Taro.showToast({
|
||||||
@@ -76,7 +82,7 @@ const LoginPage: React.FC = () => {
|
|||||||
|
|
||||||
// 跳转到验证码页面
|
// 跳转到验证码页面
|
||||||
Taro.navigateTo({
|
Taro.navigateTo({
|
||||||
url: '/pages/login/verification/index'
|
url: `/pages/login/verification/index?redirect=${redirect}`
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { View, Text, Input, Button, Image } from '@tarojs/components';
|
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 { phone_auth_login, send_sms_code } from '../../../services/loginService';
|
||||||
import './index.scss';
|
import './index.scss';
|
||||||
|
|
||||||
@@ -12,6 +12,8 @@ const VerificationPage: React.FC = () => {
|
|||||||
const [is_loading, setIsLoading] = useState(false);
|
const [is_loading, setIsLoading] = useState(false);
|
||||||
const [code_input_focus, setCodeInputFocus] = 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;
|
const can_login = phone.length === 11 && verification_code.length === 6 && !is_loading;
|
||||||
|
|
||||||
@@ -123,9 +125,13 @@ const VerificationPage: React.FC = () => {
|
|||||||
if (result.success) {
|
if (result.success) {
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
if (redirect) {
|
||||||
|
Taro.redirectTo({ url: decodeURIComponent(redirect) });
|
||||||
|
} else {
|
||||||
Taro.redirectTo({
|
Taro.redirectTo({
|
||||||
url: '/pages/list/index'
|
url: '/pages/list/index'
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}, 200);
|
}, 200);
|
||||||
} else {
|
} else {
|
||||||
Taro.showToast({
|
Taro.showToast({
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ import { View, Text, ScrollView } from '@tarojs/components'
|
|||||||
import { Avatar } from '@nutui/nutui-react-taro'
|
import { Avatar } from '@nutui/nutui-react-taro'
|
||||||
import Taro from '@tarojs/taro'
|
import Taro from '@tarojs/taro'
|
||||||
import GuideBar from '@/components/GuideBar'
|
import GuideBar from '@/components/GuideBar'
|
||||||
|
import { withAuth } from '@/components'
|
||||||
// import img from '@/config/images'
|
// import img from '@/config/images'
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
const Personal = () => {
|
const Message = () => {
|
||||||
const messageList = Array(10).fill(0).map((_, index) => ({
|
const messageList = Array(10).fill(0).map((_, index) => ({
|
||||||
id: index + 1,
|
id: index + 1,
|
||||||
title: `消息${index + 1}消息${index + 1}消息${index + 1}消息${index + 1}`,
|
title: `消息${index + 1}消息${index + 1}消息${index + 1}消息${index + 1}`,
|
||||||
@@ -40,4 +41,4 @@ const Personal = () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Personal
|
export default withAuth(Message)
|
||||||
@@ -4,6 +4,7 @@ import Taro, { useDidShow, useRouter } from '@tarojs/taro'
|
|||||||
import { delay } from '@/utils'
|
import { delay } from '@/utils'
|
||||||
import orderService from '@/services/orderService'
|
import orderService from '@/services/orderService'
|
||||||
import detailService, { GameDetail } from '@/services/detailService'
|
import detailService, { GameDetail } from '@/services/detailService'
|
||||||
|
import { withAuth } from '@/components'
|
||||||
|
|
||||||
const OrderCheck = () => {
|
const OrderCheck = () => {
|
||||||
const { params } = useRouter()
|
const { params } = useRouter()
|
||||||
@@ -67,4 +68,4 @@ const OrderCheck = () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default OrderCheck
|
export default withAuth(OrderCheck)
|
||||||
@@ -4,6 +4,7 @@ import { Checkbox } from '@nutui/nutui-react-taro'
|
|||||||
import Taro from '@tarojs/taro'
|
import Taro from '@tarojs/taro'
|
||||||
import { type ActivityType } from '../../components/ActivityTypeSwitch'
|
import { type ActivityType } from '../../components/ActivityTypeSwitch'
|
||||||
import CommonDialog from '../../components/CommonDialog'
|
import CommonDialog from '../../components/CommonDialog'
|
||||||
|
import { withAuth } from '@/components'
|
||||||
import PublishForm from './publishForm'
|
import PublishForm from './publishForm'
|
||||||
import { FormFieldConfig, publishBallFormSchema } from '../../config/formSchema/publishBallFormSchema';
|
import { FormFieldConfig, publishBallFormSchema } from '../../config/formSchema/publishBallFormSchema';
|
||||||
import { PublishBallFormData } from '../../../types/publishBall';
|
import { PublishBallFormData } from '../../../types/publishBall';
|
||||||
@@ -466,4 +467,4 @@ const PublishBall: React.FC = () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PublishBall
|
export default withAuth(PublishBall)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Input } from "@nutui/nutui-react-taro";
|
|||||||
import { useEffect, useMemo, useRef } from "react";
|
import { useEffect, useMemo, useRef } from "react";
|
||||||
import { useListState } from "@/store/listStore";
|
import { useListState } from "@/store/listStore";
|
||||||
import img from "@/config/images";
|
import img from "@/config/images";
|
||||||
|
import { withAuth } from "@/components";
|
||||||
import "./index.scss";
|
import "./index.scss";
|
||||||
import Taro from "@tarojs/taro";
|
import Taro from "@tarojs/taro";
|
||||||
|
|
||||||
@@ -197,4 +198,4 @@ const ListSearch = () => {
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export default ListSearch;
|
export default withAuth(ListSearch);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import ListContainer from "@/container/listContainer";
|
|||||||
|
|
||||||
import "./index.scss";
|
import "./index.scss";
|
||||||
import DistanceQuickFilter from "@/components/DistanceQuickFilter";
|
import DistanceQuickFilter from "@/components/DistanceQuickFilter";
|
||||||
|
import { withAuth } from "@/components";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
const SearchResult = () => {
|
const SearchResult = () => {
|
||||||
@@ -68,4 +69,4 @@ const SearchResult = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SearchResult;
|
export default withAuth(SearchResult);
|
||||||
@@ -7,6 +7,7 @@ import { UserInfoCard, UserInfo } from '@/components/UserInfo/index'
|
|||||||
import { UserService } from '@/services/userService'
|
import { UserService } from '@/services/userService'
|
||||||
import ListContainer from '@/container/listContainer'
|
import ListContainer from '@/container/listContainer'
|
||||||
import { TennisMatch } from '../../../../types/list/types'
|
import { TennisMatch } from '../../../../types/list/types'
|
||||||
|
import { withAuth } from '@/components';
|
||||||
|
|
||||||
const MyselfPage: React.FC = () => {
|
const MyselfPage: React.FC = () => {
|
||||||
// 获取页面参数
|
// 获取页面参数
|
||||||
@@ -212,4 +213,4 @@ const MyselfPage: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default MyselfPage;
|
export default withAuth(MyselfPage);
|
||||||
|
|||||||
Reference in New Issue
Block a user