React Native 版本执行0.57的规则
第一版优惠券中心,是删除已领取的状态,后来又改为全部展示,分为已领取、去使用、已使用、已过期几种状态。
import React, { Component } from 'react';
import { StyleSheet, Text, View, ScrollView, Image, ImageBackground, TouchableOpacity, StatusBar } from 'react-native';
import Nav from '../../components/Nav';
import { requestUrl } from '../../network/Url';// 请求url
import Fetch from '../../network/Fetch';
import Loading from '../../components/Loading'; // 加载层
import { StackActions, NavigationActions, NavigationEvents } from 'react-navigation';
import StatusBarModule from '../../components/StatusBarModule';
import { global } from '../../utils/Global';
import { CachedImage } from "react-native-img-cache";
export default class MyRedeemCode extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
list: [], //优惠券数据
itemObj: {}, //首页传入活动ID
wsActivityInfo: {}, //活动信息
userCouponOfActivity: [], //已使用优惠券
couponId: "",//优惠券ID
limitFlag: "",
activity: "",
limitMsg: "", //活动标题提示
wsActivityInfoProduct: [],//推荐商品列表
}
}
componentWillMount() {
if (this.props.navigation.state.params) {
this.setState({
itemObj: this.props.navigation.state.params.data
})
}
}
componentDidMount() {
this._getWsActivityInfo() //获取活动优惠券数据
}
render() {
const { navigate, goBack } = this.props.navigation;
return (
<View style={styles.container}>
<StatusBarModule isLoading={this.state.isLoading} />
<Nav title={"领券中心"} leftClick={() => { goBack() }} />
<ScrollView>
{/* 领券活动公告 */}
{
this.state.limitFlag == '1' ?
<View style={{ justifyContent: "center", alignItems: 'center', marginTop: Px2dp(25) }}>
<Text style={{ fontSize: Px2dp(16), color: Colors.text666 }}>{this.state.limitMsg}</Text>
</View>
:
this.state.limitFlag != '1' && this.state.activity == '0' ?
<View style={{ justifyContent: "center", alignItems: 'center', marginTop: Px2dp(25) }}>
<Text style={{ fontSize: Px2dp(16), color: Colors.text666 }}>活动还未开始,敬请期待~</Text>
</View>
: this.state.limitFlag != '1' && this.state.activity == '2' ?
<View style={{ justifyContent: "center", alignItems: 'center', marginTop: Px2dp(25) }}>
<Text style={{ fontSize: Px2dp(16), color: Colors.text666 }}>很遗憾该活动已结束,下次要快点哦~</Text>
</View>
: null
}
{/* 领券活动标题 */}
{
this.state.limitFlag != '1' && this.state.activity == '1' ?
<ImageBackground source={require('../../images/Bitmap.png')} style={styles.couponTitBox}>
<Text style={styles.couponTit}>{this.state.wsActivityInfo.subTittle}</Text>
</ImageBackground>
: null
}
{/* 领券活动列表 */}
{
this.state.limitFlag != '1' && this.state.activity == '1' &&
this.state.list.length > 0 ?
this.state.list.map((item, index) => {
return (
<ImageBackground key={index} style={[styles.newCoupon, { marginBottom: index == this.state.list.length - 1 ? 0 : Px2dp(5) }]} source={(item.status == "2" || item.status == "1" || item.couponNum == 0) ? require('../../images/yesActiveCoupon.png') : require('../../images/noActiveCoupon.png')} >
<View style={styles.newCouponLf}>
<Text style={styles.newCouponLfPrice}><Text style={styles.newCouponLfPriceTag}>¥</Text>{item.price}</Text>
<Text style={styles.newCouponLfTxt}>优惠券</Text>
</View>
<View style={styles.newCouponCenter}>
<Text style={styles.newCouponCenterTit}>满{item.fulCut}元可用</Text>
<Text style={styles.newCouponCenterdate}>开始时间:{this.state.wsActivityInfo.couponstartDate}</Text>
<Text style={styles.newCouponCenterdate}>结束时间:{this.state.wsActivityInfo.couponendDate}</Text>
<Text numberOfLines={1} style={styles.newCouponCenterdit}>详细说明:{item.remarks}</Text>
</View>
{
//优惠券底部按钮
this._couponDom(item)
}
</ImageBackground>
)
}) :
null
}
{/* 推荐列表 */}
{
this.state.wsActivityInfoProduct.length > 0 ?
<View style={{ flexDirection: "row", justifyContent: "center", alignItems: "center", marginBottom: Px2dp(15), marginTop: Px2dp(10) }}>
<Text style={{ fontSize: Px2dp(20), color: Colors.text333 }}>热门推荐</Text>
</View>
: null
}
{
this.state.wsActivityInfoProduct.length > 0 ?
<View style={styles.wsActivityInfoProduct}>
{
this.state.wsActivityInfoProduct.map((item, index) => {
return (
<TouchableOpacity
style={[styles.mainLi, { marginRight: Px2dp(14) }]}
activeOpacity={.8}
onPress={() => {
navigate("Commoditydetails", { "goodsInfo": { id: item.product_id } })
}}
key={index}
>
<CachedImage
source={{ uri: item.list_image }}
style={{ width: Px2dp(105), height: Px2dp(105), }} />
<Text numberOfLines={2} style={styles.mainLiTit}>{item.full_name}</Text>
<Text style={styles.mainPrice}>¥{(item.member_price / 100).toFixed(2)}</Text>
<Text style={styles.mainTxt}>¥{(item.market_price / 100).toFixed(2)}</Text>
</TouchableOpacity>
)
})
}
</View>
: null
}
</ScrollView>
<View style={styles.couponTitBom}>
<TouchableOpacity activeOpacity={0.8} style={[styles.commonBomBtn]} onPress={() => {
if (UserInfo.id) {
navigate("Coupon");
} else {
navigate("Login")
}
}}>
<Text style={styles.secondBtnTxt}>查看我的优惠券</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={0.8}
style={styles.commonBomBtn}
onPress={() => {
goBack()
}}>
<Text style={styles.firstBtntxt}>去逛</Text>
</TouchableOpacity>
</View>
{/* 加载loading */}
{this.state.isLoading ? <Loading /> : null}
</View>
);
}
//获取活动优惠券数据
_getWsActivityInfo() {
this.setState({
isLoading: true,
})
Fetch(requestUrl.URL, {
"userId": UserInfo.id ? UserInfo.id : '',
"wsActivityId": this.state.itemObj.bannerRedirectId
}).then(data => {
console.log(data)
if (data.status == "SUCCESS") {
// let wsCouponInfoList = [];
// wsCouponInfoList = data.data.wsCouponInfoList;
// let userCouponOfActivity = [];
// userCouponOfActivity = data.data.userCouponOfActivity;
// for (let i = wsCouponInfoList.length - 1; i >= 0; i--) {
// for (let j = userCouponOfActivity.length - 1; j >= 0; j--) {
// if (wsCouponInfoList[i].couponId == userCouponOfActivity[j].couponId) {
// wsCouponInfoList.splice(i, 1);
// break;
// }
// }
// }
var startTime = data.data.wsActivityInfo.startDate;
var endrtTime = data.data.wsActivityInfo.endDate;
var activity = "1";
var nowTime = new Date();
if (startTime > nowTime.getTime()) {
activity = "0";
}
if (endrtTime < nowTime.getTime()) {
activity = "2";
}
if (data.data.wsActivityInfo == null) {
activity = "0";
}
if ("0" == data.data.wsActivityInfo.status) {
activity = "2";
}
//status,0:未使用,1:已使用,2:过期
let newArr = []
newArr = data.data.wsCouponInfoList
newArr.map((items) => {
data.data.userCouponOfActivity.map((item) => {
if (items.couponId == item.couponId) {
console.log(item.couponNum)
if (item.status == "0") {
items.status = "0"
}
else if (item.status == "1") {
items.status = "1"
}
else if (item.status == "2") {
items.status = "2"
}
else {
return true
}
}
})
})
var userInfo = data.data.userInfo
if (UserInfo) {
var userFlag;
if (userInfo.status != "1") {
userFlag = '0';
} else if (userInfo.agentFlag == "1") {
userFlag = '4';
} else if (userInfo.memberFlag == "1") {
userFlag = '3';
} else if (userInfo.orderNum > 0) {
userFlag = '2';
} else {
userFlag = '1';
}
var limitFlag = "0";//1 受限 0无限制
var limitMsg = "欢迎";//限制信息
var orderNum = userInfo.orderNum;
var flag = data.data.wsActivityInfo.flag
if (userFlag == "0") {
limitFlag = "1";
limitMsg = "对不起,您无法参加该活动";
} else if ("0" == flag || userFlag == flag) {
limitFlag = "0";
limitMsg = "欢迎";
} else {
//单独对新人活动进行处理 订单为零的算新人
if ("1" == flag && orderNum < 1) {//新人活动且订单数小于1
limitFlag = "0";
limitMsg = "欢迎";
} else {
limitFlag = "1";
limitMsg = "此活动仅限" + this.getLimitMsg(flag) + "参加";
}
}
}
this.setState({
isLoading: false,
activity: activity,
limitFlag: limitFlag,
limitMsg: limitMsg,
wsActivityInfoProduct: data.data.wsActivityInfoProduct,
wsActivityInfo: data.data.wsActivityInfo,
list: newArr
})
} else if (data.status == "ERROR") {
this.setState({
isLoading: false,
})
ToastShow({ "text": '获取活动优惠券数据失败' })
} else {
this.setState({
isLoading: false,
})
}
})
}
getLimitMsg(flag) {
var msg = "";
switch (flag) {
case "1": msg = "新人"; break;
case "2": msg = "普通用户"; break;
case "3": msg = "会员"; break;
case "4": msg = "店主"; break;
}
return msg;
}
//渲染优惠券按钮
_couponDom(item) {
const { navigate, goBack } = this.props.navigation;
switch (item.couponNum == 0 ? "2" : String(item.status)) {
case '0':
// 去使用
return (
<TouchableOpacity activeOpacity={0.8} style={[styles.btnBox, { borderColor: "rgba(112,202,129,1)", }]} onPress={() => {
if (item.productIds == "0" && item.providerId == "" && item.secondCate == "") {
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Home' })],
});
this.props.navigation.dispatch(resetAction);
}
else {
navigate('SearchResult', { couponId: item.coupon_id ? item.coupon_id : item.couponId, providerId: item.providerId, product_ids: item.productIds, secondCate: item.secondCate })
}
}}>
<Text style={[styles.btnTxt, { color: "rgba(112,202,129,1)", }]}>去使用</Text>
</TouchableOpacity>
)
break;
case '1':
// 已使用
return (
<TouchableOpacity activeOpacity={0.8} style={[styles.btnBox, { borderColor: "#9B9B9B" }]} onPress={() => {
}}>
<Text style={[styles.btnTxt, { color: "#9B9B9B" }]}>已使用</Text>
</TouchableOpacity>
)
break;
case '2':
// 已抢完
return (
<TouchableOpacity activeOpacity={0.8} style={[styles.btnBox, { borderColor: "#9B9B9B" }]} onPress={() => {
}}>
<Text style={[styles.btnTxt, { color: "#9B9B9B" }]}>已抢完</Text>
</TouchableOpacity>
)
break;
default:
//点击领取
return (
<TouchableOpacity activeOpacity={0.8} style={[styles.btnBox, { borderColor: "rgba(231,95,111,1)" }]} onPress={() => {
this._selectCoupon(item.id)
}}>
<Text style={[styles.btnTxt, { color: "rgba(231,95,111,1)" }]}>点击领取</Text>
</TouchableOpacity>
)
break;
}
}
//领取活动优惠券
_selectCoupon(id) {
this.setState({
isLoading: true,
})
Fetch(requestUrl.URL, {
"userId": UserInfo.id,
"activityId": this.state.itemObj.bannerRedirectId,
"actiCouId": id
}).then(data => {
console.log(data)
if (data.status == "success") {
this.setState({
isLoading: false,
})
this._getWsActivityInfo()
} else if (data.status == "ERROR") {
this.setState({
isLoading: false,
})
ToastShow({ "text": '领取活动优惠券失败' })
} else if (data.status == '1') {
this.setState({
isLoading: false,
})
ToastShow({ "text": '该优惠券已抢光' })
} else {
this.setState({
isLoading: false,
})
}
})
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Colors.mainBg,
},
//领券标题
couponTitBox: {
marginBottom: Px2dp(14),
width: SCREEN_WIDTH,
height: Px2dp(185)
},
couponTit: {
fontSize: Px2dp(24),
color: Colors.red,
marginTop: Px2dp(47),
marginLeft: Px2dp(30),
lineHeight: Px2dp(30),
width: Px2dp(168),
},
//优惠券
commonCode: {
width: Px2dp(343),
height: Px2dp(116),
flexDirection: 'row',
marginLeft: Px2dp(16),
justifyContent: 'space-between',
position: "relative"
},
moneyBox: {
justifyContent: "center",
marginLeft: Px2dp(12),
alignItems: "center",
flex: 1,
},
money: {
fontSize: Px2dp(20),
color: Colors.red,
lineHeight: Px2dp(28)
},
moneyTxt: {
fontSize: Px2dp(13),
color: Colors.text666,
lineHeight: Px2dp(18),
marginTop: Px2dp(3)
},
detailInfo: {
justifyContent: "center",
marginLeft: Px2dp(27)
},
detailInfoTit: {
fontSize: Px2dp(18),
color: Colors.text333,
lineHeight: Px2dp(25)
},
detailInfoTxt: {
fontSize: Px2dp(12),
color: Colors.text999,
lineHeight: Px2dp(17),
marginTop: Px2dp(10),
width: Px2dp(164)
},
detailInfoDate: {
fontSize: Px2dp(11),
color: Colors.text999,
lineHeight: Px2dp(16),
marginTop: Px2dp(6)
},
btnBox: {
width: Px2dp(70),
height: Px2dp(25),
top: Px2dp(40),
right: Px2dp(25),
borderWidth: Pixel,
alignItems: "center",
justifyContent: "center",
position: "absolute"
},
btnTxt: {
fontSize: Px2dp(13),
},
// 底部跳转按钮
couponTitBom: {
backgroundColor: Colors.white,
height: Px2dp(50) + TabBar,
paddingBottom: TabBar,
flexDirection: 'row',
justifyContent: "space-between",
paddingLeft: Px2dp(24),
paddingRight: Px2dp(24)
},
commonBomBtn: {
alignItems: "center",
justifyContent: "center"
},
firstBtntxt: {
fontSize: Px2dp(16),
color: Colors.text333
},
secondBtnTxt: {
fontSize: Px2dp(16),
color: Colors.red
},
//推荐列表
mainLi: {
width: Px2dp(105)
},
mainLiTit: {
fontSize: Colors.text333,
padding: 0,
fontSize: Px2dp(12),
minHeight: Px2dp(30),
marginTop: Px2dp(8),
paddingLeft: Px2dp(4),
paddingRight: Px2dp(4)
},
mainTxt: {
fontSize: Px2dp(12),
color: Colors.text999,
paddingBottom: Px2dp(15),
textDecorationLine: "line-through"
},
mainPrice: {
fontSize: Px2dp(15),
color: Colors.red,
marginTop: Px2dp(6),
},
wsActivityInfoProduct: {
paddingLeft: Px2dp(16),
flexDirection: "row",
flexWrap: "wrap",
alignItems: "center",
backgroundColor: Colors.white,
marginBottom: Px2dp(30),
paddingTop: Px2dp(15)
},
//新领券样式
newCoupon: {
width: Px2dp(370),
height: Px2dp(124),
flexDirection: "row",
position: "relative",
},
newCouponLf: {
justifyContent: "center",
alignItems: "center",
width: Px2dp(110),
paddingLeft: Px2dp(15)
},
newCouponLfPrice: {
fontSize: Px2dp(30),
color: Colors.white,
fontWeight: "600"
},
newCouponLfPriceTag: {
fontSize: Px2dp(20),
color: Colors.white,
fontWeight: "600"
},
newCouponLfTxt: {
fontSize: Px2dp(15),
color: Colors.white,
marginTop: Px2dp(10)
},
newCouponCenter: {
justifyContent: 'center',
marginLeft: Px2dp(25)
},
newCouponCenterTit: {
fontSize: Px2dp(14),
color: Colors.text333,
marginBottom: Px2dp(7),
fontWeight: "500"
},
newCouponCenterdate: {
fontSize: Px2dp(12),
color: Colors.text999,
lineHeight: Px2dp(15)
},
newCouponCenterdit: {
fontSize: Px2dp(12),
color: "rgba(74,74,74,1)",
marginTop: Px2dp(7),
fontWeight: "400"
}
});