123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // 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 //是否显示透明蒙层,防止触摸穿透
- })
- })
- }
- })
|