index.js 4.58 KB
Newer Older
zhanghaozhe committed
1 2
import React, { PureComponent } from "react"
import "./index.scss"
xuzhenghua committed
3

zhanghaozhe committed
4
import { http } from "src/utils"
zhanghaozhe committed
5 6
import { WithFullSize } from "src/HOCs"
import { Toast } from "antd-mobile"
xuzhenghua committed
7 8

class UsePatch extends PureComponent {
zhanghaozhe committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  constructor(props) {
    super(props)
    this.state = {
      success: false,
      checkedIndex: 0,
      list: [
        {
          amount: 10,
          desc: "兑换后7日内有效",
          type: "代金券",
        },
        {
          amount: 20,
          desc: "兑换后7日内有效",
          type: "代金券",
        },
        {
          amount: 50,
          desc: "兑换后7日内有效",
          type: "代金券",
        },
        {
          amount: 100,
          desc: "兑换后7日内有效",
          type: "代金券",
        },
      ],
      couponAmount: "", // 碎片额度
xuzhenghua committed
37
    }
zhanghaozhe committed
38
  }
xuzhenghua committed
39

zhanghaozhe committed
40 41 42
  componentDidMount() {
    this.getPatchList()
  }
xuzhenghua committed
43

zhanghaozhe committed
44 45 46 47 48 49
  // 选中
  select = (index, item) => {
    if (this.state.couponAmount >= item.amount) {
      this.setState({
        checkedIndex: index,
      })
xuzhenghua committed
50
    }
zhanghaozhe committed
51
  }
xuzhenghua committed
52

zhanghaozhe committed
53 54 55 56 57 58 59 60 61 62 63 64
  // 合成
  compound = () => {
    const _this = this
    if (this.state.couponAmount >= 10) {
      http
        .post(`${API.home}/sys/red_packet/compose`, {
          type: _this.state.checkedIndex + 1,
        })
        .then((res) => {
          if (res.data.code === 200) {
            _this.setState({
              success: true,
xuzhenghua committed
65
            })
zhanghaozhe committed
66 67 68 69 70 71 72
            setTimeout(() => {
              _this.getPatchList()
            }, 2000)
          } else {
            Toast.info(res.data.msg, 2)
          }
        })
xuzhenghua committed
73
    }
zhanghaozhe committed
74
  }
xuzhenghua committed
75

zhanghaozhe committed
76 77 78 79 80 81
  getPatchList() {
    http.get(`${API.home}/sys/red_packet/balance`).then((res) => {
      if (res.data.code === 200) {
        this.setState({
          couponAmount: res.data.data.coupon_amount,
          success: false,
xuzhenghua committed
82
        })
zhanghaozhe committed
83 84 85 86 87
      } else {
        Toast.info(res.data.msg, 2)
      }
    })
  }
xuzhenghua committed
88

zhanghaozhe committed
89 90 91 92 93 94
  render() {
    return (
      <div className="my-patch">
        <div className={"couponAmount"}>
          代金券碎片余额:<span>{this.state.couponAmount}</span>
        </div>
xuzhenghua committed
95

zhanghaozhe committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        <Coupon
          data={this.state.list}
          myAmount={this.state.couponAmount}
          select={this.select}
          checkedIndex={this.state.checkedIndex}
        />
        <div className="compound-button--compose">
          <button
            className={`compound ${
              this.state.couponAmount >= 10 ? "disable-active" : "disable"
            }`}
            onClick={this.compound}
          >
            合成
          </button>
        </div>
xuzhenghua committed
112

zhanghaozhe committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        <div className="patch-desc">
          <p className={"title"}>
            <i></i>
            <span>代金券碎片说明</span>
            <i></i>
          </p>
          <p className={"desc-item"}>
            1.
            碎片可通过“分享课程领取红包”获得,也可通过线上活动获得,具体请关注官网信息;
          </p>
          <p className={"desc-item"}>
            2. 碎片在购课时不能单独使用,可合成完整代金券后用代金券抵扣现金;
          </p>
          <p className={"desc-item"}>3. 碎片合成代金券后7日内有效。</p>
        </div>

        {this.state.success && <div className="success">兑换成功</div>}
      </div>
    )
  }
xuzhenghua committed
133 134 135
}

function Coupon(props) {
zhanghaozhe committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  const { data, myAmount, select, checkedIndex } = props
  return (
    <div className={"coupons"}>
      {data &&
        data.length > 0 &&
        data.map((item, index) => {
          return (
            <div
              className="items-box"
              key={index}
              onClick={() => select(index, item)}
            >
              <div
                className={`coupon-info ${
                  myAmount >= item.amount ? "active" : null
                }`}
              >
                <div className="type">{item.type}</div>
                <div className={"amount"}>
                  <span>{item.amount}</span>
                </div>
xuzhenghua committed
157

zhanghaozhe committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                {myAmount >= item.amount && (
                  <i
                    className={`checkout ${
                      index === checkedIndex
                        ? "iconfont icondanseshixintubiao-5"
                        : "nochecked"
                    }`}
                  />
                )}
                <ul>
                  {new Array(19).fill("a").map((item, index) => {
                    return <li key={index} />
                  })}
                </ul>
              </div>
xuzhenghua committed
173

zhanghaozhe committed
174 175 176 177 178 179
              <div className={"coupon-des"}>{item.desc}</div>
            </div>
          )
        })}
    </div>
  )
xuzhenghua committed
180 181
}

zhanghaozhe committed
182
export default WithFullSize(UsePatch)