ListPoints.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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: res.data.length >= 10
  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.dataList.length > 0 && !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. ListFooterComponent={this.bottomView}
  186. refreshControl={
  187. <RefreshControl
  188. {...MyRefreshProps()}
  189. refreshing={this.state.refreshing}
  190. onRefresh={() => this.onRefresh()}
  191. />
  192. }/>
  193. );
  194. }
  195. }
  196. const styles = StyleSheet.create({
  197. container: {
  198. flex: 1,
  199. padding: 16
  200. },
  201. itemView: {
  202. padding: 16,
  203. marginTop: 16,
  204. borderWidth: 1,
  205. borderColor: '#DADADA',
  206. borderRadius: 4,
  207. alignItems: 'center',
  208. flexDirection: 'row',
  209. backgroundColor: colorLight
  210. },
  211. itemContent: {
  212. flex: 1,
  213. paddingRight: 16
  214. },
  215. voucherTitle: {
  216. color: textPrimary,
  217. fontSize: 16,
  218. fontWeight: 'bold'
  219. },
  220. voucherDesc: {
  221. color: textPrimary,
  222. fontSize: 14,
  223. paddingTop: 2,
  224. paddingBottom: 4
  225. },
  226. expireDate: {
  227. color: textCancel,
  228. fontSize: 12
  229. },
  230. purchaseButton: {
  231. padding: 4,
  232. minWidth: 75,
  233. alignItems: 'center'
  234. },
  235. claimeButton: {
  236. borderWidth: 1,
  237. borderColor: colorAccent,
  238. borderRadius: 6,
  239. backgroundColor: colorLight
  240. },
  241. claimeButtonView: {
  242. padding: 8,
  243. minWidth: 73,
  244. alignItems: 'center'
  245. },
  246. claimeButtonText: {
  247. color: colorAccent,
  248. fontSize: 12,
  249. fontWeight: 'bold'
  250. },
  251. getForText: {
  252. color: textLight,
  253. fontSize: 12,
  254. fontWeight: 'bold'
  255. },
  256. getValueText: {
  257. color: textLight,
  258. fontSize: 10
  259. },
  260. noData: {
  261. color: textPlacehoder,
  262. fontSize: 14,
  263. padding: 20,
  264. marginTop: 16,
  265. textAlign: 'center'
  266. },
  267. noMore: {
  268. color: textPlacehoder,
  269. fontSize: 14,
  270. padding: 16,
  271. textAlign: 'center'
  272. }
  273. })