// pages/realNameAuthentication/realNameAuthentication.ts import { getOcrKeyApi, authenticateApi } from "../../service/hwUser"; import { upLoadImage } from "../../utils/util"; enum IdCardType { headPortrait, nationalEmblem } const ocrSdk = require("../../ocrsdk/index"); const fs = wx.getFileSystemManager() Page({ /** * 页面的初始数据 */ data: { IdCardType, secretInfo: { secretId: "", secretKey: "" }, headPortraitImgBase64: '', headPortraitImg: '', name: '', idCard: '', nationalEmblemImgBase64: '', nationalEmblemImg: '', canSubmit: false }, submit() { if (this.checkInfo()) { const data: { id: string idcardFront: string idcardBack: string name: string idcardNumber: string setIdcardFront: string setIdcardBack: string } = { id: wx.getStorageSync("userId"), idcardFront: this.data.headPortraitImg, idcardBack: this.data.nationalEmblemImg, name: this.data.name, idcardNumber: this.data.idCard, setIdcardFront: '', setIdcardBack: '', }; upLoadImage({ url: '/api/hwUser/imgUpload', files: [data.idcardFront], name: 'file', formData: { upType: 1, } }).then(res => { console.log(res); data.setIdcardFront = res[0].imgUrl upLoadImage({ url: '/api/hwUser/imgUpload', files: [data.idcardBack], name: 'file', formData: { upType: 2, } }).then(res => { console.log(res); data.setIdcardBack = res[0].imgUrl authenticateApi({ ...data }).then(res => { console.log(res); wx.redirectTo({ url: "/pages/realNameAuthentication/success", }); }) }) }) console.log(data); } }, async chooseImage(e: any) { console.log(e); const cardType: number = e.currentTarget.dataset.idCardType const data = await this.ocrSdkStart(cardType) console.log(data); console.log(cardType); console.log(data); const filePath = `${wx.env.USER_DATA_PATH}/${cardType === IdCardType.headPortrait ? "identificationIdFrontPic" : "identificationIdBackPic"}.png`; fs.unlink({ filePath, complete: () => { const img = JSON.parse(data.AdvancedInfo).IdCard; fs.writeFile({ filePath, data: img, encoding: "base64", success: () => { if (cardType === IdCardType.headPortrait) { this.setData({ headPortraitImgBase64: "data:image/png;base64," + img, headPortraitImg: filePath, name: data.Name, idCard: data.IdNum }) } if (cardType === IdCardType.nationalEmblem) { this.setData({ nationalEmblemImgBase64: "data:image/png;base64," + img, nationalEmblemImg: filePath }) } this.setData({ canSubmit: this.checkInfo(true) }) }, }); }, }); }, ocrSdkStart(cardType: number): Promise<{ AdvancedInfo: string, Name: string, IdNum: string }> { return new Promise((resolve, reject) => { ocrSdk.start({ getAuthorization: () => { return new Promise((resolve) => { resolve({ tmpSecretId: this.data.secretInfo.secretId, tmpSecretKey: this.data.secretInfo.secretKey, }); }); }, ocrType: cardType === IdCardType.nationalEmblem ? ocrSdk.OcrType.ID_CARD_BACK : ocrSdk.OcrType.ID_CARD_FRONT, ocrOption: { Config: { CropIdCard: true, CropPortrait: true, }, }, cameraConfig: { autoMode: false, maxTry: 3, disableAlbum: false, }, success: (res: any) => { resolve(res); wx.navigateBack({ delta: 1, }); }, fail: (error: any) => { console.log(error); reject(error); }, }); }); }, nameChange(e: any) { this.setData({ name: e.detail.value }) }, checkInfo(silent = false) { if (!this.data.headPortraitImg) { !silent && wx.showToast({ title: "请上传身份证人像面", icon: "none", }); return false; } if (!this.data.nationalEmblemImg) { !silent && wx.showToast({ title: "请上传身份证国徽面", icon: "none", }); return false; } if (!this.data.name) { !silent && wx.showToast({ title: "请填写姓名或重新识别", icon: "none", }); return false; } return true; }, /** * 生命周期函数--监听页面加载 */ onLoad() { getOcrKeyApi().then(res => { const data = >res this.setData({ secretInfo: { secretId: data.data?.secretId || '', secretKey: data.data?.secretKey || '' } }) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { fs.unlink({ filePath: `${wx.env.USER_DATA_PATH}/identificationIdFrontPic.png`, }); fs.unlink({ filePath: `${wx.env.USER_DATA_PATH}/identificationIdBackPic.png`, }); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })