buy.js 11 KB

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