123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- /*
- *
- */
- import React, { Component } from 'react';
- import {
- Platform,
- StyleSheet,
- Text,
- View,
- Image,
- TouchableOpacity,
- FlatList,
- TouchableHighlight,
- DeviceEventEmitter,
- ScrollView,
- BackHandler,
- ToastAndroid,
- StatusBar,
- Modal,
- Animated,
- TextInput
- } from 'react-native';
- import BasePage from '../BasePage';
- import Dimensions from '../utils/dimensions';
- import TopicTitle from '../components/TopicTitle';
- import CourseTitle from '../components/CourseTitle';
- import wechat from '../utils/wechat';
- import PayServer from '../services/Pay';
- import Alipay from 'react-native-yunpeng-alipay';
- import ModalView from '../utils/ModalUtil';
- export default class Buy extends BasePage {
- state = {
- shopData: [
- {
- appCode: '',
- days: 0,
- gmtCreated: 0,
- gmtModified: 0,
- id: 0,
- title: '',
- originalPrice: '',
- payType: 1,
- price: '',
- sort: 0
- }
- ],
- currentTapindex: 0,
- ifDialogShow: false,
- payType: 1,
- ticketPrice: 0,
- useTicket: false,
- modalVisible: false
- };
- itemTap = (index) => {
- this.setState({
- currentTapindex: index
- });
- };
- renderItem = (item, index) => {
- return (
- <TouchableOpacity
- style={this.state.currentTapindex === index ? styles.itemWrapperTap : styles.itemWrapperNormal}
- onPress={() => {
- this.itemTap(index);
- }}
- key={index}
- >
- <Text style={this.state.currentTapindex === index ? styles.timeLengthTap : styles.timeLength}>
- {item.title}
- </Text>
- <Text style={this.state.currentTapindex === index ? styles.priceTap : styles.price}>
- ¥{item.price}元
- </Text>
- <Text style={this.state.currentTapindex === index ? styles.originPriceTap : styles.originPrice}>
- 原价:¥{item.originalPrice}
- </Text>
- </TouchableOpacity>
- );
- };
- dialogComeout = () => {
- if (this.state.modalVisible) {
- this.setState({
- modalVisible: false
- });
- } else {
- this.setState({
- modalVisible: true
- });
- }
- };
- setPayMethod = (type) => {
- this.setState(
- {
- payType: type
- },
- () => {
- setTimeout(() => {
- this.dialogComeout(false);
- }, 100);
- }
- );
- };
- componentWillMount() {
- //获取订购数据信息
- this.getMember();
- if (this.props.navigation.state.params != undefined) {
- this.choseTicketCallBack(this.props.navigation.state.params.to_ticket);
- }
- BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
- }
- componentWillUnmount() {
- BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
- }
- onBackAndroid = () => {
- if (this.state.ifDialogShow) {
- this.dialogComeout(false);
- } else {
- this.goBack();
- }
- return true;
- };
- async getMember() {
- await PayServer.getMember().then((result) => {
- this.setState({
- shopData: result.data
- });
- });
- }
- choseTicket = () => {
- this.toNextPage('Ticket', { choseTicketCallBack: this.choseTicketCallBack.bind(this) });
- };
- choseTicketCallBack(item) {
- this.setState({
- ticketPrice: item.amount,
- useTicket: true
- });
- }
- getPrice(old_price) {
- return parseInt(old_price) - parseInt(this.state.ticketPrice) < 0
- ? 0
- : parseInt(old_price) - parseInt(this.state.ticketPrice);
- }
- pay = () => {
- let data = this.state.shopData[this.state.currentTapindex];
- switch (this.state.payType) {
- case 1:
- let params = {
- productCode: data.id,
- type: '0',
- preferentialIds: '',
- useVoucher: this.state.useTicket
- };
- PayServer.payByWechat(params).then((result) => {
- wechat.pay(
- result.data.partnerid,
- result.data.prepayid,
- result.data.noncestr,
- result.data.timestamp,
- result.data.package,
- result.data.sign,
- (result) => {
- ToastAndroid.show('支付成功', ToastAndroid.SHORT);
- // console.log('wechat result', result);
- }
- );
- });
- break;
- case 2:
- //阿里支付
- let params_ali = {
- productCode: data.id,
- type: '0',
- preferentialIds: '',
- useVoucher: this.state.useTicket
- };
- //先请求后台给支付数据
- PayServer.payByAli(params_ali).then((result) => {
- Alipay.pay(result.data.response).then(
- function(data) {
- //成功
- console.log(data);
- },
- function(err) {
- //失败
- console.log(err);
- }
- );
- });
- break;
- }
- };
- render() {
- return (
- <View style={{ flex: 1 }}>
- <StatusBar barStyle={'dark-content'} backgroundColor={'white'} translucent={true} />
- <View
- style={{
- height: 50,
- backgroundColor: 'white',
- marginTop: 30
- }}
- >
- <CourseTitle
- width={this.getWindowWidth()}
- title="VIP购买"
- lefttype={1}
- textcolor={'#231F20'}
- backPress={() => this.goBack()}
- // backPress={() => alert("左侧按钮")}
- />
- </View>
- <View style={{ flex: 0.02, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
- <View style={styles.top}>
- <Text style={styles.title}>套餐选择</Text>
- <View>{this.state.shopData.map((item, index) => this.renderItem(item, index))}</View>
- </View>
- <View style={{ flex: 0.01, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
- <TouchableOpacity style={styles.payment} activeOpacity={1} onPress={this.choseTicket.bind(this)}>
- <Text style={styles.left}>使用抵用券</Text>
- <View style={styles.right}>
- <Text style={styles.method}>-¥{this.state.ticketPrice}元</Text>
- <Image source={require('../images/common/arrowRight.png')} />
- </View>
- </TouchableOpacity>
- <TouchableOpacity style={styles.payment} activeOpacity={1} onPress={() => this.dialogComeout()}>
- <Text style={styles.left}>支付方式</Text>
- <View style={styles.right}>
- {this.state.payType === 1 ? (
- <Text style={styles.method}>微信支付</Text>
- ) : (
- <Text style={styles.method}>支付宝支付</Text>
- )}
- <Image source={require('../images/common/arrowRight.png')} />
- </View>
- </TouchableOpacity>
- <View style={{ flex: 0.01, backgroundColor: 'rgba(242, 242, 242, 1)' }} />
- <View>
- <Text style={{ fontSize: 14, width: '100%', textAlignVertical: 'center', textAlign: 'center' }}>
- 开通会员即时生效,有任何问题请联系我们。
- </Text>
- </View>
- <View style={styles.bottom}>
- {this.state.ticketPrice > 0 ? (
- (price = (
- <Text style={styles.bottomLeft}>
- ¥{this.getPrice(this.state.shopData[this.state.currentTapindex].price)}元
- </Text>
- ))
- ) : (
- <Text style={styles.bottomLeft}>¥{this.state.shopData[this.state.currentTapindex].price}元</Text>
- )}
- <TouchableOpacity style={styles.bottomRight} onPress={this.pay.bind(this)}>
- <Text style={styles.bottomRightText}>支付</Text>
- </TouchableOpacity>
- </View>
- {this.getModal()}
- </View>
- );
- }
- getModal() {
- return (
- <ModalView
- close={() => {
- this.setState({ modalVisible: false });
- }}
- visible={this.state.modalVisible}
- customerlayout={{ flex: 1, justifyContent: 'flex-end' }}
- contentView={this.getView}
- />
- );
- }
- getView = () => {
- return (
- <Modal
- animationType="slide"
- transparent={true}
- visible={this.state.modalVisible}
- onRequestClose={() => {
- this.setState({ modalVisible: false });
- }}
- >
- <TouchableHighlight
- onPress={() => {
- this.dialogComeout(false);
- }}
- style={{ ...styles.dialog }}
- underlayColor={0.1}
- activeOpacity={1}
- >
- <View style={{ ...styles.payMethod, bottom: 10 }} onPress={() => {}}>
- <Text style={styles.payText}>选择支付方式</Text>
- <TouchableOpacity
- activeOpacity={0.9}
- style={styles.payDialog}
- onPress={() => this.setPayMethod(1)}
- >
- <View style={styles.dialogRow}>
- <Image style={styles.payIcon} source={require('../images/common/wxPay.png')} />
- <Text>微信支付</Text>
- </View>
- {this.state.payType === 1 ? <Image source={require('../images/common/check.png')} /> : null}
- </TouchableOpacity>
- <TouchableOpacity
- activeOpacity={0.9}
- style={styles.payDialog}
- onPress={() => this.setPayMethod(2)}
- >
- <View style={styles.dialogRow}>
- <Image style={styles.payIcon} source={require('../images/common/aliPay.png')} />
- <Text>支付宝支付</Text>
- </View>
- {this.state.payType === 2 ? <Image source={require('../images/common/check.png')} /> : null}
- </TouchableOpacity>
- {/* <TextInput style={styles.payDialog} /> */}
- <View style={{ flex: 0.2 }} />
- </View>
- </TouchableHighlight>
- </Modal>
- );
- };
- }
- const styles = StyleSheet.create({
- top: {
- width: Dimensions.width,
- flexDirection: 'column',
- alignItems: 'center',
- paddingBottom: 20
- },
- title: {
- fontSize: 20,
- color: '#a8674d',
- marginTop: 22
- },
- itemWrapperNormal: {
- borderWidth: 1,
- borderColor: '#a8674d',
- borderRadius: 27,
- backgroundColor: '#fff',
- flexDirection: 'row',
- alignItems: 'center',
- justifyContent: 'space-around',
- width: '86%',
- height: Dimensions.getHeight(53),
- marginTop: 20
- },
- itemWrapperTap: {
- // borderWidth: 1,
- // borderColor: '#a8674d',
- borderRadius: 27,
- backgroundColor: '#ff7525',
- flexDirection: 'row',
- alignItems: 'center',
- justifyContent: 'space-around',
- width: '86%',
- height: Dimensions.getHeight(53),
- marginTop: 20
- },
- originPriceTap: {
- fontSize: 14,
- color: '#fff',
- textDecorationLine: 'line-through'
- },
- originPrice: {
- fontSize: 14,
- color: '#a8674d',
- textDecorationLine: 'line-through'
- },
- priceTap: {
- fontSize: 20,
- color: '#fff'
- },
- price: {
- fontSize: 20,
- color: '#a8674d'
- },
- timeLengthTap: {
- fontSize: 18,
- color: '#fff'
- },
- timeLength: {
- fontSize: 18,
- color: '#a8674d'
- },
- payment: {
- flexDirection: 'row',
- alignItems: 'center',
- width: Dimensions.width,
- height: 60,
- justifyContent: 'space-between',
- alignItems: 'center',
- borderColor: '#f3f2f7',
- borderTopWidth: 6,
- borderBottomWidth: 6,
- paddingHorizontal: 33
- },
- left: {
- fontSize: 16
- },
- right: {
- flexDirection: 'row',
- alignItems: 'center'
- },
- method: {
- color: '#a8674d',
- fontSize: 16,
- marginRight: 7
- },
- bottom: {
- width: Dimensions.width,
- height: 60,
- flexDirection: 'row',
- position: 'absolute',
- bottom: 0
- },
- bottomLeft: {
- width: '58%',
- textAlign: 'center',
- lineHeight: 60,
- color: '#a8674d',
- fontSize: 20
- },
- bottomRight: {
- width: '42%',
- fontSize: 16,
- color: '#fff',
- backgroundColor: '#f5880d',
- alignItems: 'center'
- },
- bottomRightText: {
- fontSize: 20,
- color: '#fff',
- textAlign: 'center',
- lineHeight: 60
- },
- dialog: {
- width: Dimensions.width,
- height: Dimensions.height,
- position: 'absolute'
- },
- payMethod: {
- width: Dimensions.width,
- height: 150,
- position: 'absolute',
- // bottom: 0,
- backgroundColor: '#fff',
- flexDirection: 'column',
- alignItems: 'center',
- justifyContent: 'flex-start'
- },
- payDialog: {
- width: '90%',
- borderTopWidth: 1,
- borderColor: '#e4e4e4',
- flexDirection: 'row',
- alignItems: 'center',
- justifyContent: 'space-between',
- flex: 1
- },
- dialogRow: {
- flex: 1,
- flexDirection: 'row',
- alignItems: 'center'
- },
- payIcon: {
- marginRight: 17
- },
- payText: {
- fontSize: 16,
- color: '#191919',
- alignContent: 'center',
- flex: 1,
- lineHeight: 50
- }
- });
|