Identity.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. const app = require('../../utils/util.js');
  2. const tools = require('../../utils/tools.js');
  3. import WxValidate from '../../utils/WxValidate';
  4. const COS = require('../../utils/cos.js');
  5. Page({
  6. data: {
  7. isRz: 0,
  8. userName: "",
  9. phone: "",
  10. card: "",
  11. carWin_img: '', //存放照片路径的
  12. cosPath: "" //腾讯云上传的路径
  13. },
  14. // 错误信息回显
  15. showModal(error) {
  16. wx.showModal({
  17. content: error.msg,
  18. showCancel: false,
  19. })
  20. },
  21. openCamera() {
  22. wx.chooseImage({
  23. count: 1,
  24. sizeType: ['original', 'compressed'],
  25. sourceType: ['', 'camera'],
  26. success: (res) => {
  27. console.log(res)
  28. let path = res.tempFilePaths[0];
  29. // tempFilePath可以作为img标签的src属性显示图片
  30. console.log(path)
  31. this.setData({
  32. carWin_img: path
  33. })
  34. }
  35. })
  36. },
  37. // 提交
  38. tj(params, url) {
  39. wx.request({
  40. url: app.globalData.publicUrl + '/wx/student/uthentication',
  41. method: "post",
  42. data: {
  43. studentName: params.userName,
  44. studentCard: params.card,
  45. faceImage: url,
  46. sessionKey: this.data.sessionKey
  47. },
  48. success: function (res) {
  49. wx.hideLoading()
  50. if (res.data.code == 0) {
  51. wx.showModal({
  52. showCancel: false,
  53. content: "认证成功啦!",
  54. success(res) {
  55. if (res.confirm) {
  56. wx.switchTab({
  57. url: '../index/index'
  58. })
  59. }
  60. }
  61. })
  62. } else {
  63. wx.showModal({
  64. showCancel: false,
  65. content: res.data.msg
  66. })
  67. }
  68. }
  69. })
  70. },
  71. // 表单提交 跳转tj()
  72. async formSubmit(e) {
  73. console.log(e,"我是带过来的值")
  74. wx.showLoading({
  75. mask: true,
  76. title: '认证中',
  77. })
  78. if (this.data.isRz == 0) {
  79. const params = e.detail.value
  80. if (!this.WxValidate.checkForm(params)) {
  81. const error = this.WxValidate.errorList[0]
  82. this.showModal(error)
  83. wx.hideLoading()
  84. return false
  85. }
  86. if (this.data.carWin_img == "" || this.data.carWin_img == null) {
  87. wx.hideLoading();
  88. wx.showModal({
  89. title: '请点击上方选择照片或者视频哦',
  90. showCancel: false
  91. });
  92. return false
  93. }
  94. var Bucket = 'bucketnj1-1254259530';
  95. var Region = 'ap-nanjing';
  96. var ForcePathStyle = false;
  97. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  98. if (ForcePathStyle) {
  99. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  100. }
  101. var stsCache;
  102. var getCredentials = function (callback) {
  103. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  104. callback(data.credentials);
  105. return;
  106. }
  107. wx.request({
  108. method: 'POST',
  109. url: app.globalData.publicUrl + '/wx/course/getSts',
  110. // dataType: 'json',
  111. success: function (result) {
  112. var credentials = result.data.credential.credentials;
  113. if (credentials) {
  114. stsCache = result.data.credential
  115. } else {
  116. wx.hideLoading();
  117. wx.showModal({
  118. title: '临时密钥获取失败',
  119. content: JSON.stringify(data),
  120. showCancel: false
  121. });
  122. }
  123. callback(stsCache && stsCache.credentials);
  124. },
  125. error: function (err) {
  126. wx.hideLoading();
  127. wx.showModal({
  128. title: '临时密钥获取失败',
  129. content: JSON.stringify(err),
  130. showCancel: false
  131. });
  132. }
  133. });
  134. };
  135. // 计算签名
  136. var getAuthorization = function (options, callback) {
  137. getCredentials(function (credentials) {
  138. callback({
  139. XCosSecurityToken: credentials.sessionToken,
  140. Authorization: COS({
  141. SecretId: credentials.tmpSecretId,
  142. SecretKey: credentials.tmpSecretKey,
  143. Method: options.Method,
  144. Pathname: options.Pathname,
  145. })
  146. });
  147. });
  148. };
  149. // 上传文件
  150. var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
  151. var signPathname = '/';
  152. if (ForcePathStyle) {
  153. signPathname = '/' + Bucket + '/';
  154. }
  155. getAuthorization({
  156. Method: 'POST',
  157. Pathname: signPathname
  158. }, (AuthData) => {
  159. var requestTask = wx.uploadFile({
  160. url: prefix,
  161. name: 'file',
  162. filePath: this.data.carWin_img,
  163. formData: {
  164. 'key': "face/" + Key,
  165. 'success_action_status': 200,
  166. 'Signature': AuthData.Authorization,
  167. 'x-cos-security-token': AuthData.XCosSecurityToken,
  168. 'Content-Type': '',
  169. },
  170. success: (res) => {
  171. console.log(res.header.Location)
  172. this.tj(params, res.header.Location)
  173. },
  174. fail: function () {
  175. wx.hideLoading();
  176. wx.showModal({
  177. title: '上传腾讯云失败',
  178. showCancel: false
  179. });
  180. }
  181. });
  182. requestTask.onProgressUpdate(function (res) {
  183. console.log('进度:', res);
  184. if (res.progress == 100) {
  185. wx.hideLoading();
  186. }
  187. });
  188. });
  189. } else {
  190. wx.hideLoading()
  191. wx.showModal({
  192. showCancel: false,
  193. content: "您已经认证过了"
  194. })
  195. }
  196. },
  197. // 如果有就把信息回显出来
  198. getInformation(sessionKey) {
  199. // let _this = this;
  200. wx.request({
  201. url: app.globalData.publicUrl + '/wx/student/selStudentSessionKeyEcho',
  202. method: "post",
  203. data: {
  204. sessionKey
  205. },
  206. success: (res) => {
  207. console.log(res);
  208. if (res.data.code == 0) {
  209. console.log(res)
  210. if (res.data.data.studentOpenId != null) {
  211. this.setData({
  212. isRz: 1,
  213. userName: res.data.data.miniName,
  214. carWin_img: res.data.data.faceImage,
  215. card: res.data.data.studentCard,
  216. })
  217. }
  218. }
  219. }
  220. })
  221. },
  222. async onLoad() {
  223. const sessionKey = await tools.checkSessionAndLogin();
  224. this.setData({
  225. sessionKey
  226. })
  227. this.getInformation(sessionKey);
  228. this.initValidate();
  229. },
  230. // 验证字段的规则
  231. initValidate() {
  232. const rules = {
  233. userName: {
  234. required: true
  235. },
  236. // phone: {
  237. // required: true,
  238. // tel: true,
  239. // },
  240. card: {
  241. required: true,
  242. idcard: true,
  243. }
  244. }
  245. // 验证字段的提示信息,若不传则调用默认的信息
  246. const messages = {
  247. userName: {
  248. required: '请输入姓名'
  249. },
  250. // phone: {
  251. // required: '请输入手机号',
  252. // tel: '请输入正确的手机号',
  253. // },
  254. card: {
  255. required: '请输入身份证号码',
  256. idcard: '请输入正确的身份证号码',
  257. }
  258. }
  259. // 创建实例对象
  260. this.WxValidate = new WxValidate(rules, messages)
  261. }
  262. })