verify_idcard.js 6.6 KB
// pages/main/verify/verify_idcard/verify_idcard.js
var OSSInit;
var baseUrl = getApp().globalData.baseUrl;
Page({

  /**
   * Page initial data
   */
  data: {
    facepath: '',
    already_getdata: false,
    front_card_info: {},
    backpath: '',
    back_card_info: {},
    idInfo: {},
  },

  onLoad: function (options) {
    this.initOSS()
  },

  initOSS: function () {
    var that = this
    var Authorization = getApp().globalData.Authorization;
    //OSS 上传前init
    wx.showLoading()
    wx.request({
      url: baseUrl + "filemeta/v1/inits",
      header: {
        'Authorization': Authorization
      },
      method: 'POST',
      data: {
        "access_type": "web_upload",
        "action ": "put_object",
        "instance_id": "",
        "object_type": "wx_image"
      },
      success: function (result) {

        OSSInit = result.data
        console.log(' OSS init 成功', OSSInit)
      },
      fail: function (res) {
        console.log('OSS init  失败', res)
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow')
  },

  chooseImage: function (e) {
    console.log(e)
    var that = this
    wx.chooseImage({
      sourceType: ['camera', 'album'],
      // sizeType: ['original'],
      count: 1,
      success: function (res) {
        console.log('success', res)
        if ("id_face" == e.currentTarget.id) {
          that.setData({
            facepath: res.tempFilePaths[0]
          })
          that.uploadImage(that.data.facepath, 'id_face')
        } else if ("id_back" == e.currentTarget.id) {
          that.setData({
            backpath: res.tempFilePaths[0]
          })
          that.uploadImage(that.data.backpath, 'id_back')
        }
      }
    })
  },

  uploadImage: function (path, str) {
    var that = this
    wx.showLoading({
      title: '上传图片中...',
    })
    console.log('key', 'imagepath_' + path.substring(path.length - 10, path.length))
    wx.uploadFile({
      url: getApp().globalData.OSSUrl,
      filePath: path,
      name: 'file',
      formData: {
        'key': 'imagepath_' + path.substring(path.length - 15, path.length),
        'OSSAccessKeyId': OSSInit.access_key_id,
        'policy': OSSInit.policy,
        'signature': OSSInit.signature,
        'callback': OSSInit.callback_body,
        'x:access_token': OSSInit.callback_token,
        // 'x-oss-security-token': OSSInit.security_token,
        'success_action_status': '200',
      },
      success: function (res) {
        console.log('uploadFile', res.data)
        if (res.statusCode == 200) {
          var data = JSON.parse(res.data)
          console.log('上传成功', res)
          that.oss_bucket = data.bucket
          if ("id_face" == str) {
            that.setData({
              front_card_info: data
            })
          } else if ("id_back" == str) {
            that.setData({
              back_card_info: data
            })
          }
          that.checkAndupload()
        } else {
          var title = '图片上传失败,请重新上传'
          if (res.statusCode == 413) {
            title = '图片体积过大,请选择较小图片上传'
          }
          wx.showModal({
            title: '上传失败',
            content: title,
            showCancel: false,
            confirmColor: '#4E8FE7'
          })
        }
        wx.hideLoading()
      },
      fail: function (err) {
        console.log('fail', err)
        wx.showModal({
          title: '上传失败',
          content: '图片上传失败,请重新上传',
          showCancel: false,
          confirmColor: '#4E8FE7'
        })
        wx.hideLoading()
      },
      complete(res) {
        console.log('complete', res)
        wx.hideLoading()
      }
    })
  },

  checkAndupload: function () {
    var that = this
    var frontdata = that.data.front_card_info
    var backdata = that.data.back_card_info
    console.log('frontdata', frontdata)
    console.log('backdata', backdata)
    if (!frontdata || !frontdata.object_id) {
      return;
    }
    if (!backdata || !backdata.object_id) {
      return;
    }
    wx.showLoading({
      title: '解析图片中...',
    })
    var Authorization = getApp().globalData.Authorization;
    wx.request({
      url: baseUrl + 'common/v1/ocr/id-card-verify',
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": Authorization
      },
      data: {
        front_card_copy: that.data.front_card_info,
        back_card_copy: backdata
      },
      success: function (res) {
        console.log('suc', res)
        if (res.statusCode == 200) {
          that.setData({
            already_getdata: true,
            idInfo: res.data.item
          })
          console.log('suc', that.data.idInfo)
        } else {
          wx.showModal({
            showCancel: false,
            title: '',
            content: res.data.message ? res.data.message : "数据错误",
          })
        }
      },
      fail(res) {
        console.log('fail', res.data)
      },
      complete(res) {
        console.log('complete', res.data)
        wx.hideLoading()
      }
    })
  },

  gonext: function () {
    if (this.data.already_getdata) {
      //提交数据 
      wx.request({
        url: baseUrl + 'uaa/v1/users/auth',
        method: 'POST',
        data: {
          id_card_front: front_card_copy,
          id_card_back: back_card_copy
        },
        header: {
          'content-type': 'application/json',
          "Authorization": Authorization
        },
        success: function (res) {
          if (res.statusCode && res.statusCode >= 300) {
            this.showtoast(res.data.message);
          } else {
            this.showtoast('提交成功');
            wx.navigateBack({})
          }
        }
      })
      wx.navigateTo({
        url: '../verify_result/verify_result?result=succ',
      })
    } else {
      wx.navigateTo({
        url: '../verify_result/verify_result?result=fail',
      })
    }
  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function () {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function () {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function () {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function () {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function () {

  }
})