ListPoints.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. export default class ListPoints extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. userInfo: {},
  19. dataList: [],
  20. voucherType: "",
  21. hasMore: true,
  22. refreshing: false
  23. };
  24. }
  25. componentDidMount() {
  26. this.props.navigation.addListener('focus', () => {
  27. this.refreshUserInfo();
  28. this.getDataList();
  29. });
  30. }
  31. onRefresh() {
  32. this.setState({
  33. refreshing: true
  34. })
  35. this.refreshUserInfo();
  36. this.getDataList();
  37. }
  38. refreshUserInfo() {
  39. getUserInfo(info => {
  40. this.setState({
  41. userInfo: info
  42. });
  43. }, true);
  44. }
  45. getDataList(lastId="") {
  46. apiVoucher.getDealVouchers({
  47. lastVoucherId: lastId,
  48. voucherType: this.state.voucherType
  49. }).then(res => {
  50. if (res.data) {
  51. if (lastId) {
  52. if (res.data.length > 0) {
  53. const list = this.state.dataList;
  54. this.setState({
  55. dataList: list.concat(res.data)
  56. });
  57. } else {
  58. this.setState({
  59. hasMore: false
  60. })
  61. }
  62. } else {
  63. this.setState({
  64. dataList: res.data,
  65. hasMore: true
  66. });
  67. }
  68. } else {
  69. this.setState({
  70. dataList: []
  71. });
  72. }
  73. }).catch(err => {
  74. toastShort(err)
  75. }).finally(() => {
  76. this.setState({
  77. refreshing: false
  78. });
  79. });
  80. }
  81. getDataListPage() {
  82. if (this.state.dataList.length > 0 && this.state.hasMore) {
  83. const last = this.state.dataList[this.state.dataList.length-1]
  84. this.getDataList(last.voucherId);
  85. }
  86. }
  87. onPurchase(item) {
  88. Dialog.showProgressDialog();
  89. apiVoucher.purchaseVoucher({
  90. voucherId: item.voucherId
  91. }).then(res => {
  92. Dialog.dismissLoading();
  93. if (res.msg) {
  94. setTimeout(() => {
  95. Dialog.showDialog({
  96. title: $t("voucher.vouchers"),
  97. message: res.msg,
  98. showCancel: false
  99. })
  100. }, 500);
  101. }
  102. }).catch(err => {
  103. Dialog.dismissLoading();
  104. if (err.err) {
  105. setTimeout(() => {
  106. Dialog.showDialog({
  107. title: $t("common.error"),
  108. message: err.err,
  109. showCancel: false
  110. })
  111. }, 500);
  112. }
  113. })
  114. }
  115. changeType(type) {
  116. this.setState({
  117. voucherType: type
  118. });
  119. this.getDataList();
  120. }
  121. listItem = ({item, index, separators}) => {
  122. return (
  123. <View
  124. style={styles.itemView}>
  125. <View style={styles.itemContent}>
  126. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  127. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  128. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  129. </View>
  130. { item.purchasePoints > 0
  131. ? <Button
  132. viewStyle={styles.purchaseButton}
  133. borderRadius={6}
  134. onClick={() => this.onPurchase(item)}>
  135. <TextView
  136. style={styles.getForText}>
  137. {$t("voucher.btnGetFor")}
  138. </TextView>
  139. <TextView
  140. style={styles.getValueText}>
  141. {item.purchasePoints}
  142. {$t("voucher.btnPoints")}
  143. </TextView>
  144. </Button>
  145. : <Button
  146. style={styles.claimeButton}
  147. viewStyle={styles.claimeButtonView}
  148. textStyle={styles.claimeButtonText}
  149. text={$t("voucher.btnClaimed")}
  150. onClick={() => this.onPurchase(item)}/>
  151. }
  152. </View>
  153. )
  154. }
  155. topView = (props) => {
  156. return (
  157. <View>
  158. <ViewRedeem
  159. userInfo={this.state.userInfo}/>
  160. <VoucherType
  161. type={this.state.voucherType}
  162. onChange={type => this.changeType(type)}
  163. />
  164. </View>
  165. )
  166. }
  167. bottomView = () => {
  168. if (!this.state.hasMore) {
  169. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  170. } else {
  171. return null
  172. }
  173. }
  174. render() {
  175. return (
  176. <FlatList
  177. style={styles.container}
  178. data={this.state.dataList}
  179. renderItem={this.listItem}
  180. ListHeaderComponent={this.topView}
  181. keyExtractor={item => item.voucherId}
  182. onEndReached={() => this.getDataListPage()}
  183. onEndReachedThreshold={0.3}
  184. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  185. refreshControl={
  186. <RefreshControl
  187. {...MyRefreshProps()}
  188. refreshing={this.state.refreshing}
  189. onRefresh={() => this.onRefresh()}
  190. />
  191. }/>
  192. );
  193. }
  194. }
  195. const styles = StyleSheet.create({
  196. container: {
  197. flex: 1,
  198. padding: 16
  199. },
  200. itemView: {
  201. padding: 16,
  202. marginTop: 16,
  203. borderWidth: 1,
  204. borderColor: '#DADADA',
  205. borderRadius: 4,
  206. alignItems: 'center',
  207. flexDirection: 'row',
  208. backgroundColor: colorLight
  209. },
  210. itemContent: {
  211. flex: 1,
  212. paddingRight: 16
  213. },
  214. voucherTitle: {
  215. color: textPrimary,
  216. fontSize: 16,
  217. fontWeight: 'bold'
  218. },
  219. voucherDesc: {
  220. color: textPrimary,
  221. fontSize: 14,
  222. paddingTop: 2,
  223. paddingBottom: 4
  224. },
  225. expireDate: {
  226. color: textCancel,
  227. fontSize: 12
  228. },
  229. purchaseButton: {
  230. padding: 4,
  231. minWidth: 75,
  232. alignItems: 'center'
  233. },
  234. claimeButton: {
  235. borderWidth: 1,
  236. borderColor: colorAccent,
  237. borderRadius: 6,
  238. backgroundColor: colorLight
  239. },
  240. claimeButtonView: {
  241. padding: 8,
  242. minWidth: 73,
  243. alignItems: 'center'
  244. },
  245. claimeButtonText: {
  246. color: colorAccent,
  247. fontSize: 12,
  248. fontWeight: 'bold'
  249. },
  250. getForText: {
  251. color: textLight,
  252. fontSize: 12,
  253. fontWeight: 'bold'
  254. },
  255. getValueText: {
  256. color: textLight,
  257. fontSize: 10
  258. },
  259. noData: {
  260. color: textPlacehoder,
  261. fontSize: 14,
  262. padding: 20,
  263. marginTop: 16,
  264. textAlign: 'center'
  265. },
  266. noMore: {
  267. color: textPlacehoder,
  268. fontSize: 14,
  269. padding: 16,
  270. textAlign: 'center'
  271. }
  272. })