From c30851cdaca79665c6d1a07a9ef41c8a810b7f5d Mon Sep 17 00:00:00 2001 From: king <2229249788@qq.com> Date: Tue, 1 Aug 2023 14:06:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=9D=83=E9=99=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- .env.playedu | 1 + .idea/deployment.xml | 35 +++++++++++++ .idea/webServers.xml | 14 ++++++ package.json | 2 + src/api/request.ts | 39 +++++---------- src/app.config.ts | 2 +- src/app.tsx | 12 ++--- .../showModel/showModel.module.scss | 4 ++ src/pages/check/check.tsx | 8 +-- src/pages/index/components/videoList.tsx | 21 +++----- src/pages/index/index.tsx | 22 +++----- src/pages/login/login.tsx | 21 ++++---- src/store/profile.ts | 50 +++++++++++++++++-- 14 files changed, 152 insertions(+), 81 deletions(-) create mode 100644 .env.playedu create mode 100644 .idea/deployment.xml create mode 100644 .idea/webServers.xml diff --git a/.env b/.env index 9acc359..4a6ee80 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ TARO_APP_API=https://yjx.dev.yaojiankang.top -#TARO_APP_API=https://playedu.yaojiankang.top +TARO_APP_LGOIN=true diff --git a/.env.playedu b/.env.playedu new file mode 100644 index 0000000..b5cf466 --- /dev/null +++ b/.env.playedu @@ -0,0 +1 @@ +TARO_APP_API=https://playedu.yaojiankang.top diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..0ae59ca --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,35 @@ + + + + + \ No newline at end of file diff --git a/.idea/webServers.xml b/.idea/webServers.xml new file mode 100644 index 0000000..765706e --- /dev/null +++ b/.idea/webServers.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/package.json b/package.json index f67cc0b..2c24713 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "build:alipay": "taro build --type alipay", "build:tt": "taro build --type tt", "build:h5": "taro build --type h5", + "build:h5Playedu": "taro build --mode playedu --type h5 ", "build:rn": "taro build --type rn", "build:qq": "taro build --type qq", "build:jd": "taro build --type jd", @@ -23,6 +24,7 @@ "dev:alipay": "npm run build:alipay -- --watch", "dev:tt": "npm run build:tt -- --watch", "dev:h5": "npm run build:h5 -- --watch", + "dev:h5Playedu": "npm run build:h5 --mode playedu -- --watch ", "dev:rn": "npm run build:rn -- --watch", "dev:qq": "npm run build:qq -- --watch", "dev:jd": "npm run build:jd -- --watch", diff --git a/src/api/request.ts b/src/api/request.ts index 871524a..fcdf070 100644 --- a/src/api/request.ts +++ b/src/api/request.ts @@ -44,12 +44,6 @@ export const ERROR_STATUS: Record = { 'OVERSTEP': '请求越界~' } -// const whitelist = [ -// '/api/v1/auth/login/code', -// '/api/v1/auth/login/checkout', -// '/api/v1/auth/login/wechat' -// ] - export function request( url: string, method: keyof Method, @@ -64,21 +58,12 @@ export function request( 'Content-Type': 'application/json;charset=UTF-8', } } + const token = JSON.parse(Taro.getStorageSync('profile') || '{}')?.token if (token) { option.header ??= {} option.header['Authorization'] = `Bearer ${token}` } - // else { - /** 登录页面白名单 */ - // if (Taro.getCurrentInstance().router?.path === '/pages/login/login') { - // if (!whitelist.includes(url)) { - // return new Promise((_, reject) => reject()) - // } - // } else { - // return new Promise((_, reject) => reject()) - // } - // } if (method === 'GET' && data) { let parameter = '' Object.entries(data).forEach(([key, value], index) => { @@ -88,7 +73,6 @@ export function request( } data && (option.data = data) return new Promise((resolve, reject) => { - Taro.request({ ...option, success(res) { @@ -97,16 +81,19 @@ export function request( if (data?.code === 0 && res.statusCode === 200) { resolve(data.data || []) } else if (res.statusCode === 401) { - // Taro.showModal({ - // title: "登录过期,需重新登陆", - // showCancel: false, - // success({confirm}) { - // confirm && Taro.reLaunch({url: '/pages/login/login'}) - // } - // }) + Taro.showModal({ + title: "登录过期,需重新登陆", + showCancel: false, + success({confirm}) { + confirm && Taro.reLaunch({url: '/pages/login/login'}) + } + }) } else { reject(null) - Taro.showToast({title: data.msg || data.message || ERROR_STATUS[res.statusCode] || '请求错误~', icon: 'error'}) + Taro.showToast({ + title: data.msg || data.message || ERROR_STATUS[res.statusCode] || '请求错误~', + icon: 'error' + }) } } catch (e) { reject(null) @@ -115,7 +102,7 @@ export function request( fail(err) { const errMsg = err?.errMsg console.log(err) - Taro.showToast({title: ERROR_STATUS[errMsg] || ERROR_STATUS['DEFAULT'], icon: 'error'}) + Taro.showToast({title: ERROR_STATUS[errMsg] || ERROR_STATUS['DEFAULT'], icon: 'error'}) reject(null) } }) diff --git a/src/app.config.ts b/src/app.config.ts index 0837c61..206d523 100644 --- a/src/app.config.ts +++ b/src/app.config.ts @@ -1,8 +1,8 @@ export default defineAppConfig({ pages: [ + 'pages/index/index', 'pages/login/login', 'pages/check/check', - 'pages/index/index', 'pages/my/my' ], window: { diff --git a/src/app.tsx b/src/app.tsx index e80fe80..ff9176d 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -2,6 +2,8 @@ import Taro, {useDidShow, useDidHide} from '@tarojs/taro' import './app.scss' import {CustomWrapper} from "@tarojs/components"; import unique_ident from "@/hooks/unique_ident"; +import {Profile} from '@/store' + function updateApp() { if (Taro.canIUse('getUpdateManager.onCheckForUpdate')) { @@ -10,7 +12,6 @@ function updateApp() { console.log('新版本', res.hasUpdate) }) - updateManager.onUpdateReady(() => { Taro.showModal({ title: '更新提示', @@ -35,15 +36,10 @@ function App(props) { Taro.useLaunch(() => { updateApp() - // const token = JSON.parse(Taro.getStorageSync('profile') || '{}')?.token - // if (!token) { - // Taro.reLaunch({url: '/pages/login/login'}) - // } unique_ident.put() unique_ident.del() }) - Taro.getSystemInfo({ success(res) { Taro.getApp().globalData = { @@ -67,7 +63,9 @@ function App(props) { return ( - {props.children} + + {props.children} + ) } diff --git a/src/components/showModel/showModel.module.scss b/src/components/showModel/showModel.module.scss index 6ce34f9..b535516 100644 --- a/src/components/showModel/showModel.module.scss +++ b/src/components/showModel/showModel.module.scss @@ -4,4 +4,8 @@ padding: 20px 0; width: 100%; border-top: 1px solid #eee; + + & > * { + background: red; + } } diff --git a/src/pages/check/check.tsx b/src/pages/check/check.tsx index 82ecdf0..e431e75 100644 --- a/src/pages/check/check.tsx +++ b/src/pages/check/check.tsx @@ -2,7 +2,7 @@ import {FC, useRef, useState} from "react"; import {Button, Form, Image, Input, View} from "@tarojs/components"; import {Profile} from "@/store"; import {userApi} from "@/api"; -import Taro, {useRouter} from "@tarojs/taro"; +import Taro from "@tarojs/taro"; import {regexTel} from "@/utils/regu"; import styles from './check.module.scss' @@ -19,7 +19,7 @@ const Bing: FC = () => { const [loading, setLoading] = useState(false) const {setUser, setToken, setCompany} = Profile.useContainer() const [captchaKey, setCaptchaKey] = useState(uuid()) - const router = useRouter() + const openid = localStorage.getItem('openid') async function Submit(data) { setLoading(true) @@ -33,7 +33,7 @@ const Bing: FC = () => { const res = await userApi.checkout({ ...value, catch_key: captchaKey, - openid: router.params.openid, + openid, }) if (res) { setCompany(res.company) @@ -52,7 +52,7 @@ const Bing: FC = () => {
手机号 - + diff --git a/src/pages/index/components/videoList.tsx b/src/pages/index/components/videoList.tsx index 334541b..27b0ecf 100644 --- a/src/pages/index/components/videoList.tsx +++ b/src/pages/index/components/videoList.tsx @@ -1,5 +1,5 @@ import {FC, useEffect, useState} from "react"; -import Taro, {useReachBottom} from "@tarojs/taro"; +import {useReachBottom} from "@tarojs/taro"; import {View} from "@tarojs/components"; import {Courses, CoursesKey, publicApi} from "@/api/public"; import VideoCover from "@/components/videoCover/videoCover"; @@ -10,10 +10,10 @@ import Empty from "@/components/empty/empty"; interface Props { categoryKey: CoursesKey + ready: boolean } -export const VideoList: FC = ({categoryKey}) => { - const [init, setInit] = useState(false) +export const VideoList: FC = ({categoryKey, ready}) => { const [data, setData] = useState({ is_finished: [], is_required: [], @@ -68,17 +68,12 @@ export const VideoList: FC = ({categoryKey}) => { // getRecords() // }) - Taro.useDidShow(() => { - if (init) { - getData() - } - getRecords() - }) - useEffect(() => { - setInit(true) - getData() - }, [page]) + if (ready) { + getData().then() + getRecords().then() + } + }, [page, ready]) return ( {data?.[categoryKey]?.length ? data?.[categoryKey]?.map(c => diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index db1026b..de99a57 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -1,17 +1,17 @@ -import {FC, useEffect, useState} from "react"; +import {FC, useState} from "react"; import {View} from "@tarojs/components"; import styles from './index.module.scss' -import {Profile} from '@/store' import {VideoList} from "@/pages/index/components/videoList"; import Tabs, {OnChangOpt, TabList} from "@/components/tabs/tabs"; import {CoursesKey} from "@/api/public"; -import Taro, {useRouter} from '@tarojs/taro' +import Taro from '@tarojs/taro' +import {Profile} from '@/store' + // import {Search} from "@/pages/index/components/search"; const Index: FC = () => { const globalData = Taro.getApp().globalData - const router = useRouter(); const category: TabList[] = [ {title: "必修", value: 'is_required'}, {title: "选修", value: 'is_not_required'}, @@ -19,28 +19,22 @@ const Index: FC = () => { {title: "未完成", value: 'is_not_finished'}, ] const [categoryKey, setCategoryKey] = useState('is_required') + const {token} = Profile.useContainer() function tabChange(data: OnChangOpt) { setCategoryKey(data.tab?.value as CoursesKey) } - useEffect(() => { - if (router.params.d) { - Taro.setStorageSync("profile", decodeURIComponent(router.params.d)) - Taro.reLaunch({url:"/pages/index/index"}) - } - }, []) - return ( - + <> {process.env.TARO_ENV !== "h5" && 医学道} {/**/} - + - + ) } diff --git a/src/pages/login/login.tsx b/src/pages/login/login.tsx index 4b1a58e..88f4f2e 100644 --- a/src/pages/login/login.tsx +++ b/src/pages/login/login.tsx @@ -103,7 +103,6 @@ const Login: FC = () => { const params = Taro.getCurrentInstance()?.router?.params as unknown as { data: LoginData } useEffect(() => { - console.log(params) if (params?.data) { if (!params.data?.code) { setUser(params.data.user) @@ -175,6 +174,13 @@ const Login: FC = () => { } } + async function TESTLOGIN() { + const res = await loginApi.testLogin() + localStorage.setItem('profile', JSON.stringify({data:res})) + Taro.switchTab({url: '/pages/index/index'}) + } + + return ( setCode(null)}> @@ -187,7 +193,7 @@ const Login: FC = () => { 请完成微信授权以继续使用! - + {isLoading ? : null} @@ -201,16 +207,11 @@ const Login: FC = () => { + + {process.env.TARO_APP_LGOIN && 线下登录} ) } -const Index: FC = () => { - return ( - - - - ) -} -export default Index +export default Login diff --git a/src/store/profile.ts b/src/store/profile.ts index 2812762..817fa8a 100644 --- a/src/store/profile.ts +++ b/src/store/profile.ts @@ -3,12 +3,52 @@ import {createContainer} from "unstated-next"; import Taro from "@tarojs/taro"; +function DataKey(data: any) { + return !Array.isArray(data) + && typeof data === 'object' + && 'data' in data + && 'user' in data.data + && 'token' in data.data + && 'company' in data.data +} + +const whitelist = [ + '/pages/check/check', + '/pages/login/login', +] + + function useProfile() { let data - try { - data = JSON.parse(Taro.getStorageSync('profile') || '{}') - } catch (e) { - console.error(e) + + if (process.env.TARO_ENV === 'h5') { + try { + const profileCopy = JSON.parse(localStorage.getItem("profileCopy") || '{}') + if (DataKey(profileCopy)) { + data = profileCopy.data || null + localStorage.removeItem("profileCopy") + Taro.switchTab({url: '/pages/index/index'}) + } + } catch (e) { + console.error(e) + } + } + + if (data == null) { + try { + const cacheData = {data: JSON.parse(Taro.getStorageSync('profile') || '{}')} + if (DataKey(cacheData)) { + data = cacheData.data + } + } catch (e) { + console.error(e) + } + } + + if (data?.token == null) { + if (!whitelist.includes(Taro.getCurrentInstance().router?.path?.split('?')?.[0] || '')) { + Taro.reLaunch({url: '/pages/login/login'}) + } } const [role, setRole] = useState(data?.role || null) @@ -22,7 +62,7 @@ function useProfile() { setUser(null) setToken(null) Taro.removeStorageSync('profile') - // Taro.reLaunch({url: '/pages/login/login'}) + Taro.reLaunch({url: '/pages/login/login'}) } useEffect(() => {