index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { userWithimage, userWithverified, userWithanonymous } from "../../utils/api/api";
  2. const app = getApp();
  3. const face = require("/utils/faceCheck/faceCheck");
  4. const date = require("/utils/index/index");
  5. import getAuthThree from '/utils/api/auth_api'
  6. import bus from 'iny-bus'
  7. Page({
  8. data: {
  9. certName1: "",
  10. certNo1: "",
  11. certName2: "",
  12. certNo2: "",
  13. image: "",
  14. isLoading: false,
  15. isLoading1: false,
  16. // 输入信息
  17. inputName: "",
  18. inputIdcard: "",
  19. phoneNumber: "",
  20. userId: null,
  21. eventId: null,
  22. fileList: []
  23. },
  24. upStorage() {
  25. let that = this
  26. if (!this.data.certName1 || !this.data.certNo1) {
  27. my.showToast({
  28. type: 'none',
  29. content: '姓名身份证必填'
  30. });
  31. return
  32. }
  33. my.showLoading({
  34. content: '加载中',
  35. })
  36. this.setData({
  37. isLoading: true
  38. })
  39. userWithimage({
  40. certName: this.data.certName1,
  41. certNo: this.data.certNo1,
  42. token: 'ab051195190b4623ea62f636ae747f53',
  43. vid: null
  44. }).then(() => {
  45. my.hideLoading()
  46. my.showToast({
  47. content: "入库成功",
  48. type: "success",
  49. duration: 1000,
  50. //显示透明蒙层,防止触摸穿透
  51. mask: true,
  52. success: function () {
  53. that.setData({
  54. certName1: "",
  55. certNo1: ""
  56. })
  57. }
  58. });
  59. }).catch(error => {
  60. my.hideLoading()
  61. }).finally(() => {
  62. // 无论请求成功或失败,都需要将 isLoading 设为 false
  63. this.setData({
  64. isLoading: false
  65. });
  66. my.hideLoading();
  67. });
  68. },
  69. upStorage2() {
  70. const taskId = `${date.formatTime(Date.now(), "YYYYMMDDHHNNSS")}${parseInt((Math.random() * 9 + 1) * 10000)}`
  71. my.setStorage({
  72. key: 'auth-taskId',
  73. data: taskId
  74. })
  75. my.navigateToMiniProgram({
  76. appId: app.globalData.authAppId, // 16 位
  77. path: 'pages/authorize_three/authorize_three',
  78. query: {
  79. name: 'HHFace出入库演示小程序',
  80. token: app.globalData.authAppToken,
  81. taskId
  82. },
  83. })
  84. },
  85. onAppShowHandler(options) {
  86. if (options && options.referrerInfo) {
  87. // 从三要素认证返回
  88. if (options.referrerInfo.appId === app.globalData.authAppId && options.referrerInfo.extraData) {
  89. const authStatus = options.referrerInfo.extraData.authStatus
  90. if (authStatus) {
  91. const authTaskId = my.getStorageSync({
  92. key: 'auth-taskId'
  93. })
  94. my.removeStorage({
  95. key: 'auth-taskId'
  96. })
  97. if (authTaskId && authTaskId.data) {
  98. bus.emit('appShowHandler', authTaskId.data)
  99. }
  100. } else {
  101. my.alert({
  102. title: '提示',
  103. content: '授权失败'
  104. })
  105. }
  106. }
  107. }
  108. },
  109. async zfbAuthCallback(authTaskId) {
  110. my.showLoading({
  111. content: "加载中..."
  112. })
  113. const result = await getAuthThree({
  114. taskId: authTaskId,
  115. token: app.globalData.authAppToken
  116. })
  117. /**
  118. * certNo 身份证号
  119. * certName 姓名
  120. * phone 手机号
  121. */
  122. if (result.errcode) {
  123. my.alert({
  124. title: "提示",
  125. content: "未查询到认证结果,请重新认证!"
  126. })
  127. return
  128. }
  129. if (!result.data || !result.data.token) {
  130. my.alert({
  131. title: "提示",
  132. content: result.msg
  133. })
  134. return
  135. }
  136. this.setData({
  137. inputName: result.data.certName,
  138. inputIdcard: result.data.certNo,
  139. phoneNumber: result.data.phone,
  140. userId: result.data.userId
  141. })
  142. my.setStorage({
  143. key: 'userInfo1',
  144. data: result.data
  145. })
  146. this.doZFBFaceCheck()
  147. my.hideLoading()
  148. },
  149. // 支付宝验证人脸
  150. async doZFBFaceCheck() {
  151. let that = this
  152. let {
  153. userId
  154. } = this.data;
  155. // let res = await face.getFace_wxrlxf("/api/user/userLogin", "post", {
  156. // zfbUserId: userId
  157. // })
  158. // if (res.data.token) {
  159. // my.setStorage({
  160. // key: 'token',
  161. // data: res.data
  162. // })
  163. // }
  164. that.setCertificationState()
  165. },
  166. // 设置授权状态
  167. setCertificationState(isSuccess) {
  168. // let token = my.getStorageSync({
  169. // key: 'token'
  170. // }).data
  171. // if (token) {
  172. my.alert({
  173. title: '提示',
  174. content: '授权成功',
  175. success() {
  176. my.reLaunch({
  177. url: '/pages/home/index',
  178. })
  179. let Info = my.getStorageSync({
  180. key: 'userInfo1',
  181. })
  182. userWithverified({
  183. certName: Info.data.certName,
  184. certNo: Info.data.certNo,
  185. token: 'ab051195190b4623ea62f636ae747f53',
  186. ftoken: Info.data.token
  187. }).then(() => {
  188. my.showToast({
  189. content: "入库成功",
  190. type: "success",
  191. duration: 1000,
  192. //显示透明蒙层,防止触摸穿透
  193. mask: true,
  194. success: function () {
  195. }
  196. }).catch()
  197. })
  198. }
  199. })
  200. // } else {
  201. // my.alert({
  202. // title: "提示",
  203. // content: "未查询到认证结果,请重新认证!"
  204. // })
  205. // }
  206. },
  207. // 匿名入库
  208. upStorage3() {
  209. let that = this
  210. if (!this.data.certName2 || !this.data.certNo2 || !this.data.image) {
  211. my.showToast({
  212. type: 'none',
  213. content: '姓名身份证照片必填'
  214. });
  215. return
  216. }
  217. my.showLoading({
  218. content: '加载中',
  219. })
  220. this.setData({
  221. isLoading1: true
  222. })
  223. userWithanonymous({
  224. certName: this.data.certName2,
  225. certNo: this.data.certNo2,
  226. image: this.data.image,
  227. token: 'ab051195190b4623ea62f636ae747f53',
  228. vid: null
  229. }).then(res => {
  230. my.hideLoading()
  231. my.showToast({
  232. content: "入库成功",
  233. type: "success",
  234. duration: 1000,
  235. //显示透明蒙层,防止触摸穿透
  236. mask: true,
  237. success: function () {
  238. that.setData({
  239. certName2: "",
  240. certNo2: "",
  241. image: '',
  242. fileList: []
  243. })
  244. }
  245. });
  246. }).catch(error => {
  247. my.hideLoading()
  248. }).finally(() => {
  249. // 无论请求成功或失败,都需要将 isLoading 设为 false
  250. this.setData({
  251. isLoading1: false
  252. });
  253. my.hideLoading();
  254. });
  255. },
  256. getUserName(e) {
  257. this.setData({
  258. certName1: e.detail.value
  259. })
  260. },
  261. getCardID(e) {
  262. this.setData({
  263. certNo1: e.detail.value
  264. })
  265. },
  266. getUserName2(e) {
  267. this.setData({
  268. certName2: e.detail.value
  269. })
  270. },
  271. getCardID2(e) {
  272. this.setData({
  273. certNo2: e.detail.value
  274. })
  275. },
  276. //选择照片//原生
  277. ChooseImage(e) {
  278. my.chooseImage({
  279. success: (res) => {
  280. const fs = my.getFileSystemManager();
  281. fs.readFile({
  282. // readFile 不传入 encodding 参数,则以 ArrayBuffer 方式读取
  283. filePath: `${res.tempFilePaths[0]}`,
  284. success: ({ data }) => {
  285. const base64 = my.arrayBufferToBase64(data);
  286. this.setData({
  287. form2: { certName: this.data.form2.certName, certNo: this.data.form2.certName.certNo, image: base64 }
  288. })
  289. },
  290. });
  291. },
  292. });
  293. },
  294. //组件的选择照片
  295. onChange(fileList) {
  296. if (fileList.length) {
  297. this.setData({ fileList: fileList })
  298. const fs = my.getFileSystemManager();
  299. fs.readFile({
  300. filePath: `${fileList[0].path}`,
  301. success: ({ data }) => {
  302. const base64 = my.arrayBufferToBase64(data);
  303. this.setData({
  304. image: base64,
  305. })
  306. },
  307. });
  308. }
  309. },
  310. onUpload(file) {
  311. return new Promise((resolve) => {
  312. resolve(file.path);
  313. });
  314. },
  315. onRemove(file) {
  316. return new Promise((resolve) => {
  317. this.setData({
  318. image: '',
  319. fileList: []
  320. })
  321. resolve(file);
  322. });
  323. },
  324. onLoad() {
  325. my.onAppShow(this.onAppShowHandler)
  326. this.data.eventId = bus.on('appShowHandler', (res) => {
  327. this.zfbAuthCallback(res)
  328. })
  329. },
  330. onShow() {
  331. const userInfo = my.getStorageSync({ key: "userInfo" });
  332. if (!userInfo.data) {
  333. my.navigateTo({
  334. url: '/pages/login/index',
  335. })
  336. }
  337. },
  338. onReady() {
  339. },
  340. onUnload() {
  341. bus.remove('appShowHandler', this.data.eventId)
  342. my.offAppShow(this.onAppShowHandler)
  343. // 页面被关闭
  344. },
  345. });