PaymentListV2.js 6.7 KB

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