123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- "use strict";
- const common_vendor = require("../../common/vendor.js");
- const api_user = require("../../api/user.js");
- const common_config = require("../../common/config.js");
- const _sfc_main = {
- data() {
- return {
- mediaSource: "",
- // 拍摄的照片或视频路径
- isCameraReady: false,
- cameraContext: null,
- isPageLoaded: false,
- // 添加页面加载状态标记
- mode: "photo",
- // 默认为拍照模式,可选值:photo, video
- isRecording: false,
- // 是否正在录制视频
- recordingTime: 0,
- // 录制时间(秒)
- recordingTimer: null,
- // 录制计时器
- photoUrl: ""
- // 存储上传后返回的图片URL
- };
- },
- 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;
- console.log(res);
- 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;
- },
- // 继续流程
- continueProcess() {
- if (!this.mediaSource) {
- common_vendor.index.showToast({
- title: "请先完成拍照",
- icon: "none"
- });
- return;
- }
- common_vendor.index.showLoading({
- title: "上传中..."
- });
- this.submitPhotoUrl();
- },
- // 上传媒体文件方法 - 不再使用
- uploadMedia(callback) {
- const openid = JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "";
- const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || 1;
- if (!openid || !tenant_id) {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "用户信息不完整,请重新登录",
- icon: "none"
- });
- return;
- }
- common_vendor.index.uploadFile({
- url: `${common_config.apiBaseUrl}/api/upload/`,
- filePath: this.mediaSource,
- name: "file",
- formData: {
- openid,
- tenant_id
- },
- success: (uploadRes) => {
- const res = JSON.parse(uploadRes.data);
- if (res.code === 2e3) {
- const photoUrl = res.data.url || res.data.photoUrl || "";
- if (callback && typeof callback === "function") {
- callback(photoUrl);
- }
- } else {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: res.msg || "照片上传失败",
- icon: "none"
- });
- }
- },
- fail: (err) => {
- common_vendor.index.hideLoading();
- console.error("上传失败:", err);
- common_vendor.index.showToast({
- title: "网络错误,请重试",
- icon: "none"
- });
- }
- });
- },
- // 提交照片URL到指定接口 - 修改为直接上传文件
- submitPhotoUrl() {
- const openid = JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "";
- const tenant_id = JSON.parse(common_vendor.index.getStorageSync("userInfo")).tenant_id || 1;
- if (!this.mediaSource) {
- common_vendor.index.hideLoading();
- common_vendor.index.showToast({
- title: "照片信息不完整,请重试",
- icon: "none"
- });
- return;
- }
- console.log(this.mediaSource);
- common_vendor.index.uploadFile({
- url: `${common_config.apiBaseUrl}/job/upload_posture_photo`,
- filePath: this.mediaSource,
- name: "photo",
- // 确保文件参数名称正确
- formData: {
- // 使用formData传递参数
- "application_id": common_vendor.index.getStorageSync("appId"),
- "tenant_id": tenant_id,
- "openid": openid,
- "description": "面部照片"
- },
- success: (uploadRes) => {
- console.log("照片上传成功:", uploadRes);
- let result;
- try {
- if (typeof uploadRes.data === "string") {
- result = JSON.parse(uploadRes.data);
- } else {
- result = uploadRes.data;
- }
- console.log("解析后的上传结果:", result);
- if (result.data && result.data.url) {
- this.photoUrl = result.data.url;
- }
- common_vendor.index.showToast({
- title: "照片上传成功",
- icon: "success"
- });
- setTimeout(() => {
- common_vendor.index.navigateTo({
- url: "/pages/identity-verify/identity-verify",
- fail: (err) => {
- console.error("页面跳转失败:", err);
- common_vendor.index.showToast({
- title: "页面跳转失败",
- icon: "none"
- });
- }
- });
- }, 1500);
- } catch (e) {
- console.error("解析上传结果失败:", e);
- common_vendor.index.showToast({
- title: "处理响应失败",
- icon: "none"
- });
- }
- },
- fail: (err) => {
- console.error("照片上传失败:", err);
- common_vendor.index.showToast({
- title: "照片上传失败,请重试",
- icon: "none"
- });
- },
- complete: () => {
- common_vendor.index.hideLoading();
- }
- });
- },
- updateLocalUserInfo() {
- api_user.getUserInfo().then((res) => {
- if (res.code === 200 && res.data) {
- let userInfo = {};
- try {
- userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
- } catch (e) {
- console.error("解析本地存储用户信息失败:", e);
- userInfo = {};
- }
- const updatedUserInfo = {
- ...userInfo,
- ...res.data
- };
- common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
- }
- }).catch((err) => {
- console.error("更新本地用户信息失败:", err);
- });
- }
- }
- };
- function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
- return common_vendor.e({
- a: !$data.mediaSource
- }, !$data.mediaSource ? {
- b: $data.mode,
- c: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
- } : $data.mode === "photo" ? {
- e: $data.mediaSource
- } : $data.mode === "video" ? {
- g: $data.mediaSource
- } : {}, {
- d: $data.mode === "photo",
- f: $data.mode === "video",
- h: $data.mode === "video" && $data.isRecording
- }, $data.mode === "video" && $data.isRecording ? {
- i: common_vendor.t($options.formatTime($data.recordingTime))
- } : {}, {
- j: !$data.mediaSource
- }, !$data.mediaSource ? common_vendor.e({
- k: $data.mode === "photo"
- }, $data.mode === "photo" ? {
- l: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
- } : $data.mode === "video" && !$data.isRecording ? {
- n: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args))
- } : $data.mode === "video" && $data.isRecording ? {
- p: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
- } : {}, {
- m: $data.mode === "video" && !$data.isRecording,
- o: $data.mode === "video" && $data.isRecording
- }) : {
- q: common_vendor.t($data.mode === "photo" ? "拍照" : "录制"),
- r: common_vendor.o((...args) => $options.retakeMedia && $options.retakeMedia(...args)),
- s: common_vendor.o((...args) => $options.continueProcess && $options.continueProcess(...args))
- }, {
- t: $data.isPageLoaded ? 1 : ""
- });
- }
- const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
- wx.createPage(MiniProgramPage);
|