realNameAuthentication.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // pages/realNameAuthentication/realNameAuthentication.ts
  2. import { getOcrKeyApi, authenticateApi } from "../../service/hwUser";
  3. import { upLoadImage } from "../../utils/util";
  4. enum IdCardType {
  5. headPortrait,
  6. nationalEmblem
  7. }
  8. const ocrSdk = require("../../ocrsdk/index");
  9. const fs = wx.getFileSystemManager()
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. IdCardType,
  16. secretInfo: { secretId: "", secretKey: "" },
  17. headPortraitImgBase64: '',
  18. headPortraitImg: '',
  19. name: '',
  20. idCard: '',
  21. nationalEmblemImgBase64: '',
  22. nationalEmblemImg: '',
  23. canSubmit: false
  24. },
  25. submit() {
  26. if (this.checkInfo()) {
  27. const data: {
  28. id: string
  29. idcardFront: string
  30. idcardBack: string
  31. name: string
  32. idcardNumber: string
  33. setIdcardFront: string
  34. setIdcardBack: string
  35. } = {
  36. id: wx.getStorageSync("userId"),
  37. idcardFront: this.data.headPortraitImg,
  38. idcardBack: this.data.nationalEmblemImg,
  39. name: this.data.name,
  40. idcardNumber: this.data.idCard,
  41. setIdcardFront: '',
  42. setIdcardBack: '',
  43. };
  44. upLoadImage({ url: '/api/hwUser/imgUpload', files: [data.idcardFront], name: 'file', formData: { upType: 1, } }).then(res => {
  45. console.log(res);
  46. data.setIdcardFront = res[0].imgUrl
  47. upLoadImage({ url: '/api/hwUser/imgUpload', files: [data.idcardBack], name: 'file', formData: { upType: 2, } }).then(res => {
  48. console.log(res);
  49. data.setIdcardBack = res[0].imgUrl
  50. authenticateApi({ ...data }).then(res => {
  51. console.log(res);
  52. wx.redirectTo({
  53. url: "/pages/realNameAuthentication/success",
  54. });
  55. })
  56. })
  57. })
  58. console.log(data);
  59. }
  60. },
  61. async chooseImage(e: any) {
  62. console.log(e);
  63. const cardType: number = e.currentTarget.dataset.idCardType
  64. const data = await this.ocrSdkStart(cardType)
  65. console.log(data);
  66. console.log(cardType);
  67. console.log(data);
  68. const filePath = `${wx.env.USER_DATA_PATH}/${cardType === IdCardType.headPortrait ? "identificationIdFrontPic" : "identificationIdBackPic"}.png`;
  69. fs.unlink({
  70. filePath,
  71. complete: () => {
  72. const img = JSON.parse(data.AdvancedInfo).IdCard;
  73. fs.writeFile({
  74. filePath,
  75. data: img,
  76. encoding: "base64",
  77. success: () => {
  78. if (cardType === IdCardType.headPortrait) {
  79. this.setData({
  80. headPortraitImgBase64: "data:image/png;base64," + img,
  81. headPortraitImg: filePath,
  82. name: data.Name,
  83. idCard: data.IdNum
  84. })
  85. }
  86. if (cardType === IdCardType.nationalEmblem) {
  87. this.setData({
  88. nationalEmblemImgBase64: "data:image/png;base64," + img,
  89. nationalEmblemImg: filePath
  90. })
  91. }
  92. this.setData({
  93. canSubmit: this.checkInfo(true)
  94. })
  95. },
  96. });
  97. },
  98. });
  99. },
  100. ocrSdkStart(cardType: number): Promise<{ AdvancedInfo: string, Name: string, IdNum: string }> {
  101. return new Promise((resolve, reject) => {
  102. ocrSdk.start({
  103. getAuthorization: () => {
  104. return new Promise((resolve) => {
  105. resolve({
  106. tmpSecretId: this.data.secretInfo.secretId,
  107. tmpSecretKey: this.data.secretInfo.secretKey,
  108. });
  109. });
  110. },
  111. ocrType: cardType === IdCardType.nationalEmblem ? ocrSdk.OcrType.ID_CARD_BACK : ocrSdk.OcrType.ID_CARD_FRONT,
  112. ocrOption: {
  113. Config: {
  114. CropIdCard: true,
  115. CropPortrait: true,
  116. },
  117. },
  118. cameraConfig: {
  119. autoMode: false,
  120. maxTry: 3,
  121. disableAlbum: false,
  122. },
  123. success: (res: any) => {
  124. resolve(res);
  125. wx.navigateBack({
  126. delta: 1,
  127. });
  128. },
  129. fail: (error: any) => {
  130. console.log(error);
  131. reject(error);
  132. },
  133. });
  134. });
  135. },
  136. nameChange(e: any) {
  137. this.setData({
  138. name: e.detail.value
  139. })
  140. },
  141. checkInfo(silent = false) {
  142. if (!this.data.headPortraitImg) {
  143. !silent && wx.showToast({
  144. title: "请上传身份证人像面",
  145. icon: "none",
  146. });
  147. return false;
  148. }
  149. if (!this.data.nationalEmblemImg) {
  150. !silent && wx.showToast({
  151. title: "请上传身份证国徽面",
  152. icon: "none",
  153. });
  154. return false;
  155. }
  156. if (!this.data.name) {
  157. !silent && wx.showToast({
  158. title: "请填写姓名或重新识别",
  159. icon: "none",
  160. });
  161. return false;
  162. }
  163. return true;
  164. },
  165. /**
  166. * 生命周期函数--监听页面加载
  167. */
  168. onLoad() {
  169. getOcrKeyApi().then(res => {
  170. const data = <responseOptionsType<getOcrKeyResponseType>>res
  171. this.setData({
  172. secretInfo: {
  173. secretId: data.data?.secretId || '',
  174. secretKey: data.data?.secretKey || ''
  175. }
  176. })
  177. })
  178. },
  179. /**
  180. * 生命周期函数--监听页面初次渲染完成
  181. */
  182. onReady() {
  183. },
  184. /**
  185. * 生命周期函数--监听页面显示
  186. */
  187. onShow() {
  188. },
  189. /**
  190. * 生命周期函数--监听页面隐藏
  191. */
  192. onHide() {
  193. },
  194. /**
  195. * 生命周期函数--监听页面卸载
  196. */
  197. onUnload() {
  198. fs.unlink({
  199. filePath: `${wx.env.USER_DATA_PATH}/identificationIdFrontPic.png`,
  200. });
  201. fs.unlink({
  202. filePath: `${wx.env.USER_DATA_PATH}/identificationIdBackPic.png`,
  203. });
  204. },
  205. /**
  206. * 页面相关事件处理函数--监听用户下拉动作
  207. */
  208. onPullDownRefresh() {
  209. },
  210. /**
  211. * 页面上拉触底事件的处理函数
  212. */
  213. onReachBottom() {
  214. },
  215. /**
  216. * 用户点击右上角分享
  217. */
  218. onShareAppMessage() {
  219. }
  220. })