Identity.js 7.2 KB

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