// pages/home/authory/index.js import { getAuthSession, certificateImage } from "../../api/user" const util = require('../../../utils/util') const event = require('../../../utils/event.js') Page({ /** * 页面的初始数据 */ data: { form: { identityAuthToken: null }, flag: false, // 是否不可以编辑, false:可编辑;true:不可编辑 imgList: [null, null] }, onLoad: function (options) { event.on('EventMessage', this, this.onEvent) wx.showLoading({ title: '加载中', mask: true }) getAuthSession().then(result => { wx.hideLoading() this.data.form.identityAuthToken = result.data.token }).catch(error => { wx.hideLoading() }) }, onEvent: function (message) { if (message.what == 82) { wx.navigateBack() } }, submitForm: function () { if (util.isEmpty(this.data.form.frontImg)) { util.showToast('请上传身份证正面照片') return } if (util.isEmpty(this.data.form.backImg)) { util.showToast('请上传身份证背面照片') return } wx.showLoading({ title: '处理中', mask: true }) certificateImage(this.data.form).then(result => { wx.hideLoading() var tokenJson = JSON.stringify({token: result.data.identityAuthToken, idCardNo: result.data.idCardNo, realName: result.data.realName}) wx.setStorageSync('identityAuthToken', tokenJson) wx.navigateTo({ url: '/pages/home/authory/index' }) }).catch(error => { wx.hideLoading() util.showToast(error) }) }, /*******************************************************图片上传************************************************************/ chooseImage: function(e) { if (this.data.flag) { return } var index = e.currentTarget.dataset.index var that = this wx.chooseImage({ count: 1, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: (res) => { that.setData({ ['imgList[' + index + ']']: res.tempFilePaths[0] }) const fileSystemManager = wx.getFileSystemManager() var type = that.suffixImage(res.tempFilePaths[0]) fileSystemManager.readFile({ filePath: that.data.imgList[index], // 例如图片临时路径 encoding: 'base64', success(res) { let { data } = res // 编码后的数据 if(index == 0){ that.data.form.frontImg = {base64: data, type} } else { that.data.form.backImg = {base64: data, type} } } }) } }) }, suffixImage: function(path){ return path.substring(path.lastIndexOf('.') + 1) }, viewImage: function(e) { var urlList = [] for (var i = 0; i < this.data.imgList.length; i++) { if (!util.isEmpty(this.data.imgList[i])) { urlList.push(this.data.imgList[i]) } } wx.previewImage({ urls: urlList, current: e.currentTarget.dataset.url }) }, deleteImg: function(e) { var index = e.currentTarget.dataset.index this.data.imgList[index] = null; this.setData({ imgList: this.data.imgList }) }, onUnload: function () { event.remove('EventMessage', this) } })