| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- // 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 = <responseOptionsType<getOcrKeyResponseType>>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() {
- }
- })
|