PaymentListV2.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * 新版支付方式选择器
  3. * @邠心vbe on 2021/04/13
  4. */
  5. import React, { Component } from 'react';
  6. import { View, 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. import utils from '../../utils/utils';
  17. import VbeSkeleton from '../../components/VbeSkeleton';
  18. export default class PaymentListV2 extends Component {
  19. constructor(props) {
  20. super(props);
  21. this.state = {
  22. visible: false,
  23. isSelect: true,
  24. options: [],
  25. selectIndex: 0,
  26. selectOptions: {},
  27. isloading: true,
  28. delayTimes: 1738080000000
  29. };
  30. }
  31. componentDidMount() {
  32. this.getPaymentOptions();
  33. this.setState({
  34. isSelect: (this.props.isSelect || true)
  35. })
  36. }
  37. componentDidUpdate() {
  38. if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) {
  39. this.setState({
  40. isSelect: this.props.isSelect
  41. })
  42. }
  43. }
  44. getPaymentOptions() {
  45. if (utils.isEmpty(this.props.chargeBoxId)) {
  46. setTimeout(() => {
  47. this.getPaymentOptions();
  48. }, 1000);
  49. return;
  50. }
  51. apiCharge.getPaymentTypeOptions({
  52. chargeBoxId: this.props.chargeBoxId
  53. }).then(res => {
  54. if (utils.isNotEmpty(res.data)) {
  55. if (this.props.payType && this.props.payType.code) {
  56. this.setState({
  57. options: res.data,
  58. isloading: false
  59. }, () => {
  60. for (let i = 0; i < res.data.length; i++) {
  61. let type = res.data[i];
  62. if (type.code == this.props.payType.code) {
  63. this.changeMethod(i);
  64. break;
  65. }
  66. }
  67. });
  68. } else {
  69. let time = new Date().getTime();
  70. if (time > this.state.delayTimes) {
  71. this.setDataToChargEco(res.data);
  72. } else {
  73. this.setDataToDefault(res.data);
  74. }
  75. }
  76. }
  77. }).catch(err => {
  78. })
  79. }
  80. setDataToDefault(list) {
  81. let index = 0;
  82. for (let i = 0; i < list.length; i++) {
  83. let type = list[i];
  84. if (type.def) {
  85. index = i;
  86. break;
  87. }
  88. }
  89. this.setState({
  90. options: list,
  91. isloading: false
  92. }, () => {
  93. this.changeMethod(index);
  94. })
  95. }
  96. setDataToChargEco(list) {
  97. let index = 0;
  98. let hasDef = false;
  99. for (let i = 0; i < list.length; i++) {
  100. let type = list[i];
  101. if (type.code == "fleet_group_wallet" || type.code == "fleet_individual_wallet") {
  102. index = i;
  103. type.def = true;
  104. hasDef = true;
  105. } else {
  106. type.def = false;
  107. }
  108. }
  109. if (!hasDef) {
  110. for (let i = 0; i < list.length; i++) {
  111. let type = list[i];
  112. if (type.def) {
  113. index = i;
  114. break;
  115. }
  116. }
  117. }
  118. this.setState({
  119. options: list,
  120. isloading: false
  121. }, () => {
  122. this.changeMethod(index);
  123. });
  124. }
  125. showDialog(visible) {
  126. if (this.state.isSelect) {
  127. this.setState({
  128. visible: visible
  129. })
  130. }
  131. }
  132. changeMethod(index) {
  133. const type = this.state.options[index];
  134. this.setState({
  135. selectIndex: index,
  136. selectOptions: type
  137. }, () => {
  138. if (this.props.onMethodChange) {
  139. this.props.onMethodChange(this.state.selectOptions);
  140. }
  141. })
  142. }
  143. setDefault() {
  144. const code = this.state.selectOptions?.code;
  145. if (code && !this.state.selectOptions.def) {
  146. Dialog.showProgressDialog();
  147. apiCharge.setDefaultPaymentType({
  148. defaultPaymentMethod: code
  149. }).then(res => {
  150. toastShort("success");
  151. this.getPaymentOptions();
  152. }).catch(err => {
  153. toastShort(err)
  154. }).finally(() => {
  155. Dialog.dismissLoading();
  156. })
  157. }
  158. }
  159. render() {
  160. return (
  161. <View>
  162. <BadgeSelectItem
  163. style={this.props.style ?? ChargeStyle.stationInfoView}
  164. borderColor={this.props.borderColor}
  165. checked={false}
  166. onPress={() => this.showDialog(true)}>
  167. { app.charge.version >= 4
  168. ? <MaterialIcons
  169. name="wallet"
  170. size={32}
  171. color={textPrimary}/>
  172. : <PaymentIcon
  173. method={"WALLET"}
  174. checked={true}
  175. size={35}/>
  176. }
  177. { (this.state.isSelect && this.state.options.length > 1) &&
  178. <TextView style={styles.numberDot}>{this.state.options.length}</TextView>
  179. }
  180. <View style={styles.paymentView}>
  181. { this.state.isloading
  182. ? <VbeSkeleton
  183. style={ui.flex1}
  184. viewStyle={ui.flexc}
  185. isLoading={this.state.isloading}
  186. animationType="pulse"
  187. layout={[{width: '60%', height: 20, marginLeft: 10}]}
  188. animationDirection={"horizontalRight"}/>
  189. : <TextView
  190. style={styles.valueText}
  191. ellipsizeMode="middle"
  192. numberOfLines={1}>{this.state.selectOptions?.name}</TextView>
  193. }
  194. { this.state.isloading
  195. ? <VbeSkeleton
  196. style={ui.flex1}
  197. viewStyle={ui.flexc}
  198. isLoading={this.state.isloading}
  199. animationType="pulse"
  200. layout={[{width: '60%', height: 20, marginLeft: 20}]}
  201. animationDirection={"horizontalRight"}/>
  202. : <TextView
  203. style={styles.paymentText}
  204. ellipsizeMode="middle"
  205. numberOfLines={1}>{this.state.selectOptions?.desc}</TextView>
  206. }
  207. </View>
  208. { this.state.selectOptions?.def &&
  209. <TextView style={styles.textDefault}>Default</TextView>
  210. }
  211. { this.state.isSelect &&
  212. <Lucide
  213. name={"chevron-right"}
  214. size={24}
  215. color={colorCancel}
  216. />
  217. }
  218. </BadgeSelectItem>
  219. <BottomModal
  220. visible={this.state.visible}
  221. onHide={() => this.showDialog(false)}>
  222. <View style={styles.dialogContent}>
  223. <TextView style={styles.titleText}>Payment Methods</TextView>
  224. <ScrollView style={styles.paymentList}>
  225. { this.state.options.map((item, index) => (
  226. <Pressable
  227. key={index}
  228. style={styles.itemPayment}
  229. onPress={() => this.changeMethod(index)}>
  230. <View style={ui.flex1}>
  231. <TextView style={styles.labelText}>{item.name}</TextView>
  232. <TextView style={styles.descText}>{item.desc}</TextView>
  233. </View>
  234. { item.def &&
  235. <TextView style={styles.tagDefault}>Default</TextView>
  236. }
  237. <MaterialCommunityIcons
  238. name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
  239. size={24}
  240. color={index == this.state.selectIndex ? colorPrimary : textPrimary}
  241. />
  242. </Pressable>
  243. ))
  244. }
  245. </ScrollView>
  246. <EndView/><EndView/>
  247. <View style={ui.flexc}>
  248. <Button
  249. style={styles.defButton}
  250. text={"Set Default"}
  251. onClick={() => this.setDefault()}/>
  252. <EndView half/>
  253. <Button
  254. style={ui.flex1}
  255. text={"Confirm"}
  256. onClick={() => this.showDialog(false)}/>
  257. </View>
  258. </View>
  259. </BottomModal>
  260. </View>
  261. );
  262. }
  263. }
  264. const styles = StyleSheet.create({
  265. paymentView: {
  266. flex: 1,
  267. flexWrap: 'wrap',
  268. alignItems: 'center',
  269. flexDirection: 'row',
  270. justifyContent: 'space-around'
  271. },
  272. dialogContent: {
  273. padding: 16
  274. },
  275. paymentList: {
  276. height: $vh(50)
  277. },
  278. titleText: {
  279. fontSize: 16,
  280. fontWeight: 'bold',
  281. paddingBottom: 18
  282. },
  283. valueText: {
  284. flex: 1,
  285. color: textPrimary,
  286. fontSize: 14,
  287. fontWeight: 'bold',
  288. paddingLeft: 16
  289. },
  290. paymentText: {
  291. flex: 1,
  292. color: textSecondary,
  293. fontSize: 12,
  294. paddingLeft: 8,
  295. paddingRight: 16,
  296. textAlign: 'center'
  297. },
  298. labelText: {
  299. color: textPrimary,
  300. fontSize: 15,
  301. fontWeight: 'bold'
  302. },
  303. descText: {
  304. color: colorAccent,
  305. fontSize: 14,
  306. paddingTop: 2
  307. },
  308. itemPayment: {
  309. paddingTop: 8,
  310. paddingBottom: 8,
  311. alignItems: 'center',
  312. flexDirection: 'row'
  313. },
  314. tagDefault: {
  315. color: textLight,
  316. fontSize: 12,
  317. borderRadius: 4,
  318. ...$padding(4, 8),
  319. marginRight: 12,
  320. backgroundColor: colorAccent
  321. },
  322. textDefault: {
  323. color: colorAccent,
  324. fontSize: 12,
  325. marginRight: 8,
  326. fontWeight: 'bold'
  327. },
  328. defButton: {
  329. flex: 1,
  330. backgroundColor: colorPrimary
  331. },
  332. numberDot: {
  333. top: 10,
  334. left: app.charge.version >= 4 ? 32 : 35,
  335. width: 16,
  336. height: 16,
  337. color: textButton,
  338. fontSize: 10,
  339. fontWeight: 'bold',
  340. borderRadius: 32,
  341. position: 'absolute',
  342. alignItems: 'center',
  343. justifyContent: 'center',
  344. backgroundColor: app.charge.version >= 4 ? colorAccent : "#FF2622"
  345. }
  346. })