EnableWallet.js 2.65 KB
// pages/Login/components/EnableWallet/EnableWallet.js
import commonApi from '~/api/module/common.js'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean
    },
    walletObj: {
      type: Object,
      value: {},
      observer: function(n) {
        this.setData({
          copyWalletObj: JSON.parse(JSON.stringify(n))
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    stepDataArr: [{
      icon: 'wallet-icon-1',
      label: '开通钱包',
      tips: '一站式积分管理体验'
    }, {
      icon: 'wallet-icon-2',
      label: '查询积分',
      tips: '您的第一笔可用积分',
      pointsFlag: true
    }, {
      icon: 'wallet-icon-3',
      label: '积分随心用',
      tips: '兑换商品/分享亲友/话费充值, 想怎么用就怎么用!'
    }]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 关闭弹窗
    closeWallet() {
      let { copyWalletObj } = this.data
      this.setData({show: false})
      // 仅替换积分数据返回的内容,使用copyWalletObj 防止修改弹窗内容也做了替换
      this.replaceNickName(copyWalletObj)
    },
    // 替换积分钱包名称
    replaceNickName(data) {
      let userInfo = wx.p.cache.userInfo.getUserInfo()
      commonApi.replaceWalletName({
        nickName: data?.name,
        phone: data?.phone,
        scoreSource: data?.scoreSource
      }).then(res => {
        let { code, message } = res
        if (code == 0) {
          wx.p.storage.set('userInfo', {...userInfo, nickName: data?.name}) // 更新storage中的nickName
        } else {
          wx.p.wxApi.showToast(message)
        }
      })
    },
    input(e) {
      let { target } = e.currentTarget.dataset
      let { value } = e.detail
      value = target == 'phone' ? value.replace(/[^0-9]/g, '') : value
      this.setData({
        [`walletObj.${target}`]: value
      })
    },
    // 一键开通积分钱包
    enableWalletConfirm() {
      let { walletObj } = this.data
      commonApi.openPointsWallet(walletObj).then(res => {
        let { code, message } = res
        if (code == 0) {
          this.setData({
            showEnableSuccess: true,
            show: false
          })
          this.replaceNickName(walletObj)
        } else {
          wx.p.wxApi.showToast(message)
        }
      })
    },
    // 开通
    confirm() {
      let { walletObj } = this.data
      if (!walletObj?.name) return wx.p.wxApi.showToast('姓名不能为空')
      if (!walletObj?.phone || !/^[1]([3-9])[0-9]{9}$/.test(walletObj?.phone)) return wx.p.wxApi.showToast('请输入正确手机号')
      this.enableWalletConfirm()
    }
  }
})