// pages/home/authory/index.js import Scene from '../../index/scene' import { getAuthSession, certificateImage, certificateIdentity} from "../../../api/user" import Dialog from '../../../components/dialog/dialog' const util = require('../../../utils/util') const event = require('../../../utils/event') const app = getApp() Scene({ /** * 页面的初始数据 */ data: { step: 0, form: { identityAuthToken: null }, flag: false, // 是否不可以编辑, false:可编辑;true:不可编辑 imgList: [null, null] }, onLoad: function(){ wx.showLoading({ title: '加载中', mask: true }) getAuthSession().then(result => { wx.hideLoading() this.data.form.identityAuthToken = result.data.token }).catch(error => { wx.hideLoading() }) }, submitImage: 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() this.data.form.identityAuthToken = result.data.token this.setData({ step: 1, form: result.data }) }).catch(error => { wx.hideLoading() util.showToast(error) }) }, submitForm: function(){ if(util.isEmpty(this.data.form.realName)){ util.showToast('请输入真实姓名') return } if(util.isEmpty(this.data.form.certificateNo)){ util.showToast('请输入身份证号码') return } wx.showLoading({ title: '处理中', mask: true }) certificateIdentity(this.data.form).then(result => { wx.hideLoading() app.userInfo.isAuth = 1 app.userInfo.realName = this.data.form.realName event.emit('EventMessage', { what: 82, desc: 'Auth Success' }) Dialog.alert({ title: '温馨提示', message: '个人信息认证成功' }).then(() => { wx.navigateBack() }) }).catch(error => { wx.hideLoading() util.showToast(error) }) }, nextStep: function(){ this.setData({ step: 1 }) }, bindInput: function (e) { this.data.form[e.currentTarget.id] = e.detail.value }, /*******************************************************图片上传************************************************************/ 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 }) } })