PaymentListV2.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. { this.state.isloading
  183. ? <VbeSkeleton
  184. style={ui.flex1}
  185. viewStyle={ui.flexc}
  186. isLoading={this.state.isloading}
  187. animationType="pulse"
  188. layout={[{width: '60%', height: 20, marginLeft: 10}]}
  189. animationDirection={"horizontalRight"}/>
  190. : <TextView
  191. style={styles.valueText}
  192. ellipsizeMode="middle"
  193. numberOfLines={1}>{this.state.selectOptions?.name}</TextView>
  194. }
  195. { this.state.isloading
  196. ? <VbeSkeleton
  197. style={ui.flex1}
  198. viewStyle={ui.flexc}
  199. isLoading={this.state.isloading}
  200. animationType="pulse"
  201. layout={[{width: '60%', height: 20, marginLeft: 20}]}
  202. animationDirection={"horizontalRight"}/>
  203. : <TextView
  204. style={styles.paymentText}
  205. ellipsizeMode="middle"
  206. numberOfLines={1}>{this.state.selectOptions?.desc}</TextView>
  207. }
  208. </View>
  209. { this.state.selectOptions?.def &&
  210. <TextView style={styles.textDefault}>Default</TextView>
  211. }
  212. { this.state.isSelect &&
  213. <Lucide
  214. name={"chevron-right"}
  215. size={24}
  216. color={colorCancel}
  217. />
  218. }
  219. </BadgeSelectItem>
  220. <BottomModal
  221. visible={this.state.visible}
  222. onHide={() => this.showDialog(false)}>
  223. <View style={styles.dialogContent}>
  224. <TextView style={styles.titleText}>Payment Methods</TextView>
  225. <ScrollView style={styles.paymentList}>
  226. { this.state.options.map((item, index) => (
  227. <Pressable
  228. key={index}
  229. style={styles.itemPayment}
  230. onPress={() => this.changeMethod(index)}>
  231. <View style={ui.flex1}>
  232. <TextView style={styles.labelText}>{item.name}</TextView>
  233. <TextView style={styles.descText}>{item.desc}</TextView>
  234. </View>
  235. { item.def &&
  236. <TextView style={styles.tagDefault}>Default</TextView>
  237. }
  238. <MaterialCommunityIcons
  239. name={index == this.state.selectIndex ? "radiobox-marked" : "radiobox-blank"}
  240. size={24}
  241. color={index == this.state.selectIndex ? colorPrimary : textPrimary}
  242. />
  243. </Pressable>
  244. ))
  245. }
  246. </ScrollView>
  247. <EndView/><EndView/>
  248. <View style={ui.flexc}>
  249. <Button
  250. style={styles.defButton}
  251. text={"Set Default"}
  252. onClick={() => this.setDefault()}/>
  253. <EndView half/>
  254. <Button
  255. style={ui.flex1}
  256. text={"Confirm"}
  257. onClick={() => this.showDialog(false)}/>
  258. </View>
  259. </View>
  260. </BottomModal>
  261. </View>
  262. );
  263. }
  264. }
  265. const styles = StyleSheet.create({
  266. paymentView: {
  267. flex: 1,
  268. flexWrap: 'wrap',
  269. alignItems: 'center',
  270. flexDirection: 'row',
  271. justifyContent: 'space-around'
  272. },
  273. dialogContent: {
  274. padding: 16
  275. },
  276. paymentList: {
  277. height: $vh(50)
  278. },
  279. titleText: {
  280. fontSize: 16,
  281. fontWeight: 'bold',
  282. paddingBottom: 18
  283. },
  284. valueText: {
  285. flex: 1,
  286. color: textPrimary,
  287. fontSize: 14,
  288. fontWeight: 'bold',
  289. paddingLeft: 16
  290. },
  291. paymentText: {
  292. flex: 1,
  293. color: textSecondary,
  294. fontSize: 12,
  295. paddingLeft: 8,
  296. paddingRight: 16,
  297. textAlign: 'center'
  298. },
  299. labelText: {
  300. color: textPrimary,
  301. fontSize: 15,
  302. fontWeight: 'bold'
  303. },
  304. descText: {
  305. color: colorAccent,
  306. fontSize: 14,
  307. paddingTop: 2
  308. },
  309. itemPayment: {
  310. paddingTop: 8,
  311. paddingBottom: 8,
  312. alignItems: 'center',
  313. flexDirection: 'row'
  314. },
  315. tagDefault: {
  316. color: textLight,
  317. fontSize: 12,
  318. borderRadius: 4,
  319. ...$padding(4, 8),
  320. marginRight: 12,
  321. backgroundColor: colorAccent
  322. },
  323. textDefault: {
  324. color: colorAccent,
  325. fontSize: 12,
  326. marginRight: 8,
  327. fontWeight: 'bold'
  328. },
  329. defButton: {
  330. flex: 1,
  331. backgroundColor: colorPrimary
  332. },
  333. numberDot: {
  334. top: 10,
  335. left: app.charge.version >= 4 ? 32 : 35,
  336. width: 16,
  337. height: 16,
  338. color: textButton,
  339. fontSize: 10,
  340. fontWeight: 'bold',
  341. borderRadius: 32,
  342. position: 'absolute',
  343. alignItems: 'center',
  344. justifyContent: 'center',
  345. backgroundColor: app.charge.version >= 4 ? colorAccent : "#FF2622"
  346. }
  347. })