ListPoints.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. confirmPurchase(item) {
  88. let msg = "" + $t("voucher.confirmPurchase");
  89. if (item.purchasePoints) {
  90. msg = msg.replace("{value}", item.purchasePoints);
  91. } else {
  92. msg = msg.replace("{value}", "0");
  93. }
  94. Dialog.showDialog({
  95. title: $t("voucher.purchaseVoucher"),
  96. message: msg,
  97. ok: $t("nav.confirm"),
  98. callback: btn => {
  99. if (btn == Dialog.BUTTON_OK) {
  100. this.onPurchase(item)
  101. }
  102. }
  103. })
  104. }
  105. onPurchase(item) {
  106. Dialog.showProgressDialog();
  107. apiVoucher.purchaseVoucher({
  108. voucherId: item.voucherId
  109. }).then(res => {
  110. Dialog.dismissLoading();
  111. if (res.msg) {
  112. setTimeout(() => {
  113. Dialog.showDialog({
  114. title: $t("voucher.purchaseVoucher"),
  115. message: res.msg,
  116. showCancel: false
  117. })
  118. }, 500);
  119. }
  120. }).catch(err => {
  121. Dialog.dismissLoading();
  122. if (err.err) {
  123. setTimeout(() => {
  124. Dialog.showDialog({
  125. title: $t("voucher.vouchers"),
  126. message: err.err,
  127. showCancel: false
  128. })
  129. }, 500);
  130. }
  131. })
  132. }
  133. changeType(type) {
  134. this.setState({
  135. voucherType: type
  136. });
  137. this.getDataList();
  138. }
  139. listItem = ({item, index, separators}) => {
  140. return (
  141. <View
  142. style={styles.itemView}>
  143. <View style={styles.itemContent}>
  144. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  145. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  146. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  147. </View>
  148. { item.purchasePoints > 0
  149. ? <Button
  150. viewStyle={styles.purchaseButton}
  151. borderRadius={6}
  152. onClick={() => this.confirmPurchase(item)}>
  153. <TextView
  154. style={styles.getForText}>
  155. {$t("voucher.btnGetFor")}
  156. </TextView>
  157. <TextView
  158. style={styles.getValueText}>
  159. {item.purchasePoints}
  160. {$t("voucher.btnPoints")}
  161. </TextView>
  162. </Button>
  163. : <Button
  164. style={styles.claimeButton}
  165. viewStyle={styles.claimeButtonView}
  166. textStyle={styles.claimeButtonText}
  167. text={$t("voucher.btnClaimed")}
  168. onClick={() => this.onPurchase(item)}/>
  169. }
  170. </View>
  171. )
  172. }
  173. topView = (props) => {
  174. return (
  175. <View>
  176. <ViewRedeem
  177. userInfo={this.state.userInfo}/>
  178. <VoucherType
  179. type={this.state.voucherType}
  180. onChange={type => this.changeType(type)}
  181. />
  182. </View>
  183. )
  184. }
  185. bottomView = () => {
  186. if (this.state.dataList.length > 0 && !this.state.hasMore) {
  187. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  188. } else {
  189. return null
  190. }
  191. }
  192. render() {
  193. return (
  194. <FlatList
  195. style={styles.container}
  196. data={this.state.dataList}
  197. renderItem={this.listItem}
  198. ListHeaderComponent={this.topView}
  199. keyExtractor={item => item.voucherId}
  200. onEndReached={() => this.getDataListPage()}
  201. onEndReachedThreshold={0.3}
  202. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  203. ListFooterComponent={this.bottomView}
  204. refreshControl={
  205. <RefreshControl
  206. {...MyRefreshProps()}
  207. refreshing={this.state.refreshing}
  208. onRefresh={() => this.onRefresh()}
  209. />
  210. }/>
  211. );
  212. }
  213. }
  214. const styles = StyleSheet.create({
  215. container: {
  216. flex: 1,
  217. padding: 16
  218. },
  219. itemView: {
  220. padding: 16,
  221. marginTop: 16,
  222. borderWidth: 1,
  223. borderColor: '#DADADA',
  224. borderRadius: 4,
  225. alignItems: 'center',
  226. flexDirection: 'row',
  227. backgroundColor: colorLight
  228. },
  229. itemContent: {
  230. flex: 1,
  231. paddingRight: 16
  232. },
  233. voucherTitle: {
  234. color: textPrimary,
  235. fontSize: 16,
  236. fontWeight: 'bold'
  237. },
  238. voucherDesc: {
  239. color: textPrimary,
  240. fontSize: 14,
  241. paddingTop: 2,
  242. paddingBottom: 4
  243. },
  244. expireDate: {
  245. color: textCancel,
  246. fontSize: 12
  247. },
  248. purchaseButton: {
  249. padding: 4,
  250. minWidth: 75,
  251. alignItems: 'center'
  252. },
  253. claimeButton: {
  254. borderWidth: 1,
  255. borderColor: colorAccent,
  256. borderRadius: 6,
  257. backgroundColor: colorLight
  258. },
  259. claimeButtonView: {
  260. padding: 8,
  261. minWidth: 73,
  262. alignItems: 'center'
  263. },
  264. claimeButtonText: {
  265. color: colorAccent,
  266. fontSize: 12,
  267. fontWeight: 'bold'
  268. },
  269. getForText: {
  270. color: textLight,
  271. fontSize: 12,
  272. fontWeight: 'bold'
  273. },
  274. getValueText: {
  275. color: textLight,
  276. fontSize: 10
  277. },
  278. noData: {
  279. color: textPlacehoder,
  280. fontSize: 14,
  281. padding: 20,
  282. marginTop: 16,
  283. textAlign: 'center'
  284. },
  285. noMore: {
  286. color: textPlacehoder,
  287. fontSize: 14,
  288. padding: 16,
  289. textAlign: 'center'
  290. }
  291. })