liyongli 2 years ago
parent
commit
42ac4fdc43

+ 1 - 0
miniprogram/app.json

@@ -2,6 +2,7 @@
   "pages": [
     "pages/home/index",
     "pages/faceRecognition/index",
+    "pages/faceRecognitionVK/index",
     "pages/entry/index",
     "pages/downloadPage/index"
   ],

+ 3 - 6
miniprogram/components/camera-face/index.ts

@@ -131,14 +131,10 @@ Component({
     // 获取消息授权
     getMssage() {
       return new Promise((resolve) => {
-        let T = wx.getStorageSync("messageAuth") || "";
-        if (T) resolve(T);
-        else wx.requestSubscribeMessage({
+        wx.requestSubscribeMessage({
           tmplIds: app.globalData.configPage?.messageID || [],
           success: (res: WechatMiniprogram.RequestSubscribeMessageSuccessCallbackResult) => {
-            const isAccept = res.p1mpCydIQ6OtxCSa62NaSFiEkQiTsb8KPFaAs1SuKMw === 'accept';
-            wx.setStorageSync("messageAuth", isAccept)
-            resolve(isAccept);
+            resolve(res.p1mpCydIQ6OtxCSa62NaSFiEkQiTsb8KPFaAs1SuKMw || 'reject');
           },
           fail: () => {
             resolve(false);
@@ -281,6 +277,7 @@ Component({
            */
           console.log("录制结束", res, isVadeo);
           if (!isVadeo) return;
+          console.log("是否授权:", this.data.messageAuto)
           this.triggerEvent('complete', { path: res.tempVideoPath, msg: this.data.messageAuto })
           wx.stopFaceDetect({
             success: () => {

+ 12 - 9
miniprogram/pages/downloadPage/index.ts

@@ -36,23 +36,20 @@ Page({
     }, 200);
   },
   downloadVideo() {
+    wx.showLoading({
+      title: ""
+    })
     app.getUser({
       data: {},
       success: (rule: any) => {
-        if(!rule.PAY) return
+        if (!rule.PAY) return
         if (t) t = clearTimeout(t);
         t = setTimeout(() => {
-          wx.showLoading({
-            title: ""
-          })
           app.getVideo({
             success: (r: any) => {
               wx.downloadFile({
                 url: r.url,
                 success: (res) => {
-                  wx.showLoading({
-                    title: ""
-                  })
                   this.setData({
                     videoSrc: r.url
                   })
@@ -77,11 +74,18 @@ Page({
                 fail: () => {
                   wx.hideLoading();
                 },
-                complete: () => { }
+                complete: () => {
+                }
               })
             },
+            fail: () => {
+              wx.hideLoading();
+            }
           })
         }, 200);
+      },
+      fail: () => {
+        wx.hideLoading();
       }
     })
   },
@@ -130,7 +134,6 @@ Page({
         }
         app.getVideo({
           success: (r: any) => {
-            console.log(r);
             this.setData({
               videoSrc: r.url,
               paragraph: res.paragraph || [],

+ 5 - 7
miniprogram/pages/entry/index.ts

@@ -1,6 +1,6 @@
 // pages/entry/index.ts
 const app = getApp<IAppOption>();
-// import { compareVersion } from "../../utils/util";
+import { compareVersion } from "../../utils/util";
 Page({
 
   /**
@@ -41,16 +41,14 @@ Page({
       success: () => {
         // 进入人脸识别
         let url = "/pages/faceRecognition/index";
-        // const version:string = app.globalData.SystemInfo?.SDKVersion || "";
-        // if(compareVersion(version, "2.20.0")) url = "/pages/faceRecognitionVk/index"
-        wx.navigateTo({
-          url
-        })
+        const version:string = app.globalData.SystemInfo?.SDKVersion || "";
+        if(compareVersion(version, "2.20.0")) url = "/pages/faceRecognitionVK/index"
+        wx.navigateTo({ url })
       }
     })
   },
   getPhoneNumber(e: WechatMiniprogram.CustomEvent) {
-    if(!e.detail.code) {
+    if (!e.detail.code) {
       wx.showToast({
         title: "请升级最新版微信。"
       })

+ 4 - 0
miniprogram/pages/faceRecognitionVK/index.json

@@ -0,0 +1,4 @@
+{
+  "usingComponents": {
+  }
+}

+ 23 - 0
miniprogram/pages/faceRecognitionVK/index.scss

@@ -0,0 +1,23 @@
+/* pages/faceRecognition/index.wxss */
+@import "../../components/camera-face/index.wxss";
+
+page {
+  background: #ffffff;
+  height: 100vh;
+  display: flex;
+  align-items: center;
+}
+
+.page {
+  width: 100%;
+  height: 750rpx;
+
+  .camera{
+    width: 80%;
+    height: 80%;
+    margin: 0 auto 2em auto;
+    border-radius: 50%;
+    background-color: #000000;
+  }
+}
+

+ 238 - 0
miniprogram/pages/faceRecognitionVK/index.ts

@@ -0,0 +1,238 @@
+// pages/faceRecognition/index.ts
+import { base } from "../../config/index";
+import { compareVersion } from "../../utils/util";
+let ctx: WechatMiniprogram.CameraContext | undefined = undefined;
+let createVKSession: WechatMiniprogram.VKSession | undefined = undefined;
+const app = getApp<IAppOption>();
+Page({
+
+  /**
+   * 页面的初始数据
+   */
+  data: {
+    isStartVideo: false, // 是否点击开始按钮
+    isEmpower: false, // 是否授权
+    isUpLoad: false, // 是否上传中
+    isTranscribe: false, // 是否录制中
+
+    buttomText: "录制"
+  },
+
+  // 开始初始化录制
+  initCamera() {
+    if (this.data.isStartVideo) return;
+    this.setData({
+      isStartVideo: true
+    })
+    wx.nextTick(() => {
+      this.createVK();
+      wx.showLoading({
+        title: ""
+      })
+      ctx = wx.createCameraContext();
+      ctx.startRecord({
+        timeout: 15, // 录制时长
+        timeoutCallback: () => {
+          // 超出录制时长 关闭录屏
+          this.overVideo();
+        },
+        success: () => {
+          // 调用成功,开始录制
+          wx.hideLoading();
+        },
+        fail: () => {
+          // 调用失败
+          wx.hideLoading();
+          wx.showToast({
+            icon: "none",
+            title: "相机调用失败",
+            duration: 2000
+          })
+        },
+        complete: () => { }
+      })
+    })
+  },
+
+  // 创建人脸追踪
+  createVK() {
+    console.log("开始追踪人脸");
+    createVKSession = wx.createVKSession({
+      version: "v1",
+      track: {
+        plane: {
+          mode: 3
+        },
+        face: {
+          mode: 1
+        }
+      },
+    })
+
+    createVKSession.on("updateAnchors", anchors => {
+      console.log("检测到人脸", anchors)
+      if (!this.data.isTranscribe) {
+        this.setData({
+          isTranscribe: true
+        })
+      }
+    })
+
+    // 当人脸从相机中离开时,会触发 removeAnchors 事件
+    createVKSession.on('removeAnchors', () => {
+      wx.showToast({
+        icon: "none",
+        title: "未检测到人脸,请重新录制",
+        duration: 2000
+      })
+      //  录制失败
+      this.overVideo();
+    })
+
+    createVKSession.start((errno) => {
+      console.log(errno);
+    })
+  },
+
+  // 结束录像
+  overVideo() {
+    if (!ctx || !this.data.isStartVideo) return;
+    wx.showLoading({
+      title: ""
+    })
+    createVKSession?.stop();
+    ctx.stopRecord({
+      compressed: true,
+      success: res => {
+        // 成功结束
+        wx.hideLoading();
+        this.getUrl(res);
+      },
+      fail: () => {
+        // 结束失败
+        wx.hideLoading();
+        wx.showToast({
+          icon: "none",
+          title: "相机关闭失败",
+          duration: 2000
+        })
+      },
+      complete: () => {
+        this.setData({
+          isStartVideo: false
+        })
+      }
+    })
+  },
+
+  // 获取本地临时地址
+  getUrl(res: WechatMiniprogram.StartRecordTimeoutCallbackResult) {
+    this.setData({
+      isUpLoad: true
+    })
+    wx.showLoading({
+      title: ""
+    })
+    wx.uploadFile({
+      // 仅为示例,非真实的接口地址,视频上传
+      url: base.url + '/v3/upload',
+      filePath: res.tempVideoPath,
+      name: 'file', //服务器定义的Key值
+      formData: {},
+      header: {
+        Authorization: wx.getStorageSync("token")
+      },
+      success: function (res) {
+        console.log(res)
+        wx.hideLoading();
+        wx.showToast({
+          title: "上传成功",
+          duration: 2000,
+          icon: "none"
+        })
+      },
+      fail: function (res) {
+        console.log(res)
+        wx.hideLoading();
+        wx.showToast({
+          title: "上传失败",
+          icon: "none",
+          duration: 2000
+        })
+      },
+      complete: () => {
+        this.setData({
+          isUpLoad: false
+        })
+      }
+    })
+  },
+
+  // 非正常中止
+  improperStop() { },
+
+  // 未授权
+  unauthorized() { },
+
+  // 初始化完成
+  initialization() { },
+
+  /**
+   * 生命周期函数--监听页面加载
+   */
+  onLoad(opt) {
+    console.log(opt)
+    const SDKVersion: string = app.globalData.SystemInfo?.SDKVersion || "";
+    compareVersion(SDKVersion, '1.9.6');
+  },
+
+  /**
+   * 生命周期函数--监听页面初次渲染完成
+   */
+  onReady() {
+
+  },
+
+  /**
+   * 生命周期函数--监听页面显示
+   */
+  onShow() {
+  },
+
+  /**
+   * 生命周期函数--监听页面隐藏
+   */
+  onHide() {
+    // 在录制中退出后台页面隐藏,返回上一页,确保重新进入当前页
+  },
+
+  /**
+   * 生命周期函数--监听页面卸载
+   */
+  onUnload() {
+
+  },
+
+  /**
+   * 页面相关事件处理函数--监听用户下拉动作
+   */
+  onPullDownRefresh() {
+
+  },
+
+  /**
+   * 页面上拉触底事件的处理函数
+   */
+  onReachBottom() {
+
+  },
+
+  /**
+   * 用户点击右上角分享
+   */
+  onShareAppMessage() {
+
+  },
+})
+
+export { }

+ 16 - 0
miniprogram/pages/faceRecognitionVK/index.wxml

@@ -0,0 +1,16 @@
+<view class="page-body">
+  <view class="head">
+    <!-- 请拿起手机正对摄像头——请向左转——请向右转 -->
+    <view class="top-tips">{{topTips}}</view>
+
+    <camera class="camera" mode="normal" resolution="medium" device-position="front" flash="off" frame-size="medium" bindstop="improperStop" binderror="unauthorized" 	bindinitdone="initialization">
+      <view wx:if="{{seconds>0}}" class="half-circle">
+        {{seconds}}s
+      </view>
+    </camera>
+  </view>
+
+  <!-- 开始人脸验证——人脸验证中——人脸验证成功 -->
+  <view wx:if="{{!isStartVideo}}" class="faceBtn" bindtap="initCamera">开始人脸采集</view>
+  <view wx:if="{{isStartVideo}}" class="bottom-tips">{{bottomTips}}</view>
+</view>

+ 4 - 6
miniprogram/pages/home/index.ts

@@ -1,6 +1,6 @@
 // pages/home/index.ts
 const app = getApp<IAppOption>();
-// import { compareVersion } from "../../utils/util";
+import { compareVersion } from "../../utils/util";
 Page({
 
   /**
@@ -25,11 +25,9 @@ Page({
         if (!res.UPLOAD_FACE) {
           // 未上传人脸信息,跳转人脸采集页
           let url = "/pages/faceRecognition/index";
-          // const version: string = app.globalData.SystemInfo?.SDKVersion || "";
-          // if (compareVersion(version, "2.20.0")) url = "/pages/faceRecognitionVk/index"
-          wx.navigateTo({
-            url
-          })
+          const version: string = app.globalData.SystemInfo?.SDKVersion || "";
+          if (compareVersion(version, "2.20.0")) url = "/pages/faceRecognitionVK/index"
+          wx.navigateTo({ url })
           return;
         }
         wx.redirectTo({

+ 1 - 1
miniprogram/utils/request.ts

@@ -12,7 +12,7 @@ export default function request(params: WechatMiniprogram.requestParamsData) {
     header: params.header,
     dataType: params.dataType || 'json',
     success: (res: any) => {
-      wx.hideLoading()
+      wx.hideLoading();
       if (res.statusCode !== 200 || res.data.code !== 0) {
         // code 401  token过期
         errHttp(res.data.message || res.data.message);