const app = require('../../utils/util.js'); var util = require('../../utils/md5.js') import WxValidate from '../../utils/WxValidate'; Page({ data: { postindex: null, incomeindex: null, educationindex: null, posttext: '请选择', incometext: '请选择', educationtext: '请选择', userName: '', //姓名 idCardNumber: '', //身份证号码 age: '', //年龄 parameter: [{ name: '男' }, { name: '女', }], postList: [{ name: '在职人员' }, { name: '两委人员' }, { name: '社会组织人员' }, { name: '志愿者' }, { name: '学生' }, { name: '其他' }, ], incomeList: [{ name: '暂无' }, { name: '500元以下' }, { name: '500-1000元' }, { name: '1000-2000元' }, { name: '2000-5000元' }, { name: '5000元-1万元' }, { name: '1-2万元' }, { name: '2万元以上' }, ], educationList: [{ name: '小学' }, { name: '初中' }, { name: '高中' }, { name: '大专' }, { name: '本科' }, { name: '本科以上' }, ], }, goOcr() { wx.request({ url: app.globalData.publicUrl + '/applet/getToken', method: "GET", header: { appletsId: wx.getStorageSync('openId') }, success: (res) => { console.log(res.data, "0000"); if (res.data.data.status == 200) { let token = res.data.data.token; if (token) { wx.showLoading({ title: '加载中', }) var _this = this; wx.chooseImage({ count: 1, // 默认9 sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { // console.log('拍照获取图片路径',res); // 读取本地文件内容 wx.showLoading({ title: '加载中', }) wx.getFileSystemManager().readFile({ filePath: res.tempFilePaths[0], encoding: 'base64', success: res1 => { //返回临时文件路径 console.log('读取本地文件basema ', res1.data) let img = 'data:image/png;base64,' + res1.data wx.request({ url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + token, method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded' }, data: { id_card_side: 'front', image: img }, dataType: 'json', success: (e) => { if (e.data.idcard_number_type == 1) { let result = e.data.words_result console.log(result, "00000000"); _this.data.parameter.forEach(item => { if (item.name == result['性别'].words) { item.checked = true; } }) result['性别'].words, _this.setData({ userName: result['姓名'].words, idCardNumber: result['公民身份号码'].words, parameter: _this.data.parameter, }) } else { wx.showToast({ title: "身份证OCR识别失败", icon: 'none', duration: 2000, }) } }, complete: (e) => { wx.hideLoading(); } }) }, // 错误信息 fail: console.error }) }, fail: function () { wx.hideLoading(); } }) } else { wx.showToast({ title: '身份证识别失败,token失效', icon: 'none', duration: 2000, }) } } }, }) }, postListChange: function (e) { this.setData({ postindex: e.detail.value, posttext: '', }) }, incomeListChange: function (e) { this.setData({ incomeindex: e.detail.value, incometext: '', }) }, educationListChange: function (e) { this.setData({ educationindex: e.detail.value, educationtext: '', }) }, initValidate() { const rules = { userName: { required: true, }, sex: { required: true, }, idCardNumber: { required: true, idcard: true, }, age: { required: true, }, job: { required: true, }, income: { required: true, }, education: { required: true, }, }; const messages = { userName: { required: '姓名不能为空', }, sex: { required: '性别不能为空', }, idCardNumber: { required: '身份证号不能为空', idcard: '身份证号格式错误 ', }, age: { required: '年龄不能为空', }, job: { required: '职务不能为空', }, income: { required: '收入不能为空', }, education: { required: '学历不能为空', }, }; // 创建实例对象 this.WxValidate = new WxValidate(rules, messages) }, showModal(error) { wx.showToast({ title: error.msg, icon: 'none', duration: 2000, }) }, formSubmit(e) { console.log(e.detail, "daying"); /***4-3(表单提交校验)*/ const params = e.detail.value if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0] this.showModal(error) return false } /*** 这里添写验证成功以后的逻辑**/ //验证通过以后-> this.submitInfo(params); }, submitInfo(params) { wx.request({ url: app.globalData.publicUrl + '/applet/suppleUserInfo', method: "POST", data: params, header: { appletsId: wx.getStorageSync('openId') }, success: (res) => { if (res.data.code == 0) { console.log(res, "0000"); } } }) }, userInfo() { wx.request({ url: app.globalData.publicUrl + '/sys/login/session', method: "GET", header: { appletsId: wx.getStorageSync('openId') }, success: (res) => { console.log(res.data.data, "000"); this.data.parameter.forEach(item => { if (item.name == res.data.data.sex) { item.checked = true; } }) this.setData({ userName: res.data.data.userName, parameter: this.data.parameter, idCardNumber: res.data.data.idCardNumber, age: res.data.data.age, posttext: res.data.data.job, incometext: res.data.data.income, educationtext: res.data.data.education, }) } }) }, onLoad: function () { this.initValidate(); this.userInfo(); }, })