Commit ebde4b1f authored by hewenhao's avatar hewenhao

feat: 兑换答题卡

parent f70c4484
...@@ -33,7 +33,6 @@ const StageTag = (props: StageTagProps) => { ...@@ -33,7 +33,6 @@ const StageTag = (props: StageTagProps) => {
useEffect(() => { useEffect(() => {
let observer: IntersectionObserver | null = null let observer: IntersectionObserver | null = null
const fn = () => { const fn = () => {
console.log(ready, isFocus, 111)
if (ready && isFocus) { if (ready && isFocus) {
const navVal = nav.current const navVal = nav.current
// @ts-ignore // @ts-ignore
......
@import '@/styles/mixins.scss';
.card-action {
@include flexLayout(column, _, space-between);
flex-grow: 1;
.card-description {
flex-grow: 1;
@include flexLayout(column, _, space-between);
.card-title {
font-size: 36px;
font-weight: 500;
color: #465164;
line-height: 50px;
margin-bottom: 8px;
display: flex;
justify-content: space-between;
align-items: center;
}
.card-subtitle {
font-size: 32px;
font-weight: 400;
color: rgba(70,81,100,0.8);
line-height: 44px;
@include multi-ellipsis(2);
}
}
.stoke-left {
font-size: 24px;
font-weight: 400;
color: rgba(70,81,100,0.5);
line-height: 34px;
}
.left_value {
color: #1786F7;
}
.card-footer {
margin-top: 32px;
@include flexLayout(_, _, space-between);
.card-score {
@include flexLayout(_, center);
&-icon {
width: 32PX;
height: 32PX;
margin-right: 4px;
}
&-text {
font-size: 32px;
font-weight: 500;
color: #1786F7;
line-height: 38px;
}
}
.card-exchange {
@include btn();
}
}
}
.btn {
width: 248px;
height: 72px;
background-image: url(../assets/btn-off.png);
background-size: 100% 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
font-weight: 500;
color: #FFFFFF;
line-height: 40px;
&.btn_on {
background-image: url(../assets/btn-on.png);
}
&.disabled {
background-image: none;
background-color: #E2E6ED;
color: #C2C8D1;
border-radius: 36px;
}
}
.card-main {
@include flexLayout(column, _, space-between);
margin-right: 16px;
.card-image {
width: 140px;
height: 140px;
}
}
import { View, Image, Text, Button } from '@tarojs/components' import { View, Image, Text } from '@tarojs/components'
import classNames from 'classnames'
import styles from './card-main.module.scss'
interface CardMainProps { interface CardMainProps {
onClick: () => void; onClick: () => void;
cover?: string; cover?: string;
title: string; title: string;
subtitle: string; subtitle: string;
extra?: React.ReactNode disabled?: boolean;
extra?: React.ReactNode;
price: number;
stoke: number | null;
} }
export default ({ export default ({
...@@ -14,24 +19,43 @@ export default ({ ...@@ -14,24 +19,43 @@ export default ({
extra, extra,
subtitle, subtitle,
onClick, onClick,
disabled,
price,
stoke
}: CardMainProps) => { }: CardMainProps) => {
const _onClick = () => {
if (!disabled) {
onClick()
}
}
return ( return (
<> <>
<View className="card-main"> <View className={styles['card-main']}>
<Image className="card-image" src={cover} defaultSource="https://cdn.jkwljy.com/pbb/6gkGHSsPmxc_coeGYahBkelo_2w_/96x96" /> <Image className={styles['card-image']} src={cover} defaultSource="https://cdn.jkwljy.com/pbb/6gkGHSsPmxc_coeGYahBkelo_2w_/96x96" />
{extra} {extra}
</View> </View>
<View className="card-action"> <View className={styles['card-action']}>
<View className="card-description"> <View className={styles['card-description']}>
<Text className="card-title">{title}</Text> <View className={styles['card-title']}>
<Text className="card-subtitle">{subtitle}</Text> {title}
{stoke !== null && (
<View className={styles['stoke-left']}>本月库存 <Text className={styles.left_value}>{stoke}</Text></View>
)}
</View> </View>
<View className="card-footer"> <Text className={styles['card-subtitle']}>{subtitle}</Text>
<View className="card-score"> </View>
<Image src={require('../../main/assets/score.png')} className="card-score-icon" /> <View className={styles['card-footer']}>
<Text className="card-score-text">积分271</Text> <View className={styles['card-score']}>
<Image src={require('../../main/assets/score.png')} className={styles['card-score-icon']} />
<Text className={styles['card-score-text']}>{ price }</Text>
</View>
<View
className={classNames(styles.btn, disabled && styles.disabled)}
onClick={_onClick}
hoverClass={!disabled ? styles.btn_on : ''}
>
兑换
</View> </View>
<Button className="card-exchange" onClick={onClick}>兑换</Button>
</View> </View>
</View> </View>
</> </>
......
.card { .card {
position: relative; position: relative;
background-color: #fff; background-color: #fff;
margin-bottom: 12PX; margin-bottom: 64px;
padding: 12PX;
} }
.bordered { .bordered {
...@@ -19,9 +18,14 @@ ...@@ -19,9 +18,14 @@
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
margin-bottom: 8PX; margin-bottom: 24px;
} }
.title { .title {
font-weight: 500; font-weight: 500;
font-size: 40px;
color: #465164;
line-height: 56px;
} }
...@@ -10,7 +10,6 @@ interface CardProps { ...@@ -10,7 +10,6 @@ interface CardProps {
headStyle?: React.CSSProperties; headStyle?: React.CSSProperties;
loading?: boolean; loading?: boolean;
title?: React.ReactNode; title?: React.ReactNode;
hoverable?: boolean;
extra?: React.ReactNode; extra?: React.ReactNode;
bodyStyle?: React.CSSProperties; bodyStyle?: React.CSSProperties;
} }
...@@ -20,7 +19,6 @@ const Card = ({ ...@@ -20,7 +19,6 @@ const Card = ({
extra, extra,
loading, loading,
bordered, bordered,
hoverable,
headStyle, headStyle,
bodyStyle, bodyStyle,
children children
...@@ -30,7 +28,7 @@ const Card = ({ ...@@ -30,7 +28,7 @@ const Card = ({
}) })
return ( return (
<View className={cardClass} hoverClass={hoverable && styles.hoverable}> <View className={cardClass}>
{(title || extra) && ( {(title || extra) && (
<View className={styles.head} style={headStyle}> <View className={styles.head} style={headStyle}>
{title && <View className={styles.title}>{title}</View>} {title && <View className={styles.title}>{title}</View>}
......
.cancel, .confirm {
width: 246px;
height: 72px;
border-radius: 36px;
border: 4px solid rgba(70,81,100,0.3);
font-size: 28px;
font-weight: 500;
color: rgba(70,81,100,0.5);
line-height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.confirm {
background: #1786F7;
border-color: transparent;
color: #fff;
&.disbaled {
background: #C7C9D0;
}
}
.footer {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 32px;
}
.input {
display: flex;
align-items: center;
font-size: 32px;
font-weight: 600;
color: #465164;
line-height: 44px;
padding: 32px 32px 104px 32px;
}
.input_main {
flex: 1;
display: flex;
align-items: center;
padding: 16px;
background: #F0F2F9;
border-radius: 48px;
margin-right: 16px;
}
.num {
flex: 1;
text-align: center;
font-size: 48px;
font-weight: bold;
color: #FFC056;
line-height: 58px;
}
.input_main_wrap {
flex: 1;
position: relative;
}
.tips {
position: absolute;
left: 0;
right: 0;
bottom: -56px;
text-align: center;
font-size: 28px;
font-weight: 400;
color: rgba(88,97,112,0.5);
line-height: 40px;
}
.tips_val {
color: #1786F7;
}
.btn {
background: #9BA1AF;
border-radius: 32px;
&.is_add {
background-color: #FFC056;
}
&.disabled {
background: #C7C9D0;
}
.icon {
width: 64px;
height: 64px;
display: block;
}
}
import { View, Image, Text } from '@tarojs/components'
import { showToast } from '@tarojs/taro'
import { useStoreActions } from '@/store/hooks'
import { useState, useCallback, useMemo, useEffect } from 'react'
import classNames from 'classnames'
import styles from './exchange-item.module.scss'
type ExchangeItemProps = {
stepValue: number;
maxValue: number;
title: string;
// onConfirm: (arg: number) => void;
onCancel: () => void;
visible: boolean;
}
const ExchangeItem = (props: ExchangeItemProps) => {
const { stepValue, maxValue, onCancel, visible, title } = props
const exchangeItem = useStoreActions(actions => actions.userModel.exchangeItem)
const [val, setVal] = useState(0)
const handleMinus = useCallback(() => {
if (val < 1) {
return
}
setVal(value => value - 1)
}, [val])
useEffect(() => {
if (visible) {
setVal(0)
}
}, [visible])
const handleAdd = useCallback(() => {
const next = val + 1
if (next * stepValue > maxValue) {
return showToast({ title: '学分不足~', icon: 'none' })
}
setVal(next)
}, [val, stepValue, maxValue])
const disableAdd = useMemo(() => {
return (val + 1) * stepValue > maxValue
}, [val, stepValue, maxValue])
const disableMinus = useMemo(() => {
return val < 1
}, [val])
const handleConfirm = () => {
if (val < 1) {
return showToast({ title: '兑换数量必须大于0', icon: 'none' })
}
exchangeItem({
name: title,
num: val
}).then(onCancel)
}
return (
<>
<View className={styles.input}>
<View>兑换:</View>
<View className={styles.input_main_wrap}>
<View className={styles.input_main}>
<View className={classNames(styles.btn, disableMinus && styles.disabled)} onClick={handleMinus}>
<Image src={require('../assets/minus.png')} className={styles.icon}></Image>
</View>
<View className={styles.num}>{val}</View>
<View className={classNames(styles.btn, styles.is_add, disableAdd && styles.disabled)} onClick={handleAdd}>
<Image src={require('../assets/add.png')} className={styles.icon}></Image>
</View>
</View>
<View className={styles.tips}>将消耗 <Text className={styles.tips_val}>{val * stepValue}</Text> 水晶</View>
</View>
<View>张</View>
</View>
<View className={styles.footer}>
<View className={styles.cancel} onClick={() => onCancel()}>取消</View>
<View className={classNames(styles.confirm, val < 1 && styles.disbaled)} onClick={handleConfirm}>确定</View>
</View>
</>
)
}
export default ExchangeItem
export default definePageConfig({ export default definePageConfig({
navigationBarTitleText: 'pk-math', navigationBarTitleText: '学分小店',
navigationStyle: 'custom' navigationStyle: 'default',
navigationBarBackgroundColor: '#2B3347',
navigationBarTextStyle: 'white'
}) })
@import '../../styles/mixins.scss';
.card-body {
display: flex;
padding: 32px 28px;
position: relative;
border-radius: 32px;
border: 6px solid #F0F2F9;
}
.card-stock {
color: #666;
font-size: 14PX;
text-align: center;
}
.card_list {
position: relative;
z-index: 1;
background: #FFFFFF;
border-radius: 40px 40px 0px 0px;
margin-top: -40px;
overflow: hidden;
padding: 60px 32px 40px 32px;
}
.head {
background-color: #2B3347;
padding: 34px 64px 94px 40px;
display: flex;
justify-content: space-between;
}
.moal_body {
width: 654px;
box-sizing: border-box;
}
.score_icon {
width: 112px;
height: 112px;
}
.score_text {
padding-top: 18px;
font-size: 64px;
font-weight: 500;
color: #FFFFFF;
line-height: 78px;
display: flex;
align-items: flex-end;
.unit {
font-size: 24px;
font-weight: 500;
color: rgba(255,255,255,0.5);
line-height: 34px;
margin-left: 8px;
}
}
@import '../../styles/mixins.scss';
.wrapper {
padding: 16px;
}
.card-body {
display: flex;
padding: 12PX;
position: relative;
border: 1px solid red;
.card-main {
@include flexLayout(column, _, space-between);
min-width: 160px;
margin-right: 12PX;
.card-image {
width: 100%;
height: 180px;
background-color: #000;
}
.card-stock {
color: #666;
font-size: 14PX;
text-align: center;
}
}
.card-action {
@include flexLayout(column, center, space-between);
flex-grow: 1;
.card-description {
flex-grow: 1;
@include flexLayout(column, _, space-between);
.card-title {
color: #333;
font-weight: 500;
margin-bottom: 8px;
}
.card-subtitle {
color: #666;
@include multi-ellipsis(2);
}
}
.card-footer {
width: 100%;
margin-top: 48px;
@include flexLayout(_, _, space-between);
.card-score {
@include flexLayout(_, center);
&-icon {
width: 32PX;
height: 32PX;
margin-right: 4px;
}
&-text {
color: #666;
margin-right: 8px;
}
}
.card-exchange {
@include btn();
}
}
}
}
\ No newline at end of file
import { useState } from 'react' /* eslint-disable import/no-commonjs */
import { View, Text, Button } from '@tarojs/components' import { useState, useCallback } from 'react'
import { getMenuButtonBoundingClientRect, showToast, showModal } from '@tarojs/taro' import { View, Text, Image } from '@tarojs/components'
import { useDidShow } from '@tarojs/taro'
import { useBoolean, useToggle, useRequest } from '@/hooks' import { useBoolean } from '@/hooks'
import { pointToCard } from '@/api/account'
import Modal from '@/components/Modal' import Modal from '@/components/Modal'
import { useStoreState, useStoreActions } from '@/store/hooks'
import MainHeader from '../main/components/header' import ExchangeItem from './components/exchange-item'
import Card from './components/card' import Card from './components/card'
import CardMain from './components/card-main' import CardMain from './components/card-main'
import { Demo } from './types' import styles from './index.module.scss'
import './index.scss'
const AnswerSheet = () => { type ExchangeGoods = {
return ( title: string;
<Text>答题卡</Text> subTitle: string;
) description: string;
price: number;
stoke: number | null;
img: string;
} }
const MysteryBox = () => { const ExchangeItems: ExchangeGoods[] = [
return ( {
<Text>MysteryBox</Text> title: '答题卡',
) subTitle: '答题卡',
} description: '答题卡是不断追求更高档位与排名的必备道具答题卡是不断追求更高档位与排名的必备道具',
price: 30,
stoke: null,
img: require('./assets/ph.png'),
},
const StudyMarket = () => { {
const [nav] = useState(getMenuButtonBoundingClientRect()) title: '月度神秘大礼',
const paddingTop = `${nav.height + nav.top + 5}px` subTitle: '神秘大礼',
description: '兑换神秘大礼,线下领取超级大礼',
price: 30,
stoke: 10,
img: require('./assets/gift.png'),
}
]
const StudyMarket = () => {
const [isVisible, { setTrue, setFalse }] = useBoolean(false) const [isVisible, { setTrue, setFalse }] = useBoolean(false)
const [state, { setDefault, setReverse }] = useToggle({ defaultValue: Demo.AnswerSheet, reverseValue: Demo.Mystery }) const [currentGoods, setCurrentGoods] = useState<ExchangeGoods | null>(null)
const point = useStoreState(state => state.userModel.point)
const fetchAccount = useStoreActions(actions => actions.userModel.fetchAccount)
const { loading } = useRequest(pointToCard, { useDidShow(() => {
immediate: false, fetchAccount()
onSuccess () {
showToast({ title: '成功' })
setFalse()
},
onError (error) {
showModal({ title: error.msg || error.message })
}
}) })
return ( const open = useCallback((goods: ExchangeGoods) => {
<View className="wrapper"> setCurrentGoods(goods)
<MainHeader />
<View style={{ paddingTop }}>
<Card hoverable title="答题卡">
<View className="card-body">
<CardMain
title="答题卡"
subtitle="答题卡是不断追求更高档位与排名的必备道具答题卡是不断追求更高档位与排名的必备道具"
onClick={() => {
setDefault()
setTrue() setTrue()
}} }, [setTrue])
/>
</View>
</Card>
<Card hoverable title="月度神秘大礼"> return (
<View className="card-body"> <>
<View className={styles.head}>
<View className={styles.score_text}>{point} <Text className={styles.unit}>水晶</Text></View>
<Image src={require('./assets/score.png')} className={styles.score_icon}></Image>
</View>
<View className={styles.card_list}>
{ExchangeItems.map(item => (
<Card title={item.title} key={item.title}>
<View className={styles['card-body']}>
<CardMain <CardMain
title="神秘大礼" title={item.subTitle}
subtitle="兑换神秘大礼,线下领取超级大礼" cover={item.img}
extra={<Text className="card-stock">本月库存10</Text>} stoke={item.stoke}
price={item.price}
disabled={item.stoke !== null && item.stoke < 1}
subtitle={item.description}
onClick={() => { onClick={() => {
setReverse() open(item)
setTrue()
}} }}
/> />
</View> </View>
</Card> </Card>
))}
</View> </View>
<Modal isVisible={isVisible} onClose={setFalse}> <Modal isVisible={isVisible} bodyClassName={styles.moal_body} onClose={setFalse} maskClosable={false} tilte={`兑换${currentGoods?.title ?? ''}`}>
{state} <ExchangeItem
<View> title={currentGoods?.title ?? ''}
{state === Demo.AnswerSheet && <AnswerSheet />} stepValue={currentGoods?.price ?? Infinity}
{state === Demo.Mystery && <MysteryBox /> } maxValue={point}
<Button size="mini" loading={loading} onClick={setTrue}>兑换</Button> visible={isVisible}
</View> onCancel={setFalse}
/>
</Modal> </Modal>
</View> </>
) )
} }
......
import { import {
action, Action, Thunk, thunk action, Action, Thunk, thunk, State
} from 'easy-peasy' } from 'easy-peasy'
import { getAccountInfo } from '@/api' import { getAccountInfo } from '@/api'
import type { UserInfo } from '@/types' import type { UserInfo } from '@/types'
import { pointToCard } from '@/api/account'
import { showModal, showToast, showLoading, hideLoading } from '@tarojs/taro'
import { getStorage, StorageKeys } from '@/utils/storage' import { getStorage, StorageKeys } from '@/utils/storage'
import { AppStoreModel } from '../store-model' import { AppStoreModel } from '../store-model'
...@@ -10,21 +12,42 @@ export interface UserModel { ...@@ -10,21 +12,42 @@ export interface UserModel {
userInfo: UserInfo; userInfo: UserInfo;
cardNum: number; cardNum: number;
point: number; point: number;
updateState: Action<UserModel, { cardNum: number; point: number }>; updateState: Action<UserModel, Partial<State<UserModel>>>;
fetchAccount: Thunk<UserModel, void, void, AppStoreModel>; fetchAccount: Thunk<UserModel, void, void, AppStoreModel>;
updateUserInfo: Action<UserModel, UserInfo>; updateUserInfo: Action<UserModel, UserInfo>;
exchangeItem: Thunk<UserModel, { name: string; num: number; }, void, AppStoreModel>;
} }
const userModel: UserModel = { const userModel: UserModel = {
userInfo: getStorage(StorageKeys.userInfo)!, userInfo: getStorage(StorageKeys.userInfo)!,
cardNum: 0, cardNum: 0,
point: 0, point: 0,
exchangeItem: thunk(async (actions, payload) => {
try {
showLoading({ mask: true, title: '兑换中...' })
const { data: list } = await pointToCard({ num: payload.num })
const cardNum = list?.find(c => c.accountType === 'CARD')?.amount ?? 0
const point = list?.find(c => c.accountType === 'POINT')?.amount ?? 0
actions.updateState({ cardNum, point })
hideLoading()
showToast({ title: '兑换成功', icon: 'success' })
} catch (err) {
hideLoading()
showModal({
title: '提示',
content: err.message,
showCancel: false
})
}
}),
updateUserInfo: action((state, user) => { updateUserInfo: action((state, user) => {
state.userInfo = user state.userInfo = user
}), }),
updateState: action((state, { cardNum, point }) => { updateState: action((state, payload) => {
state.cardNum = cardNum for (const key in payload) {
state.point = point state[key] = payload[key]
}
}), }),
fetchAccount: thunk(async (actions) => { fetchAccount: thunk(async (actions) => {
......
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