index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // pages/faceRecognition/index.ts
  2. import { base } from "../../config/index";
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. isBack: false, // 是否返回上一页,用于页面隐藏时判断
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad() {
  14. },
  15. /**
  16. * 生命周期函数--监听页面初次渲染完成
  17. */
  18. onReady() {
  19. },
  20. /**
  21. * 生命周期函数--监听页面显示
  22. */
  23. onShow() {
  24. console.log('页面显示')
  25. this.setData({
  26. isBack: true
  27. })
  28. },
  29. /**
  30. * 生命周期函数--监听页面隐藏
  31. */
  32. onHide() {
  33. // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页
  34. // 防止在录制中退出后台导致下次重新录制失败 "operateCamera:fail:is stopping"
  35. console.log('页面隐藏')
  36. if (this.data.isBack) {
  37. wx.navigateBack();
  38. }
  39. },
  40. /**
  41. * 生命周期函数--监听页面卸载
  42. */
  43. onUnload() {
  44. },
  45. /**
  46. * 页面相关事件处理函数--监听用户下拉动作
  47. */
  48. onPullDownRefresh() {
  49. },
  50. /**
  51. * 页面上拉触底事件的处理函数
  52. */
  53. onReachBottom() {
  54. },
  55. /**
  56. * 用户点击右上角分享
  57. */
  58. onShareAppMessage() {
  59. },
  60. // 当取消授权或者打开设置授权
  61. handleNoAuth(res: any) {
  62. console.log("用户拒绝授权:", res)
  63. // 因为在设置里授权摄像头不会立即生效,所以要返回上一页,确保重新进入当前页使摄像头生效
  64. let t = setTimeout(() => {
  65. wx.navigateBack();
  66. wx.stopFaceDetect();
  67. clearTimeout(t);
  68. }, 500)
  69. },
  70. // 版本号过低的回调
  71. handleCannotuse() {
  72. console.log('版本号过低无法使用, 组件内已经弹窗提示过了')
  73. wx.navigateBack();
  74. wx.stopFaceDetect();
  75. },
  76. // 视频录制完成
  77. handleComplete(e: WechatMiniprogram.CustomEvent) {
  78. this.getVideoData(e);
  79. },
  80. // 获取视频信息
  81. getVideoData(e: WechatMiniprogram.CustomEvent){
  82. console.log('视频文件路径:', e.detail.path)
  83. // e.detail: 视频临时路径
  84. this.setData({
  85. isBack: false
  86. })
  87. // 打印视频信息文件
  88. const fileSystemManager = wx.getFileSystemManager();
  89. fileSystemManager.getFileInfo({
  90. filePath: e.detail.path?.toString(),
  91. success: (res) => {
  92. const {
  93. size
  94. } = res
  95. console.log("视频文件大小M:", size / Math.pow(1024, 2))
  96. },
  97. fail: (err) => {
  98. console.log("获取视频文件失败", err)
  99. }
  100. })
  101. this.uploadVideo(e.detail.path.toString(), e.detail.msg)
  102. },
  103. uploadVideo(url: string, msg: boolean) {
  104. var src = url;
  105. wx.showLoading({
  106. title: '上传进度:0%',
  107. mask: true //是否显示透明蒙层,防止触摸穿透
  108. })
  109. // todo: 添加上传服务器的接口
  110. const uploadTask = wx.uploadFile({
  111. url: base.url + '/v3/upload',
  112. filePath: src,
  113. name: 'file', //服务器定义的Key值
  114. formData: {
  115. notify: msg
  116. },
  117. header: {
  118. Authorization: wx.getStorageSync("token")
  119. },
  120. success: function () {
  121. console.log('视频上传成功')
  122. wx.showToast({
  123. title: "人脸上传完成",
  124. icon: "none",
  125. duration: 2000
  126. })
  127. let time = setTimeout(() => {
  128. clearTimeout(time);
  129. wx.reLaunch({
  130. url: "/pages/downloadPage/index"
  131. })
  132. }, 2000)
  133. },
  134. fail: (err: any) => {
  135. console.log('接口调用失败', err);
  136. const son = this.selectComponent("#cameraFace");
  137. son.stopRecord();
  138. son.stopUI();
  139. wx.showToast({
  140. title: err.message || "上传失败",
  141. icon: "none",
  142. duration: 2000
  143. })
  144. }
  145. })
  146. //监听上传进度变化事件
  147. uploadTask.onProgressUpdate((res) => {
  148. wx.showLoading({
  149. title: '上传进度:' + res.progress + '%',
  150. mask: true //是否显示透明蒙层,防止触摸穿透
  151. })
  152. })
  153. }
  154. })