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: '本科以上' }, ], }, // 百度身份证OCR接口 首先去后台获得token 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: ['camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { wx.showLoading({ title: '加载中', }) // 拿文件转换base64 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; } }) var myDate = new Date(); var tYear = myDate.getFullYear(); let aa = parseInt(result['公民身份号码'].words.slice(6, 10)); if (_this.data.userName == result['姓名'].words) { _this.setData({ userName: result['姓名'].words, idCardNumber: result['公民身份号码'].words, parameter: _this.data.parameter, age: tYear - aa }) } else { wx.showModal({ showCancel: false, content: '登录巡访员名字和扫描身份证名字不一致,请重新扫描' }) } } 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: '', }) }, changeIdCard(e) { console.log(e.detail.value, "121212"); var myDate = new Date(); var tYear = myDate.getFullYear(); let aa = parseInt(e.detail.value.slice(6, 10)); console.log(aa); this.setData({ age: tYear - aa }) }, 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"); wx.showModal({ showCancel: false, content: '提交成功', success() { wx.switchTab({ url: '/pages/index/index', }) } }) } } }) }, //获得当前登陆人的信息 userInfo() { wx.request({ url: app.globalData.publicUrl + '/applet/findUserInfo', 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; } }) //赋值问题 let jonIndex = this.data.postList.findIndex(item => { return item.name == res.data.data.job }) if (jonIndex != -1) { this.setData({ posttext: '', postindex: jonIndex, }) } else { this.setData({ posttext: '请选择', }) } let incomeindex = this.data.incomeList.findIndex(item => { return item.name == res.data.data.income }) if (incomeindex != -1) { this.setData({ incometext: '', incomeindex: incomeindex, }) } else { this.setData({ incometext: '请选择', }) } let educationindex = this.data.educationList.findIndex(item => { return item.name == res.data.data.education }) if (educationindex != -1) { this.setData({ educationtext: '', educationindex: educationindex, }) } else { this.setData({ educationtext: '请选择', }) } 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, }) console.log(this.data.postList, "赋值上了么"); } }) }, onLoad: function () { this.initValidate(); this.userInfo(); }, })