toCash.js 6.13 KB
// integration/pages/point/toCash/toCash.js
import MineApi from '../../../api'
import api from '~/api/api'
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  data: {
    sumbitScore: '',
    showRate: false,
    showModal: false,
    tabData: ['阳光激励兑换', '微信兑换'],
    tabIndex: 0,
    user: {}
  },
  onLoad() {
    this.getStaffInfo();
  },
  tabClick(e) {
    let {
      tabData,
      score
    } = this.data
    this.setData({
      tabIndex: e.detail,
      sumbitScore: tabData[e.detail] != '阳光激励兑换' ? score.wechatMotivationWithdrawalScore : score?.motivationWithdrawalScore
    })
  },
  toShowBindModal(e) {
    const {
      name
    } = e.detail
    this.setData({
      [name]: true
    })
  },
  toCloseModal(e) {
    const {
      name
    } = e.currentTarget.dataset
    this.setData({
      [name]: false
    });
  },
  async toBindWechat(e) {
    let _this = this
    if (e.detail.errMsg === "getPhoneNumber:ok") {
      wx.$util.showLoading();
      wx.login({
        success({
          code
        }) {
          if (code) {
            console.log(code,wx.getAccountInfoSync().miniProgram.appId)
            api
              .ssoLogin({
                code,
                appId: wx.getAccountInfoSync().miniProgram.appId
              })
              .then(res => {
                if (res.code == '0') {
                  let {
                    user
                  } = _this.data;
                  let para = {
                    staffUuid: user.staffUuid,
                    openId: res.data.openId,
                    appId: wx.getAccountInfoSync().miniProgram.appId
                  }
                  MineApi.wechatBindApi(para).then((res) => {
                    if (res.code == 0) {
                      wx.$util.showToast('绑定成功')
                      _this.getStaffInfo();
                    }

                  });
                }
              })
          } else {
            wx.$util.showToast('登录失败')
          }
        }
      })
    }
  },
  getStaffInfo() {
    const userStorage = wx.$util.getUserInfo()
    let para = {
      staffUuid: userStorage.uuid,
    };
    MineApi.getStaffDetailApi(para).then((res) => {
      wx.hideLoading();
      const temp = this.handleUserInfo(res.data)
      this.setData({
        user: temp
      })
    });
  },
  handleUserInfo(data) {
    const userStorage = wx.$util.getUserInfo()
    const USERINFO = {
      ...data,
      staffUuid: userStorage.uuid,
    };
    return USERINFO;
  },
  toSumbitData() {
    if (this.submit) return wx.$api.showToast('有一笔兑换正在进行')
    let {
      sumbitScore,
      tabIndex,
      user,
      score: {
        existWechatMotivationWithdrawal
      },
      rateInfo,
      tabData,
    } = this.data
    let withDrawType = 3
    let rateQuery = {}
    const money = this.accSub(Number(sumbitScore), 0)
    if (tabData[tabIndex] == '微信兑换' && !user?.openId) { // 微信兑换方式未绑定微信提示
      this.setData({
        showModal: true
      })
      return
    }
    if (existWechatMotivationWithdrawal && tabData[tabIndex] == '微信兑换') {
      withDrawType = 4
    }
    if (money <= 0) return wx.$api.showToast(`您的积分不足,请确认后重新申请兑换`)
    // if(rateInfo){
    //   rateQuery = {
    //     withdrawalRate:rateInfo,
    //     arrivedAmount:money
    //   }
    //   this.tixianQuery = {
    //     sumbitScore,
    //     withDrawType,
    //     rateQuery
    //   }
    //   this.toShowModal(sumbitScore)
    //   return
    // }
    this.comfirmSumbit(sumbitScore, withDrawType)
  },
  // 提现确认模块
  comfirmSumbit(sumbitScore, withDrawType, rateQuery = {}) {
    let {
      tabIndex,
      tabData,
    } = this.data;
    Dialog.confirm({
      title: '提示',
      message: `请确认是否将${sumbitScore}积分进行兑换`,
    }).then(() => {
      if (this.submit) return
      this.submit = true
      wx.$api.showLoading('提交中')
      MineApi.WithdrawalApi({
        withdrawalScore: sumbitScore,
        withDrawType,
        withdrawalSource: 2,
        wechatAppId: wx.getAccountInfoSync().miniProgram.appId,
        sourceFrom: 1 // 来源 0:活动 1:阳光激励
      }).then(res => {
        wx.hideLoading();
        if (res.code == 0) {
          wx.$router.redirectTo(`integration.success`, {
            title: tabData[tabIndex]
          })
        } else {
          wx.$api.showToast(res.message)
          this.submit = false
        }
      }).catch((err) => {
        wx.$api.showToast(JSON.stringify(err))
        this.submit = false
      })
    }).catch(() => {
      this.submit = false
    });
  },
  onRoute(options) {
    console.log(options)
    let {
      tabData
    } = this.data
    if (!options.score.existWechatMotivationWithdrawal) {
      tabData.findIndex(v => v == '微信兑换') > -1 && tabData.splice(tabData.findIndex(v => v == '微信兑换'), 1)
    }
    this.setData({
      tabData,
      score: options.score,
      sumbitScore: options?.score?.motivationWithdrawalScore
    })
  },

  /**添加手续费提示 */
  toShowModal(sumbitScore) {
    const Rate = Number(this.data.rateInfo)
    const money = this.accSub(Number(sumbitScore), (Number(sumbitScore) * Rate))
    const obj = {
      count: sumbitScore,
      money: money,
      rate: `${Rate*100}%`
    }
    this.setData({
      showRate: true,
      rateMoney: obj
    })
  },
  /**获取手续费 */
  getRateInfo() {
    const userStorage = wx.$storage.get("userInfo");
    MineApi.getRate({
      merchantId: userStorage.merchantId
    }).then(res => {
      this.setData({
        rateInfo: res
      })
    })
  },
  toJump() {
    this.comfirmSumbit(this.tixianQuery.sumbitScore, this.tixianQuery.withDrawType, this.tixianQuery.rateQuery)
  },
  accSub(arg1, arg2) {
    let r1, r2, m, n;
    try {
      r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
  }
})