index.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. import { Image } from 'expo-image';
  2. import { useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import {
  5. Alert,
  6. ImageBackground,
  7. ScrollView,
  8. StatusBar,
  9. StyleSheet,
  10. Text,
  11. TouchableOpacity,
  12. View,
  13. } from 'react-native';
  14. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  15. import { Images } from '@/constants/images';
  16. import services from '@/services/api';
  17. interface DayInfo {
  18. text: string;
  19. date: string;
  20. isFutureDate: boolean;
  21. istoday: boolean;
  22. isSignIn: boolean;
  23. }
  24. interface RecordItem {
  25. id: string;
  26. credit: number;
  27. createTime: string;
  28. }
  29. export default function IntegralScreen() {
  30. const router = useRouter();
  31. const insets = useSafeAreaInsets();
  32. const [integral, setIntegral] = useState(0);
  33. const [daysIntegral, setDaysIntegral] = useState(0);
  34. const [istodaySignIn, setIstodaySignIn] = useState(false);
  35. const [record, setRecord] = useState<RecordItem[]>([]);
  36. const [dataInfo, setDataInfo] = useState<DayInfo[]>([]);
  37. const [todayIntegral, setTodayIntegral] = useState(1);
  38. const [tomorrowIntegral, setTomorrowIntegral] = useState(1);
  39. const [isTooltip, setIsTooltip] = useState(false);
  40. // 格式化数字为两位
  41. const padWithZeros = (number: number, length: number) => {
  42. return String(number).padStart(length, '0');
  43. };
  44. // 设置日期数据
  45. const setDate = useCallback(() => {
  46. const now = new Date();
  47. const dataInfoArr: DayInfo[] = [];
  48. // 前4天
  49. for (let i = -4; i <= 0; i++) {
  50. const date = new Date(now);
  51. date.setDate(date.getDate() + i);
  52. const m = date.getMonth() + 1;
  53. const d = date.getDate();
  54. const y = date.getFullYear();
  55. dataInfoArr.push({
  56. text: `${m}.${d}`,
  57. date: `${y}-${padWithZeros(m, 2)}-${padWithZeros(d, 2)}`,
  58. isFutureDate: false,
  59. istoday: i === 0,
  60. isSignIn: false,
  61. });
  62. }
  63. // 明天
  64. const tomorrow = new Date(now);
  65. tomorrow.setDate(tomorrow.getDate() + 1);
  66. const tm = tomorrow.getMonth() + 1;
  67. const td = tomorrow.getDate();
  68. const ty = tomorrow.getFullYear();
  69. dataInfoArr.push({
  70. text: `${tm}.${td}`,
  71. date: `${ty}-${padWithZeros(tm, 2)}-${padWithZeros(td, 2)}`,
  72. isFutureDate: true,
  73. istoday: false,
  74. isSignIn: false,
  75. });
  76. setDataInfo(dataInfoArr);
  77. return dataInfoArr;
  78. }, []);
  79. // 获取积分信息
  80. const getInfo = useCallback(async () => {
  81. try {
  82. // Use wallet service info instead of user service getWalletInfo
  83. const info = await services.wallet.info('USER_CREDIT');
  84. setIntegral(info?.balance || 0);
  85. } catch (error) {
  86. console.error('获取积分失败:', error);
  87. }
  88. }, []);
  89. // 获取积分规则
  90. const showRule = async () => {
  91. try {
  92. const res = await services.user.getParamConfig('credit_sign');
  93. if (res && res.data) {
  94. Alert.alert('积分规则', res.data.replace(/<[^>]+>/g, '')); // Strip HTML tags for Alert
  95. }
  96. } catch (error) {
  97. console.error('获取规则失败:', error);
  98. }
  99. };
  100. // 获取签到记录
  101. const getData = useCallback(async (dateInfo: DayInfo[]) => {
  102. try {
  103. const fourDaysAgo = new Date();
  104. fourDaysAgo.setDate(fourDaysAgo.getDate() - 4);
  105. const m = fourDaysAgo.getMonth() + 1;
  106. const d = fourDaysAgo.getDate();
  107. const y = fourDaysAgo.getFullYear();
  108. const param = {
  109. createTime: `${y}-${padWithZeros(m, 2)}-${padWithZeros(d, 2)}`,
  110. };
  111. const res = await services.user.getCreditRecord(param);
  112. const recordData = res?.data || [];
  113. setRecord(recordData);
  114. // 今天日期
  115. const now = new Date();
  116. const todayStr = `${now.getFullYear()}-${padWithZeros(now.getMonth() + 1, 2)}-${padWithZeros(now.getDate(), 2)}`;
  117. // 更新签到状态
  118. const updatedDataInfo = dateInfo.map(item => {
  119. const newItem = { ...item };
  120. // 检查是否已签到
  121. for (const rec of recordData) {
  122. const createTime = rec.createTime?.slice(0, 10);
  123. if (createTime === item.date) {
  124. newItem.isSignIn = true;
  125. if (item.date === todayStr) {
  126. setIstodaySignIn(true);
  127. }
  128. }
  129. }
  130. return newItem;
  131. });
  132. setDataInfo(updatedDataInfo);
  133. // 计算签到积分
  134. let total = 0;
  135. for (const rec of recordData) {
  136. if (rec.credit > 0) {
  137. total += rec.credit;
  138. }
  139. }
  140. setDaysIntegral(total);
  141. // 计算今天和明天的积分
  142. if (recordData.length > 0) {
  143. const lastCredit = recordData[0]?.credit || 1;
  144. const yesterday = new Date();
  145. yesterday.setDate(yesterday.getDate() - 1);
  146. const yesterdayStr = `${yesterday.getFullYear()}-${padWithZeros(yesterday.getMonth() + 1, 2)}-${padWithZeros(yesterday.getDate(), 2)}`;
  147. const lastRecordDate = recordData[0]?.createTime?.slice(0, 10);
  148. if (yesterdayStr === lastRecordDate) {
  149. setTodayIntegral(lastCredit + 1);
  150. setTomorrowIntegral(lastCredit + 2);
  151. } else {
  152. setTodayIntegral(lastCredit);
  153. setTomorrowIntegral(Math.min(lastCredit + 1, 7));
  154. }
  155. }
  156. } catch (error) {
  157. console.error('获取签到记录失败:', error);
  158. }
  159. }, []);
  160. // 初始化
  161. useEffect(() => {
  162. const dateInfo = setDate();
  163. getInfo();
  164. getData(dateInfo);
  165. }, [setDate, getInfo, getData]);
  166. // 签到
  167. const handleSignIn = async () => {
  168. if (istodaySignIn) {
  169. Alert.alert('提示', '今天已签到');
  170. return;
  171. }
  172. try {
  173. const res = await services.user.signIn();
  174. if (res?.code === 0) {
  175. Alert.alert('提示', '签到成功');
  176. setIstodaySignIn(true);
  177. const dateInfo = setDate();
  178. getInfo();
  179. getData(dateInfo);
  180. } else {
  181. Alert.alert('提示', res?.msg || '签到失败');
  182. }
  183. } catch (error) {
  184. console.error('签到失败:', error);
  185. Alert.alert('提示', '签到失败');
  186. }
  187. };
  188. const handleBack = () => {
  189. router.back();
  190. };
  191. return (
  192. <View style={styles.container}>
  193. <StatusBar barStyle="dark-content" />
  194. {/* 顶部导航 */}
  195. <View style={[styles.header, { paddingTop: insets.top }]}>
  196. <TouchableOpacity style={styles.backBtn} onPress={handleBack}>
  197. <Text style={styles.backIcon}>‹</Text>
  198. </TouchableOpacity>
  199. <Text style={styles.title}>我的积分</Text>
  200. <View style={styles.placeholder} />
  201. </View>
  202. <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
  203. {/* 头部积分展示 */}
  204. <ImageBackground
  205. source={{ uri: Images.integral?.head || Images.common.commonBg }}
  206. style={styles.headerBg}
  207. resizeMode="cover"
  208. >
  209. <Text style={styles.integralNum}>{integral}</Text>
  210. <TouchableOpacity
  211. style={styles.allBox}
  212. onPress={() => setIsTooltip(!isTooltip)}
  213. >
  214. <Text style={styles.allText}>所有积分</Text>
  215. <Image
  216. source={{ uri: Images.integral?.greetings }}
  217. style={styles.infoIcon}
  218. contentFit="contain"
  219. />
  220. {isTooltip && (
  221. <ImageBackground
  222. source={{uri: Images.integral?.tooltip }}
  223. style={styles.tooltip}
  224. resizeMode="stretch"
  225. >
  226. <Text style={styles.tooltipText}>消费积分与签到积分总和</Text>
  227. </ImageBackground>
  228. )}
  229. </TouchableOpacity>
  230. <View style={styles.todayBox}>
  231. <Text style={styles.todayText}>
  232. 今日已获得<Text style={styles.todayNum}>{istodaySignIn ? todayIntegral : 0}</Text>积分
  233. </Text>
  234. </View>
  235. <TouchableOpacity style={styles.ruleBtn} onPress={showRule}>
  236. <Text style={styles.ruleText}>积分规则</Text>
  237. </TouchableOpacity>
  238. </ImageBackground>
  239. <View style={styles.content}>
  240. {/* 签到面板 */}
  241. <View style={styles.panel}>
  242. <View style={styles.panelTitle}>
  243. <View style={styles.panelTitleLeft}>
  244. <Image
  245. source={{ uri: Images.integral?.goldCoins }}
  246. style={styles.goldIcon}
  247. contentFit="contain"
  248. />
  249. <Text style={styles.panelTitleText}>签到领积分</Text>
  250. </View>
  251. <Text style={[styles.panelTitleRight, istodaySignIn && styles.signedText]}>
  252. {istodaySignIn ? '今日已签到' : '今日未签到'}
  253. </Text>
  254. </View>
  255. <Text style={styles.explain}>
  256. 已签到{record.filter(r => r.credit > 0).length}天,共获得<Text style={styles.highlightText}>{daysIntegral}</Text>积分
  257. </Text>
  258. {/* 签到进度 */}
  259. <View style={styles.progressBox}>
  260. <View style={styles.lineBox}>
  261. {[1, 2, 3, 4, 5].map((_, index) => (
  262. <View key={index} style={styles.lineSection}>
  263. <View style={[styles.line, styles.lineOn]} />
  264. </View>
  265. ))}
  266. </View>
  267. <View style={styles.daysBox}>
  268. {dataInfo.map((item, index) => (
  269. <View key={index} style={styles.dayItem}>
  270. <TouchableOpacity
  271. style={styles.dayCircleBox}
  272. onPress={item.istoday && !istodaySignIn ? handleSignIn : undefined}
  273. activeOpacity={item.istoday && !istodaySignIn ? 0.7 : 1}
  274. >
  275. {item.istoday && !istodaySignIn ? (
  276. <ImageBackground
  277. source={{ uri: Images.integral?.basisBg }}
  278. style={styles.dayCircleBg}
  279. resizeMode="contain"
  280. >
  281. <View style={[styles.dayCircle, styles.todayCircle]}>
  282. <Text style={styles.todayCircleText}>+{todayIntegral}</Text>
  283. </View>
  284. </ImageBackground>
  285. ) : item.isFutureDate ? (
  286. <View style={[styles.dayCircle, styles.futureCircle]}>
  287. <Text style={styles.futureText}>+{tomorrowIntegral}</Text>
  288. </View>
  289. ) : item.isSignIn ? (
  290. <View style={[styles.dayCircle, styles.signedCircle]}>
  291. <Text style={styles.checkIcon}>✓</Text>
  292. </View>
  293. ) : (
  294. <View style={[styles.dayCircle, styles.missedCircle]}>
  295. <Text style={styles.missedIcon}>✗</Text>
  296. </View>
  297. )}
  298. </TouchableOpacity>
  299. <Text style={[styles.dayText, item.istoday && styles.todayDayText]}>
  300. {item.istoday ? '今天' : item.text}
  301. </Text>
  302. </View>
  303. ))}
  304. </View>
  305. </View>
  306. </View>
  307. {/* 积分明细 */}
  308. <View style={styles.listSection}>
  309. <Text style={styles.listTitle}>积分明细</Text>
  310. {record.map((item, index) => (
  311. <View key={item.id || index} style={styles.listItem}>
  312. <View style={styles.listItemLeft}>
  313. <Text style={styles.listItemTitle}>
  314. {item.credit > 0 ? '积分签到获得' : '大转盘消费'}
  315. </Text>
  316. <Text style={styles.listItemTime}>{item.createTime}</Text>
  317. </View>
  318. <Text style={[
  319. styles.listItemCredit,
  320. item.credit > 0 ? styles.creditPositive : styles.creditNegative
  321. ]}>
  322. {item.credit > 0 ? '+' : ''}{item.credit}
  323. </Text>
  324. </View>
  325. ))}
  326. {record.length === 0 && (
  327. <View style={styles.emptyBox}>
  328. <Text style={styles.emptyText}>暂无积分记录</Text>
  329. </View>
  330. )}
  331. </View>
  332. </View>
  333. </ScrollView>
  334. </View>
  335. );
  336. }
  337. const styles = StyleSheet.create({
  338. container: {
  339. flex: 1,
  340. backgroundColor: '#F8FAFB',
  341. },
  342. header: {
  343. flexDirection: 'row',
  344. alignItems: 'center',
  345. justifyContent: 'space-between',
  346. paddingHorizontal: 10,
  347. height: 80,
  348. backgroundColor: 'transparent',
  349. position: 'absolute',
  350. top: 0,
  351. left: 0,
  352. right: 0,
  353. zIndex: 100,
  354. },
  355. backBtn: {
  356. width: 40,
  357. height: 40,
  358. justifyContent: 'center',
  359. alignItems: 'center',
  360. },
  361. backIcon: {
  362. fontSize: 32,
  363. color: '#000',
  364. fontWeight: 'bold',
  365. },
  366. title: {
  367. fontSize: 15,
  368. fontWeight: 'bold',
  369. color: '#000',
  370. },
  371. placeholder: {
  372. width: 40,
  373. },
  374. scrollView: {
  375. flex: 1,
  376. },
  377. headerBg: {
  378. width: '100%',
  379. height: 283,
  380. paddingTop: 100,
  381. alignItems: 'center',
  382. position: 'relative',
  383. },
  384. integralNum: {
  385. fontSize: 48,
  386. fontWeight: '400',
  387. color: '#5B460F',
  388. textAlign: 'center',
  389. },
  390. allBox: {
  391. flexDirection: 'row',
  392. alignItems: 'center',
  393. marginTop: 7,
  394. marginBottom: 12,
  395. position: 'relative',
  396. },
  397. allText: {
  398. fontSize: 12,
  399. color: '#C8B177',
  400. },
  401. infoIcon: {
  402. width: 16,
  403. height: 16,
  404. marginLeft: 4,
  405. },
  406. tooltip: {
  407. position: 'absolute',
  408. width: 150,
  409. height: 27,
  410. top: 15,
  411. left: 30, // Adjust execution based on design
  412. justifyContent: 'center',
  413. alignItems: 'center',
  414. zIndex: 999,
  415. paddingHorizontal: 5,
  416. },
  417. tooltipText: {
  418. color: '#fff',
  419. fontSize: 10,
  420. },
  421. todayBox: {
  422. backgroundColor: 'rgba(0,0,0,0.08)',
  423. borderRadius: 217,
  424. paddingHorizontal: 15,
  425. paddingVertical: 5,
  426. },
  427. todayText: {
  428. fontSize: 12,
  429. color: '#8A794F',
  430. },
  431. todayNum: {
  432. fontWeight: '800',
  433. color: '#5B460F',
  434. },
  435. ruleBtn: {
  436. position: 'absolute',
  437. right: 0,
  438. top: 130, // Half of 260rpx approx
  439. backgroundColor: '#fff',
  440. borderTopLeftRadius: 20,
  441. borderBottomLeftRadius: 20,
  442. paddingHorizontal: 10,
  443. paddingVertical: 6,
  444. zIndex: 10,
  445. },
  446. ruleText: {
  447. fontSize: 12,
  448. color: '#333',
  449. },
  450. content: {
  451. paddingHorizontal: 10,
  452. marginTop: -50,
  453. },
  454. panel: {
  455. backgroundColor: '#fff',
  456. borderRadius: 15,
  457. padding: 12,
  458. },
  459. panelTitle: {
  460. flexDirection: 'row',
  461. justifyContent: 'space-between',
  462. alignItems: 'center',
  463. paddingHorizontal: 10,
  464. },
  465. panelTitleLeft: {
  466. flexDirection: 'row',
  467. alignItems: 'center',
  468. },
  469. goldIcon: {
  470. width: 24,
  471. height: 24,
  472. marginRight: 6,
  473. },
  474. panelTitleText: {
  475. fontSize: 16,
  476. fontWeight: '700',
  477. color: '#3D3D3D',
  478. },
  479. panelTitleRight: {
  480. fontSize: 12,
  481. color: '#FC7D2E',
  482. },
  483. signedText: {
  484. color: '#999',
  485. },
  486. explain: {
  487. fontSize: 12,
  488. color: '#999',
  489. paddingHorizontal: 10,
  490. marginTop: 7,
  491. marginBottom: 11,
  492. },
  493. highlightText: {
  494. color: '#FC7D2E',
  495. },
  496. progressBox: {
  497. paddingTop: 20,
  498. },
  499. lineBox: {
  500. flexDirection: 'row',
  501. paddingHorizontal: 10,
  502. },
  503. lineSection: {
  504. flex: 1,
  505. },
  506. line: {
  507. height: 1,
  508. backgroundColor: '#9E9E9E',
  509. },
  510. lineOn: {
  511. backgroundColor: '#FC7D2E',
  512. },
  513. daysBox: {
  514. flexDirection: 'row',
  515. marginTop: -25,
  516. },
  517. dayItem: {
  518. flex: 1,
  519. alignItems: 'center',
  520. },
  521. dayCircleBox: {
  522. width: 46,
  523. height: 46,
  524. justifyContent: 'center',
  525. alignItems: 'center',
  526. marginBottom: 3,
  527. },
  528. dayCircleBg: {
  529. width: 46,
  530. height: 46,
  531. justifyContent: 'center',
  532. alignItems: 'center',
  533. },
  534. dayCircle: {
  535. width: 32,
  536. height: 32,
  537. borderRadius: 16,
  538. justifyContent: 'center',
  539. alignItems: 'center',
  540. },
  541. todayCircle: {
  542. backgroundColor: '#FC7D2E',
  543. },
  544. todayCircleText: {
  545. color: '#fff',
  546. fontSize: 12,
  547. fontWeight: 'bold',
  548. },
  549. futureCircle: {
  550. backgroundColor: '#F4F6F8',
  551. borderWidth: 1,
  552. borderColor: 'rgba(226,226,226,0.5)',
  553. },
  554. futureText: {
  555. color: '#505050',
  556. fontSize: 12,
  557. },
  558. signedCircle: {
  559. backgroundColor: '#FFE7C4',
  560. },
  561. checkIcon: {
  562. color: '#FC7D2E',
  563. fontSize: 16,
  564. fontWeight: 'bold',
  565. },
  566. missedCircle: {
  567. backgroundColor: '#F4F6F8',
  568. },
  569. missedIcon: {
  570. color: '#999',
  571. fontSize: 16,
  572. },
  573. dayText: {
  574. fontSize: 12,
  575. color: '#9E9E9E',
  576. },
  577. todayDayText: {
  578. color: '#FC7D2E',
  579. },
  580. listSection: {
  581. backgroundColor: '#fff',
  582. borderRadius: 15,
  583. padding: 12,
  584. marginTop: 10,
  585. marginBottom: 30,
  586. },
  587. listTitle: {
  588. fontSize: 16,
  589. fontWeight: '700',
  590. color: '#3D3D3D',
  591. marginBottom: 10,
  592. },
  593. listItem: {
  594. flexDirection: 'row',
  595. justifyContent: 'space-between',
  596. alignItems: 'center',
  597. paddingVertical: 14,
  598. borderBottomWidth: 1,
  599. borderBottomColor: 'rgba(0,0,0,0.05)',
  600. },
  601. listItemLeft: {},
  602. listItemTitle: {
  603. fontSize: 14,
  604. color: '#333',
  605. marginBottom: 3,
  606. },
  607. listItemTime: {
  608. fontSize: 12,
  609. color: '#999',
  610. },
  611. listItemCredit: {
  612. fontSize: 18,
  613. fontWeight: '700',
  614. },
  615. creditPositive: {
  616. color: '#588CFF',
  617. },
  618. creditNegative: {
  619. color: '#FC7D2E',
  620. },
  621. emptyBox: {
  622. paddingVertical: 50,
  623. alignItems: 'center',
  624. },
  625. emptyText: {
  626. fontSize: 14,
  627. color: '#999',
  628. },
  629. });