PaymentListV2.js 9.4 KB

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