authUtil.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* eslint-disable */
  2. const formatTime = date => {
  3. const year = date.getFullYear()
  4. const month = date.getMonth() + 1
  5. const day = date.getDate()
  6. const hour = date.getHours()
  7. const minute = date.getMinutes()
  8. const second = date.getSeconds()
  9. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  10. }
  11. const formatNumber = n => {
  12. n = n.toString()
  13. return n[1] ? n : '0' + n
  14. }
  15. module.exports = {
  16. formatTime: formatTime,
  17. initProduct: initProduct,
  18. uploadImage: uploadImage,
  19. uploadBase64Image: uploadBase64Image,
  20. livedetect: livedetect,
  21. idcardOCR: idcardOCR,
  22. realNameAuth: realNameAuth,
  23. compare: compare
  24. }
  25. function initProduct(membershipKey, callback) {
  26. wx.request({
  27. url: 'https://www.chinadatapay.com/MyAccount/checkMembership', // 初始化产品
  28. dataType: 'json',
  29. method: 'POST',
  30. data: {
  31. membershipKey: membershipKey
  32. },
  33. header: {
  34. 'content-type': 'application/x-www-form-urlencoded'
  35. },
  36. success(res) {
  37. //执行回调
  38. return typeof callback == "function" && callback(res.data);
  39. }
  40. })
  41. }
  42. function uploadImage(appkey, tempFilePath, callback) {
  43. wx.uploadFile({
  44. url: 'https://file.chinadatapay.com/img/upload', // 图片上传
  45. filePath: tempFilePath,
  46. name: 'file',
  47. dataType: 'json',
  48. formData: {
  49. appkey: appkey
  50. },
  51. success(res) {
  52. const data = JSON.parse(res.data);
  53. //执行回调
  54. return typeof callback == "function" && callback(data);
  55. }
  56. })
  57. }
  58. function uploadBase64Image(appkey, base64Image, callback) {
  59. wx.request({
  60. url: 'https://file.chinadatapay.com/img/uploadByBase64',
  61. dataType: 'json',
  62. method: 'POST',
  63. data: {
  64. appkey: appkey,
  65. base64Image: base64Image
  66. },
  67. header: {
  68. 'content-type': 'application/x-www-form-urlencoded'
  69. },
  70. success(res) {
  71. const data = res.data;
  72. //执行回调
  73. return typeof callback == "function" && callback(data);
  74. }
  75. })
  76. }
  77. function livedetect(membershipKey, key, tempFilePath, apiKey, apiSecret, callback) {
  78. this.uploadImage(key, tempFilePath, function (result) {
  79. var imageId = result.data;
  80. wx.request({
  81. url: 'https://api.chinadatapay.com/trade/user/2104', // 活体检测
  82. dataType: 'json',
  83. method: 'POST',
  84. data: {
  85. imageId: imageId,
  86. key: key,
  87. apiSecret: apiSecret,
  88. return_image: true,
  89. apiKey: apiKey,
  90. membershipkey: membershipKey
  91. },
  92. header: {
  93. 'content-type': 'application/x-www-form-urlencoded'
  94. },
  95. success(res) {
  96. const data = res.data
  97. //执行回调
  98. return typeof callback == "function" && callback(data);
  99. }
  100. })
  101. });
  102. }
  103. function compare(membershipKey, key, base64Image, name, idcard, callback) {
  104. this.uploadBase64Image(key, base64Image, function (result) {
  105. var imageId = result.data;
  106. wx.request({
  107. url: 'https://api.chinadatapay.com/communication/personal/2061', // 人像比对
  108. dataType: 'json',
  109. method: 'POST',
  110. data: {
  111. imageId: imageId,
  112. key: key,
  113. name: name,
  114. idcard: idcard,
  115. membershipkey: membershipKey
  116. },
  117. header: {
  118. 'content-type': 'application/x-www-form-urlencoded'
  119. },
  120. success(res) {
  121. const data = res.data
  122. //执行回调
  123. return typeof callback == "function" && callback(data);
  124. }
  125. })
  126. });
  127. }
  128. function realNameAuth(membershipKey, key, name, idcard, callback) {
  129. wx.request({
  130. url: 'https://api.chinadatapay.com/communication/personal/1882', // 实名认证
  131. dataType: 'json',
  132. method: 'POST',
  133. data: {
  134. key: key,
  135. name: name,
  136. idcard: idcard,
  137. membershipkey: membershipKey
  138. },
  139. header: {
  140. 'content-type': 'application/x-www-form-urlencoded'
  141. },
  142. success(res) {
  143. const data = res.data
  144. //执行回调
  145. return typeof callback == "function" && callback(data);
  146. }
  147. })
  148. }
  149. function idcardOCR(membershipKey, key, tempFilePath, callback) {
  150. this.uploadImage(key, tempFilePath, function (result) {
  151. var imageId = result.data;
  152. wx.request({
  153. url: 'https://api.chinadatapay.com/trade/user/1985', // 身份证OCR识别
  154. dataType: 'json',
  155. method: 'POST',
  156. data: {
  157. imageId: imageId,
  158. key: key,
  159. membershipkey: membershipKey
  160. },
  161. header: {
  162. 'content-type': 'application/x-www-form-urlencoded'
  163. },
  164. success(res) {
  165. const data = res.data;
  166. //执行回调
  167. return typeof callback == "function" && callback(data);
  168. }
  169. })
  170. })
  171. }