123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const _sfc_main = {
- data() {
- return {
- mediaSource: "",
- // 拍摄的照片或视频路径
- isCameraReady: false,
- cameraContext: null,
- isPageLoaded: false,
- // 添加页面加载状态标记
- mode: "photo",
- // 默认为拍照模式,可选值:photo, video
- isRecording: false,
- // 是否正在录制视频
- recordingTime: 0,
- // 录制时间(秒)
- recordingTimer: null
- // 录制计时器
- };
- },
- onReady() {
- this.cameraContext = common_vendor.index.createCameraContext();
- setTimeout(() => {
- this.isPageLoaded = true;
- }, 100);
- },
- methods: {
- // 切换拍照/录制模式
- switchMode(newMode) {
- if (this.mediaSource) {
- this.retakeMedia();
- }
- this.mode = newMode;
- },
- // 处理相机错误
- handleCameraError(e) {
- console.error("相机错误:", e);
- common_vendor.index.showToast({
- title: "相机初始化失败,请检查权限设置",
- icon: "none"
- });
- },
- // 拍照方法
- takePhoto() {
- if (!this.cameraContext) {
- common_vendor.index.showToast({
- title: "相机未就绪",
- icon: "none"
- });
- return;
- }
- common_vendor.index.showLoading({
- title: "拍照中..."
- });
- this.cameraContext.takePhoto({
- quality: "high",
- success: (res) => {
- this.mediaSource = res.tempImagePath;
- common_vendor.index.hideLoading();
- },
- fail: (err) => {
- console.error("拍照失败:", err);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "拍照失败,请重试",
- icon: "none"
- });
- }
- });
- },
- // 开始录制视频
- startRecording() {
- if (!this.cameraContext) {
- common_vendor.index.showToast({
- title: "相机未就绪",
- icon: "none"
- });
- return;
- }
- this.isRecording = true;
- this.recordingTime = 0;
- this.recordingTimer = setInterval(() => {
- this.recordingTime++;
- if (this.recordingTime >= 60) {
- this.stopRecording();
- }
- }, 1e3);
- this.cameraContext.startRecord({
- success: () => {
- console.log("开始录制视频");
- },
- fail: (err) => {
- console.error("开始录制失败:", err);
- this.isRecording = false;
- clearInterval(this.recordingTimer);
- common_vendor.index.showToast({
- title: "录制失败,请重试",
- icon: "none"
- });
- }
- });
- },
- // 停止录制视频
- stopRecording() {
- if (!this.isRecording)
- return;
- clearInterval(this.recordingTimer);
- this.isRecording = false;
- common_vendor.index.showLoading({
- title: "处理中..."
- });
- this.cameraContext.stopRecord({
- success: (res) => {
- this.mediaSource = res.tempVideoPath;
- common_vendor.index.hideLoading();
- },
- fail: (err) => {
- console.error("停止录制失败:", err);
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "视频保存失败,请重试",
- icon: "none"
- });
- }
- });
- },
- // 格式化时间显示 (秒 -> MM:SS)
- formatTime(seconds) {
- const minutes = Math.floor(seconds / 60);
- const remainingSeconds = seconds % 60;
- return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
- },
- // 重新拍照或录制
- retakeMedia() {
- this.mediaSource = "";
- this.recordingTime = 0;
- },
- startInterview() {
- if (!this.mediaSource) {
- common_vendor.index.showToast({
- title: `请先完成${this.mode === "photo" ? "拍照" : "视频录制"}`,
- icon: "none"
- });
- return;
- }
- common_vendor.index.showLoading({
- title: "验证中..."
- });
- setTimeout(() => {
- common_vendor.index.hideLoading();
- common_vendor.index.navigateTo({
- url: "/pages/camera/camera",
- fail: (err) => {
- console.error("页面跳转失败:", err);
- common_vendor.index.showToast({
- title: "页面跳转失败",
- icon: "none"
- });
- }
- });
- }, 1500);
- }
- // 上传媒体文件方法(示例)
- /*
- uploadMedia() {
- uni.uploadFile({
- url: 'https://your-api-endpoint.com/upload',
- filePath: this.mediaSource,
- name: this.mode === 'photo' ? 'photo' : 'video',
- success: (res) => {
- const data = JSON.parse(res.data);
- if (data.success) {
- // 验证成功,继续流程
- } else {
- // 验证失败,提示用户
- uni.showToast({
- title: data.message || '验证失败,请重试',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- console.error('上传失败:', err);
- uni.showToast({
- title: '网络错误,请重试',
- icon: 'none'
- });
- }
- });
- }
- */
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: $data.mode === "photo" ? 1 : "",
- b: common_vendor.o(($event) => $options.switchMode("photo")),
- c: $data.mode === "video" ? 1 : "",
- d: common_vendor.o(($event) => $options.switchMode("video")),
- e: !$data.mediaSource
- }, !$data.mediaSource ? {
- f: $data.mode,
- g: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
- } : $data.mode === "photo" ? {
- i: $data.mediaSource
- } : $data.mode === "video" ? {
- k: $data.mediaSource
- } : {}, {
- h: $data.mode === "photo",
- j: $data.mode === "video",
- l: $data.mode === "video" && $data.isRecording
- }, $data.mode === "video" && $data.isRecording ? {
- m: common_vendor.t($options.formatTime($data.recordingTime))
- } : {}, {
- n: !$data.mediaSource
- }, !$data.mediaSource ? common_vendor.e({
- o: $data.mode === "photo"
- }, $data.mode === "photo" ? {
- p: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
- } : $data.mode === "video" && !$data.isRecording ? {
- r: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args))
- } : $data.mode === "video" && $data.isRecording ? {
- t: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
- } : {}, {
- q: $data.mode === "video" && !$data.isRecording,
- s: $data.mode === "video" && $data.isRecording
- }) : {
- v: common_vendor.t($data.mode === "photo" ? "拍照" : "录制"),
- w: common_vendor.o((...args) => $options.retakeMedia && $options.retakeMedia(...args)),
- x: common_vendor.o((...args) => $options.startInterview && $options.startInterview(...args))
- }, {
- y: $data.isPageLoaded ? 1 : ""
- });
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
- wx.createPage(MiniProgramPage);
|