App.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <script>
  2. import instApi from "./api/inst";
  3. import to from "await-to-js";
  4. const module = uni.requireNativePlugin("leven-arcFace-ArcFaceModule");
  5. export default {
  6. onLaunch: function () {
  7. this.getAllPhoto();
  8. setInterval(() => {
  9. this.getAllPhoto();
  10. }, 60000);
  11. plus.navigator.hideSystemNavigation();
  12. plus.screen.lockOrientation("landscape-primary");
  13. plus.navigator.setFullscreen(true); //隐藏状态栏(应用全屏:只能隐藏状态栏,标题栏和虚拟返回键都还可以显示)
  14. },
  15. onShow: function () {
  16. console.log("App Show");
  17. },
  18. onHide: function () {
  19. console.log("App Hide");
  20. },
  21. methods: {
  22. // 获取本地和线上注册的人脸图片信息
  23. async getAllPhoto() {
  24. let onLineFacePhoto = [];
  25. let localFacePhoto = [];
  26. // 获取线上所有的人脸图片
  27. let [err, res] = await to(instApi.getPhoto({}));
  28. if (err) return;
  29. if (res.code == 200 && res.data.length > 0) {
  30. onLineFacePhoto = res.data.map((item) => {
  31. console.log("头像", this.completionImgPath(item.url));
  32. return {
  33. ...item,
  34. url: this.completionImgPath(item.url),
  35. name: btoa(encodeURIComponent(this.completionImgPath(item.url))),
  36. };
  37. });
  38. console.log("在线", onLineFacePhoto);
  39. // 获取注册了的人脸信息
  40. module.getAllFace((localRes) => {
  41. localFacePhoto = localRes.code == 0 ? localRes.data.list : [];
  42. console.log("本地", localFacePhoto);
  43. // 如果本地注册量0 并且 线上有图片 则直接全部注册
  44. if (localFacePhoto.length == 0 && onLineFacePhoto.length > 0) {
  45. this.register(onLineFacePhoto);
  46. } else if (
  47. localFacePhoto.length > 0 &&
  48. onLineFacePhoto.length > 0
  49. ) {
  50. this.handleFacePhoto(onLineFacePhoto, localFacePhoto);
  51. }
  52. });
  53. }
  54. },
  55. completionImgPath(path) {
  56. const filePath =
  57. uni.getStorageSync("labsop_filePath") || process.uniEnv.VITE_FILE;
  58. let url = path;
  59. if (url.indexOf("http") == -1) {
  60. url = filePath + url;
  61. }
  62. return url;
  63. },
  64. // 对比在线人脸和本地人脸的数组是否有差异
  65. diffFacePhoto(onlineArr, localArr) {
  66. const unregistered = onlineArr.filter(
  67. (onlineItem) =>
  68. !localArr.some((localItem) => localItem.id === onlineItem.id)
  69. );
  70. console.log("unregistered", unregistered);
  71. return unregistered;
  72. },
  73. // 批量注册
  74. register(list) {
  75. console.log("需注册的人脸列表", list);
  76. if (list.length > 0) {
  77. module.batchRegister(
  78. {
  79. // 同一人是否可以多次注册,默认true
  80. registerMultiple: false,
  81. list,
  82. },
  83. (res) => {
  84. console.log("注册信息", res);
  85. }
  86. );
  87. }
  88. },
  89. handleFacePhoto(onLineFacePhoto, localFacePhoto) {
  90. let localArr = localFacePhoto;
  91. let onlineArr = onLineFacePhoto;
  92. // 创建两个空数组来存放结果
  93. let notInLocalArr = [];
  94. let nameUrlMismatch = [];
  95. // 将localArr转换为对象以便快速查找
  96. let localArrMap = new Map();
  97. localArr.forEach((item) => {
  98. localArrMap.set(item.id, decodeURIComponent(atob(item.name)));
  99. });
  100. // 遍历onlineArr来检查id是否在localArr中不存在,以及name和url是否匹配
  101. onlineArr.forEach((item) => {
  102. if (!localArrMap.has(item.id)) {
  103. notInLocalArr.push(item);
  104. } else if (localArrMap.get(item.id) !== item.url) {
  105. nameUrlMismatch.push(item);
  106. }
  107. });
  108. console.log("IDs not in localArr:", notInLocalArr);
  109. console.log("Name and URL mismatch:", nameUrlMismatch);
  110. // 如果有需要注册的人脸,则进行注册
  111. if (notInLocalArr.length > 0) {
  112. this.register(notInLocalArr);
  113. }
  114. // 如果有需要更新的人脸,则进行删除更新
  115. if (nameUrlMismatch.length > 0) {
  116. this.updateFace(nameUrlMismatch);
  117. }
  118. },
  119. // 更新人脸(删除后重新注册)
  120. updateFace(list) {
  121. console.log("需更新的人脸列表", list);
  122. if (list.length > 0) {
  123. list.map((item) => {
  124. // 先删除
  125. console.log("item.id", item.id);
  126. module.getFace(
  127. {
  128. id: item.id,
  129. },
  130. (res) => {
  131. console.log("是否存在要删除的人脸", res);
  132. module.deleteFace(
  133. {
  134. id: item.id,
  135. },
  136. (res) => {
  137. console.log("删除信息", res);
  138. if (res.code == 0) {
  139. module.imageFaceRegister(
  140. {
  141. url: item.url,
  142. id: item.id,
  143. name: item.name,
  144. // 同一人是否可以多次注册,默认true
  145. registerMultiple: false,
  146. },
  147. (res) => {
  148. console.log(res);
  149. }
  150. );
  151. }
  152. }
  153. );
  154. }
  155. );
  156. });
  157. }
  158. },
  159. },
  160. };
  161. </script>
  162. <style lang="scss">
  163. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  164. @import "@/uni_modules/uview-ui/index.scss";
  165. @import "./style/common.scss";
  166. </style>