ListPoints.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * 购买代金券列表
  3. * @邠心vbe on 2024/04/09
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, RefreshControl, FlatList } from 'react-native';
  7. import ViewRedeem from './ViewRedeem';
  8. import apiVoucher from '../../api/apiVoucher';
  9. import { MyRefreshProps } from '../../components/ThemesConfig';
  10. import TextView from '../../components/TextView';
  11. import Button from '../../components/Button';
  12. import Dialog from '../../components/Dialog';
  13. import VoucherType from './VoucherType';
  14. import app from '../../../app.json';
  15. import VbeSkeleton from '../../components/VbeSkeleton';
  16. export default class ListPoints extends Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. loading: true,
  21. userInfo: {},
  22. dataList: [],
  23. voucherType: "",
  24. hasMore: true,
  25. refreshing: false,
  26. loadingList: ["","","",""]
  27. };
  28. }
  29. componentDidMount() {
  30. this.props.navigation.addListener('focus', () => {
  31. this.refreshUserInfo();
  32. this.getDataList();
  33. });
  34. }
  35. onRefresh() {
  36. this.setState({
  37. refreshing: true
  38. })
  39. this.refreshUserInfo();
  40. this.getDataList();
  41. }
  42. refreshUserInfo() {
  43. getUserInfo(info => {
  44. this.setState({
  45. userInfo: info
  46. });
  47. }, true);
  48. }
  49. getDataList(lastId="") {
  50. apiVoucher.getDealVouchers({
  51. lastVoucherId: lastId,
  52. voucherType: this.state.voucherType
  53. }).then(res => {
  54. if (res.data) {
  55. if (lastId) {
  56. if (res.data.length > 0) {
  57. const list = this.state.dataList;
  58. this.setState({
  59. dataList: list.concat(res.data)
  60. });
  61. } else {
  62. this.setState({
  63. hasMore: false
  64. })
  65. }
  66. } else {
  67. this.setState({
  68. dataList: res.data,
  69. hasMore: res.data.length >= 10
  70. });
  71. }
  72. } else {
  73. this.setState({
  74. dataList: []
  75. });
  76. }
  77. }).catch(err => {
  78. toastShort(err)
  79. }).finally(() => {
  80. this.setState({
  81. loading: false,
  82. refreshing: false
  83. });
  84. });
  85. }
  86. getDataListPage() {
  87. if (this.state.dataList.length > 0 && this.state.hasMore) {
  88. const last = this.state.dataList[this.state.dataList.length-1]
  89. this.getDataList(last.voucherId);
  90. }
  91. }
  92. confirmPurchase(item) {
  93. let msg = "" + $t("voucher.confirmPurchase");
  94. if (item.purchasePoints) {
  95. msg = msg.replace("{value}", item.purchasePoints);
  96. } else {
  97. msg = msg.replace("{value}", "0");
  98. }
  99. Dialog.showDialog({
  100. title: $t("voucher.purchaseVoucher"),
  101. message: msg,
  102. ok: $t("nav.confirm"),
  103. callback: btn => {
  104. if (btn == Dialog.BUTTON_OK) {
  105. this.onPurchase(item)
  106. }
  107. }
  108. })
  109. }
  110. onPurchase(item) {
  111. Dialog.showProgressDialog();
  112. apiVoucher.purchaseVoucher({
  113. voucherId: item.voucherId
  114. }).then(res => {
  115. Dialog.dismissLoading();
  116. if (res.msg) {
  117. setTimeout(() => {
  118. Dialog.showDialog({
  119. title: $t("voucher.purchaseVoucher"),
  120. message: res.msg,
  121. showCancel: false
  122. })
  123. }, 500);
  124. }
  125. setTimeout(() => {
  126. this.refreshUserInfo();
  127. }, 2000)
  128. }).catch(err => {
  129. Dialog.dismissLoading();
  130. if (err.err) {
  131. setTimeout(() => {
  132. Dialog.showDialog({
  133. title: $t("voucher.vouchers"),
  134. message: err.err,
  135. showCancel: false
  136. })
  137. }, 500);
  138. }
  139. })
  140. }
  141. changeType(type) {
  142. this.setState({
  143. voucherType: type
  144. });
  145. this.getDataList();
  146. }
  147. listItem = ({item, index, separators}) => {
  148. return (
  149. <View
  150. style={styles.itemView}>
  151. <View style={styles.itemBox}></View>
  152. <View style={styles.itemContent}>
  153. <TextView style={styles.voucherTitle} numberOfLines={1}>{item.voucherName}</TextView>
  154. <TextView style={styles.voucherDesc} numberOfLines={2}>{item.voucherDesc}</TextView>
  155. <TextView style={styles.expireDate} numberOfLines={1}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  156. { !app.isLumiWhitelabel && <>
  157. <View style={styles.rightDash}></View>
  158. <View style={styles.topTikDot}></View>
  159. <View style={styles.bottomTikDot}></View>
  160. </> }
  161. </View>
  162. { item.purchasePoints > 0
  163. ? <Button
  164. viewStyle={styles.purchaseButton}
  165. borderRadius={6}
  166. onClick={() => this.confirmPurchase(item)}>
  167. <TextView
  168. style={styles.getForText}>
  169. {$t("voucher.btnGetFor")}
  170. </TextView>
  171. <TextView
  172. style={styles.getValueText}>
  173. {item.purchasePoints}
  174. {$t("voucher.btnPoints")}
  175. </TextView>
  176. </Button>
  177. : <Button
  178. style={styles.claimeButton}
  179. viewStyle={styles.claimeButtonView}
  180. textStyle={styles.claimeButtonText}
  181. text={$t("voucher.btnClaimed")}
  182. onClick={() => this.onPurchase(item)}/>
  183. }
  184. </View>
  185. )
  186. }
  187. topView = (props) => {
  188. return (
  189. <View>
  190. <ViewRedeem
  191. userInfo={this.state.userInfo}/>
  192. <VoucherType
  193. type={this.state.voucherType}
  194. onChange={type => this.changeType(type)}
  195. />
  196. </View>
  197. )
  198. }
  199. bottomView = () => {
  200. if (this.state.dataList.length > 0 && !this.state.hasMore) {
  201. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  202. } else {
  203. return null
  204. }
  205. }
  206. render() {
  207. if (this.state.loading) {
  208. return (
  209. <View style={styles.container}>
  210. <VbeSkeleton
  211. style={{}}
  212. layout={[
  213. {width: '100%', height: 42, borderRadius: 10},
  214. {width: '40%', height: 18, marginTop: 16}
  215. ]}
  216. animationDirection={"horizontalRight"}
  217. />
  218. <EndView/>
  219. { this.state.loadingList.map((item, index) =>
  220. <View style={styles.loadingView} key={index}>
  221. <VbeSkeleton
  222. style={ui.flex1}
  223. layout={[
  224. {width: '50%', height: 18},
  225. {width: '90%', height: 12, marginTop: 8},
  226. {width: '40%', height: 12, marginTop: 4}
  227. ]}
  228. animationDirection={"horizontalRight"}
  229. />
  230. <VbeSkeleton
  231. style={{width: 56}}
  232. layout={[
  233. {width: 56, height: 30, borderRadius: 30}
  234. ]}
  235. animationDirection={"horizontalRight"}
  236. />
  237. </View>
  238. )}
  239. </View>
  240. )
  241. }
  242. return (
  243. <FlatList
  244. style={styles.container}
  245. data={this.state.dataList}
  246. renderItem={this.listItem}
  247. ListHeaderComponent={this.topView}
  248. keyExtractor={item => item.voucherId}
  249. onEndReached={() => this.getDataListPage()}
  250. onEndReachedThreshold={0.3}
  251. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  252. ListFooterComponent={this.bottomView}
  253. refreshControl={
  254. <RefreshControl
  255. {...MyRefreshProps()}
  256. refreshing={this.state.refreshing}
  257. onRefresh={() => this.onRefresh()}
  258. />
  259. }/>
  260. );
  261. }
  262. }
  263. const styles = StyleSheet.create({
  264. container: {
  265. flex: 1,
  266. padding: 16
  267. },
  268. itemView: {
  269. marginTop: 16,
  270. borderRadius: 4,
  271. alignItems: 'center',
  272. flexDirection: 'row',
  273. backgroundColor: colorLight,
  274. ...$padding(0, 20, 0, 16)
  275. },
  276. itemBox: {
  277. top: 0,
  278. left: 0,
  279. right: 0,
  280. bottom: 0,
  281. borderWidth: 1,
  282. borderColor: '#DADADA',
  283. borderRadius: 4,
  284. position: 'absolute'
  285. },
  286. itemContent: {
  287. flex: 1,
  288. marginRight: 16,
  289. paddingTop: 8,
  290. paddingRight: 16,
  291. paddingBottom: 8,
  292. overflow: 'hidden'
  293. },
  294. rightDash: {
  295. top: 0,
  296. right: 6,
  297. bottom: 0,
  298. position: 'absolute',
  299. borderStyle: 'dashed',
  300. borderRightWidth: 1,
  301. borderRightColor: '#DADADA'
  302. },
  303. topTikDot: {
  304. top: -8,
  305. right: 0,
  306. width: 13,
  307. height: 14,
  308. borderWidth: 1,
  309. borderColor: '#DADADA',
  310. borderRadius: 30,
  311. position: 'absolute',
  312. backgroundColor: pageBackground
  313. },
  314. bottomTikDot: {
  315. bottom: -8,
  316. right: 0,
  317. width: 13,
  318. height: 14,
  319. borderWidth: 1,
  320. borderColor: '#DADADA',
  321. borderRadius: 30,
  322. position: 'absolute',
  323. backgroundColor: pageBackground
  324. },
  325. voucherTitle: {
  326. color: textPrimary,
  327. fontSize: 16,
  328. fontWeight: 'bold'
  329. },
  330. voucherDesc: {
  331. color: textPrimary,
  332. fontSize: 12,
  333. paddingTop: 2,
  334. paddingBottom: 4
  335. },
  336. expireDate: {
  337. color: textCancel,
  338. fontSize: 12
  339. },
  340. purchaseButton: {
  341. padding: 4,
  342. minWidth: 75,
  343. marginRight: 4,
  344. alignItems: 'center'
  345. },
  346. claimeButton: {
  347. borderWidth: 1,
  348. borderColor: colorAccent,
  349. borderRadius: 6,
  350. backgroundColor: colorLight
  351. },
  352. claimeButtonView: {
  353. padding: 8,
  354. minWidth: 73,
  355. alignItems: 'center'
  356. },
  357. claimeButtonText: {
  358. color: colorAccent,
  359. fontSize: 12,
  360. fontWeight: 'bold'
  361. },
  362. getForText: {
  363. color: textLight,
  364. fontSize: 12,
  365. fontWeight: 'bold'
  366. },
  367. getValueText: {
  368. color: textLight,
  369. fontSize: 10
  370. },
  371. noData: {
  372. color: textPlacehoder,
  373. fontSize: 14,
  374. padding: 20,
  375. marginTop: 16,
  376. textAlign: 'center'
  377. },
  378. noMore: {
  379. color: textPlacehoder,
  380. fontSize: 14,
  381. padding: 16,
  382. marginBottom: 16,
  383. textAlign: 'center'
  384. },
  385. loadingView: {
  386. paddingTop: 16,
  387. paddingBottom: 16,
  388. alignItems: 'center',
  389. flexDirection: 'row'
  390. }
  391. })