click.js 6.04 KB
/*
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2024-04-08 11:28:48
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2024-04-26 10:04:31
 * @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();
const plug = uni.requireNativePlugin("html5app-recordaudio");
import upload from "@/utils/ossUploadFile/uploadFile.js";
import { mapMutations, mapState } from "vuex";
import utils from "@/utils/utils.js";
export default {
  data () {
    return {
      voicePath: "",
      timer: null,
      times: 0,
      timeout: 240,
      reaction: 1,
    };
  },
  mounted () {
    this.initTime();
    // this.checkVoice();
  },
  computed: {
    ...mapState("audio", [
      "isEnd",
      "receptionStatus",
      "endReasonType",
      "booksList",
      "audioList",
      "isStart",
      "receptionEndTime",
      "receptionStartTime",
    ]),
    ...mapState(["user.customerInfo"]),
  },
  created () {
    console.log("created");
  },
  onHide () {
    console.log("隐藏");
    // recorderManager.stop();
    // recorderManager.start({
    //   duration: 10000,
    // });
  },
  onShow () {
    // console.log(utils.requestAndroidPermission());
  },
  methods: {
    ...mapMutations("audio", [
      "setReceptionStatus",
      "setReasonType",
      "setAudioStart",
      "setAudioList",
      "setAudioEnd",
      "setBooks",
    ]),
    //关闭提示
    tipCancel () {
      this.stopAudio();
      this.setReceptionStatus(0);
      this.setReasonType(1);
      this.$refs.tipBox.close();
      let { sceneType } = uni.$storage.get("userInfo");
      uni.reLaunch({
        url: sceneType == 0 ? "/pages/home/home" : "/pages/boutique/index" ,
      });
    },
    //结束提示
    tipConfirm () {
      this.times = 0;
      this.$refs.tipBox.close();
    },
    //检查缓存录音
    checkVoice () {
      const file = uni.$storage.get("storageFile");
      if (file) this.uploadVoice(file);
    },
    //保存录音记录及其他信息
    async uploadVoice (tempFilePath) {
      try {
        const { shopMerchantId, phone,sceneType } = uni.$storage.get("userInfo"),
          { customerName, phoneNumber, salesUuid, carModel, staffName } =
            this.$store.state.user.customerInfo,
          receptionCustomerPaySourceList = this.booksList.map((item) => {
            return {
              id: item.id,
              sourceType: item.id,
              sourceUrl: item.imgUrl,
            };
          }),
          params = {
            carModel,
            endReasonType: this.endReasonType, //原因 1.异常 0.正常
            customerName,
            phoneNumber,
            loginPhone: phone,
            salesName: staffName,
            salesUuid,
            receptionStatus: this.receptionStatus, //接待结果 1.成交 0.战败
            recordUrlList: tempFilePath,
            receptionStartTime: this.receptionStartTime,
            receptionEndTime: this.receptionEndTime,
            receptionCustomerPaySourceList, //1.付款凭证文件路径 2.客户购车发票文件路径 3.客户延保合同文件路径
            merchantId: shopMerchantId,
            bussinessType: sceneType == 0 ? 1 : 2 // sceneType 0延保 1精品。 bussinessType 1.延保 2.轻改
          };
        console.log(
          customerName,
          phoneNumber,
          salesUuid,
          carModel,
          tempFilePath
        );
        if (customerName && phoneNumber && salesUuid && carModel)
          uni.$api.commonApi.saveRecCustomer(params).then((res) => {
            console.log(res, params);
          });
        this.setBooks([]);
        this.$store.dispatch("clearCustomerInfo");
        this.setReceptionStatus(1);
        this.setReasonType(0);
        uni.$storage.set("storageFile", "");
      } catch (error) {
        this.$store.dispatch("clearCustomerInfo");
        console.log(error);
      }
    },
    //开始录音
    async startAudio () {
      if (this.isStart) return;
      this.setAudioStart(true);
      this.setAudioList([]);
      this.setAudioEnd(false);
      this.initTime();
      recorderManager.start({
        duration: 600000,
        sampleRate: 16000,
        numberOfChannels: 1,
      });
      recorderManager.onStop(async (res) => {
        console.log("结束录音");
        if (!this.isEnd) {
          uni.getRecorderManager().start({
            duration: 600000,
            sampleRate: 16000,
            numberOfChannels: 1,
          });
        }
        const result = await upload(res.tempFilePath);
        const storageFile = [...this.audioList, result];
        uni.$storage.set("storageFile", storageFile);
        this.setAudioList(storageFile);
        console.log(storageFile, this.isEnd, "this.isEnd");
        if (!this.isEnd) return true;
        if (this.isEnd) this.uploadVoice(storageFile);
        return true;
      });
    },
    //结束录音
    stopAudio: utils.throttle(async function () {
      const result = await this.$permision.requestAndroidPermission(
        "android.permission.RECORD_AUDIO"
      );
      if (result !== 1) {
        uni.showToast({
          title: "为了记录语音通话,请打开录音权限",
          icon: "none",
        });
        return;
      }
      this.setAudioStart(false);
      this.setAudioEnd(true);
      this.times = 0;
      clearInterval(this.timer);
      this.timer = null;
      recorderManager.stop();
    }, 1500),
    //监听点击事件
    initTime () {
      if (!this.isStart) return;
      this.timer = setInterval(() => {
        this.times++;
        if (this.times >= this.timeout) {
          this.$refs.tipBox.open();
        }
        if (this.times >= this.reaction + this.timeout) {
          //结束接待
          this.tipCancel();
          clearInterval(this.timer);
        }
      }, 1000 * 10);
    },
    handleTouchStart () {
      this.times = 0;
    },
  },
  beforeCreate () {
    clearInterval(this.timer);
    this.timer = null;
  },
};