Skip to content

Commit

Permalink
feat: 增加常用实体 & 调整result和useRequest返回内容
Browse files Browse the repository at this point in the history
  • Loading branch information
HardenSG committed May 12, 2024
1 parent e5eb496 commit 9a7b49d
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 配置要求:必须以TARO_APP_开头否则不可以通过process.env.TARO_APP_XX获得

# 域名
TARO_APP_DOMAIN="https://mock.apifox.com"
TARO_APP_DOMAIN="https://805807.cn:8080"
2 changes: 1 addition & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 配置要求:必须以TARO_APP_开头否则不可以通过process.env.TARO_APP_XX获得

# 域名
TARO_APP_DOMAIN="https://mock.apifox.com"
TARO_APP_DOMAIN="https://805807.cn:8080"
28 changes: 18 additions & 10 deletions src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Provider } from 'react-redux'
import Taro, { useError, useLaunch } from '@tarojs/taro'
import Error from 'src/components/baseBuiltComp/Error'
import { TBizStudentGetInfo } from 'src/utils'
import { store } from './store'
import './app.scss'
import { Get, getStorage, preloadResource, requestUrlCreator } from './utils'
import { Get, getStorage, preloadResource, requestUrlCreator, trackJsError } from './utils'
import { JSON_MAP } from './static/SVG/lottie-map'

function App(props) {
useError((err) => {
console.log('错了', err)
trackJsError({
token: getStorage('userInfo')?.token ?? 'no_login',
errMsg: err.toString(),
location: '全局错误捕捉 ===> ' + err.toString(),
errName: 'global_err_catch',
})
return <Error />
})

Expand All @@ -18,36 +25,37 @@ function App(props) {

/** 2. 校验获取用户信息 */
/** TODO: 需和后端确定响应体 */
let userinfo: any
let userinfo: TBizStudentGetInfo | null = null
if (token) {
try {
const wrap = await Get<{ [key in string]: any }>({
const wrap = await Get<TBizStudentGetInfo>({
url: requestUrlCreator({ absolutePath: '/userInfo' }),
silent: true,
})
userinfo = wrap.isSuccess && wrap.data
if (wrap.isSuccess && wrap.data) {
userinfo = wrap.data
store.dispatch.common.update_UserInfo({
...(userinfo as any) /** TODO: 更新响应体结构 */,
})
}
} catch (err) {
console.log(' === 获取用户信息请求错误 === ', err)
}
}

/** 预加载静态资源 */
preloadResource(JSON_MAP['forbidden'], false)

/** 预加载字体 */
Taro.loadFontFace({
family: 'AlibabaPuHuiTi',
source: 'url("https://hx.404fwf.cn/notifyBoard/AlibabaPuHuiTi.ttf")',
})

// store.dispatch.common.update_UserInfo({ ...userinfo })
// TODO: 需要更新为实际的userinfo参数
store.dispatch.common.update_SystemInfo({
sessionStatus: !token ? 'fail' : 'ok',
coolStartSuccess: true,
hasAccountCompleted: !!(userinfo || false),
coldStartSuccess: true,
hasAccountCompleted: !!(userinfo || false) /** TODO: 是否具备完整信息需要再确认下 */,
})

console.log('==== 应用冷启动初始化成功! ====')
})
return (
Expand Down
7 changes: 3 additions & 4 deletions src/common-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createModel } from '@rematch/core'
import { RootModel } from './models'
import { getStorage, setStorage } from './utils/storage'

/** 用户信息 */
/** TODO: 用户信息需要确定好内容 */
export interface IUserInfo {
token?: string
username?: string
Expand All @@ -21,7 +21,7 @@ export interface ISystemInfo {
/** session状态 */
sessionStatus?: string
/** 冷启动初始化是否完成 */
coolStartSuccess: boolean
coldStartSuccess: boolean
/** 是否完善用户信息 */
hasAccountCompleted?: boolean
/** 是否弹过授权弹窗 */
Expand All @@ -37,7 +37,7 @@ export interface ICommonState {
export const defaultValue: ICommonState = {
userInfo: getStorage('userInfo') || {},
system: {
coolStartSuccess: false,
coldStartSuccess: false,
hasAccountCompleted: false,
isAuthorizeSubscribePop: false,
},
Expand All @@ -53,7 +53,6 @@ export const common = createModel<RootModel>()({
},
update_SystemInfo: (state: ICommonState, newSystemInfo: ISystemInfo) => {
state.system = newSystemInfo
console.log(' === 更新到了', newSystemInfo)
return state
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/baseBuiltComp/HocWrap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Guide from './components/guideInit'
export const Authorize = (Component) => {
return React.memo(() => {
const state = useSelector((root: RootState) => root.common.system)
if (!state.coolStartSuccess) {
if (!state.coldStartSuccess) {
return <Loading isShow />
} else {
if (state.sessionStatus === 'ok' && state.hasAccountCompleted) return <Component />
Expand All @@ -24,7 +24,7 @@ export const Authorize = (Component) => {
export const Forbidden = (Component, props?: any) => {
return React.memo(() => {
const state = useSelector((root: RootState) => root.common)
if (!state.system.coolStartSuccess) {
if (!state.system.coldStartSuccess) {
return <Loading isShow />
} else {
if (state.userInfo.isBindManage) return <Component />
Expand Down
13 changes: 12 additions & 1 deletion src/hooks/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ type TRequestOptions = {
* )
* ```
*/
const useRequest = <T>(requestFn: TRequestFn, options?: TRequestOptions) => {
const useRequest = <T, V extends { [key: string]: any } = any>(
requestFn: TRequestFn,
options?: TRequestOptions,
) => {
const { deps = [], auto = true, complete = () => {}, preFetch = () => {} } = options || {}
const [loading, setLoading] = useState<boolean>(!!auto)
const [data, setData] = useState<T>()
const [extraData, setExtraData] = useState<V>()
const [err, setErr] = useState<any>()
const PerfTackerInstance = usePerfTrack()
const { type, url, reqData, silent } = useMemo(() => requestFn(), [deps, requestFn])
Expand Down Expand Up @@ -78,6 +82,7 @@ const useRequest = <T>(requestFn: TRequestFn, options?: TRequestOptions) => {
/** 校验是否成功 */
if (res.isSuccess) {
setData(res.data)
setExtraData(res.extraData as V)
/** 请求完成 */
PerfTackerInstance.responseReady({ api: url, data: res.data as any })
} else {
Expand All @@ -101,9 +106,15 @@ const useRequest = <T>(requestFn: TRequestFn, options?: TRequestOptions) => {
}, [run])

return {
/** loading状态 */
loading,
/** 响应体 */
data,
/** 响应额外参数 */
extraData,
/** 错误信息 */
err,
/** 运行函数 */
run,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/perfTrack/business.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const trackAPIError = ({
})
}

/** 请求错误埋点 */
/** JS逻辑错误埋点 */
type TTrackJsErrorParams = Pick<TTrackEventParams, 'extraParams'> & {
token: string
errMsg: any
Expand Down
2 changes: 1 addition & 1 deletion src/utils/request/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ResultWrapBase<T> implements Partial<IResultWrapBase<T>> {

/** 获取请求数据 */
get data() {
return this._data
return this._data as T
}

/** 获取请求响应信息 */
Expand Down
2 changes: 1 addition & 1 deletion src/utils/request/business.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const request = <T, U extends string | TGeneralObject>(
...baseExtraConfig,
...params,
header: {
token: getStorage('userInfo').token,
WXToken: getStorage('userInfo')?.token,
...params.header,
},
})
Expand Down
9 changes: 7 additions & 2 deletions src/utils/request/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
* ```
*/
export const BUSINESS_API_ROOT_PATH = {
API: '/m1/4182382-0-default/api',
/** 学生模块 */
WX_STU: '/wx/studentManager/student',
/** 组织 */
WX_ORG: '/wx/origination/complexAll',
/** 订阅消息 */
WX_SUB: '/wx/notificationWork',
}

/**
Expand All @@ -24,7 +29,7 @@ export const BUSINESS_DOMAIN = {
*/
export const enum STATUS_CODE {
/** sucess */
SUCCESS = 200,
SUCCESS = 0,
/** 传参错误 */
FAIL = 400,
/** 没有登陆 */
Expand Down
4 changes: 2 additions & 2 deletions src/utils/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TGeneralObject } from 'src/types'
import Taro from '@tarojs/taro'
import { DefinedEnumKeys } from '../types/enumHelper'
import { DefinedEnumKeys } from '../types/helpers/enumHelper'
import { request } from './business'
import { BUSINESS_API_ROOT_PATH, BUSINESS_DOMAIN } from './constants'
import { TCustomRequestParams, TResponseType, baseRequest } from './request'
Expand Down Expand Up @@ -81,7 +81,7 @@ export const requestUrlCreator = <
},
>({
absolutePath,
rootPath = 'API',
rootPath = 'WX_STU',
domain = 'BUSINESS',
}: T): string => {
return BUSINESS_DOMAIN[domain] + BUSINESS_API_ROOT_PATH[rootPath] + absolutePath
Expand Down
1 change: 1 addition & 0 deletions src/utils/types/business/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 这里封装业务响应类型
1 change: 1 addition & 0 deletions src/utils/types/business/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './student'
8 changes: 8 additions & 0 deletions src/utils/types/business/student.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IStudentStruct } from '../entity'

/** 获取学生信息接口返回类型 */
type TBizStudentGetInfo = {
studentVo: IStudentStruct
}

export { TBizStudentGetInfo }
52 changes: 52 additions & 0 deletions src/utils/types/entity/college-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/** 课程信息 */
interface ICourse {
/** 学院ID */
collegeId: number
/** 课程ID */
id: number
/** 课程名称 */
name: string
/** 教师id */
teacherId: number
}

/** 学院信息 */
interface ICollege {
/** 学院ID */
id: number
/** 学院logo */
logoUrl: string
/** 学院名称 */
name: string
}

/** 年级信息 */
interface IGrade {
/** 学院ID */
collegeId: number
/** 年级ID */
id: number
/** 年级名称 */
name: string
}

/** 年级信息 */
interface IClass {
/** 年级ID */
gradeId: number
/** 班级ID */
id: number
/** 班级名称 */
name: string
}

/** 学生的组织详细信息 */
interface IStudentCollegeInfo {
/** 班级信息 */
classVo?: IClass
/** 年级信息 */
grade?: IGrade
/** 组织信息 */
college?: ICollege
}
export { ICourse, IClass, ICollege, IGrade, IStudentCollegeInfo }
3 changes: 3 additions & 0 deletions src/utils/types/entity/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './task'
export * from './student'
export * from './college-info'
export * from './work'
25 changes: 25 additions & 0 deletions src/utils/types/entity/student.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** 学生基础信息 */
interface IBaseStudentStruct {
/** 学号 */
studentId?: string
/** openid */
wechatId?: string
/** 学生姓名 */
nickname?: string
/** 班级ID */
classId?: number
/** 学院ID */
collegeId: number
/** 年级ID */
gradeId?: number
}

/** 学生实体 */
interface IStudentStruct extends IBaseStudentStruct {
/** 手机号 */
phone?: string
/** 头像 */
avatarUrl: string
}

export { IBaseStudentStruct, IStudentStruct }
53 changes: 53 additions & 0 deletions src/utils/types/entity/work.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/** 任务基础信息 */
interface IBaseWork {
/** 任务ID */
id: number
/** 创建时间(timestamp) */
createdAt: number
/** 更新时间(timestamp) */
updatedAt: number
/** 作业标题 */
title: string
/** 任务状态 */
status: 0 | 1
/** TODO: 没有标签 */
}

/** 作业 */
interface IWork extends IBaseWork {
/** 课程ID */
courseId: number
/** 作业描述 */
description: string
/** 截止日期(timestamp) */
deadline: number
/** 图片链接 */
pictureLinks: string
/** 发布平台 */
publishPlatform: string
/** 发布者ID */
publisherId: number
}

/** 通知 */
interface INotify extends IBaseWork {
/** 通知描述 */
message: string
/** 通知类型 */
type: string /** TODO: 需要再看下 */
}

/** 任务状态枚举 */
const workEnum = {
0: 'actived',
1: 'deactived',
} as const

/** 作业状态枚举 */
const homeWorkEnum = {
0: 'unread_unresolve',
1: 'read_unresolve',
2: 'read_resolve',
} as const

export { IWork, INotify, workEnum, homeWorkEnum }
File renamed without changes.
1 change: 1 addition & 0 deletions src/utils/types/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './enumHelper'
3 changes: 2 additions & 1 deletion src/utils/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './enumHelper'
export * from './helpers'
export * from './entity'
export * from './business'

0 comments on commit 9a7b49d

Please sign in to comment.