face-photo.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. data() {
  5. return {
  6. mediaSource: "",
  7. // 拍摄的照片或视频路径
  8. isCameraReady: false,
  9. cameraContext: null,
  10. isPageLoaded: false,
  11. // 添加页面加载状态标记
  12. mode: "photo",
  13. // 默认为拍照模式,可选值:photo, video
  14. isRecording: false,
  15. // 是否正在录制视频
  16. recordingTime: 0,
  17. // 录制时间(秒)
  18. recordingTimer: null
  19. // 录制计时器
  20. };
  21. },
  22. onReady() {
  23. this.cameraContext = common_vendor.index.createCameraContext();
  24. setTimeout(() => {
  25. this.isPageLoaded = true;
  26. }, 100);
  27. },
  28. methods: {
  29. // 切换拍照/录制模式
  30. switchMode(newMode) {
  31. if (this.mediaSource) {
  32. this.retakeMedia();
  33. }
  34. this.mode = newMode;
  35. },
  36. // 处理相机错误
  37. handleCameraError(e) {
  38. console.error("相机错误:", e);
  39. common_vendor.index.showToast({
  40. title: "相机初始化失败,请检查权限设置",
  41. icon: "none"
  42. });
  43. },
  44. // 拍照方法
  45. takePhoto() {
  46. if (!this.cameraContext) {
  47. common_vendor.index.showToast({
  48. title: "相机未就绪",
  49. icon: "none"
  50. });
  51. return;
  52. }
  53. common_vendor.index.showLoading({
  54. title: "拍照中..."
  55. });
  56. this.cameraContext.takePhoto({
  57. quality: "high",
  58. success: (res) => {
  59. this.mediaSource = res.tempImagePath;
  60. common_vendor.index.hideLoading();
  61. },
  62. fail: (err) => {
  63. console.error("拍照失败:", err);
  64. common_vendor.index.hideLoading();
  65. common_vendor.index.showToast({
  66. title: "拍照失败,请重试",
  67. icon: "none"
  68. });
  69. }
  70. });
  71. },
  72. // 开始录制视频
  73. startRecording() {
  74. if (!this.cameraContext) {
  75. common_vendor.index.showToast({
  76. title: "相机未就绪",
  77. icon: "none"
  78. });
  79. return;
  80. }
  81. this.isRecording = true;
  82. this.recordingTime = 0;
  83. this.recordingTimer = setInterval(() => {
  84. this.recordingTime++;
  85. if (this.recordingTime >= 60) {
  86. this.stopRecording();
  87. }
  88. }, 1e3);
  89. this.cameraContext.startRecord({
  90. success: () => {
  91. console.log("开始录制视频");
  92. },
  93. fail: (err) => {
  94. console.error("开始录制失败:", err);
  95. this.isRecording = false;
  96. clearInterval(this.recordingTimer);
  97. common_vendor.index.showToast({
  98. title: "录制失败,请重试",
  99. icon: "none"
  100. });
  101. }
  102. });
  103. },
  104. // 停止录制视频
  105. stopRecording() {
  106. if (!this.isRecording)
  107. return;
  108. clearInterval(this.recordingTimer);
  109. this.isRecording = false;
  110. common_vendor.index.showLoading({
  111. title: "处理中..."
  112. });
  113. this.cameraContext.stopRecord({
  114. success: (res) => {
  115. this.mediaSource = res.tempVideoPath;
  116. common_vendor.index.hideLoading();
  117. },
  118. fail: (err) => {
  119. console.error("停止录制失败:", err);
  120. common_vendor.index.hideLoading();
  121. common_vendor.index.showToast({
  122. title: "视频保存失败,请重试",
  123. icon: "none"
  124. });
  125. }
  126. });
  127. },
  128. // 格式化时间显示 (秒 -> MM:SS)
  129. formatTime(seconds) {
  130. const minutes = Math.floor(seconds / 60);
  131. const remainingSeconds = seconds % 60;
  132. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  133. },
  134. // 重新拍照或录制
  135. retakeMedia() {
  136. this.mediaSource = "";
  137. this.recordingTime = 0;
  138. },
  139. startInterview() {
  140. if (!this.mediaSource) {
  141. common_vendor.index.showToast({
  142. title: `请先完成${this.mode === "photo" ? "拍照" : "视频录制"}`,
  143. icon: "none"
  144. });
  145. return;
  146. }
  147. common_vendor.index.showLoading({
  148. title: "验证中..."
  149. });
  150. setTimeout(() => {
  151. common_vendor.index.hideLoading();
  152. common_vendor.index.navigateTo({
  153. url: "/pages/camera/camera",
  154. fail: (err) => {
  155. console.error("页面跳转失败:", err);
  156. common_vendor.index.showToast({
  157. title: "页面跳转失败",
  158. icon: "none"
  159. });
  160. }
  161. });
  162. }, 1500);
  163. }
  164. // 上传媒体文件方法(示例)
  165. /*
  166. uploadMedia() {
  167. uni.uploadFile({
  168. url: 'https://your-api-endpoint.com/upload',
  169. filePath: this.mediaSource,
  170. name: this.mode === 'photo' ? 'photo' : 'video',
  171. success: (res) => {
  172. const data = JSON.parse(res.data);
  173. if (data.success) {
  174. // 验证成功,继续流程
  175. } else {
  176. // 验证失败,提示用户
  177. uni.showToast({
  178. title: data.message || '验证失败,请重试',
  179. icon: 'none'
  180. });
  181. }
  182. },
  183. fail: (err) => {
  184. console.error('上传失败:', err);
  185. uni.showToast({
  186. title: '网络错误,请重试',
  187. icon: 'none'
  188. });
  189. }
  190. });
  191. }
  192. */
  193. }
  194. };
  195. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  196. return common_vendor.e({
  197. a: $data.mode === "photo" ? 1 : "",
  198. b: common_vendor.o(($event) => $options.switchMode("photo")),
  199. c: $data.mode === "video" ? 1 : "",
  200. d: common_vendor.o(($event) => $options.switchMode("video")),
  201. e: !$data.mediaSource
  202. }, !$data.mediaSource ? {
  203. f: $data.mode,
  204. g: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  205. } : $data.mode === "photo" ? {
  206. i: $data.mediaSource
  207. } : $data.mode === "video" ? {
  208. k: $data.mediaSource
  209. } : {}, {
  210. h: $data.mode === "photo",
  211. j: $data.mode === "video",
  212. l: $data.mode === "video" && $data.isRecording
  213. }, $data.mode === "video" && $data.isRecording ? {
  214. m: common_vendor.t($options.formatTime($data.recordingTime))
  215. } : {}, {
  216. n: !$data.mediaSource
  217. }, !$data.mediaSource ? common_vendor.e({
  218. o: $data.mode === "photo"
  219. }, $data.mode === "photo" ? {
  220. p: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
  221. } : $data.mode === "video" && !$data.isRecording ? {
  222. r: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args))
  223. } : $data.mode === "video" && $data.isRecording ? {
  224. t: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
  225. } : {}, {
  226. q: $data.mode === "video" && !$data.isRecording,
  227. s: $data.mode === "video" && $data.isRecording
  228. }) : {
  229. v: common_vendor.t($data.mode === "photo" ? "拍照" : "录制"),
  230. w: common_vendor.o((...args) => $options.retakeMedia && $options.retakeMedia(...args)),
  231. x: common_vendor.o((...args) => $options.startInterview && $options.startInterview(...args))
  232. }, {
  233. y: $data.isPageLoaded ? 1 : ""
  234. });
  235. }
  236. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  237. wx.createPage(MiniProgramPage);