import * as React from 'react'; import { Props, State } from './type'; import * as Styled from './index.style'; import {get, post} from '../utils/request'; import {rem} from '../style/function.style'; import {showMessage} from '../common/toast/toast'; import { getCouponData4Show, getPreferentialText } from '../utils/couponFormat' import { redirectToLogin } from '../utils/login' import { LofterTitle } from '../lofterTitle' import { genSpmLink } from '../utils/statistics' import throttle from '../utils/throttle'; import { BaseComponent } from '../base-component' const isOnline = location.host === 'du.163.com'; const couponPageUrl = '//www.lofter.com/market/fe/html/couponProducts.html?couponId=' const textMap = { 'has-own':'已领取', 'empty-stock':'已抢完 ' } export class LofterCoupon extends React.Component { static defaultProps = new Props(); state = new State(); componentDidMount(){ const ids = this.props.coupons ? this.props.coupons.map(coupon => coupon.id).join(',') : '' const url = `//www.lofter.com/market/cms/coupons.json?couponIds=${ids}` get(url).then(res => { this.setState({ couponsList: res.list }) this.state.couponsList && this.state.couponsList.forEach(item=>{ return this.state.btnStatusMap.set(item.couponId || item.id, item.userHasCoupon ? 'has-own' : '') }) this.forceUpdate() }) } private render1Column(coupon, index) { const couponshow = getCouponData4Show(coupon, 0) const styleColor = this.props.styleColor || 'red'; const fontColor = this.props.fontColor || 'white'; let statusType = this.state.btnStatusMap.get(couponshow.couponId) const text = statusType ? textMap[statusType] : `领 取` return ( {}} styleColor={styleColor} fontColor={fontColor} >
{couponshow.facevalue}
{couponshow.doorsill}

{couponshow.name}

有效期:{couponshow.startTime}至{couponshow.endTime}
查看更多活动商品 {text}
) } private render2Column(coupon, index) { const couponshow = getCouponData4Show(coupon, 0) const styleColor = this.props.styleColor || 'red'; const fontColor = this.props.fontColor || 'white'; let statusType = this.state.btnStatusMap.get(couponshow.couponId) const text = statusType ? textMap[statusType] : `领取` return (
{couponshow.facevalue}
{couponshow.doorsill}
{couponshow.name}
{text}
查看
) } private render3Column(coupon, index) { const couponshow = getCouponData4Show(coupon, 0) const styleColor = this.props.styleColor || 'red'; const fontColor = this.props.fontColor || 'white'; let statusType = this.state.btnStatusMap.get(couponshow.couponId) const text = statusType ? textMap[statusType] : `领取` return (
{couponshow.facevalue}
{couponshow.doorsill}
{couponshow.name}
{text}
查看
) } private setBtnStatus(couponId,status){ this.setState((preState)=>{ preState.btnStatusMap.set(couponId,status) return { btnStatusMap:preState.btnStatusMap } }) } private getCoupon(couponId,e){ console.log(this.state.btnStatusMap); e.stopPropagation(); if(!!this.state.btnStatusMap.get(couponId)){ return showMessage({text:'重复领取~(°ー°〃)'}); } console.log(couponId); if (this.props.activeType === 2) { post('//www.lofter.com/newweb/giftPacks/receive.json', { type: 3, id: couponId, }).then(res => { if (res.code !== 0) { return showMessage(res.msg) } if (res.data.res) { this.setBtnStatus(couponId,'has-own') showMessage({text:'领取成功~o(*≧▽≦)o'}); } else { showMessage('商品库存不足,等待补货中'); } }).catch(error => { showMessage({text:'出了点小错,请重试~(°ー°〃) '}) }) return } post('//www.lofter.com/market/coupon/fetch.json', { couponId: couponId }).then(resp => { if (resp.code === 401) { redirectToLogin() } else if (resp.code === 200) { this.setBtnStatus(couponId,'has-own') showMessage({text:'领取成功~o(*≧▽≦)o'}); } else { if(resp.code === -240){ // 领过 this.setBtnStatus(couponId,'has-own') } if(resp.code === -251){ // 领完 this.setBtnStatus(couponId,'empty-stock') } showMessage({text:`${resp.msg}~(°ー°〃)`}) } }).catch(error => { showMessage({text:'出了点小错,请重试~(°ー°〃) '}) }) } private getCouponThrottle = throttle(this.getCoupon,1000); private seeMoreActProduct(couponId, e){ const spmLink = genSpmLink({url: `${couponPageUrl}${couponId}`, name: 'spmCmspageCoupon'}) setTimeout(() => { location.href = spmLink; }, 200) } public render() { // console.log('render', this.props, this.state); const {style, color, title, coupons=[], type, isEdit, marginTop} = this.props; return ( 0 && 'f-before'}> { title && } {}} onClick={() => {}} > {coupons.map((coupon, index) => ( this[`render${type}Column`](coupon, index) ))} { (!coupons || coupons && coupons.length == 0 )&& isEdit &&
lofter优惠券模块
}
) } }