buy.js 11 KB

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