import { axios } from './libs' export default class Coupons { map = {}; updates = {}; constructor() { } async getVerified() { const { getOpenid } = getApp() const openid = await getOpenid() const list = await axios.get('/Api/Coupon/UserCouponList', { "userCode": openid, "couponType": -1, "verified": true, "paging": 0, }) list.forEach(coupon => { this.set(coupon) }) return list.map(({ code }) => code) } async getMallCodes(mallCode, mallName) { const { getOpenid } = getApp() const openid = await getOpenid() const list = await axios.get('/Api/Coupon/MallCouponPublishList', { "mallCode": mallCode, userCode: openid }) list.forEach(coupon => { Object.assign(coupon, { mallCode, mallName }) this.set(coupon) }) return list.map(({ code }) => code) } async getUserCodes(type) { const { getOpenid } = getApp() const openid = await getOpenid() const list = await axios.get('/Api/Coupon/UserCouponList', { "userCode": openid, "couponType": type, "verified": false, "paging": 0, }) list.forEach(coupon => { this.set(coupon) }) return list.map(({ code }) => code) } get(code) { return this.map[code] } set(coupon) { this.map[coupon.code] = { ...this.map[coupon.code], ...coupon } this.updates[coupon.code] && this.updates[coupon.code].forEach(cb => cb(this.map[coupon.code])) return this.map[coupon.code] } onUpdate(code, cb) { this.updates[code] = this.updates[code] ? [...this.updates[code], cb] : [cb] } }