PaymentListV2.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * 新版支付方式选择器
  3. * @邠心vbe on 2021/04/13
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Pressable, ScrollView } from 'react-native';
  7. import BottomModal from '../../components/BottomModal';
  8. import BadgeSelectItem from '../../components/BadgeSelectItem';
  9. import { ChargeStyle } from './Charging';
  10. import { PAYTYPE, PaymentIcon } from '../payment/PaymentConfig';
  11. import TextView from '../../components/TextView';
  12. import apiCharge from '../../api/apiCharge';
  13. import Button from '../../components/Button';
  14. import Dialog from '../../components/Dialog';
  15. export default class PaymentListV2 extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. visible: false,
  20. isSelect: true,
  21. options: [],
  22. selectIndex: 0,
  23. selectOptions: {}
  24. };
  25. }
  26. componentDidMount() {
  27. this.getPaymentOptions();
  28. this.setState({
  29. isSelect: (this.props.isSelect || true)
  30. })
  31. }
  32. componentDidUpdate() {
  33. if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) {
  34. this.setState({
  35. isSelect: this.props.isSelect
  36. })
  37. }
  38. }
  39. getPaymentOptions() {
  40. apiCharge.getPaymentTypeOptions().then(res => {
  41. if (res.data) {
  42. this.setState({
  43. options: res.data
  44. })
  45. if (this.props.payType && this.props.payType.code) {
  46. for (let i = 0; i < res.data.length; i++) {
  47. let type = res.data[i];
  48. if (type.code == this.props.payType.code) {
  49. this.changeMethod(i);
  50. break;
  51. }
  52. }
  53. } else {
  54. let index = 0;
  55. for (let i = 0; i < res.data.length; i++) {
  56. let type = res.data[i];
  57. if (type.def) {
  58. index = i;
  59. break;
  60. }
  61. }
  62. this.changeMethod(index);
  63. }
  64. }
  65. }).catch(err => {
  66. })
  67. }
  68. showDialog(visible) {
  69. if (this.state.isSelect) {
  70. this.setState({
  71. visible: visible
  72. })
  73. }
  74. }
  75. changeMethod(index) {
  76. const type = this.state.options[index];
  77. this.setState({
  78. selectIndex: index,
  79. selectOptions: type
  80. }, () => {
  81. if (this.props.onMethodChange) {
  82. this.props.onMethodChange(this.state.selectOptions);
  83. }
  84. })
  85. }
  86. setDefault() {
  87. const code = this.state.selectOptions?.code;
  88. if (code && !this.state.selectOptions.def) {
  89. Dialog.showProgressDialog();
  90. apiCharge.setDefaultPaymentType({
  91. defaultPaymentMethod: code
  92. }).then(res => {
  93. toastShort("success");
  94. this.getPaymentOptions();
  95. }).catch(err => {
  96. toastShort(err)
  97. }).finally(() => {
  98. Dialog.dismissLoading();
  99. })
  100. }
  101. }
  102. render() {
  103. return (
  104. <View>
  105. <BadgeSelectItem
  106. style={ChargeStyle.stationInfoView}
  107. checked={false}
  108. onPress={() => this.showDialog(true)}>
  109. <PaymentIcon
  110. method={"WALLET"}
  111. checked={true}/>
  112. <View style={styles.paymentView}>
  113. <TextView style={styles.valueText}>{this.state.selectOptions?.name}</TextView>
  114. <TextView style={styles.paymentText}>{this.state.selectOptions?.desc}</TextView>
  115. </View>
  116. { this.state.selectOptions?.def &&
  117. <TextView style={styles.textDefault}>Default</TextView>
  118. }
  119. { this.state.isSelect &&
  120. <FontAwesome6
  121. name={"angle-right"}
  122. size={16}
  123. color={colorCancel}
  124. />
  125. }
  126. </BadgeSelectItem>
  127. <BottomModal
  128. visible={this.state.visible}
  129. onHide={() => this.showDialog(false)}>
  130. <View style={styles.dialogContent}>
  131. <TextView style={styles.titleText}>Payment Methods</TextView>
  132. <ScrollView style={styles.paymentList}>
  133. { this.state.options.map((item, index) => (
  134. <Pressable
  135. key={index}
  136. style={styles.itemPayment}
  137. onPress={() => this.changeMethod(index)}>
  138. <View style={ui.flex1}>
  139. <TextView style={styles.valueText}>{item.name}</TextView>
  140. <TextView style={styles.descText}>{item.desc}</TextView>
  141. </View>
  142. { item.def &&
  143. <TextView style={styles.tagDefault}>Default</TextView>
  144. }
  145. <MaterialCommunityIcons
  146. name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
  147. size={24}
  148. color={index == this.state.selectIndex ? colorPrimary : textPrimary}
  149. />
  150. </Pressable>
  151. ))
  152. }
  153. </ScrollView>
  154. <EndView/><EndView/>
  155. <View style={ui.flexc}>
  156. <Button
  157. style={styles.defButton}
  158. text={"Set Default"}
  159. onClick={() => this.setDefault()}/>
  160. <EndView half/>
  161. <Button
  162. style={ui.flex1}
  163. text={"Confirm"}
  164. onClick={() => this.showDialog(false)}/>
  165. </View>
  166. </View>
  167. </BottomModal>
  168. </View>
  169. );
  170. }
  171. }
  172. const styles = StyleSheet.create({
  173. paymentView: {
  174. flex: 1,
  175. alignItems: 'center',
  176. flexDirection: 'row',
  177. justifyContent: 'space-around'
  178. },
  179. dialogContent: {
  180. padding: 16
  181. },
  182. paymentList: {
  183. height: $vh(50)
  184. },
  185. titleText: {
  186. fontSize: 16,
  187. fontWeight: 'bold',
  188. paddingBottom: 18
  189. },
  190. valueText: {
  191. color: textPrimary,
  192. fontSize: 15,
  193. fontWeight: 'bold'
  194. },
  195. paymentText: {
  196. color: textSecondary,
  197. fontSize: 12,
  198. paddingLeft: 8,
  199. paddingRight: 16
  200. },
  201. descText: {
  202. color: colorAccent,
  203. fontSize: 12,
  204. paddingTop: 2
  205. },
  206. itemPayment: {
  207. paddingTop: 8,
  208. paddingBottom: 8,
  209. alignItems: 'center',
  210. flexDirection: 'row'
  211. },
  212. tagDefault: {
  213. color: textLight,
  214. fontSize: 12,
  215. borderRadius: 4,
  216. ...$padding(4, 8),
  217. marginRight: 12,
  218. backgroundColor: colorAccent
  219. },
  220. textDefault: {
  221. color: colorAccent,
  222. fontSize: 12,
  223. marginRight: 8,
  224. fontWeight: 'bold'
  225. },
  226. defButton: {
  227. flex: 1,
  228. backgroundColor: colorPrimary
  229. }
  230. })