Commit ca005426 authored by raca's avatar raca

feat: 优化

parent 1118e50d
...@@ -45,19 +45,19 @@ export type PkRecord = { ...@@ -45,19 +45,19 @@ export type PkRecord = {
recipientErrNum: number, recipientErrNum: number,
recipientName: string, recipientName: string,
recipientResultTime: number, recipientResultTime: number,
result: string, result: 'LOSE' | 'WIN',
stageName: string stageName: string
} }
export type PkRecords = { export type PkSummary = {
records: PkRecord[] records: PkRecord[]
} & Pick<PkDetails, 'allPkNum' | 'userRanks' | 'winPkNum'> } & Pick<PkDetails, 'allPkNum' | 'userRanks' | 'winPkNum'>
export function fetchUserRecords (data: PkParams) { export function fetchUserSummary (data: PkParams) {
if (!data.stageId) { if (!data.stageId) {
delete data.stageId delete data.stageId
} }
return requestAfterSilentLogin<PkRecords, PkParams>((userInfo) => { return requestAfterSilentLogin<PkSummary, PkParams>((userInfo) => {
data.userId = data.userId || userInfo?.userId data.userId = data.userId || userInfo?.userId
return { return {
method: 'GET', method: 'GET',
......
...@@ -10,23 +10,20 @@ import { UserInfo, GameResultProps } from './game-result' ...@@ -10,23 +10,20 @@ import { UserInfo, GameResultProps } from './game-result'
import styles from './battles-modal.module.scss' import styles from './battles-modal.module.scss'
import gameResultStyles from './game-result.module.scss' import gameResultStyles from './game-result.module.scss'
import { rankTranslate } from '../utils'
const BattlesModal = () => { const BattlesModal = () => {
const isVisible = useStoreState(state => state.battlesModel.modalIsVisible) const isVisible = useStoreState(state => state.battlesModel.modalIsVisible)
const setModalIsVisible = useStoreActions(actions => actions.battlesModel.setModalIsVisible) const setModalIsVisible = useStoreActions(actions => actions.battlesModel.setModalIsVisible)
const details = useStoreState((state) => state.battlesModel.details) const summary = useStoreState((state) => state.battlesModel.summary)
const records = useStoreState((state) => state.battlesModel.records)
const fetchUserDetails = useStoreActions( const fetchUserSummary = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails (actions) => actions.battlesModel.fetchUserSummary
)
const fetchUserRecords = useStoreActions(
(actions) => actions.battlesModel.fetchUserRecords
) )
useEffect(() => { useEffect(() => {
Promise.allSettled([fetchUserDetails({}), fetchUserRecords({})]) fetchUserSummary({})
}, [fetchUserDetails, fetchUserRecords]) }, [fetchUserSummary])
return ( return (
<Modal <Modal
...@@ -56,7 +53,7 @@ const BattlesModal = () => { ...@@ -56,7 +53,7 @@ const BattlesModal = () => {
/> />
<View className={styles.detailsRight}> <View className={styles.detailsRight}>
<Text className={styles.badgeText}> <Text className={styles.badgeText}>
{details?.userRanks.level || '-'} {rankTranslate(summary?.userRanks.level)}
</Text> </Text>
<View className={styles.badgeBar}> <View className={styles.badgeBar}>
{[...Array(3)].map((_, index) => ( {[...Array(3)].map((_, index) => (
...@@ -64,7 +61,7 @@ const BattlesModal = () => { ...@@ -64,7 +61,7 @@ const BattlesModal = () => {
key={index} key={index}
className={classNames( className={classNames(
styles.badgeBarItem, styles.badgeBarItem,
index < (details?.userRanks.star || 0) ? styles.active : null index < (summary?.userRanks.star || 0) ? styles.active : null
)} )}
></Text> ></Text>
))} ))}
...@@ -73,8 +70,8 @@ const BattlesModal = () => { ...@@ -73,8 +70,8 @@ const BattlesModal = () => {
</View> </View>
<ScrollView className={styles.gameResult} scrollY> <ScrollView className={styles.gameResult} scrollY>
{records.length > 0 {summary && summary?.records.length > 0
? records.map((item) => <GameResult key={item.id} record={item} />) ? summary?.records.map((item) => <GameResult key={item.id} record={item} />)
: <Text className={styles.emptyText}>暂无PK记录</Text> : <Text className={styles.emptyText}>暂无PK记录</Text>
} }
</ScrollView> </ScrollView>
...@@ -84,18 +81,19 @@ const BattlesModal = () => { ...@@ -84,18 +81,19 @@ const BattlesModal = () => {
} }
const GameResult = ({ record }: GameResultProps) => { const GameResult = ({ record }: GameResultProps) => {
const wined = record.result === 'WIN'
return ( return (
<View className={gameResultStyles.gameResult}> <View className={gameResultStyles.gameResult}>
<View <View
className={classNames( className={classNames(
gameResultStyles.levelInfo, gameResultStyles.levelInfo,
record.result ? gameResultStyles.win : gameResultStyles.lose wined ? gameResultStyles.win : gameResultStyles.lose
)} )}
> >
<Text className={gameResultStyles.levelName}>{record.stageName}</Text> <Text className={gameResultStyles.levelName}>{record.stageName}</Text>
<Image <Image
mode="widthFix" mode="widthFix"
src={record.result ? assets['win.png'].u : assets['lose.png'].u} src={wined ? assets['win.png'].u : assets['lose.png'].u}
className={gameResultStyles.levelResult} className={gameResultStyles.levelResult}
/> />
<Text className={gameResultStyles.levelDate}>{convertDateTime(record.createTime)}</Text> <Text className={gameResultStyles.levelDate}>{convertDateTime(record.createTime)}</Text>
...@@ -103,11 +101,11 @@ const GameResult = ({ record }: GameResultProps) => { ...@@ -103,11 +101,11 @@ const GameResult = ({ record }: GameResultProps) => {
<View className={styles.gameBody}> <View className={styles.gameBody}>
<View className={styles.playerSection}> <View className={styles.playerSection}>
<View className={classNames(styles.userInfo, record.result ? styles.win : styles.lose)}> <View className={classNames(styles.userInfo, wined ? styles.win : styles.lose)}>
<UserInfo name={record.initiatorName} /> <UserInfo name={record.initiatorName} />
</View> </View>
<Image className={gameResultStyles.vs} src={assets['vs.png'].u} /> <Image className={gameResultStyles.vs} src={assets['vs.png'].u} />
<View className={classNames(styles.userInfo, !record.result ? styles.win : styles.lose)}> <View className={classNames(styles.userInfo, !wined ? styles.win : styles.lose)}>
<UserInfo style={{ alignItems: 'flex-end' }} name={record.recipientName} /> <UserInfo style={{ alignItems: 'flex-end' }} name={record.recipientName} />
</View> </View>
</View> </View>
......
...@@ -14,18 +14,19 @@ export interface GameResultProps { ...@@ -14,18 +14,19 @@ export interface GameResultProps {
} }
const GameResult = ({ record }: GameResultProps) => { const GameResult = ({ record }: GameResultProps) => {
const wined = record.result === 'WIN'
return ( return (
<View className={styles.gameResult}> <View className={styles.gameResult}>
<View <View
className={classNames( className={classNames(
styles.levelInfo, styles.levelInfo,
record.result ? styles.win : styles.lose wined ? styles.win : styles.lose
)} )}
> >
<Text className={styles.levelName}>{record.stageName}</Text> <Text className={styles.levelName}>{record.stageName}</Text>
<Image <Image
mode="widthFix" mode="widthFix"
src={assets[record.result ? 'win.png' : 'lose.png'].u} src={assets[wined ? 'win.png' : 'lose.png'].u}
className={styles.levelResult} className={styles.levelResult}
/> />
<Text className={styles.levelDate}>{convertDateTime(record.createTime)}</Text> <Text className={styles.levelDate}>{convertDateTime(record.createTime)}</Text>
...@@ -41,7 +42,7 @@ const GameResult = ({ record }: GameResultProps) => { ...@@ -41,7 +42,7 @@ const GameResult = ({ record }: GameResultProps) => {
/> />
</View> </View>
<GameStats <GameStats
result={record.result} result={wined}
initiator={{ initiator={{
err: record.initiatorErrNum, err: record.initiatorErrNum,
time: record.initiatorResultTime, time: record.initiatorResultTime,
......
...@@ -5,9 +5,10 @@ import assets from '@/assets/oss/assets-pk' ...@@ -5,9 +5,10 @@ import assets from '@/assets/oss/assets-pk'
import styles from './player-details.module.scss' import styles from './player-details.module.scss'
import { badgeImages } from '../const' import { badgeImages } from '../const'
import { rankTranslate } from '../utils'
const PlayerDetails = () => { const PlayerDetails = () => {
const details = useStoreState((state) => state.battlesModel.details) const summary = useStoreState((state) => state.battlesModel.summary)
return ( return (
<View className={styles.player}> <View className={styles.player}>
<View className={styles.playerHeader}> <View className={styles.playerHeader}>
...@@ -16,7 +17,7 @@ const PlayerDetails = () => { ...@@ -16,7 +17,7 @@ const PlayerDetails = () => {
className={styles.badgeIcon} className={styles.badgeIcon}
></Image> ></Image>
<Text className={styles.badgeText}> <Text className={styles.badgeText}>
{details?.userRanks.level || '-'} {rankTranslate(summary?.userRanks.level)}
</Text> </Text>
<View className={styles.badgeBar}> <View className={styles.badgeBar}>
{[...Array(3)].map((_, index) => ( {[...Array(3)].map((_, index) => (
...@@ -24,7 +25,7 @@ const PlayerDetails = () => { ...@@ -24,7 +25,7 @@ const PlayerDetails = () => {
key={index} key={index}
className={classNames( className={classNames(
styles.badgeBarItem, styles.badgeBarItem,
index < (details?.userRanks.star || 0) ? styles.active : null index < (summary?.userRanks.star || 0) ? styles.active : null
)} )}
></Text> ></Text>
))} ))}
...@@ -33,13 +34,13 @@ const PlayerDetails = () => { ...@@ -33,13 +34,13 @@ const PlayerDetails = () => {
<View className={styles.playerFooter}> <View className={styles.playerFooter}>
<View className={styles.stats}> <View className={styles.stats}>
<View> <View>
胜<Text className={styles.winText}>{details?.winPkNum || 0}</Text> 胜<Text className={styles.winText}>{summary?.winPkNum || 0}</Text>
</View> </View>
<Image className={styles.icon} src={assets['stats.png'].u} /> <Image className={styles.icon} src={assets['stats.png'].u} />
<View> <View>
负 负
<Text className={styles.loseText}> <Text className={styles.loseText}>
{(details?.allPkNum || 0) - (details?.winPkNum || 0)} {(summary?.allPkNum || 0) - (summary?.winPkNum || 0)}
</Text> </Text>
</View> </View>
</View> </View>
......
...@@ -12,17 +12,14 @@ import styles from './index.module.scss' ...@@ -12,17 +12,14 @@ import styles from './index.module.scss'
const Battles = () => { const Battles = () => {
const nav = useMemo(getMenuButtonBoundingClientRect, []) const nav = useMemo(getMenuButtonBoundingClientRect, [])
const records = useStoreState((state) => state.battlesModel.records) const summary = useStoreState((state) => state.battlesModel.summary)
const fetchUserDetails = useStoreActions( const fetchUserSummary = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails (actions) => actions.battlesModel.fetchUserSummary
)
const fetchUserRecords = useStoreActions(
(actions) => actions.battlesModel.fetchUserRecords
) )
useDidShow(() => { useDidShow(() => {
Promise.allSettled([fetchUserDetails({}), fetchUserRecords({})]) fetchUserSummary({})
}) })
return ( return (
...@@ -49,8 +46,8 @@ const Battles = () => { ...@@ -49,8 +46,8 @@ const Battles = () => {
</View> </View>
<View className={styles.gameResult}> <View className={styles.gameResult}>
{records.length > 0 {summary && summary.records.length > 0
? records.map((item) => <GameResult key={item.id} record={item} />) ? summary.records.map((item) => <GameResult key={item.id} record={item} />)
: <Text className={styles.emptyText}>暂无PK记录</Text> : <Text className={styles.emptyText}>暂无PK记录</Text>
} }
</View> </View>
......
import { Action, Thunk, action, thunk } from 'easy-peasy' import { Action, Thunk, action, thunk } from 'easy-peasy'
import { fetchUserDetails, fetchUserRecords, type PkParams, type PkDetails, type PkRecord } from '@/api/pk' import { fetchUserDetails, fetchUserSummary, type PkParams, type PkDetails, type PkSummary } from '@/api/pk'
interface BattlesModel { interface BattlesModel {
details: PkDetails | null, details: PkDetails | null,
records: PkRecord[]; summary: PkSummary | null;
modalIsVisible: boolean; modalIsVisible: boolean;
setModalIsVisible: Action<BattlesModel, boolean>; setModalIsVisible: Action<BattlesModel, boolean>;
setUserDetails: Action<BattlesModel, PkDetails>; setUserDetails: Action<BattlesModel, PkDetails>;
setUserRecords: Action<BattlesModel, PkRecord[]>; setUserSummary: Action<BattlesModel, PkSummary>;
fetchUserDetails: Thunk<BattlesModel, PkParams>; fetchUserDetails: Thunk<BattlesModel, PkParams>;
fetchUserRecords: Thunk<BattlesModel, PkParams>; fetchUserSummary: Thunk<BattlesModel, PkParams>;
} }
const battlesModel: BattlesModel = { const battlesModel: BattlesModel = {
details: null, details: null,
records: [], summary: null,
modalIsVisible: true, modalIsVisible: true,
setModalIsVisible: action((state, payload) => { setModalIsVisible: action((state, payload) => {
...@@ -25,8 +25,8 @@ const battlesModel: BattlesModel = { ...@@ -25,8 +25,8 @@ const battlesModel: BattlesModel = {
state.details = payload state.details = payload
}), }),
setUserRecords: action((state, payload) => { setUserSummary: action((state, payload) => {
state.records = payload state.summary = payload
}), }),
fetchUserDetails: thunk(async (actions, payload) => { fetchUserDetails: thunk(async (actions, payload) => {
...@@ -37,10 +37,10 @@ const battlesModel: BattlesModel = { ...@@ -37,10 +37,10 @@ const battlesModel: BattlesModel = {
} }
}), }),
fetchUserRecords: thunk(async (actions, payload) => { fetchUserSummary: thunk(async (actions, payload) => {
try { try {
const res = await fetchUserRecords(payload) const res = await fetchUserSummary(payload)
actions.setUserRecords(res.data.records) actions.setUserSummary(res.data)
} catch (err) { } catch (err) {
} }
}), }),
......
type Rank = 'BRONZE' | 'SILVER' | 'GOLD' | 'PLATINUM' | 'DIAMOND'
function toRomanNumeral (num: number): string {
const roman: { [key: number]: string } = {
1: 'I',
2: 'II',
3: 'III',
}
return roman[num]
}
export function rankTranslate (rankWithLevel: string | undefined): string {
if (!rankWithLevel) {
return '-'
}
const [rank, level] = rankWithLevel.split('_')
const rankName: { [key in Rank]?: string } = {
BRONZE: '青铜',
SILVER: '白银',
GOLD: '黄金',
PLATINUM: '铂金',
DIAMOND: '钻石',
}
return `${rankName[rank as Rank]} ${toRomanNumeral(Number(level))}`
}
...@@ -79,7 +79,7 @@ const ExchangeRenameCard = (props: ExchangeItemProps) => { ...@@ -79,7 +79,7 @@ const ExchangeRenameCard = (props: ExchangeItemProps) => {
}) })
.then(() => { .then(() => {
changeNickName(name).then(() => { changeNickName(name).then(() => {
showToast({ title: '操作成功', icon: 'success' }) showToast({ title: '修改成功', icon: 'success' })
onCancel && onCancel() onCancel && onCancel()
}) })
}) })
......
import { View, Image, Text } from '@tarojs/components' import { View, Image, Text } from '@tarojs/components'
import { useMemo } from 'react' import { useMemo } from 'react'
import { navigateTo, getMenuButtonBoundingClientRect, navigateBack, getSystemInfoSync } from '@tarojs/taro' import { navigateTo, getMenuButtonBoundingClientRect, navigateBack, getSystemInfoSync } from '@tarojs/taro'
import assets from '@/assets/oss/assets-main-next'
import { useStoreState } from '@/store/hooks' import { useStoreState } from '@/store/hooks'
import Scene3 from '../guide/complete/scene-3' import Scene3 from '../guide/complete/scene-3'
...@@ -42,7 +43,7 @@ export default () => { ...@@ -42,7 +43,7 @@ export default () => {
<Text>{point}</Text> <Text>{point}</Text>
</Scene3> </Scene3>
<View className={styles.ph} onClick={goStore}> <View className={styles.ph} onClick={goStore}>
<Image src={require('@/pages/main/assets/ph.png')} className={styles.icon} /> <Image src={assets['star.png'].u} className={styles.icon} />
<Text>{cardNum}</Text> <Text>{cardNum}</Text>
</View> </View>
</View> </View>
......
...@@ -179,22 +179,24 @@ const userModel: UserModel = { ...@@ -179,22 +179,24 @@ const userModel: UserModel = {
const { data: list } = await accountExchange(payload) const { data: list } = await accountExchange(payload)
const point = list.find(c => c.accountType === 'POINT')?.amount ?? 0 const point = list.find(c => c.accountType === 'POINT')?.amount ?? 0
actions.updateState({ point }) actions.updateState({ point })
hideLoading()
} catch (err) { } catch (err) {
hideLoading()
showModal({ showModal({
title: '提示', title: '提示',
content: err.message, content: err.message,
showCancel: false showCancel: false
}) })
throw err throw err
} finally {
hideLoading()
} }
}), }),
changeNickName: thunk(async (_, payload) => { changeNickName: thunk(async (actions, payload) => {
try { try {
const { data } = await changeNickName(payload) const { code } = await changeNickName(payload)
console.log(data) if (code === 0) {
actions.updateUserInfo({ nickName: payload })
}
} catch (err) { } catch (err) {
showModal({ showModal({
title: '提示', title: '提示',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment