idinfo.js 5.6 KB
var OSSInit;
var baseUrl = getApp().globalData.baseUrl;
var OSSUrl = getApp().globalData.OSSUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    facepath: '',
    front_card_info: {},
    backpath: '',
    back_card_info: {},
    idInfo: {},
    flag: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  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() {

  },

  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: '上传图片中...',
    })
    wx.uploadFile({
      url: OSSUrl,
      filePath: path,
      name: 'file',
      formData: {
        'key': 'imagepath_' + path.substring(path.length - 10, path.length),
        'OSSAccessKeyId': OSSInit.access_key_id,
        'policy': OSSInit.policy,
        'signature': OSSInit.signature,
        'callback': OSSInit.callback_body,
        'x:access_token': OSSInit.callback_token,
        'success_action_status': '200',
      },
      success: function(res) {
        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()
          wx.hideLoading()
        } else {
          var title = '图片上传失败,请重新上传'
          if (res.statusCode == 413) {
            title = '图片体积过大,请选择较小图片上传'
          }
          wx.showModal({
            title: '上传失败',
            content: title,
            showCancel: false,
            confirmColor: '#4E8FE7'
          })
        }
      },
      fail: function(err) {
        wx.showModal({
          title: '上传失败',
          content: '图片上传失败,请重新上传',
          showCancel: false,
          confirmColor: '#4E8FE7'
        })
      },
    })
  },

  checkAndupload: function() {
    wx.showLoading({
      title: '解析图片中...',
    })
    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;
    }
    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 < 300) {
          that.setData({
            idInfo: res.data.item
          })
          console.log('suc', that.data.idInfo)
        } else {
          wx.showToast({
            image: "/images/warn.png",
            duration: 1000,
            title: res.data.message?res.data.message:"数据错误"
          });
        }
        wx.hideLoading()
      },
      fail(res) {
        console.log('fail', res.data)
        wx.hideLoading()
      }
    })
  },

  savedata:function(){
    wx.setStorage({
      key: 'id_info',
      data: JSON.stringify(this.data.idInfo),
    })
      wx.navigateBack({
        delta:1
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
      wx.showModal({
        title: '',
        content: '\n您的信息还未保存是否确认返回?\n',
      })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})