ListPoints.js 8.1 KB

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