index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import {
  2. postApprove,
  3. getOcrKey
  4. } from '../../services/index.js'
  5. import {
  6. uploadImg
  7. } from '../../services/uploadFile.js'
  8. import {
  9. apiUrl
  10. } from '../../config/config.js'
  11. import { url } from '../../api/api'
  12. import {
  13. imgServerUrl,
  14. imgServerUrl_new
  15. } from '../../config/config.js'
  16. import { isIDNum } from '../../utils/base'
  17. var app = getApp()
  18. const ocrSdk = require("../../ocrsdk/index");
  19. const fs = wx.getFileSystemManager()
  20. Page({
  21. data: {
  22. imgServerUrl: imgServerUrl,
  23. imgServerUrl_new: imgServerUrl_new,
  24. realName: '',
  25. idNum: '',
  26. nation: '',
  27. idFrontPic: '',
  28. idBackPic: '',
  29. idPersonPic: '',
  30. showMessage: false,
  31. showPass: false,
  32. message: '',
  33. canUpdate: false,
  34. from: '',
  35. tmpSecretId: '',
  36. tmpSecretKey: '',
  37. frontStatus: '',
  38. backStatus: ''
  39. },
  40. onLoad: function (options) {
  41. if (options.from) {
  42. this.setData({
  43. from: options.from
  44. })
  45. }
  46. },
  47. onShow: function () {
  48. getOcrKey().then(data => {
  49. this.setData({
  50. tmpSecretId: data.data.secretId,
  51. tmpSecretKey: data.data.secretKey,
  52. })
  53. })
  54. },
  55. changeShowMessage() {
  56. this.setData({
  57. showMessage: !this.data.showMessage
  58. })
  59. },
  60. changeShowPass() {
  61. this.setData({
  62. showPass: !this.data.showPass
  63. })
  64. },
  65. testCanUpdate() {
  66. let {
  67. realName,
  68. idNum,
  69. idFrontPic,
  70. idBackPic,
  71. // idPersonPic
  72. } = this.data;
  73. this.setData({
  74. canUpdate: false
  75. })
  76. if (realName == "") {
  77. return false;
  78. }
  79. if (!isIDNum(idNum)) {
  80. return false;
  81. }
  82. if (idFrontPic == "") {
  83. return false;
  84. }
  85. if (idBackPic == "") {
  86. return false;
  87. }
  88. this.setData({
  89. canUpdate: true
  90. })
  91. return true
  92. },
  93. callPhone() {
  94. wx.makePhoneCall({
  95. phoneNumber: '4006900099',
  96. success: () => {
  97. this.setData({
  98. showMessage: false
  99. })
  100. }
  101. })
  102. },
  103. async chooseImage(e) {
  104. let index = e.currentTarget.dataset.index
  105. const data = await this.ocrSdkStart(index)
  106. var up_type;
  107. switch (index) {
  108. case 1:
  109. up_type = '1';
  110. this.setData({
  111. idFrontPic: false
  112. }, _ => {
  113. fs.unlink({
  114. filePath: this.data.idFrontPic,
  115. complete: () => {
  116. const filePath = `${wx.env.USER_DATA_PATH}/identificationIdFrontPic${new Date().getTime()}.png`
  117. fs.writeFile({
  118. filePath,
  119. data: JSON.parse(data.AdvancedInfo).IdCard,
  120. encoding: 'base64',
  121. success: res => {
  122. this.setData({
  123. realName: data.Name,
  124. idNum: data.IdNum,
  125. nation: data.Nation,
  126. idFrontPic: filePath
  127. })
  128. this.testCanUpdate()
  129. },
  130. fail: (res) => {
  131. console.log(res)
  132. }
  133. })
  134. }
  135. })
  136. })
  137. break;
  138. case 2:
  139. up_type = '2';
  140. this.setData({
  141. idBackPic: false
  142. }, _ => {
  143. fs.unlink({
  144. filePath: this.data.idBackPic,
  145. complete: () => {
  146. const filePath = `${wx.env.USER_DATA_PATH}/identificationIdBackPic${new Date().getTime()}.png`
  147. fs.writeFile({
  148. filePath,
  149. data: JSON.parse(data.AdvancedInfo).IdCard,
  150. encoding: 'base64',
  151. success: res => {
  152. this.setData({
  153. idBackPic: filePath
  154. })
  155. this.testCanUpdate()
  156. },
  157. fail: (res) => {
  158. console.log(res)
  159. }
  160. })
  161. }
  162. })
  163. })
  164. break;
  165. case 3:
  166. up_type = '3';
  167. break;
  168. }
  169. // uploadImg(up_type).then(res => {
  170. // let image = res.data.imgUrl
  171. // if (index == 1) {
  172. // this.setData({
  173. // idFrontPic: image
  174. // }, _ => {
  175. // this.testCanUpdate()
  176. // })
  177. // } else if (index == 2) {
  178. // this.setData({
  179. // idBackPic: image
  180. // }, _ => {
  181. // this.testCanUpdate()
  182. // })
  183. // } else if (index == 3) {
  184. // this.setData({
  185. // idPersonPic: image
  186. // }, _ => {
  187. // this.testCanUpdate()
  188. // })
  189. // }
  190. // })
  191. },
  192. changeName(e) {
  193. this.setData({
  194. realName: e.detail.value.trim()
  195. }, _ => {
  196. this.testCanUpdate()
  197. })
  198. },
  199. changeId(e) {
  200. this.setData({
  201. idNum: e.detail.value.trim()
  202. }, _ => {
  203. this.testCanUpdate()
  204. })
  205. },
  206. async submit(e) {
  207. this.testCanUpdate();
  208. let {
  209. realName,
  210. idNum,
  211. idFrontPic,
  212. idBackPic,
  213. nation,
  214. // idPersonPic
  215. } = this.data;
  216. if (realName == "") {
  217. wx.showToast({
  218. title: '请输入姓名',
  219. icon: 'none'
  220. })
  221. return false;
  222. }
  223. if (!isIDNum(idNum)) {
  224. wx.showToast({
  225. title: '请输入18位身份证号码',
  226. icon: 'none'
  227. })
  228. return false;
  229. }
  230. if (idFrontPic == "") {
  231. wx.showToast({
  232. title: '请上传身份证正面照',
  233. icon: 'none'
  234. })
  235. return false;
  236. }
  237. if (idBackPic == "") {
  238. wx.showToast({
  239. title: '请上传身份证反面照',
  240. icon: 'none'
  241. })
  242. return false;
  243. }
  244. // if (idPersonPic == "") {
  245. // wx.showToast({
  246. // title: '请上传手持身份证照片',
  247. // icon: 'none'
  248. // })
  249. // return false;
  250. // }
  251. wx.showLoading({
  252. title: '正在创建...',
  253. mask: true
  254. })
  255. let paramsObj = {
  256. realName: realName,
  257. idCardNumber: idNum,
  258. idFrontPic: idFrontPic,
  259. idBackPic: idBackPic,
  260. nation: nation,
  261. // idPersonPic: idPersonPic,
  262. minaType: 1, //1:代表小程序
  263. user_id: app.globalData.userId,
  264. user_token: app.globalData.userToken,
  265. member_id: app.globalData.memberId
  266. }
  267. const idFrontPicPath = paramsObj.idFrontPic
  268. paramsObj.idFrontPic = await this.uploadFile(1, idFrontPicPath)
  269. fs.unlinkSync(idFrontPicPath)
  270. const idBackPicPath = paramsObj.idBackPic
  271. paramsObj.idBackPic = await this.uploadFile(2, idBackPicPath)
  272. fs.unlinkSync(idBackPicPath)
  273. wx.hideLoading()
  274. wx.showLoading({
  275. title: '正在提交...',
  276. mask: true
  277. })
  278. postApprove(paramsObj).then(data => {
  279. wx.hideLoading()
  280. wx.showToast({
  281. title: '提交成功',
  282. })
  283. if (this.data.from === 'clock') {
  284. wx.redirectTo({
  285. url: '../result/index?type=auth&status=2&from=clock',
  286. })
  287. // this.setData({
  288. // showPass: true,
  289. // message: '您的实名认证已通过'
  290. // })
  291. return
  292. }
  293. wx.redirectTo({
  294. url: '../result/index?type=auth&status=2',
  295. })
  296. }).catch(data => {
  297. wx.hideLoading()
  298. if (data.errcode === 2100) {
  299. this.setData({
  300. showMessage: true,
  301. message: '您的身份证已经被实名认证'
  302. })
  303. return
  304. }
  305. if (data.errcode === 2101) {
  306. this.setData({
  307. showMessage: true,
  308. message: '您已实名认证,无需重复认证'
  309. })
  310. return
  311. }
  312. })
  313. // 身份认证上报订阅信息
  314. wx.aldPushSubscribeMessage({
  315. eventId: '5ea25ae37739104342928e96'
  316. });
  317. },
  318. uploadFile(up_type, filePath) {
  319. return new Promise((reslove, reject) => {
  320. wx.uploadFile({
  321. url: apiUrl + url.imgUp,
  322. filePath,
  323. name: 'file',
  324. header: {},
  325. formData: {
  326. up_type,
  327. user_id: wx.getStorageSync("user_id"),
  328. user_token: wx.getStorageSync("user_token"),
  329. member_id: wx.getStorageSync("member_id")
  330. },
  331. success: (res) => {
  332. let data = JSON.parse(res.data)
  333. reslove(data.data.imgUrl)
  334. },
  335. fail: () => {
  336. reject()
  337. }
  338. })
  339. })
  340. },
  341. ocrSdkStart(type) {
  342. return new Promise((reslove, reject) => {
  343. ocrSdk.start({
  344. getAuthorization: () => {
  345. return new Promise((reslove, reject) => {
  346. reslove({
  347. tmpSecretId: this.data.tmpSecretId,
  348. tmpSecretKey: this.data.tmpSecretKey,
  349. })
  350. })
  351. },
  352. ocrType: type === 1 ? ocrSdk.OcrType.ID_CARD_FRONT : ocrSdk.OcrType.ID_CARD_BACK,
  353. ocrOption: {
  354. Config: {
  355. "CropIdCard": true,
  356. "CropPortrait": true,
  357. }
  358. },
  359. cameraConfig: {
  360. autoMode: false,
  361. maxTry: 3,
  362. disableAlbum: false
  363. },
  364. success: (res) => {
  365. reslove(res)
  366. if (type === 1) {
  367. this.setData({
  368. frontStatus: true
  369. })
  370. } else {
  371. this.setData({
  372. backStatus: true
  373. })
  374. }
  375. wx.navigateBack({
  376. delta: 1,
  377. })
  378. },
  379. fail: (error) => {
  380. reject(error)
  381. if (type === 1) {
  382. this.setData({
  383. frontStatus: false
  384. })
  385. } else {
  386. this.setData({
  387. backStatus: false
  388. })
  389. }
  390. }
  391. })
  392. })
  393. },
  394. onError(err) {
  395. app.aldstat.sendEvent('报错', {
  396. 'err': err
  397. });
  398. },
  399. })