ListPoints.js 8.0 KB

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