face_verify.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. this.data.flag = null;
  287. let img = '';
  288. if (type == 2) {
  289. img = app.data.catchFaceUrl || '';
  290. app.data.catchFaceUrl = null;
  291. //抓拍失败
  292. if (!img) {
  293. wx.showModal({
  294. content: '认证失败,再试一次吧',
  295. showCancel: false,
  296. complete: (res) => {
  297. }
  298. })
  299. return;
  300. }
  301. }
  302. let {
  303. idNumber,
  304. phone,
  305. username,
  306. cardIdEx
  307. } = this.data
  308. let data = {
  309. avatar: img,
  310. idNumber: idNumber,
  311. cardIdEx: cardIdEx,
  312. username: username,
  313. phone: phone,
  314. type: 3,
  315. code: app.data.wxCode
  316. }
  317. let that = this
  318. wx.showLoading({
  319. title: '信息验证中...',
  320. })
  321. doConfirmFaceNotifyNew(data).then(res => {
  322. wx.hideLoading()
  323. getWxLogin()
  324. // 接口请求失败
  325. if (res.code !== 200) {
  326. wx.showToast({
  327. title: res.mag,
  328. icon: 'none',
  329. });
  330. this.triggerEvent('certificationState', false)
  331. return;
  332. }
  333. //接口请求成功
  334. let userInfo = res.data
  335. userInfo.needLoginList = this.data.needLoginList;
  336. userInfo.needPhone = this.data.needPhone;
  337. userInfo.needPhoto = this.data.needPhoto;
  338. userInfo.needRealname = this.data.needRealname;
  339. wx.setStorageSync('language', app.data.language);
  340. //访客认证结果
  341. if (that.data.userType == '访客') {
  342. wx.setStorageSync('visitorUserInfo', userInfo);
  343. app.data.visitorUserInfo = userInfo;
  344. }
  345. //常客认证结果
  346. if (that.data.userType == '常客') {
  347. wx.setStorageSync('userInfo', userInfo);
  348. app.data.userInfo = userInfo;
  349. }
  350. wx.showToast({
  351. title: `恭喜认证成功`,
  352. icon: 'none'
  353. })
  354. this.triggerEvent('certificationState', true)
  355. })
  356. },
  357. // 实名认证时&卡号&微信 openId 绑定认证信息 userId
  358. openIdAndCardIdSet(userInfo, cardIdEx) {
  359. const that = this;
  360. let openId = "";
  361. //openId绑定
  362. let doOpenIdset = new Promise((reslove, reject) => {
  363. wx.login({
  364. success: res => {
  365. openId = res.code
  366. let data = {
  367. code: openId,
  368. type: 3,
  369. userId: userInfo.userId
  370. }
  371. openIdSet(data).then(res => {
  372. getWxLogin()
  373. res.code == 200 ? reslove() : reject();
  374. })
  375. }
  376. })
  377. });
  378. // cardIdEx绑定
  379. let doCardIdExSet = new Promise((reslove, reject) => {
  380. let data = {
  381. cardIdEx: that.data.cardIdEx,
  382. userId: userInfo.userId
  383. }
  384. cardIdExSet(data).then(res => {
  385. res.code == 200 ? reslove() : reject();
  386. })
  387. })
  388. //判断是否需要绑定卡号(openId 一定绑定)
  389. let taskList = [doOpenIdset];
  390. if (this.data.needLoginList.indexOf(4) > -1) {
  391. taskList = [doOpenIdset, doCardIdExSet];
  392. }
  393. Promise.all(taskList).then(res => {
  394. userInfo.openId = openId;
  395. userInfo.cardIdEx = cardIdEx;
  396. //存下登录条件
  397. userInfo.needLoginList = this.data.needLoginList;
  398. userInfo.needPhone = this.data.needPhone;
  399. userInfo.needPhoto = this.data.needPhoto;
  400. userInfo.needRealname = this.data.needRealname;
  401. if (this.data.userType == '访客') {
  402. wx.setStorageSync('visitorUserInfo', userInfo);
  403. app.data.visitorUserInfo = userInfo
  404. } else {
  405. wx.setStorageSync('userInfo', userInfo);
  406. app.data.userInfo = userInfo
  407. }
  408. this.triggerEvent('certificationState', true)
  409. }).catch(err => {
  410. wx.showToast({
  411. title: err.msg,
  412. icon: 'none'
  413. })
  414. this.triggerEvent('certificationState', false)
  415. })
  416. },
  417. //外宾时,执行 OCR 识别
  418. goOCR() {
  419. let {
  420. needPhoto,
  421. needRealname,
  422. } = this.data
  423. this.data.flag = 2;
  424. wx.navigateTo({
  425. url: '/pages/ocr/ocr?needPhoto=' + needPhoto + '&needRealname=' + needRealname
  426. })
  427. },
  428. // ocr 完成后,渲染数据
  429. updateOcrInfo() {
  430. let ocrInfo = JSON.parse(JSON.stringify(app.data.ocrInfo))
  431. app.data.ocrInfo = null
  432. let data = {
  433. username: ocrInfo.name,
  434. idNumber: ocrInfo.passportCode,
  435. avatar: ocrInfo.faceImage,
  436. countryCode: ocrInfo.countryCode,
  437. gender: ocrInfo.gender,
  438. passportUrl: ocrInfo.passportUrl,
  439. hztx: ocrInfo.hztx,
  440. birthday: ocrInfo.birthday,
  441. efficientTime: ocrInfo.efficientTime
  442. }
  443. this.setData({
  444. myUserInfo: data,
  445. username: ocrInfo.name,
  446. 'warnName.status': true,
  447. idNumber: ocrInfo.passportCode,
  448. 'warnIdcard.status': true,
  449. })
  450. },
  451. // 外宾确认或修改用户信息
  452. update() {
  453. let {
  454. warnPhone,
  455. warnIdcard,
  456. warnName,
  457. warnCardId
  458. } = this.data
  459. let allFill = this.canOperation(warnName.status, warnIdcard.status, warnPhone.status, warnCardId.status)
  460. //未填写完整
  461. if (!allFill) {
  462. wx.showToast({
  463. icon: 'none',
  464. title: 'Please complete your personal information!',
  465. })
  466. return;
  467. }
  468. //填写完整
  469. let data = {
  470. username: this.data.myUserInfo.username,
  471. idNumber: this.data.myUserInfo.idNumber,
  472. avatar: this.data.myUserInfo.avatar,
  473. phone: this.data.phone,
  474. cardIdEx: this.data.cardIdEx,
  475. countryCode: this.data.myUserInfo.countryCode,
  476. gender: this.data.myUserInfo.gender,
  477. passportUrl: this.data.myUserInfo.passportUrl,
  478. hztx: this.data.myUserInfo.hztx,
  479. birthday: this.data.myUserInfo.birthday,
  480. efficientTime: this.data.myUserInfo.efficientTime
  481. }
  482. wx.showLoading({
  483. title: '信息提交中...',
  484. })
  485. foreignAuthentication(data).then(res => {
  486. wx.hideLoading()
  487. if (res.code !== 200) {
  488. wx.showToast({
  489. icon: 'none',
  490. title: res.msg,
  491. });
  492. return;
  493. }
  494. let userInfo = Object.assign(res.data, data)
  495. this.openIdAndCardIdSet(userInfo, this.data.cardIdEx)
  496. })
  497. },
  498. //预览照片
  499. previewImg(e) {
  500. let currentUrl = e.currentTarget.dataset.src;
  501. if (!currentUrl) {
  502. return;
  503. }
  504. wx.previewImage({
  505. current: currentUrl,
  506. urls: [currentUrl]
  507. })
  508. }
  509. },
  510. observers: {
  511. 'userInfo': function (userInfo) {
  512. let myUserInfo = userInfo || null;
  513. if (!userInfo) {
  514. return;
  515. }
  516. this.setData({
  517. username: myUserInfo.username || "",
  518. idNumber: myUserInfo.idNumber || "",
  519. phone: myUserInfo.phone || "",
  520. cardIdEx: myUserInfo.cardIdEx || "",
  521. myUserInfo: myUserInfo
  522. })
  523. },
  524. },
  525. lifetimes: {
  526. attached: function () {
  527. }
  528. },
  529. pageLifetimes: {
  530. show() {
  531. switch (this.data.flag) {
  532. //实名认证完成后操作
  533. case 1:
  534. this.doFaceCheck()
  535. break;
  536. //OCR识别完成后操作
  537. case 2:
  538. this.updateOcrInfo()
  539. break;
  540. //抓拍照片完成后操作
  541. case 3:
  542. this.doConfirmFaceNotifyNew(2)
  543. break;
  544. default:
  545. break;
  546. }
  547. }
  548. }
  549. })