buy.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. Modal,
  17. Animated,
  18. TextInput
  19. } from 'react-native';
  20. import BasePage from '../BasePage';
  21. import Dimensions from '../utils/dimensions';
  22. import TopicTitle from '../components/TopicTitle';
  23. import CourseTitle from '../components/CourseTitle';
  24. import wechat from '../utils/wechat';
  25. import aliPay from '../utils/aliPay';
  26. export default class Buy extends BasePage {
  27. state = {
  28. shopData: [
  29. { title: '1个月', originPrice: '199', price: '49' },
  30. { title: '12个月', originPrice: '499', price: '199' }
  31. ],
  32. currentTapindex: 0,
  33. slideAnim: new Animated.Value(-150),
  34. ifDialogShow: false,
  35. payType: 1,
  36. ticketPrice: 0
  37. };
  38. itemTap = (index) => {
  39. this.setState({
  40. currentTapindex: index
  41. });
  42. };
  43. renderItem = (item, index) => {
  44. return (
  45. <TouchableOpacity
  46. style={this.state.currentTapindex === index ? styles.itemWrapperTap : styles.itemWrapperNormal}
  47. onPress={() => {
  48. this.itemTap(index);
  49. }}
  50. key={index}
  51. >
  52. <Text style={this.state.currentTapindex === index ? styles.timeLengthTap : styles.timeLength}>
  53. {item.title}
  54. </Text>
  55. <Text style={this.state.currentTapindex === index ? styles.priceTap : styles.price}>
  56. ¥{item.price}元
  57. </Text>
  58. <Text style={this.state.currentTapindex === index ? styles.originPriceTap : styles.originPrice}>
  59. 原价:¥{item.originPrice}
  60. </Text>
  61. </TouchableOpacity>
  62. );
  63. };
  64. dialogComeout = (index) => {
  65. if (index) {
  66. this.setState(
  67. {
  68. ifDialogShow: true
  69. },
  70. () => {
  71. Animated.timing(this.state.slideAnim, {
  72. toValue: 0,
  73. duration: 300
  74. }).start();
  75. }
  76. );
  77. } else {
  78. Animated.timing(this.state.slideAnim, {
  79. toValue: -150,
  80. duration: 200
  81. }).start();
  82. setTimeout(() => {
  83. this.setState({
  84. ifDialogShow: false
  85. });
  86. }, 210);
  87. }
  88. };
  89. setPayMethod = (type) => {
  90. this.setState(
  91. {
  92. payType: type
  93. },
  94. () => {
  95. setTimeout(() => {
  96. this.dialogComeout(false);
  97. }, 100);
  98. }
  99. );
  100. };
  101. choseTicket = () => {
  102. this.toNextPage('Ticket', { choseTicketCallBack: this.choseTicketCallBack.bind(this) });
  103. };
  104. choseTicketCallBack(item) {
  105. /**
  106. * type: 1,
  107. price: 12,
  108. num: 6,
  109. time: '2019-12-12'
  110. *
  111. */
  112. this.setState({
  113. ticketPrice: item.price
  114. });
  115. }
  116. pay = () => {
  117. switch (this.state.payType) {
  118. case 1:
  119. //微信支付
  120. wechat.pay();
  121. break;
  122. case 2:
  123. //阿里支付
  124. let params = {
  125. partner: 'asdfasdf', //app_id
  126. seller_id: '115681592@qq.com', //下单账号,用的芮总QQ邮箱,人家说没有可不填,但为了代表下单身份才用芮总QQ邮箱的
  127. out_trade_no: 'ALIPAY15307684880120000000001', //第三方订单号
  128. subject: '描述',
  129. body: '',
  130. total_fee: '0.01', //价格
  131. notify_url: 'http://xx.xx.xx.xx/api/0/alipay/alipayCallback', //回调地址
  132. service: 'm.pay',
  133. payment_type: '1',
  134. _input_charset: 'utf-8',
  135. it_b_pay: '30m',
  136. sign: 'sdfasdf', //签名
  137. sign_type: 'RSA'
  138. };
  139. aliPay.pay(params);
  140. break;
  141. }
  142. };
  143. render() {
  144. return (
  145. <View style={{ flex: 1 }}>
  146. <View
  147. style={{
  148. height: 30,
  149. marginTop: 30
  150. }}
  151. >
  152. <CourseTitle
  153. width={this.getWindowWidth()}
  154. title="VIP购买"
  155. lefttype={1}
  156. textcolor={'#231F20'}
  157. backPress={() => this.goBack()}
  158. // backPress={() => alert("左侧按钮")}
  159. />
  160. </View>
  161. <View style={{ flex: 0.02, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
  162. <View style={styles.top}>
  163. <Text style={styles.title}>套餐选择</Text>
  164. <View>{this.state.shopData.map((item, index) => this.renderItem(item, index))}</View>
  165. </View>
  166. <View style={{ flex: 0.01, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
  167. <TouchableOpacity style={styles.payment} activeOpacity={1} onPress={() => this.dialogComeout(true)}>
  168. <Text style={styles.left}>支付方式</Text>
  169. <View style={styles.right}>
  170. {this.state.payType === 1 ? (
  171. <Text style={styles.method}>微信支付</Text>
  172. ) : (
  173. <Text style={styles.method}>支付宝支付</Text>
  174. )}
  175. <Image source={require('../images/common/arrowRight.png')} />
  176. </View>
  177. </TouchableOpacity>
  178. <TouchableOpacity style={styles.payment} activeOpacity={1} onPress={this.choseTicket.bind(this)}>
  179. <Text style={styles.left}>使用抵用券</Text>
  180. <View style={styles.right}>
  181. <Text style={styles.method}>-¥{this.state.ticketPrice}元</Text>
  182. <Image source={require('../images/common/arrowRight.png')} />
  183. </View>
  184. </TouchableOpacity>
  185. <View style={{ flex: 0.01, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
  186. <View>
  187. <Text style={{ fontSize: 14, width: '100%', textAlignVertical: 'center', textAlign: 'center' }}>
  188. 开通会员即时生效,有任何问题请联系我们。
  189. </Text>
  190. </View>
  191. <View style={styles.bottom}>
  192. {this.state.ticketPrice > 0 ? (
  193. (price = (
  194. <Text style={styles.bottomLeft}>
  195. ¥{parseInt(this.state.shopData[this.state.currentTapindex].price) -
  196. parseInt(this.state.ticketPrice)}元
  197. </Text>
  198. ))
  199. ) : (
  200. <Text style={styles.bottomLeft}>¥{this.state.shopData[this.state.currentTapindex].price}元</Text>
  201. )}
  202. <TouchableOpacity style={styles.bottomRight} onPress={this.pay.bind(this)}>
  203. <Text style={styles.bottomRightText}>支付</Text>
  204. </TouchableOpacity>
  205. </View>
  206. {this.state.ifDialogShow ? (
  207. <TouchableHighlight
  208. onPress={() => {
  209. this.dialogComeout(false);
  210. }}
  211. style={{ ...styles.dialog }}
  212. underlayColor={0.1}
  213. >
  214. <Animated.View
  215. style={{ ...styles.payMethod, bottom: this.state.slideAnim }}
  216. onPress={() => {
  217. alert(12311);
  218. }}
  219. >
  220. <Text style={styles.payText}>选择支付方式</Text>
  221. <TouchableOpacity
  222. activeOpacity={0.9}
  223. style={styles.payDialog}
  224. onPress={() => this.setPayMethod(1)}
  225. >
  226. <View style={styles.dialogRow}>
  227. <Image style={styles.payIcon} source={require('../images/common/wxPay.png')} />
  228. <Text>微信支付</Text>
  229. </View>
  230. {this.state.payType === 1 ? (
  231. <Image source={require('../images/common/check.png')} />
  232. ) : null}
  233. </TouchableOpacity>
  234. <TouchableOpacity
  235. activeOpacity={0.9}
  236. style={styles.payDialog}
  237. onPress={() => this.setPayMethod(2)}
  238. >
  239. <View style={styles.dialogRow}>
  240. <Image style={styles.payIcon} source={require('../images/common/aliPay.png')} />
  241. <Text>支付宝支付</Text>
  242. </View>
  243. {this.state.payType === 2 ? (
  244. <Image source={require('../images/common/check.png')} />
  245. ) : null}
  246. </TouchableOpacity>
  247. {/* <TextInput style={styles.payDialog} /> */}
  248. </Animated.View>
  249. </TouchableHighlight>
  250. ) : null}
  251. {/* <Modal
  252. animationType="none "
  253. transparent={true}
  254. visible={true}
  255. onRequestClose={() => {
  256. alert("Modal has been closed.");
  257. }}
  258. ></Modal> */}
  259. </View>
  260. );
  261. }
  262. }
  263. const styles = StyleSheet.create({
  264. top: {
  265. width: Dimensions.width,
  266. flexDirection: 'column',
  267. alignItems: 'center',
  268. paddingBottom: 20
  269. },
  270. title: {
  271. fontSize: 20,
  272. color: '#a8674d',
  273. marginTop: 22
  274. },
  275. itemWrapperNormal: {
  276. borderWidth: 1,
  277. borderColor: '#a8674d',
  278. borderRadius: 27,
  279. backgroundColor: '#fff',
  280. flexDirection: 'row',
  281. alignItems: 'center',
  282. justifyContent: 'space-around',
  283. width: '86%',
  284. height: Dimensions.getHeight(53),
  285. marginTop: 20
  286. },
  287. itemWrapperTap: {
  288. // borderWidth: 1,
  289. // borderColor: '#a8674d',
  290. borderRadius: 27,
  291. backgroundColor: '#ff7525',
  292. flexDirection: 'row',
  293. alignItems: 'center',
  294. justifyContent: 'space-around',
  295. width: '86%',
  296. height: Dimensions.getHeight(53),
  297. marginTop: 20
  298. },
  299. originPriceTap: {
  300. fontSize: 14,
  301. color: '#fff',
  302. textDecorationLine: 'line-through'
  303. },
  304. originPrice: {
  305. fontSize: 14,
  306. color: '#a8674d',
  307. textDecorationLine: 'line-through'
  308. },
  309. priceTap: {
  310. fontSize: 20,
  311. color: '#fff'
  312. },
  313. price: {
  314. fontSize: 20,
  315. color: '#a8674d'
  316. },
  317. timeLengthTap: {
  318. fontSize: 18,
  319. color: '#fff'
  320. },
  321. timeLength: {
  322. fontSize: 18,
  323. color: '#a8674d'
  324. },
  325. payment: {
  326. flexDirection: 'row',
  327. alignItems: 'center',
  328. width: Dimensions.width,
  329. height: 60,
  330. justifyContent: 'space-between',
  331. alignItems: 'center',
  332. borderColor: '#f3f2f7',
  333. borderTopWidth: 6,
  334. borderBottomWidth: 6,
  335. paddingHorizontal: 33
  336. },
  337. left: {
  338. fontSize: 16
  339. },
  340. right: {
  341. flexDirection: 'row',
  342. alignItems: 'center'
  343. },
  344. method: {
  345. color: '#a8674d',
  346. fontSize: 16,
  347. marginRight: 7
  348. },
  349. bottom: {
  350. width: Dimensions.width,
  351. height: 60,
  352. flexDirection: 'row',
  353. position: 'absolute',
  354. bottom: 0
  355. },
  356. bottomLeft: {
  357. width: '58%',
  358. textAlign: 'center',
  359. lineHeight: 60,
  360. color: '#a8674d',
  361. fontSize: 20
  362. },
  363. bottomRight: {
  364. width: '42%',
  365. fontSize: 16,
  366. color: '#fff',
  367. backgroundColor: '#f5880d',
  368. alignItems: 'center'
  369. },
  370. bottomRightText: {
  371. fontSize: 20,
  372. color: '#fff',
  373. textAlign: 'center',
  374. lineHeight: 60
  375. },
  376. dialog: {
  377. width: Dimensions.width,
  378. height: Dimensions.height,
  379. position: 'absolute',
  380. backgroundColor: 'rgba(0,0,0,0.4)'
  381. },
  382. payMethod: {
  383. width: Dimensions.width,
  384. height: 150,
  385. position: 'absolute',
  386. // bottom: 0,
  387. backgroundColor: '#fff',
  388. flexDirection: 'column',
  389. alignItems: 'center',
  390. justifyContent: 'flex-start'
  391. },
  392. payDialog: {
  393. width: '90%',
  394. borderTopWidth: 1,
  395. borderColor: '#e4e4e4',
  396. flexDirection: 'row',
  397. alignItems: 'center',
  398. justifyContent: 'space-between',
  399. flex: 1
  400. },
  401. dialogRow: {
  402. flex: 1,
  403. flexDirection: 'row',
  404. alignItems: 'center'
  405. },
  406. payIcon: {
  407. marginRight: 17
  408. },
  409. payText: {
  410. fontSize: 16,
  411. color: '#191919',
  412. alignContent: 'center',
  413. flex: 1,
  414. lineHeight: 50
  415. }
  416. });