const app = require('../../utils/util.js'); const tools = require('../../utils/tools.js'); import WxValidate from '../../utils/WxValidate'; const COS = require('../../utils/cos.js'); Page({ data: { isRz: 0, userName: "", phone: "", card: "", carWin_img: '', //存放照片路径的 cosPath: "" //腾讯云上传的路径 }, // 错误信息回显 showModal(error) { wx.hideLoading() wx.showModal({ content: error.msg, showCancel: false, }) }, openCamera() { wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['', 'camera'], success: (res) => { console.log(res) let path = res.tempFilePaths[0]; // tempFilePath可以作为img标签的src属性显示图片 console.log(path) this.setData({ carWin_img: path }) } }) }, // 提交 tj(params, url) { wx.request({ url: app.globalData.publicUrl + '/wx/student/uthentication', method: "post", data: { studentName: params.userName, studentCard: params.card, faceImage: url, sessionKey: this.data.sessionKey }, success: function (res) { wx.hideLoading() if (res.data.code == 0) { wx.showModal({ showCancel: false, content: "认证成功啦!", success(res) { if (res.confirm) { wx.switchTab({ url: '../index/index' }) } } }) } else { wx.hideLoading() wx.showModal({ showCancel: false, content: res.data.msg }) } } }) }, // 表单提交 跳转tj() async formSubmit(e) { console.log(e,"我是带过来的值") wx.showLoading({ mask: true, title: '认证中', }) if (this.data.isRz == 0) { const params = e.detail.value if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0] wx.hideLoading() this.showModal(error) return false } if (this.data.carWin_img == "" || this.data.carWin_img == null) { wx.hideLoading() wx.showModal({ title: '请点击上方选择照片或者视频哦', showCancel: false }); return false } var Bucket = 'bucketnj1-1254259530'; var Region = 'ap-nanjing'; var ForcePathStyle = false; var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/'; if (ForcePathStyle) { prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/'; } var stsCache; var getCredentials = function (callback) { if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) { callback(data.credentials); return; } wx.request({ method: 'POST', url: app.globalData.publicUrl + '/wx/course/getSts', // dataType: 'json', success: function (result) { var credentials = result.data.credential.credentials; if (credentials) { stsCache = result.data.credential } else { wx.hideLoading() wx.showModal({ title: '临时密钥获取失败', content: JSON.stringify(data), showCancel: false }); } callback(stsCache && stsCache.credentials); }, error: function (err) { wx.hideLoading() wx.showModal({ title: '临时密钥获取失败', content: JSON.stringify(err), showCancel: false }); } }); }; // 计算签名 var getAuthorization = function (options, callback) { getCredentials(function (credentials) { callback({ XCosSecurityToken: credentials.sessionToken, Authorization: COS({ SecretId: credentials.tmpSecretId, SecretKey: credentials.tmpSecretKey, Method: options.Method, Pathname: options.Pathname, }) }); }); }; // 上传文件 var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名 var signPathname = '/'; if (ForcePathStyle) { signPathname = '/' + Bucket + '/'; } getAuthorization({ Method: 'POST', Pathname: signPathname }, (AuthData) => { var requestTask = wx.uploadFile({ url: prefix, name: 'file', filePath: this.data.carWin_img, formData: { 'key': "face/" + Key, 'success_action_status': 200, 'Signature': AuthData.Authorization, 'x-cos-security-token': AuthData.XCosSecurityToken, 'Content-Type': '', }, success: (res) => { console.log(res.header.Location) this.tj(params, res.header.Location) }, fail: function () { wx.hideLoading() wx.showModal({ title: '上传腾讯云失败', showCancel: false }); } }); requestTask.onProgressUpdate(function (res) { console.log('进度:', res.progress); // if (res.progress == 100) { // wx.hideLoading(); // } }); }); } else { wx.hideLoading() wx.showModal({ showCancel: false, content: "您已经认证过了" }) } }, // 如果有就把信息回显出来 getInformation(sessionKey) { // let _this = this; wx.request({ url: app.globalData.publicUrl + '/wx/student/selStudentSessionKeyEcho', method: "post", data: { sessionKey }, success: (res) => { console.log(res); if (res.data.code == 0) { console.log(res) if (res.data.data.studentOpenId != null) { this.setData({ isRz: 1, userName: res.data.data.miniName, carWin_img: res.data.data.faceImage, card: res.data.data.studentCard, }) } } } }) }, async onLoad() { const sessionKey = await tools.checkSessionAndLogin(); this.setData({ sessionKey }) this.getInformation(sessionKey); this.initValidate(); }, // 验证字段的规则 initValidate() { const rules = { userName: { required: true }, // phone: { // required: true, // tel: true, // }, card: { required: true, idcard: true, } } // 验证字段的提示信息,若不传则调用默认的信息 const messages = { userName: { required: '请输入姓名' }, // phone: { // required: '请输入手机号', // tel: '请输入正确的手机号', // }, card: { required: '请输入身份证号码', idcard: '请输入正确的身份证号码', } } // 创建实例对象 this.WxValidate = new WxValidate(rules, messages) } })