// pages/faceRecognition/index.ts import { base } from "../../config/index"; Page({ /** * 页面的初始数据 */ data: { isBack: false, // 是否返回上一页,用于页面隐藏时判断 }, /** * 生命周期函数--监听页面加载 */ onLoad() { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { console.log('页面显示') this.setData({ isBack: true }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页 // 防止在录制中退出后台导致下次重新录制失败 "operateCamera:fail:is stopping" console.log('页面隐藏') if (this.data.isBack) { wx.navigateBack(); } }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, // 当取消授权或者打开设置授权 handleNoAuth(res: any) { console.log("用户拒绝授权:", res) // 因为在设置里授权摄像头不会立即生效,所以要返回上一页,确保重新进入当前页使摄像头生效 let t = setTimeout(() => { wx.navigateBack(); wx.stopFaceDetect(); clearTimeout(t); }, 500) }, // 版本号过低的回调 handleCannotuse() { console.log('版本号过低无法使用, 组件内已经弹窗提示过了') wx.navigateBack(); wx.stopFaceDetect(); }, // 视频录制完成 handleComplete(e: WechatMiniprogram.CustomEvent) { this.getVideoData(e); }, // 获取视频信息 getVideoData(e: WechatMiniprogram.CustomEvent){ console.log('视频文件路径:', e.detail.path) // e.detail: 视频临时路径 this.setData({ isBack: false }) // 打印视频信息文件 const fileSystemManager = wx.getFileSystemManager(); fileSystemManager.getFileInfo({ filePath: e.detail.path?.toString(), success: (res) => { const { size } = res console.log("视频文件大小M:", size / Math.pow(1024, 2)) }, fail: (err) => { console.log("获取视频文件失败", err) } }) this.uploadVideo(e.detail.path.toString(), e.detail.msg) }, uploadVideo(url: string, msg: boolean) { var src = url; wx.showLoading({ title: '上传进度:0%', mask: true //是否显示透明蒙层,防止触摸穿透 }) // todo: 添加上传服务器的接口 const uploadTask = wx.uploadFile({ url: base.url + '/v3/upload', filePath: src, name: 'file', //服务器定义的Key值 formData: { notify: msg }, header: { Authorization: wx.getStorageSync("token") }, success: function () { console.log('视频上传成功') wx.showToast({ title: "人脸上传完成", icon: "none", duration: 2000 }) let time = setTimeout(() => { clearTimeout(time); wx.reLaunch({ url: "/pages/downloadPage/index" }) }, 2000) }, fail: (err: any) => { console.log('接口调用失败', err); const son = this.selectComponent("#cameraFace"); son.stopRecord(); son.stopUI(); wx.showToast({ title: err.message || "上传失败", icon: "none", duration: 2000 }) } }) //监听上传进度变化事件 uploadTask.onProgressUpdate((res) => { wx.showLoading({ title: '上传进度:' + res.progress + '%', mask: true //是否显示透明蒙层,防止触摸穿透 }) }) } })