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.showModal({
  90. title: '请点击上方选择照片或者视频哦',
  91. showCancel: false
  92. });
  93. return false
  94. }
  95. var Bucket = 'bucketnj1-1254259530';
  96. var Region = 'ap-nanjing';
  97. var ForcePathStyle = false;
  98. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  99. if (ForcePathStyle) {
  100. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  101. }
  102. var stsCache;
  103. var getCredentials = function (callback) {
  104. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  105. callback(data.credentials);
  106. return;
  107. }
  108. wx.request({
  109. method: 'POST',
  110. url: app.globalData.publicUrl + '/wx/course/getSts',
  111. // dataType: 'json',
  112. success: function (result) {
  113. var credentials = result.data.credential.credentials;
  114. if (credentials) {
  115. stsCache = result.data.credential
  116. } else {
  117. wx.hideLoading()
  118. wx.showModal({
  119. title: '临时密钥获取失败',
  120. content: JSON.stringify(data),
  121. showCancel: false
  122. });
  123. }
  124. callback(stsCache && stsCache.credentials);
  125. },
  126. error: function (err) {
  127. wx.hideLoading()
  128. wx.showModal({
  129. title: '临时密钥获取失败',
  130. content: JSON.stringify(err),
  131. showCancel: false
  132. });
  133. }
  134. });
  135. };
  136. // 计算签名
  137. var getAuthorization = function (options, callback) {
  138. getCredentials(function (credentials) {
  139. callback({
  140. XCosSecurityToken: credentials.sessionToken,
  141. Authorization: COS({
  142. SecretId: credentials.tmpSecretId,
  143. SecretKey: credentials.tmpSecretKey,
  144. Method: options.Method,
  145. Pathname: options.Pathname,
  146. })
  147. });
  148. });
  149. };
  150. // 上传文件
  151. var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
  152. var signPathname = '/';
  153. if (ForcePathStyle) {
  154. signPathname = '/' + Bucket + '/';
  155. }
  156. getAuthorization({
  157. Method: 'POST',
  158. Pathname: signPathname
  159. }, (AuthData) => {
  160. var requestTask = wx.uploadFile({
  161. url: prefix,
  162. name: 'file',
  163. filePath: this.data.carWin_img,
  164. formData: {
  165. 'key': "face/" + Key,
  166. 'success_action_status': 200,
  167. 'Signature': AuthData.Authorization,
  168. 'x-cos-security-token': AuthData.XCosSecurityToken,
  169. 'Content-Type': '',
  170. },
  171. success: (res) => {
  172. console.log(res.header.Location)
  173. this.tj(params, res.header.Location)
  174. },
  175. fail: function () {
  176. wx.hideLoading()
  177. wx.showModal({
  178. title: '上传腾讯云失败',
  179. showCancel: false
  180. });
  181. }
  182. });
  183. requestTask.onProgressUpdate(function (res) {
  184. console.log('进度:', res.progress);
  185. // if (res.progress == 100) {
  186. // wx.hideLoading();
  187. // }
  188. });
  189. });
  190. } else {
  191. wx.hideLoading()
  192. wx.showModal({
  193. showCancel: false,
  194. content: "您已经认证过了"
  195. })
  196. }
  197. },
  198. // 如果有就把信息回显出来
  199. getInformation(sessionKey) {
  200. // let _this = this;
  201. wx.request({
  202. url: app.globalData.publicUrl + '/wx/student/selStudentSessionKeyEcho',
  203. method: "post",
  204. data: {
  205. sessionKey
  206. },
  207. success: (res) => {
  208. console.log(res);
  209. if (res.data.code == 0) {
  210. console.log(res)
  211. if (res.data.data.studentOpenId != null) {
  212. this.setData({
  213. isRz: 1,
  214. userName: res.data.data.miniName,
  215. carWin_img: res.data.data.faceImage,
  216. card: res.data.data.studentCard,
  217. })
  218. }
  219. }
  220. }
  221. })
  222. },
  223. async onLoad() {
  224. const sessionKey = await tools.checkSessionAndLogin();
  225. this.setData({
  226. sessionKey
  227. })
  228. this.getInformation(sessionKey);
  229. this.initValidate();
  230. },
  231. // 验证字段的规则
  232. initValidate() {
  233. const rules = {
  234. userName: {
  235. required: true
  236. },
  237. // phone: {
  238. // required: true,
  239. // tel: true,
  240. // },
  241. card: {
  242. required: true,
  243. idcard: true,
  244. }
  245. }
  246. // 验证字段的提示信息,若不传则调用默认的信息
  247. const messages = {
  248. userName: {
  249. required: '请输入姓名'
  250. },
  251. // phone: {
  252. // required: '请输入手机号',
  253. // tel: '请输入正确的手机号',
  254. // },
  255. card: {
  256. required: '请输入身份证号码',
  257. idcard: '请输入正确的身份证号码',
  258. }
  259. }
  260. // 创建实例对象
  261. this.WxValidate = new WxValidate(rules, messages)
  262. }
  263. })