buy.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. *
  3. */
  4. import React, { Component } from 'react';
  5. import {
  6. Platform,
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TouchableOpacity,
  12. FlatList,
  13. TouchableHighlight,
  14. DeviceEventEmitter,
  15. ScrollView,
  16. BackHandler,
  17. StatusBar,
  18. Modal,
  19. Animated,
  20. TextInput
  21. } from 'react-native';
  22. import BasePage from '../BasePage';
  23. import Dimensions from '../utils/dimensions';
  24. import TopicTitle from '../components/TopicTitle';
  25. import CourseTitle from '../components/CourseTitle';
  26. import wechat from '../utils/wechat';
  27. import PayUtil from '../services/PayUtil';
  28. import Alipay from 'react-native-yunpeng-alipay';
  29. export default class Buy extends BasePage {
  30. state = {
  31. shopData: [
  32. {
  33. appCode: '1006',
  34. days: 365,
  35. gmtCreated: 1520843765000,
  36. gmtModified: 1541127465000,
  37. id: 10018,
  38. title: '',
  39. originalPrice: '499',
  40. payType: 3,
  41. price: '199',
  42. sort: 1
  43. },
  44. {
  45. appCode: '1006',
  46. days: 92,
  47. gmtCreated: 1520843761000,
  48. gmtModified: 1534387968000,
  49. id: 10017,
  50. title: '',
  51. originalPrice: '199',
  52. payType: 2,
  53. price: '99',
  54. sort: 2
  55. },
  56. {
  57. appCode: '1006',
  58. days: 31,
  59. gmtCreated: 1520843757000,
  60. gmtModified: 1537953249000,
  61. id: 10016,
  62. title: '',
  63. originalPrice: '99',
  64. payType: 1,
  65. price: '49',
  66. sort: 3
  67. }
  68. ],
  69. currentTapindex: 0,
  70. slideAnim: new Animated.Value(-150),
  71. ifDialogShow: false,
  72. payType: 1,
  73. ticketPrice: 0,
  74. useTicket: false
  75. };
  76. itemTap = (index) => {
  77. this.setState({
  78. currentTapindex: index
  79. });
  80. };
  81. renderItem = (item, index) => {
  82. return (
  83. <TouchableOpacity
  84. style={this.state.currentTapindex === index ? styles.itemWrapperTap : styles.itemWrapperNormal}
  85. onPress={() => {
  86. this.itemTap(index);
  87. }}
  88. key={index}
  89. >
  90. <Text style={this.state.currentTapindex === index ? styles.timeLengthTap : styles.timeLength}>
  91. {item.title}
  92. </Text>
  93. <Text style={this.state.currentTapindex === index ? styles.priceTap : styles.price}>
  94. ¥{item.price}元
  95. </Text>
  96. <Text style={this.state.currentTapindex === index ? styles.originPriceTap : styles.originPrice}>
  97. 原价:¥{item.originalPrice}
  98. </Text>
  99. </TouchableOpacity>
  100. );
  101. };
  102. dialogComeout = (index) => {
  103. if (index) {
  104. this.setState(
  105. {
  106. ifDialogShow: true
  107. },
  108. () => {
  109. Animated.timing(this.state.slideAnim, {
  110. toValue: 0,
  111. duration: 100
  112. }).start();
  113. }
  114. );
  115. } else {
  116. Animated.timing(this.state.slideAnim, {
  117. toValue: -150,
  118. duration: 100
  119. }).start();
  120. setTimeout(() => {
  121. this.setState({
  122. ifDialogShow: false
  123. });
  124. }, 210);
  125. }
  126. };
  127. setPayMethod = (type) => {
  128. this.setState(
  129. {
  130. payType: type
  131. },
  132. () => {
  133. setTimeout(() => {
  134. this.dialogComeout(false);
  135. }, 100);
  136. }
  137. );
  138. };
  139. componentWillMount() {
  140. //获取订购数据信息
  141. PayUtil.getMember().then((result) => {
  142. console.log('====================================');
  143. console.log('result', result);
  144. console.log('====================================');
  145. this.setState({
  146. shopData: result.data
  147. });
  148. });
  149. BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
  150. }
  151. componentWillUnmount() {
  152. BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
  153. }
  154. onBackAndroid = () => {
  155. if (this.state.ifDialogShow) {
  156. this.dialogComeout(false);
  157. } else {
  158. this.goBack();
  159. }
  160. return true;
  161. };
  162. choseTicket = () => {
  163. this.toNextPage('Ticket', { choseTicketCallBack: this.choseTicketCallBack.bind(this) });
  164. };
  165. choseTicketCallBack(item) {
  166. /**item参数
  167. * type: 1,
  168. price: 12,
  169. num: 6,
  170. time: '2019-12-12'
  171. *
  172. */
  173. this.setState({
  174. ticketPrice: item.price,
  175. useTicket: true
  176. });
  177. }
  178. getPrice(old_price) {
  179. return parseInt(old_price) - parseInt(this.state.ticketPrice) < 0
  180. ? 0
  181. : parseInt(old_price) - parseInt(this.state.ticketPrice);
  182. }
  183. pay = () => {
  184. switch (this.state.payType) {
  185. case 1:
  186. let option = {
  187. method: 'POST',
  188. body: {
  189. productCode: '',
  190. type: '0',
  191. preferentialIds: '',
  192. useVoucher: this.state.useTicket
  193. }
  194. };
  195. PayUtil.payByWechat(option).then((result) => {
  196. //微信支付
  197. wechat.pay(
  198. result.partnerId,
  199. result.prepayId,
  200. result.nonceStr,
  201. result.timeStamp,
  202. result.package,
  203. result.sign
  204. );
  205. });
  206. break;
  207. case 2:
  208. //阿里支付
  209. let option_ali = {
  210. method: 'POST',
  211. body: {
  212. productCode: '',
  213. type: '0',
  214. preferentialIds: '',
  215. useVoucher: this.state.useTicket
  216. }
  217. };
  218. //先请求后台给支付数据
  219. PayUtil.payByAli(option_ali).then((result) => {
  220. let data = 'partner=' + result.partner + '&';
  221. 'seller_id=' + result.seller_id + '&';
  222. 'out_trade_no=' + result.out_trade_no + '&';
  223. 'subject=' + result.subject + '&';
  224. 'body=' + result.body + '&';
  225. 'total_fee=' + result.total_fee + '&';
  226. 'notify_url=' + result.notify_url + '&';
  227. 'service=' + result.service + '&';
  228. 'payment_type=' + result.payment_type + '&';
  229. '_input_charset=' + result._input_charset + '&';
  230. 'it_b_pay=' + result.it_b_pay + '&';
  231. 'sign=' + result.sign + '&';
  232. 'sign_type=' + result.sign_type;
  233. //调用阿里支付
  234. Alipay.pay(data).then(
  235. function(data) {
  236. //成功
  237. console.log(data);
  238. },
  239. function(err) {
  240. //失败
  241. console.log(err);
  242. }
  243. );
  244. });
  245. break;
  246. }
  247. };
  248. render() {
  249. return (
  250. <View style={{ flex: 1 }}>
  251. <StatusBar barStyle={'dark-content'} backgroundColor={'white'} translucent={true} />
  252. <View
  253. style={{
  254. height: 50,
  255. backgroundColor: 'white',
  256. marginTop: 30
  257. }}
  258. >
  259. <CourseTitle
  260. width={this.getWindowWidth()}
  261. title="VIP购买"
  262. lefttype={1}
  263. textcolor={'#231F20'}
  264. backPress={() => this.goBack()}
  265. // backPress={() => alert("左侧按钮")}
  266. />
  267. </View>
  268. <View style={{ flex: 0.02, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
  269. <View style={styles.top}>
  270. <Text style={styles.title}>套餐选择</Text>
  271. <View>{this.state.shopData.map((item, index) => this.renderItem(item, index))}</View>
  272. </View>
  273. <View style={{ flex: 0.01, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
  274. <TouchableOpacity style={styles.payment} activeOpacity={1} onPress={this.choseTicket.bind(this)}>
  275. <Text style={styles.left}>使用抵用券</Text>
  276. <View style={styles.right}>
  277. <Text style={styles.method}>-¥{this.state.ticketPrice}元</Text>
  278. <Image source={require('../images/common/arrowRight.png')} />
  279. </View>
  280. </TouchableOpacity>
  281. <TouchableOpacity style={styles.payment} activeOpacity={1} onPress={() => this.dialogComeout(true)}>
  282. <Text style={styles.left}>支付方式</Text>
  283. <View style={styles.right}>
  284. {this.state.payType === 1 ? (
  285. <Text style={styles.method}>微信支付</Text>
  286. ) : (
  287. <Text style={styles.method}>支付宝支付</Text>
  288. )}
  289. <Image source={require('../images/common/arrowRight.png')} />
  290. </View>
  291. </TouchableOpacity>
  292. <View style={{ flex: 0.01, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
  293. <View>
  294. <Text style={{ fontSize: 14, width: '100%', textAlignVertical: 'center', textAlign: 'center' }}>
  295. 开通会员即时生效,有任何问题请联系我们。
  296. </Text>
  297. </View>
  298. <View style={styles.bottom}>
  299. {this.state.ticketPrice > 0 ? (
  300. (price = (
  301. <Text style={styles.bottomLeft}>
  302. ¥{this.getPrice(this.state.shopData[this.state.currentTapindex].price)}元
  303. </Text>
  304. ))
  305. ) : (
  306. <Text style={styles.bottomLeft}>¥{this.state.shopData[this.state.currentTapindex].price}元</Text>
  307. )}
  308. <TouchableOpacity style={styles.bottomRight} onPress={this.pay.bind(this)}>
  309. <Text style={styles.bottomRightText}>支付</Text>
  310. </TouchableOpacity>
  311. </View>
  312. {this.state.ifDialogShow ? (
  313. <TouchableHighlight
  314. onPress={() => {
  315. this.dialogComeout(false);
  316. }}
  317. style={{ ...styles.dialog }}
  318. underlayColor={0.1}
  319. >
  320. <Animated.View style={{ ...styles.payMethod, bottom: this.state.slideAnim }} onPress={() => {}}>
  321. <Text style={styles.payText}>选择支付方式</Text>
  322. <TouchableOpacity
  323. activeOpacity={0.9}
  324. style={styles.payDialog}
  325. onPress={() => this.setPayMethod(1)}
  326. >
  327. <View style={styles.dialogRow}>
  328. <Image style={styles.payIcon} source={require('../images/common/wxPay.png')} />
  329. <Text>微信支付</Text>
  330. </View>
  331. {this.state.payType === 1 ? (
  332. <Image source={require('../images/common/check.png')} />
  333. ) : null}
  334. </TouchableOpacity>
  335. <TouchableOpacity
  336. activeOpacity={0.9}
  337. style={styles.payDialog}
  338. onPress={() => this.setPayMethod(2)}
  339. >
  340. <View style={styles.dialogRow}>
  341. <Image style={styles.payIcon} source={require('../images/common/aliPay.png')} />
  342. <Text>支付宝支付</Text>
  343. </View>
  344. {this.state.payType === 2 ? (
  345. <Image source={require('../images/common/check.png')} />
  346. ) : null}
  347. </TouchableOpacity>
  348. {/* <TextInput style={styles.payDialog} /> */}
  349. <View style={{ flex: 0.2 }} />
  350. </Animated.View>
  351. </TouchableHighlight>
  352. ) : null}
  353. {/* <Modal
  354. animationType="none "
  355. transparent={true}
  356. visible={true}
  357. onRequestClose={() => {
  358. alert("Modal has been closed.");
  359. }}
  360. ></Modal> */}
  361. </View>
  362. );
  363. }
  364. }
  365. const styles = StyleSheet.create({
  366. top: {
  367. width: Dimensions.width,
  368. flexDirection: 'column',
  369. alignItems: 'center',
  370. paddingBottom: 20
  371. },
  372. title: {
  373. fontSize: 20,
  374. color: '#a8674d',
  375. marginTop: 22
  376. },
  377. itemWrapperNormal: {
  378. borderWidth: 1,
  379. borderColor: '#a8674d',
  380. borderRadius: 27,
  381. backgroundColor: '#fff',
  382. flexDirection: 'row',
  383. alignItems: 'center',
  384. justifyContent: 'space-around',
  385. width: '86%',
  386. height: Dimensions.getHeight(53),
  387. marginTop: 20
  388. },
  389. itemWrapperTap: {
  390. // borderWidth: 1,
  391. // borderColor: '#a8674d',
  392. borderRadius: 27,
  393. backgroundColor: '#ff7525',
  394. flexDirection: 'row',
  395. alignItems: 'center',
  396. justifyContent: 'space-around',
  397. width: '86%',
  398. height: Dimensions.getHeight(53),
  399. marginTop: 20
  400. },
  401. originPriceTap: {
  402. fontSize: 14,
  403. color: '#fff',
  404. textDecorationLine: 'line-through'
  405. },
  406. originPrice: {
  407. fontSize: 14,
  408. color: '#a8674d',
  409. textDecorationLine: 'line-through'
  410. },
  411. priceTap: {
  412. fontSize: 20,
  413. color: '#fff'
  414. },
  415. price: {
  416. fontSize: 20,
  417. color: '#a8674d'
  418. },
  419. timeLengthTap: {
  420. fontSize: 18,
  421. color: '#fff'
  422. },
  423. timeLength: {
  424. fontSize: 18,
  425. color: '#a8674d'
  426. },
  427. payment: {
  428. flexDirection: 'row',
  429. alignItems: 'center',
  430. width: Dimensions.width,
  431. height: 60,
  432. justifyContent: 'space-between',
  433. alignItems: 'center',
  434. borderColor: '#f3f2f7',
  435. borderTopWidth: 6,
  436. borderBottomWidth: 6,
  437. paddingHorizontal: 33
  438. },
  439. left: {
  440. fontSize: 16
  441. },
  442. right: {
  443. flexDirection: 'row',
  444. alignItems: 'center'
  445. },
  446. method: {
  447. color: '#a8674d',
  448. fontSize: 16,
  449. marginRight: 7
  450. },
  451. bottom: {
  452. width: Dimensions.width,
  453. height: 60,
  454. flexDirection: 'row',
  455. position: 'absolute',
  456. bottom: 0
  457. },
  458. bottomLeft: {
  459. width: '58%',
  460. textAlign: 'center',
  461. lineHeight: 60,
  462. color: '#a8674d',
  463. fontSize: 20
  464. },
  465. bottomRight: {
  466. width: '42%',
  467. fontSize: 16,
  468. color: '#fff',
  469. backgroundColor: '#f5880d',
  470. alignItems: 'center'
  471. },
  472. bottomRightText: {
  473. fontSize: 20,
  474. color: '#fff',
  475. textAlign: 'center',
  476. lineHeight: 60
  477. },
  478. dialog: {
  479. width: Dimensions.width,
  480. height: Dimensions.height,
  481. position: 'absolute',
  482. backgroundColor: 'rgba(0,0,0,0.4)'
  483. },
  484. payMethod: {
  485. width: Dimensions.width,
  486. height: 150,
  487. position: 'absolute',
  488. // bottom: 0,
  489. backgroundColor: '#fff',
  490. flexDirection: 'column',
  491. alignItems: 'center',
  492. justifyContent: 'flex-start'
  493. },
  494. payDialog: {
  495. width: '90%',
  496. borderTopWidth: 1,
  497. borderColor: '#e4e4e4',
  498. flexDirection: 'row',
  499. alignItems: 'center',
  500. justifyContent: 'space-between',
  501. flex: 1
  502. },
  503. dialogRow: {
  504. flex: 1,
  505. flexDirection: 'row',
  506. alignItems: 'center'
  507. },
  508. payIcon: {
  509. marginRight: 17
  510. },
  511. payText: {
  512. fontSize: 16,
  513. color: '#191919',
  514. alignContent: 'center',
  515. flex: 1,
  516. lineHeight: 50
  517. }
  518. });