forked from gzzfw/frontEnd/gzDyh

liuwh
2024-09-13 6613c11627b5e0be1629f3784acf0245a8040ea1
gz-wxparty/pages/realNameAuthentication/index.js
@@ -3,182 +3,213 @@
// 实名认证
function realNameAuthenticationApi(submitData) {
   return $$.request({ url: 'paUser/realAuth', type: 'post', submitData, service: 'cust' });
  return $$.request({
    url: 'paUser/realAuth',
    type: 'post',
    submitData,
    service: 'cust'
  });
}
Page({
   /**
    * 页面的初始数据
    */
   backNum: 1, // 如果从完善资料进入则返回两级到个人中心
   data: {
      steps: ['完善信息', '刷脸认证'],
      submitData: {
         trueName: '',
         mobile: '',
         idcardType: '09_00015-1',
         idcardTypeName: '身份证',
         idcard: '',
         prov: '',
         provName: '',
         city: '',
         cityName: '',
         area: '',
         areaName: '',
         road: '',
         roadName: '',
         village: '',
         villageName: '',
         addr: '',
         agree: false,
      },
      popup: {}, // 下拉弹出框数据
   },
  /**
   * 页面的初始数据
   */
  backNum: 1, // 如果从完善资料进入则返回两级到个人中心
  data: {
    steps: ['完善信息', '刷脸认证'],
    submitData: {
      trueName: '',
      mobile: '',
      idcardType: '09_00015-1',
      idcardTypeName: '身份证',
      idcard: '',
      prov: '',
      provName: '',
      city: '',
      cityName: '',
      area: '',
      areaName: '',
      road: '',
      roadName: '',
      village: '',
      villageName: '',
      addr: '',
      agree: false,
    },
    popup: {}, // 下拉弹出框数据
  },
   // 展示弹出层
   handleShowPopup(e) {
      this.setData({ popup: e.detail });
   },
  // 展示弹出层
  handleShowPopup(e) {
    this.setData({
      popup: e.detail
    });
  },
   // 关闭弹出层
   handleClosePopup() {
      this.data.popup.visible = false;
      this.setData({ popup: this.data.popup });
   },
  // 关闭弹出层
  handleClosePopup() {
    this.data.popup.visible = false;
    this.setData({
      popup: this.data.popup
    });
  },
   // 下拉框确认选择
   handleConfirmPicker({ detail }) {
      let type = this.data.popup.type;
      let value = detail.detail.value;
      let arr = [];
      this.data.popup.visible = false;
      if (type === 'location') {
         arr = [
            ['prov', 'provName'],
            ['city', 'cityName'],
            ['area', 'areaName'],
            ['road', 'roadName'],
            ['village', 'villageName'],
         ];
         arr.forEach((x, t) => {
            this.data.submitData[x[0]] = value[t]?.value || '';
            this.data.submitData[x[1]] = value[t]?.label || '';
         });
      } else {
         if (type === 'cardType') {
            arr = ['idcardType', 'idcardTypeName'];
         } else if (type === 'sex') {
            arr = ['sex', 'sexName'];
         }
         arr.forEach((x, t) => {
            this.data.submitData[x] = value[t === 0 ? 'value' : 'label'];
         });
      }
      this.setData({ popup: this.data.popup, submitData: this.data.submitData });
   },
  // 下拉框确认选择
  handleConfirmPicker({
    detail
  }) {
    let type = this.data.popup.type;
    let value = detail.detail.value;
    let arr = [];
    this.data.popup.visible = false;
    if (type === 'location') {
      arr = [
        ['prov', 'provName'],
        ['city', 'cityName'],
        ['area', 'areaName'],
        ['road', 'roadName'],
        ['village', 'villageName'],
      ];
      arr.forEach((x, t) => {
        this.data.submitData[x[0]] = value[t]?.value || '';
        this.data.submitData[x[1]] = value[t]?.label || '';
      });
    } else {
      if (type === 'cardType') {
        arr = ['idcardType', 'idcardTypeName'];
      } else if (type === 'sex') {
        arr = ['sex', 'sexName'];
      }
      arr.forEach((x, t) => {
        this.data.submitData[x] = value[t === 0 ? 'value' : 'label'];
      });
    }
    this.setData({
      popup: this.data.popup,
      submitData: this.data.submitData
    });
  },
   // form的change
   handleChange(e) {
      const { key, value } = e.detail;
      this.data.submitData[key] = value;
      this.setData({ submitData: this.data.submitData });
   },
  // form的change
  handleChange(e) {
    const {
      key,
      value
    } = e.detail;
    this.data.submitData[key] = value;
    this.setData({
      submitData: this.data.submitData
    });
  },
   // 修改picker的列时触发
   handleChangeColumns(e) {
      if (this.data.popup.type === 'location') {
         let selectData = $$.changeLocation(e, this.selectComponent('#personal-data-dom').location, this.data.popup.selectData);
         this.setData({ popup: { ...this.data.popup, selectData } });
      }
   },
  // 修改picker的列时触发
  handleChangeColumns(e) {
    if (this.data.popup.type === 'location') {
      let selectData = $$.changeLocation(e, this.selectComponent('#personal-data-dom').location, this.data.popup.selectData);
      this.setData({
        popup: {
          ...this.data.popup,
          selectData
        }
      });
    }
  },
   // 进行刷脸认证
   handleRealName() {
      let submitData = this.data.submitData;
      let msg = '';
      if (!submitData.trueName) {
         msg = '请输入姓名';
      } else if (!submitData.idcard) {
         msg = '请输入证件号码';
      } else if (!$$.mobileRegExp(submitData.mobile)) {
         msg = '请输入正确的手机号码';
      } else if (!submitData.agree) {
         msg = '请勾选确认信息';
      }
      if (msg) {
         $$.showToast({ title: msg });
         return false;
      }
      let that = this;
      wx.checkIsSupportFacialRecognition({
         success() {
            wx.startFacialRecognitionVerify({
               name: that.data.submitData.trueName,
               idCardNumber: that.data.submitData.idcard,
               success() {
                  console.log('识别成功');
                  that.realNameAuthentication();
               },
               fail() {
                  $$.showToast({ title: '抱歉!识别失败,请稍后重试' });
               },
            });
         },
         fail(res) {
            $$.showToast({ title: '抱歉!请求失败,稍后重试' });
         },
      });
   },
   // 上传实名认证数据
   async realNameAuthentication() {
      $$.showLoading();
      const res = await realNameAuthenticationApi(this.data.submitData);
      $$.hideLoading();
      if (res.type) {
         wx.getStorage({
            key: 'userInfo',
            success(res2) {
               res2.data.realStatus = '1';
               wx.setStorage({ key: 'userInfo', data: res2.data });
            },
         });
            $$.showToast({ icon: 'success', title: '实名认证成功' });
            wx.removeStorage({
                key: 'access_token'
  // 进行刷脸认证
  handleRealName() {
    let submitData = this.data.submitData;
    let msg = '';
    if (!submitData.trueName) {
      msg = '请输入姓名';
    } else if (!submitData.idcard) {
      msg = '请输入证件号码';
    } else if (!$$.mobileRegExp(submitData.mobile)) {
      msg = '请输入正确的手机号码';
    } else if (!submitData.agree) {
      msg = '请勾选确认信息';
    }
    if (msg) {
      $$.showToast({
        title: msg
      });
      return false;
    }
    let that = this;
    wx.checkIsSupportFacialRecognition({
      success() {
        wx.startFacialRecognitionVerify({
          name: that.data.submitData.trueName,
          idCardNumber: that.data.submitData.idcard,
          success() {
            console.log('识别成功');
            that.realNameAuthentication();
          },
          fail() {
            $$.showToast({
              title: '抱歉!识别失败,请稍后重试'
            });
         await $$.sleep();
         wx.navigateBack({
            delta: this.backNum || 1,
         });
      }
   },
          },
        });
      },
      fail(res) {
        $$.showToast({
          title: '抱歉!请求失败,稍后重试'
        });
      },
    });
  },
   // 获取个人信息
   async getUserInfo(e) {
      let data = e.detail || {};
      let keys = Object.keys(this.data.submitData);
      keys.forEach((x) => {
         if (data[x]) {
            this.data.submitData[x] = data[x];
         }
      });
      this.setData({ submitData: this.data.submitData });
   },
  // 上传实名认证数据
  async realNameAuthentication() {
    $$.showLoading();
    const res = await realNameAuthenticationApi(this.data.submitData);
    $$.hideLoading();
    if (res.type) {
      this.getUserInfo()
      $$.showToast({
        icon: 'success',
        title: '实名认证成功'
      });
      wx.removeStorage({
        key: 'access_token'
      });
      await $$.sleep();
      wx.navigateBack({
        delta: this.backNum || 1,
      });
    }
  },
   // 获取手机号码
   async handleGetPhoneNumber(e) {
      let phone = await this.selectComponent('#common-page').handleGetPhoneNumber(e.detail);
      this.data.submitData.mobile = phone;
      this.setData({ submitData: this.data.submitData });
   },
  // 获取个人信息
  async getUserInfo(e) {
    let data = e.detail || {};
    let keys = Object.keys(this.data.submitData);
    keys.forEach((x) => {
      if (data[x]) {
        this.data.submitData[x] = data[x];
      }
    });
    this.setData({
      submitData: this.data.submitData
    });
  },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      if (options.backNum) {
         this.backNum = 2;
      }
   },
});
  // 获取手机号码
  async handleGetPhoneNumber(e) {
    let phone = await this.selectComponent('#common-page').handleGetPhoneNumber(e.detail);
    this.data.submitData.mobile = phone;
    this.setData({
      submitData: this.data.submitData
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.backNum) {
      this.backNum = 2;
    }
  },
});