ListPoints.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.itemBox}></View>
  144. <View style={styles.itemContent}>
  145. <TextView style={styles.voucherTitle}>{item.voucherName}</TextView>
  146. <TextView style={styles.voucherDesc}>{item.voucherDesc}</TextView>
  147. <TextView style={styles.expireDate}>{$t("voucher.expiresOn") + item.expiresOn}</TextView>
  148. <View style={styles.rightDash}></View>
  149. <View style={styles.topTikDot}></View>
  150. <View style={styles.bottomTikDot}></View>
  151. </View>
  152. { item.purchasePoints > 0
  153. ? <Button
  154. viewStyle={styles.purchaseButton}
  155. borderRadius={6}
  156. onClick={() => this.confirmPurchase(item)}>
  157. <TextView
  158. style={styles.getForText}>
  159. {$t("voucher.btnGetFor")}
  160. </TextView>
  161. <TextView
  162. style={styles.getValueText}>
  163. {item.purchasePoints}
  164. {$t("voucher.btnPoints")}
  165. </TextView>
  166. </Button>
  167. : <Button
  168. style={styles.claimeButton}
  169. viewStyle={styles.claimeButtonView}
  170. textStyle={styles.claimeButtonText}
  171. text={$t("voucher.btnClaimed")}
  172. onClick={() => this.onPurchase(item)}/>
  173. }
  174. </View>
  175. )
  176. }
  177. topView = (props) => {
  178. return (
  179. <View>
  180. <ViewRedeem
  181. userInfo={this.state.userInfo}/>
  182. <VoucherType
  183. type={this.state.voucherType}
  184. onChange={type => this.changeType(type)}
  185. />
  186. </View>
  187. )
  188. }
  189. bottomView = () => {
  190. if (this.state.dataList.length > 0 && !this.state.hasMore) {
  191. return (<Text style={styles.noMore}>{$t('voucher.noMore')}</Text>)
  192. } else {
  193. return null
  194. }
  195. }
  196. render() {
  197. return (
  198. <FlatList
  199. style={styles.container}
  200. data={this.state.dataList}
  201. renderItem={this.listItem}
  202. ListHeaderComponent={this.topView}
  203. keyExtractor={item => item.voucherId}
  204. onEndReached={() => this.getDataListPage()}
  205. onEndReachedThreshold={0.3}
  206. ListEmptyComponent={<Text style={styles.noData}>{$t('voucher.noData')}</Text>}
  207. ListFooterComponent={this.bottomView}
  208. refreshControl={
  209. <RefreshControl
  210. {...MyRefreshProps()}
  211. refreshing={this.state.refreshing}
  212. onRefresh={() => this.onRefresh()}
  213. />
  214. }/>
  215. );
  216. }
  217. }
  218. const styles = StyleSheet.create({
  219. container: {
  220. flex: 1,
  221. padding: 16
  222. },
  223. itemView: {
  224. marginTop: 16,
  225. borderRadius: 4,
  226. alignItems: 'center',
  227. flexDirection: 'row',
  228. backgroundColor: colorLight,
  229. ...$padding(0, 20, 0, 16)
  230. },
  231. itemBox: {
  232. top: 0,
  233. left: 0,
  234. right: 0,
  235. bottom: 0,
  236. borderWidth: 1,
  237. borderColor: '#DADADA',
  238. borderRadius: 4,
  239. position: 'absolute'
  240. },
  241. itemContent: {
  242. flex: 1,
  243. paddingTop: 12,
  244. marginRight: 16,
  245. paddingBottom: 12,
  246. overflow: 'hidden'
  247. },
  248. rightDash: {
  249. top: 0,
  250. right: 6,
  251. bottom: 0,
  252. position: 'absolute',
  253. borderStyle: 'dashed',
  254. borderRightWidth: 1,
  255. borderRightColor: '#DADADA'
  256. },
  257. topTikDot: {
  258. top: -8,
  259. right: 0,
  260. width: 13,
  261. height: 14,
  262. borderWidth: 1,
  263. borderColor: '#DADADA',
  264. borderRadius: 30,
  265. position: 'absolute',
  266. backgroundColor: pageBackground
  267. },
  268. bottomTikDot: {
  269. bottom: -8,
  270. right: 0,
  271. width: 13,
  272. height: 14,
  273. borderWidth: 1,
  274. borderColor: '#DADADA',
  275. borderRadius: 30,
  276. position: 'absolute',
  277. backgroundColor: pageBackground
  278. },
  279. voucherTitle: {
  280. color: textPrimary,
  281. fontSize: 16,
  282. fontWeight: 'bold'
  283. },
  284. voucherDesc: {
  285. color: textPrimary,
  286. fontSize: 14,
  287. paddingTop: 2,
  288. paddingBottom: 4
  289. },
  290. expireDate: {
  291. color: textCancel,
  292. fontSize: 12
  293. },
  294. purchaseButton: {
  295. padding: 4,
  296. minWidth: 75,
  297. marginRight: 4,
  298. alignItems: 'center'
  299. },
  300. claimeButton: {
  301. borderWidth: 1,
  302. borderColor: colorAccent,
  303. borderRadius: 6,
  304. backgroundColor: colorLight
  305. },
  306. claimeButtonView: {
  307. padding: 8,
  308. minWidth: 73,
  309. alignItems: 'center'
  310. },
  311. claimeButtonText: {
  312. color: colorAccent,
  313. fontSize: 12,
  314. fontWeight: 'bold'
  315. },
  316. getForText: {
  317. color: textLight,
  318. fontSize: 12,
  319. fontWeight: 'bold'
  320. },
  321. getValueText: {
  322. color: textLight,
  323. fontSize: 10
  324. },
  325. noData: {
  326. color: textPlacehoder,
  327. fontSize: 14,
  328. padding: 20,
  329. marginTop: 16,
  330. textAlign: 'center'
  331. },
  332. noMore: {
  333. color: textPlacehoder,
  334. fontSize: 14,
  335. padding: 16,
  336. textAlign: 'center'
  337. }
  338. })