face-photo.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const _sfc_main = {
  4. data() {
  5. return {
  6. photoSrc: "",
  7. // 拍摄的照片路径
  8. isCameraReady: false,
  9. cameraContext: null,
  10. isPageLoaded: false
  11. // 添加页面加载状态标记
  12. };
  13. },
  14. onReady() {
  15. this.cameraContext = common_vendor.index.createCameraContext();
  16. setTimeout(() => {
  17. this.isPageLoaded = true;
  18. }, 100);
  19. },
  20. methods: {
  21. // 处理相机错误
  22. handleCameraError(e) {
  23. console.error("相机错误:", e);
  24. common_vendor.index.showToast({
  25. title: "相机初始化失败,请检查权限设置",
  26. icon: "none"
  27. });
  28. },
  29. // 拍照方法
  30. takePhoto() {
  31. if (!this.cameraContext) {
  32. common_vendor.index.showToast({
  33. title: "相机未就绪",
  34. icon: "none"
  35. });
  36. return;
  37. }
  38. common_vendor.index.showLoading({
  39. title: "拍照中..."
  40. });
  41. this.cameraContext.takePhoto({
  42. quality: "high",
  43. success: (res) => {
  44. this.photoSrc = res.tempImagePath;
  45. common_vendor.index.hideLoading();
  46. },
  47. fail: (err) => {
  48. console.error("拍照失败:", err);
  49. common_vendor.index.hideLoading();
  50. common_vendor.index.showToast({
  51. title: "拍照失败,请重试",
  52. icon: "none"
  53. });
  54. }
  55. });
  56. },
  57. // 重新拍照
  58. retakePhoto() {
  59. this.photoSrc = "";
  60. },
  61. startInterview() {
  62. if (!this.photoSrc) {
  63. common_vendor.index.showToast({
  64. title: "请先完成拍照",
  65. icon: "none"
  66. });
  67. return;
  68. }
  69. common_vendor.index.showLoading({
  70. title: "验证中..."
  71. });
  72. setTimeout(() => {
  73. common_vendor.index.hideLoading();
  74. common_vendor.index.navigateTo({
  75. url: "/pages/camera/camera",
  76. fail: (err) => {
  77. console.error("页面跳转失败:", err);
  78. common_vendor.index.showToast({
  79. title: "页面跳转失败",
  80. icon: "none"
  81. });
  82. }
  83. });
  84. }, 1500);
  85. }
  86. // 上传照片方法(示例)
  87. /*
  88. uploadPhoto() {
  89. uni.uploadFile({
  90. url: 'https://your-api-endpoint.com/upload',
  91. filePath: this.photoSrc,
  92. name: 'photo',
  93. success: (res) => {
  94. const data = JSON.parse(res.data);
  95. if (data.success) {
  96. // 验证成功,继续流程
  97. } else {
  98. // 验证失败,提示用户
  99. uni.showToast({
  100. title: data.message || '验证失败,请重试',
  101. icon: 'none'
  102. });
  103. }
  104. },
  105. fail: (err) => {
  106. console.error('上传失败:', err);
  107. uni.showToast({
  108. title: '网络错误,请重试',
  109. icon: 'none'
  110. });
  111. }
  112. });
  113. }
  114. */
  115. }
  116. };
  117. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  118. return common_vendor.e({
  119. a: !$data.photoSrc
  120. }, !$data.photoSrc ? {
  121. b: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  122. } : {
  123. c: $data.photoSrc
  124. }, {
  125. d: !$data.photoSrc
  126. }, !$data.photoSrc ? {
  127. e: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
  128. } : {
  129. f: common_vendor.o((...args) => $options.retakePhoto && $options.retakePhoto(...args)),
  130. g: common_vendor.o((...args) => $options.startInterview && $options.startInterview(...args))
  131. }, {
  132. h: $data.isPageLoaded ? 1 : ""
  133. });
  134. }
  135. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  136. wx.createPage(MiniProgramPage);