Commit 1167b821 authored by raca's avatar raca

feat: 调接口

parent 3dea30a8
import { requestAfterSilentLogin } from '@/utils/request'
export type PkParams = {
stageId?: number,
userId?: number,
}
export type PkDetails = {
allPkNum: number,
errNum: number,
nickName: string,
resultTime: number,
stageId: number,
stageName: string,
userId: number,
userRanks: {
level: string,
star: number
},
winPkNum: number
}
export function fetchUserDetails (data: PkParams) {
return requestAfterSilentLogin<PkDetails, PkParams>((userInfo) => ({
method: 'GET',
url: '/service-idea/pk/personal/stage/detail',
data: {
stageId: data.stageId,
userId: data.userId || userInfo?.userId,
}
}))
}
export type PkRecord = {
createTime: string;
id: number,
initiator: number,
initiatorAvatar: string,
initiatorErrNum: number,
initiatorName: string,
initiatorResultTime: number,
prizeReceive: string,
recipient: number,
recipientAvatar: string,
recipientErrNum: number,
recipientName: string,
recipientResultTime: number,
result: string,
stageName: string
}
export type PkRecords = {
records: PkRecord[]
} & Pick<PkDetails, 'allPkNum' | 'userRanks' | 'winPkNum'>
export function fetchUserRecords (data: PkParams) {
return requestAfterSilentLogin<PkRecords, PkParams>((userInfo) => ({
method: 'GET',
url: '/service-idea/pk/personal/stage/records',
data: {
stageId: data.stageId,
userId: data.userId || userInfo?.userId,
}
}))
}
......@@ -71,7 +71,7 @@ const ModalName = ({
/>
<View className={styles.title}>请问大侠,尊姓大名</View>
<View className={styles.tips}>
请输入您的角色名(7字以内)或随机生成
请输入您的角色名(7字以内)
</View>
<View className={styles.input}>
<View className={styles.inputName}>{name}</View>
......
import { useDidShow, useRouter } from '@tarojs/taro'
import { View, Image, Text } from '@tarojs/components'
import { useBoolean } from '@/hooks'
import { useStoreState, useStoreActions } from '@/store/hooks'
import assetsPk from '@/assets/oss/assets-pk'
import assetsMainNext from '@/assets/oss/assets-main-next'
import { badgeImages } from '../battles/const'
......@@ -9,6 +11,21 @@ import ModalName from './components/modal-name'
const AcceptPk = () => {
const [isVisible, { setTrue, setFalse }] = useBoolean(false)
const router = useRouter<{ initiator?: string; stageId?: string }>()
const details = useStoreState((state) => state.battlesModel.details)
const fetchUserDetails = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails
)
useDidShow(async () => {
if (router.params.initiator && router.params.stageId) {
await fetchUserDetails({
userId: +router.params.initiator,
stageId: +router.params.stageId,
})
}
})
return (
<View className={styles.container}>
......@@ -22,7 +39,10 @@ const AcceptPk = () => {
style={{ left: 0 }}
src={assetsMainNext['pk-flash.png'].u}
/>
<Image className={styles.pk} src={assetsMainNext['pk-logo.png'].u} />
<Image
className={styles.pk}
src={assetsMainNext['pk-logo.png'].u}
/>
<Image
className={styles.flash}
style={{ right: 0 }}
......@@ -39,11 +59,13 @@ const AcceptPk = () => {
<View className={styles.inner}>
<View className={styles.userSection}>
<Text className={styles.userName}>用户名称用户名</Text>
<Text className={styles.userName}>
{details?.nickName || '-'}
</Text>
<Text className={styles.challenge}>向你发起挑战</Text>
</View>
<Text>关卡-连加连减</Text>
<Text>关卡-{details?.stageName}</Text>
<Image
className={styles.avatar}
......@@ -56,10 +78,15 @@ const AcceptPk = () => {
src={assetsPk[badgeImages.bronze.default].u}
className={styles.badgeIcon}
/>
<Text className={styles.rankText}>青铜</Text>
<Text className={styles.rankText}>
{details?.userRanks.level}
</Text>
</View>
<Text>当前战绩:胜 2 负 3</Text>
<Text>最佳表现:全对 | 29.3s</Text>
<Text>
当前战绩:胜 {details?.winPkNum || 0} 负
{(details?.allPkNum || 0) - (details?.winPkNum || 0)}
</Text>
<Text>最佳表现:全对 | {details?.resultTime || 0}s</Text>
</View>
</View>
</View>
......
......@@ -80,6 +80,7 @@
.gameResult {
display: flex;
flex-direction: column;
text-align: center;
gap: 16px;
max-height: 500px;
}
......@@ -111,4 +112,10 @@
.lose {
background-color: #FEEDED;
}
.emptyText {
margin-top: 48px;
font-size: 14PX;
color: rgba(70, 81, 100, 0.50);
}
\ No newline at end of file
import { useEffect } from 'react'
import { View, Text, Image, ScrollView } from '@tarojs/components'
import classNames from 'classnames'
import Modal from '@/components/Modal'
......@@ -12,31 +13,19 @@ import gameResultStyles from './game-result.module.scss'
const BattlesModal = () => {
const isVisible = useStoreState(state => state.battlesModel.modalIsVisible)
const setModalIsVisible = useStoreActions(actions => actions.battlesModel.setModalIsVisible)
const details = useStoreState((state) => state.battlesModel.details)
const records = useStoreState((state) => state.battlesModel.records)
const levelInfo = {
name: '关卡',
result: true,
date: '9月22日 12:22',
}
const levelInfo2 = {
name: '关卡名称关卡名称',
result: false,
date: '9月22日 12:22',
}
const fetchUserDetails = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails
)
const fetchUserRecords = useStoreActions(
(actions) => actions.battlesModel.fetchUserRecords
)
const players = [
{
name: '用户名称用',
icon: 'string',
level: '青铜Ⅰ',
},
{
name: '用户2',
icon: 'string',
level: '青铜Ⅰ',
},
]
useEffect(() => {
Promise.allSettled([fetchUserDetails({}), fetchUserRecords({})])
}, [fetchUserDetails, fetchUserRecords])
return (
<Modal
......@@ -65,55 +54,60 @@ const BattlesModal = () => {
className={styles.badgeIcon}
/>
<View className={styles.detailsRight}>
<Text className={styles.badgeText}>青铜 1</Text>
<Text className={styles.badgeText}>
{details?.userRanks.level || '-'}
</Text>
<View className={styles.badgeBar}>
<Text
className={classNames(styles.badgeBarItem, styles.active)}
></Text>
<Text className={classNames(styles.badgeBarItem)}></Text>
<Text className={classNames(styles.badgeBarItem)}></Text>
{[...Array(3)].map((_, index) => (
<Text
key={index}
className={classNames(
styles.badgeBarItem,
index < (details?.userRanks.star || 0) ? styles.active : null
)}
></Text>
))}
</View>
</View>
</View>
<ScrollView className={styles.gameResult} scrollY>
<GameResult levelInfo={levelInfo} players={players} />
<GameResult levelInfo={levelInfo2} players={players} />
<GameResult levelInfo={levelInfo} players={players} />
<GameResult levelInfo={levelInfo2} players={players} />
<GameResult levelInfo={levelInfo} players={players} />
{records.length > 0
? records.map((item) => <GameResult key={item.id} record={item} />)
: <Text className={styles.emptyText}>暂无PK记录</Text>
}
</ScrollView>
</View>
</Modal>
)
}
const GameResult = ({ players, levelInfo }: GameResultProps) => {
const GameResult = ({ record }: GameResultProps) => {
return (
<View className={gameResultStyles.gameResult}>
<View
className={classNames(
gameResultStyles.levelInfo,
levelInfo.result ? gameResultStyles.win : gameResultStyles.lose
record.result ? gameResultStyles.win : gameResultStyles.lose
)}
>
<Text className={gameResultStyles.levelName}>{levelInfo.name}</Text>
<Text className={gameResultStyles.levelName}>{record.stageName}</Text>
<Image
mode="widthFix"
src={levelInfo.result ? assets['win.png'].u : assets['lose.png'].u}
src={record.result ? assets['win.png'].u : assets['lose.png'].u}
className={gameResultStyles.levelResult}
/>
<Text className={gameResultStyles.levelDate}>{levelInfo.date}</Text>
<Text className={gameResultStyles.levelDate}>{record.createTime}</Text>
</View>
<View className={styles.gameBody}>
<View className={styles.playerSection}>
<View className={classNames(styles.userInfo, levelInfo.result ? styles.win : styles.lose)}>
<UserInfo {...players[0]} />
<View className={classNames(styles.userInfo, record.result ? styles.win : styles.lose)}>
<UserInfo name={record.initiatorName} />
</View>
<Image className={gameResultStyles.vs} src={assets['vs.png'].u} />
<View className={classNames(styles.userInfo, !levelInfo.result ? styles.win : styles.lose)}>
<UserInfo style={{ alignItems: 'flex-end' }} {...players[1]} />
<View className={classNames(styles.userInfo, !record.result ? styles.win : styles.lose)}>
<UserInfo style={{ alignItems: 'flex-end' }} name={record.recipientName} />
</View>
</View>
</View>
......
......@@ -3,71 +3,96 @@ import { View, Text, Image } from '@tarojs/components'
import classNames from 'classnames'
import assets from '@/assets/oss/assets-pk'
import assets2 from '@/assets/oss/assets-prepare'
import { PkRecord } from '@/api/pk'
import { badgeImages } from '../const'
import styles from './game-result.module.scss'
export interface LevelInfo {
name: string;
result: boolean;
date: string;
}
interface Player {
name: string;
icon: string;
level: string;
}
interface Stat {
score: string;
label: string;
value: string;
}
export interface GameResultProps {
levelInfo: LevelInfo;
players: Player[];
gameStats?: Stat[];
record: PkRecord
}
const GameResult = ({ players, levelInfo }: GameResultProps) => {
const GameResult = ({ record }: GameResultProps) => {
return (
<View className={styles.gameResult}>
<View className={classNames(styles.levelInfo, levelInfo.result ? styles.win : styles.lose)}>
<Text className={styles.levelName}>{levelInfo.name}</Text>
<Image mode="widthFix" src={assets[levelInfo.result ? 'win.png' : 'lose.png'].u} className={styles.levelResult} />
<Text className={styles.levelDate}>{levelInfo.date}</Text>
<View
className={classNames(
styles.levelInfo,
record.result ? styles.win : styles.lose
)}
>
<Text className={styles.levelName}>{record.stageName}</Text>
<Image
mode="widthFix"
src={assets[record.result ? 'win.png' : 'lose.png'].u}
className={styles.levelResult}
/>
<Text className={styles.levelDate}>{record.createTime}</Text>
</View>
<View className={styles.gameBody}>
<View className={styles.playerSection}>
<UserInfo {...players[0]} />
<UserInfo name={record.initiatorName} />
<Image className={styles.vs} src={assets['vs.png'].u} />
<UserInfo style={{ alignItems: 'flex-end' }} {...players[1]} />
<UserInfo
style={{ alignItems: 'flex-end' }}
name={record.recipientName}
/>
</View>
<GameStats result={levelInfo.result} />
<GameStats
result={record.result}
initiator={{
err: record.initiatorErrNum,
time: record.initiatorResultTime,
}}
recipient={{
err: record.recipientErrNum,
time: record.recipientResultTime,
}}
/>
</View>
</View>
)
}
export const UserInfo = (player: Player & { style?: CSSProperties }) => (
<View style={player.style} className={styles.userInfo}>
<Text className={styles.userName}>{player.name}</Text>
export const UserInfo = (user: { name: string; style?: CSSProperties }) => (
<View style={user.style} className={styles.userInfo}>
<Text className={styles.userName}>{user.name}</Text>
<View className={styles.userBadge}>
<Image className={styles.userIcon} src={assets[badgeImages.diamond.small].u} />
<Text className={styles.userLevel}>{player.level}</Text>
<Image
className={styles.userIcon}
src={assets[badgeImages.diamond.small].u}
/>
<Text className={styles.userLevel}>{user.name}</Text>
</View>
</View>
)
const GameStats = ({ result }: { result: boolean }) => {
export type StatParams = {
result: boolean | string
initiator: {
err: number
time: number
}
recipient: {
err: number
time: number
}
}
const GameStats = ({ initiator, recipient, result }: StatParams) => {
return (
<View className={styles.gameStats}>
<View className={classNames(styles.stats, result ? styles.statsWin : styles.statsLose)}>
<Text className={styles.value}>全队</Text>
<Text className={styles.value}>13.2s</Text>
<View
className={classNames(
styles.stats,
result ? styles.statsWin : styles.statsLose
)}
>
<Text className={styles.value}>
{initiator.err > 0 ? `错${initiator.err}题` : '全对'}
</Text>
<Text className={styles.value}>{initiator.time}s</Text>
<View className={classNames(styles.value)}>
<Image className={styles.diamond} src={assets2['diamond.png'].u} />
<Text>+10</Text>
......@@ -78,9 +103,16 @@ const GameStats = ({ result }: { result: boolean }) => {
<Text className={styles.value}>答题用时</Text>
<Text className={styles.value}>PK奖励</Text>
</View>
<View className={classNames(styles.stats, !result ? styles.statsWin : styles.statsLose)}>
<Text className={styles.value}>错2题</Text>
<Text className={styles.value}>23.2s</Text>
<View
className={classNames(
styles.stats,
!result ? styles.statsWin : styles.statsLose
)}
>
<Text className={styles.value}>
{recipient.err > 0 ? `错${recipient.err}题` : '全对'}
</Text>
<Text className={styles.value}>{recipient.time}s</Text>
<View className={styles.value}>
<Image className={styles.diamond} src={assets2['diamond.png'].u} />
<Text>+15</Text>
......
......@@ -43,15 +43,16 @@
display: flex;
gap: 32px;
margin: 0 38px;
justify-content: center;
.stats, .rank {
.stats {
display: flex;
align-items: center;
justify-content: space-between;
flex: 1;
border-radius: 12px;
border: 4px solid #57627D;
padding: 12px 32px;
width: 380px;
.icon {
width: 56px;
......
import { View, Image, Text } from '@tarojs/components'
import classNames from 'classnames'
import { useStoreState } from '@/store/hooks'
import assets from '@/assets/oss/assets-pk'
import styles from './player-details.module.scss'
import { badgeImages } from '../const'
const PlayerDetails = () => {
const details = useStoreState((state) => state.battlesModel.details)
return (
<View className={styles.player}>
<View className={styles.playerHeader}>
<Image src={assets[badgeImages.platinum.default].u} className={styles.badgeIcon}></Image>
<Text className={styles.badgeText}>青铜 1</Text>
<Image
src={assets[badgeImages.bronze.default].u}
className={styles.badgeIcon}
></Image>
<Text className={styles.badgeText}>
{details?.userRanks.level || '-'}
</Text>
<View className={styles.badgeBar}>
<Text className={classNames(styles.badgeBarItem, styles.active)}></Text>
<Text className={classNames(styles.badgeBarItem)}></Text>
<Text className={classNames(styles.badgeBarItem)}></Text>
{[...Array(3)].map((_, index) => (
<Text
key={index}
className={classNames(
styles.badgeBarItem,
index < (details?.userRanks.star || 0) ? styles.active : null
)}
></Text>
))}
</View>
</View>
<View className={styles.playerFooter}>
<View className={styles.stats}>
<View>胜<Text className={styles.winText}>4</Text></View>
<View>
胜<Text className={styles.winText}>{details?.winPkNum || 0}</Text>
</View>
<Image className={styles.icon} src={assets['stats.png'].u} />
<View>负<Text className={styles.loseText}>4</Text></View>
<View>
负
<Text className={styles.loseText}>
{(details?.allPkNum || 0) - (details?.winPkNum || 0)}
</Text>
</View>
</View>
<View className={styles.rank}>
{/* <View className={styles.rank}>
<Image className={styles.icon} src={assets['rank.png'].u} />
<View>排名第<Text className={styles.rankText}>32</Text>名</View>
</View>
<View>
第<Text className={styles.rankText}>32</Text>名
</View>
</View> */}
</View>
</View>
)
......
.wrapper {
background-color: #F0F2F9;
min-height: 100vh;
}
.pk {
margin-top: -60px;
display: flex;
......@@ -62,4 +67,11 @@
.bottomHoverButton {
background-image: url('https://cdn.jkwljy.com/pk-math/3UiwFgrVHVQEcaPdxdf1XinBb64o/343x56');
}
.emptyText {
margin-top: 48px;
text-align: center;
font-size: 14PX;
color: rgba(70, 81, 100, 0.50);
}
\ No newline at end of file
import { useMemo } from 'react'
import { getMenuButtonBoundingClientRect } from '@tarojs/taro'
import { getMenuButtonBoundingClientRect, useDidShow } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import Button from '@/components/Button'
import { useStoreState, useStoreActions } from '@/store/hooks'
import assets from '@/assets/oss/assets-pk'
import TopHeader from './components/top-header'
import PlayerDetails from './components/player-details'
......@@ -9,37 +10,31 @@ import GameResult from './components/game-result'
import styles from './index.module.scss'
const levelInfo = {
name: '关卡',
result: true,
date: '9月22日 12:22',
}
const Battles = () => {
const nav = useMemo(getMenuButtonBoundingClientRect, [])
const payload = { stageId: 1 }
const records = useStoreState((state) => state.battlesModel.records)
const levelInfo2 = {
name: '关卡名称关卡名称',
result: false,
date: '9月22日 12:22',
}
const fetchUserDetails = useStoreActions(
(actions) => actions.battlesModel.fetchUserDetails
)
const fetchUserRecords = useStoreActions(
(actions) => actions.battlesModel.fetchUserRecords
)
const players = [
{
name: '用户名称用',
icon: 'string',
level: '青铜Ⅰ'
},
{
name: '用户2',
icon: 'string',
level: '青铜Ⅰ'
}
]
useDidShow(() => {
Promise.allSettled([fetchUserDetails(payload), fetchUserRecords(payload)])
})
const Battles = () => {
const nav = useMemo(getMenuButtonBoundingClientRect, [])
return (
<View style={{ backgroundColor: '#F0F2F9' }}>
<View className={styles.wrapper}>
<TopHeader />
<View style={{ paddingTop: `${nav.height + nav.top + 5}px`, paddingBottom: 100 }}>
<View
style={{
paddingTop: `${nav.height + nav.top + 5}px`,
paddingBottom: 100,
}}
>
<PlayerDetails />
<View className={styles.pk}>
......@@ -55,12 +50,18 @@ const Battles = () => {
</View>
<View className={styles.gameResult}>
<GameResult levelInfo={levelInfo} players={players} />
<GameResult levelInfo={levelInfo2} players={players} />
{records.length > 0
? records.map((item) => <GameResult key={item.id} record={item} />)
: <Text className={styles.emptyText}>暂无PK记录</Text>
}
</View>
</View>
<View className={styles.bottom}>
<Button size="large" image={styles.bottomButton} hoverImage={styles.bottomHoverButton}>
<Button
size="large"
image={styles.bottomButton}
hoverImage={styles.bottomHoverButton}
>
<Image src={assets['pk.png'].u} className={styles.bottomText} />
</Button>
</View>
......
import { Action, action } from 'easy-peasy'
import { Action, Thunk, action, thunk } from 'easy-peasy'
import { fetchUserDetails, fetchUserRecords, type PkParams, type PkDetails, type PkRecord } from '@/api/pk'
interface BattlesModel {
details: PkDetails | null,
records: PkRecord[];
modalIsVisible: boolean;
setModalIsVisible: Action<BattlesModel, boolean>;
setUserDetails: Action<BattlesModel, PkDetails>;
setUserRecords: Action<BattlesModel, PkRecord[]>;
fetchUserDetails: Thunk<BattlesModel, PkParams>;
fetchUserRecords: Thunk<BattlesModel, PkParams>;
}
const battlesModel: BattlesModel = {
details: null,
records: [],
modalIsVisible: true,
setModalIsVisible: action((state, payload) => {
state.modalIsVisible = payload
})
}),
setUserDetails: action((state, payload) => {
state.details = payload
}),
setUserRecords: action((state, payload) => {
state.records = payload
}),
fetchUserDetails: thunk(async (actions, payload) => {
try {
const res = await fetchUserDetails(payload)
if (res.code === 200) {
console.log('res', res)
actions.setUserDetails(res.data)
}
} catch (err) {
}
}),
fetchUserRecords: thunk(async (actions, payload) => {
try {
const res = await fetchUserRecords(payload)
if (res.code === 200) {
console.log('res', res)
actions.setUserRecords(res.data.records)
}
} catch (err) {
}
}),
}
export default battlesModel
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