click.js 3.2 KB
/*
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2024-04-08 11:28:48
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2024-04-09 13:36:48
 * @FilePath: \gl_tape\mixin\click.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const recorderManager = uni.getRecorderManager();
import upload from "@/utils/ossUploadFile/uploadFile.js";
import { mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
      voicePath: "",
      timer: null,
      times: 0,
      timeout: 10 * 60,
      reaction: 10,
      message: "3",
    };
  },
  mounted() {
    this.initTime();
    this.checkVoice();
  },
  computed: {
    ...mapState("audio", [
      "isEnd",
      "audioList",
      "isStart",
      "receptionEndTime",
      "receptionStartTime",
    ]),
  },
  methods: {
    ...mapMutations("audio", ["setAudioStart", "setAudioList", "setAudioEnd"]),
    //检查缓存录音
    checkVoice() {
      const file = uni.$storage.get("storageFile");
      if (file) this.uploadVoice(file);
    },
    //保存录音记录及其他信息
    async uploadVoice(tempFilePath) {
      const { phone, merchantId, uuid, name } = uni.$storage.get("userInfo"),
        params = {
          carModel: "问界",
          customerName: name,
          phoneNumber: phone,
          loginPhone: phone,
          salesUuid: uuid,
          recordUrlList: tempFilePath,
          receptionStartTime: this.receptionEndTime,
          receptionEndTime: this.receptionEndTime,
          merchantId,
        };
      console.log(params, "params");
      uni.$api.commonApi.saveRecCustomer(params).then((res) => {
        console.log(res);
      });
      uni.$storage.set("storageFile", "");
    },
    //开始录音
    startAudio() {
      if (this.isStart) return;
      this.setAudioStart(true);
      this.setAudioList([]);
      this.setAudioEnd(false);
      recorderManager.start({
        duration: 600000,
      });
      recorderManager.onStop(async (res) => {
        if (!this.isEnd) {
          recorderManager.start({
            duration: 600000,
          });
        }
        const result = await upload(res.tempFilePath);
        const storageFile = [...this.audioList, result];
        uni.$storage.set("storageFile", storageFile);
        this.setAudioList(storageFile);
        if (this.isEnd) this.uploadVoice(storageFile);
      });
    },
    //结束录音
    stopAudio() {
      this.setAudioStart(false);
      this.setAudioEnd(true);
      recorderManager.stop();
    },
    //监听点击事件
    initTime() {
      this.timer = setInterval(() => {
        this.times++;
        if (this.times >= this.timeout) {
          //   this.showToast();
        }
        if (this.times >= this.reaction + this.timeout) {
          this.handleEnd();
        }
      }, 1000);
    },
    handleTouchStart() {
      this.times = 0;
    },
    handleVisibilityChange() {
      this.message = 999;
      if (document.visibilityState === "hidden") {
        // 屏幕被关闭
        this.message = 999;
      }
    },
  },
  beforeCreate() {
    clearInterval(this.timer);
    this.timer = null;
  },
};