face_verify.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // components/face_verify/face_verify.js
  2. import international from '../../international/appointment_scence/index'
  3. import {
  4. foreignAuthentication
  5. } from '../../utils/api/make_appointment'
  6. import {
  7. cardIdExSet,
  8. doConfirmFaceNotifyNew, openIdSet
  9. } from '../../utils/api/api'
  10. const face = require('../../utils/faceCheck/faceCheck')
  11. const {
  12. throttle
  13. } = require('../../utils/throttle/throttle');
  14. const getPhone = require('../../utils/getPhone/wx_getPhone');
  15. var verify = require('../../utils/util/util')
  16. import {
  17. getWxLogin
  18. } from '../../utils/util/util'
  19. const app = getApp()
  20. Component({
  21. /**
  22. * 组件的属性列表
  23. */
  24. properties: {
  25. title: {
  26. type: String,
  27. value: '人员信息'
  28. },
  29. language: {
  30. type: String,
  31. value: 'ch'
  32. },
  33. isDisabled: {
  34. type: Boolean,
  35. value: true,
  36. },
  37. userInfo: {
  38. type: Object,
  39. },
  40. // 常客、访客
  41. userType: {
  42. value: null
  43. },
  44. // 登录参数列表 1姓名2身份证3手机号4卡号
  45. needLoginList: {
  46. type: Array,
  47. value: [],
  48. },
  49. //是否输入手机号 0-否 1-需要
  50. needPhone: {
  51. type: Boolean,
  52. value: false,
  53. },
  54. //是否需要照片 0-否 1-需要
  55. needPhoto: {
  56. type: Boolean,
  57. value: false,
  58. },
  59. //是否需要实名 0-否 1-需要
  60. needRealname: {
  61. type: Boolean,
  62. value: false,
  63. },
  64. //
  65. pageType: {
  66. type: String,
  67. value: ''
  68. },
  69. },
  70. /**
  71. * 组件的初始数据
  72. */
  73. data: {
  74. //国际化语言包
  75. international: international,
  76. myUserInfo: null,
  77. username: '',
  78. idNumber: '',
  79. phone: '',
  80. cardIdEx: '',
  81. warnCardId: { //卡号校验信息
  82. status: false,
  83. errMsg: null
  84. },
  85. warnName: { //姓名校验信息
  86. status: false,
  87. errMsg: null
  88. },
  89. warnIdcard: { //身份证校验信息
  90. status: false,
  91. errMsg: null
  92. },
  93. warnPhone: { //手机号校验信息
  94. status: false,
  95. errMsg: null
  96. },
  97. },
  98. /**
  99. * 组件的方法列表
  100. */
  101. methods: {
  102. //输入姓名
  103. getInputName(e) {
  104. let {
  105. warnName,
  106. username
  107. } = this.data;
  108. username = e.detail
  109. if (e.detail) {
  110. warnName.status = true
  111. } else {
  112. warnName.status = false
  113. warnName.errMsg = '您输入的姓名不可以为空!'
  114. };
  115. this.setData({
  116. warnName,
  117. username
  118. })
  119. },
  120. //输入身份证号
  121. getInputIdcard(e) {
  122. let {
  123. warnIdcard,
  124. idNumber
  125. } = this.data
  126. idNumber = e.detail
  127. if (app.data.language === 'en') {
  128. warnIdcard.status = true;
  129. } else {
  130. let res = verify.isCardID(idNumber);
  131. if (res.state) {
  132. warnIdcard.status = true;
  133. wx.hideKeyboard()
  134. } else {
  135. warnIdcard.status = false
  136. warnIdcard.errMsg = res.data
  137. };
  138. }
  139. this.setData({
  140. warnIdcard,
  141. idNumber
  142. });
  143. },
  144. // 输入手机号
  145. getInputPhone(e) {
  146. let {
  147. warnPhone,
  148. phone
  149. } = this.data
  150. phone = e.detail
  151. let res = verify.isCornet(phone);
  152. if (res.state) {
  153. warnPhone.status = true;
  154. } else {
  155. warnPhone.status = false
  156. warnPhone.errMsg = res.data
  157. };
  158. if (phone.length >= 11) {
  159. wx.hideKeyboard()
  160. }
  161. this.setData({
  162. warnPhone,
  163. phone
  164. });
  165. },
  166. //点击获取手机号
  167. getPhoneNumber(e) {
  168. getPhone(e).then(phone => {
  169. if (phone) {
  170. this.setData({
  171. 'warnPhone.status': true,
  172. phone: phone
  173. })
  174. app.data.phone = phone
  175. } else {
  176. wx.showToast({
  177. icon: 'none',
  178. title: '网络异常请重新获取哦~',
  179. })
  180. }
  181. }).catch((err) => {
  182. console.log(err)
  183. })
  184. },
  185. // 输入卡号
  186. getCardNumber(e) {
  187. let {
  188. warnCardId,
  189. cardIdEx
  190. } = this.data;
  191. cardIdEx = e.detail
  192. if (e.detail) {
  193. warnCardId.status = true
  194. } else {
  195. warnCardId.status = false
  196. warnCardId.errMsg = '卡号不可以为空!'
  197. };
  198. this.setData({
  199. warnCardId,
  200. cardIdEx
  201. });
  202. },
  203. // 点击认证按钮
  204. Certification() {
  205. let {
  206. warnPhone,
  207. warnIdcard,
  208. warnName,
  209. warnCardId
  210. } = this.data
  211. let allFill = this.canOperation(warnName.status, warnIdcard.status, warnPhone.status, warnCardId.status)
  212. // 判断信息是否填写完整
  213. if (!allFill) {
  214. wx.showToast({
  215. icon: 'none',
  216. title: '请将个人信息填写完整!',
  217. })
  218. return;
  219. }
  220. // 实名认证
  221. if (this.data.needRealname) {
  222. this.submit()
  223. }
  224. // 无需实名认证&需要抓拍
  225. if (!this.data.needRealname && this.data.needPhoto) {
  226. //抓拍人脸 flag-3
  227. this.data.flag = 3
  228. wx.navigateTo({
  229. url: '/pages/faceCheck/faceCheck'
  230. })
  231. }
  232. // 无需实名认证&无需抓拍
  233. if (!this.data.needRealname && !this.data.needPhoto) {
  234. this.doConfirmFaceNotifyNew(1)
  235. }
  236. },
  237. // 判断提交按钮是否禁用
  238. canOperation(username, idNumber, phone, cardIdEx) {
  239. let {
  240. needLoginList
  241. } = this.data;
  242. let allFill = true
  243. for (let index = 0; index < needLoginList.length; index++) {
  244. if (!Array.from(arguments)[needLoginList[index] - 1]) {
  245. allFill = false;
  246. break;
  247. }
  248. }
  249. return allFill
  250. },
  251. //跳转执行实名认证
  252. submit: throttle(function () {
  253. const that = this;
  254. let {
  255. username,
  256. idNumber,
  257. phone,
  258. } = this.data
  259. let orgId = app.data.adminId;
  260. face.intoFace_wxrlxf(username, idNumber, phone, orgId = '00').then((res) => {
  261. that.data.flag = 1
  262. }).catch(err => { })
  263. }, 5000),
  264. //验证人脸
  265. async doFaceCheck() {
  266. wx.showLoading({
  267. title: '信息验证中...',
  268. })
  269. let taskId = app.data.faceTaskId;
  270. // taskId = '00-2023041314125357889'; //本地
  271. // taskId = '00-993306131355291200'; //线上
  272. // taskId = '00-2024050112393311886' //zyh线上
  273. if (!taskId) return;
  274. let res = await face.getFace_wxrlxf('/api/next/doQueryFaceSuccess', 'get', {
  275. taskId: taskId
  276. });
  277. wx.hideLoading()
  278. if (res.data.status === true) {
  279. this.openIdAndCardIdSet(res.data, this.data.cardIdEx);
  280. } else {
  281. this.triggerEvent('certificationState', false)
  282. }
  283. },
  284. // 无需实名认证&需抓拍人脸照片 注册信息 1-不需要照片 2-需要照片
  285. doConfirmFaceNotifyNew(type) {
  286. let img = '';
  287. if (type == 2) {
  288. img = app.data.catchFaceUrl || '';
  289. app.data.catchFaceUrl = null;
  290. //抓拍失败
  291. if (!img) {
  292. wx.showModal({
  293. content: '认证失败,再试一次吧',
  294. showCancel: false,
  295. complete: (res) => {
  296. }
  297. })
  298. return;
  299. }
  300. }
  301. let {
  302. idNumber,
  303. phone,
  304. username,
  305. cardIdEx
  306. } = this.data
  307. let data = {
  308. avatar: img,
  309. idNumber: idNumber,
  310. cardIdEx: cardIdEx,
  311. username: username,
  312. phone: phone,
  313. type: 3,
  314. code: app.data.wxCode
  315. }
  316. let that = this
  317. wx.showLoading({
  318. title: '信息验证中...',
  319. })
  320. doConfirmFaceNotifyNew(data).then(res => {
  321. wx.hideLoading()
  322. getWxLogin()
  323. // 接口请求失败
  324. if (res.code !== 200) {
  325. wx.showToast({
  326. title: res.mag,
  327. icon: 'none',
  328. });
  329. this.triggerEvent('certificationState', false)
  330. return;
  331. }
  332. //接口请求成功
  333. let userInfo = res.data
  334. userInfo.needLoginList = this.data.needLoginList;
  335. userInfo.needPhone = this.data.needPhone;
  336. userInfo.needPhoto = this.data.needPhoto;
  337. userInfo.needRealname = this.data.needRealname;
  338. wx.setStorageSync('language', app.data.language);
  339. //访客认证结果
  340. if (that.data.userType == '访客') {
  341. wx.setStorageSync('visitorUserInfo', userInfo);
  342. app.data.visitorUserInfo = userInfo;
  343. }
  344. //常客认证结果
  345. if (that.data.userType == '常客') {
  346. wx.setStorageSync('userInfo', userInfo);
  347. app.data.userInfo = userInfo;
  348. }
  349. wx.showToast({
  350. title: `恭喜认证成功`,
  351. icon: 'none'
  352. })
  353. this.triggerEvent('certificationState', true)
  354. })
  355. },
  356. // 实名认证时&卡号&微信 openId 绑定认证信息 userId
  357. openIdAndCardIdSet(userInfo, cardIdEx) {
  358. const that = this;
  359. let openId = "";
  360. //openId绑定
  361. let doOpenIdset = new Promise((reslove, reject) => {
  362. wx.login({
  363. success: res => {
  364. openId = res.code
  365. let data = {
  366. code: openId,
  367. type: 3,
  368. userId: userInfo.userId
  369. }
  370. openIdSet(data).then(res => {
  371. getWxLogin()
  372. res.code == 200 ? reslove() : reject();
  373. })
  374. }
  375. })
  376. });
  377. // cardIdEx绑定
  378. let doCardIdExSet = new Promise((reslove, reject) => {
  379. let data = {
  380. cardIdEx: that.data.cardIdEx,
  381. userId: userInfo.userId
  382. }
  383. cardIdExSet(data).then(res => {
  384. res.code == 200 ? reslove() : reject();
  385. })
  386. })
  387. //判断是否需要绑定卡号(openId 一定绑定)
  388. let taskList = [doOpenIdset];
  389. if (this.data.needLoginList.indexOf(4) > -1) {
  390. taskList = [doOpenIdset, doCardIdExSet];
  391. }
  392. Promise.all(taskList).then(res => {
  393. userInfo.openId = openId;
  394. userInfo.cardIdEx = cardIdEx;
  395. //存下登录条件
  396. userInfo.needLoginList = this.data.needLoginList;
  397. userInfo.needPhone = this.data.needPhone;
  398. userInfo.needPhoto = this.data.needPhoto;
  399. userInfo.needRealname = this.data.needRealname;
  400. if (this.data.userType == '访客') {
  401. wx.setStorageSync('visitorUserInfo', userInfo);
  402. app.data.visitorUserInfo = userInfo
  403. } else {
  404. wx.setStorageSync('userInfo', userInfo);
  405. app.data.userInfo = userInfo
  406. }
  407. this.triggerEvent('certificationState', true)
  408. }).catch(err => {
  409. wx.showToast({
  410. title: err.msg,
  411. icon: 'none'
  412. })
  413. this.triggerEvent('certificationState', false)
  414. })
  415. },
  416. //外宾时,执行 OCR 识别
  417. goOCR() {
  418. let {
  419. needPhoto,
  420. needRealname,
  421. } = this.data
  422. this.data.flag = 2;
  423. wx.navigateTo({
  424. url: '/pages/ocr/ocr?needPhoto=' + needPhoto + '&needRealname=' + needRealname
  425. })
  426. },
  427. // ocr 完成后,渲染数据
  428. updateOcrInfo() {
  429. let ocrInfo = JSON.parse(JSON.stringify(app.data.ocrInfo))
  430. app.data.ocrInfo = null
  431. let data = {
  432. username: ocrInfo.name,
  433. idNumber: ocrInfo.passportCode,
  434. avatar: ocrInfo.faceImage,
  435. countryCode: ocrInfo.countryCode,
  436. gender: ocrInfo.gender,
  437. passportUrl: ocrInfo.passportUrl,
  438. hztx: ocrInfo.hztx,
  439. birthday: ocrInfo.birthday,
  440. efficientTime: ocrInfo.efficientTime
  441. }
  442. this.setData({
  443. myUserInfo: data,
  444. username: ocrInfo.name,
  445. 'warnName.status': true,
  446. idNumber: ocrInfo.passportCode,
  447. 'warnIdcard.status': true,
  448. })
  449. },
  450. // 外宾确认或修改用户信息
  451. update() {
  452. let {
  453. warnPhone,
  454. warnIdcard,
  455. warnName,
  456. warnCardId
  457. } = this.data
  458. let allFill = this.canOperation(warnName.status, warnIdcard.status, warnPhone.status, warnCardId.status)
  459. //未填写完整
  460. if (!allFill) {
  461. wx.showToast({
  462. icon: 'none',
  463. title: 'Please complete your personal information!',
  464. })
  465. return;
  466. }
  467. //填写完整
  468. let data = {
  469. username: this.data.myUserInfo.username,
  470. idNumber: this.data.myUserInfo.idNumber,
  471. avatar: this.data.myUserInfo.avatar,
  472. phone: this.data.phone,
  473. cardIdEx: this.data.cardIdEx,
  474. countryCode: this.data.myUserInfo.countryCode,
  475. gender: this.data.myUserInfo.gender,
  476. passportUrl: this.data.myUserInfo.passportUrl,
  477. hztx: this.data.myUserInfo.hztx,
  478. birthday: this.data.myUserInfo.birthday,
  479. efficientTime: this.data.myUserInfo.efficientTime
  480. }
  481. wx.showLoading({
  482. title: '信息提交中...',
  483. })
  484. foreignAuthentication(data).then(res => {
  485. wx.hideLoading()
  486. if (res.code !== 200) {
  487. wx.showToast({
  488. icon: 'none',
  489. title: res.msg,
  490. });
  491. return;
  492. }
  493. let userInfo = Object.assign(res.data, data)
  494. this.openIdAndCardIdSet(userInfo, this.data.cardIdEx)
  495. })
  496. },
  497. //预览照片
  498. previewImg(e) {
  499. let currentUrl = e.currentTarget.dataset.src;
  500. if (!currentUrl) {
  501. return;
  502. }
  503. wx.previewImage({
  504. current: currentUrl,
  505. urls: [currentUrl]
  506. })
  507. }
  508. },
  509. observers: {
  510. 'userInfo': function (userInfo) {
  511. let myUserInfo = userInfo || null;
  512. if (!userInfo) {
  513. return;
  514. }
  515. this.setData({
  516. username: myUserInfo.username || "",
  517. idNumber: myUserInfo.idNumber || "",
  518. phone: myUserInfo.phone || "",
  519. cardIdEx: myUserInfo.cardIdEx || "",
  520. myUserInfo: myUserInfo
  521. })
  522. },
  523. },
  524. lifetimes: {
  525. attached: function () {
  526. }
  527. },
  528. pageLifetimes: {
  529. show() {
  530. switch (this.data.flag) {
  531. //实名认证完成后操作
  532. case 1:
  533. this.doFaceCheck()
  534. break;
  535. //OCR识别完成后操作
  536. case 2:
  537. this.updateOcrInfo()
  538. break;
  539. //抓拍照片完成后操作
  540. case 3:
  541. this.doConfirmFaceNotifyNew(2)
  542. break;
  543. default:
  544. break;
  545. }
  546. this.data.flag = null;
  547. }
  548. }
  549. })