index.vue 10.7 KB
<template>
  <el-drawer
    :title="form.id ? '编辑产品词条' : '新增产品词条'"
    size="50%"
    :before-close="cancelForm"
    :visible.sync="dialogVisible"
    direction="rtl"
    custom-class="drawer-panel"
    ref="drawer"
  >
    <div class="drawer__content">
      <div class="tips-box">
        <div class="tips-title">
          <i class="el-icon-warning-outline"></i>功能提示
        </div>
        <ul>
          <li>每个轻改、权益产品仅可配置单一词条。</li>
          <li>该词条将用于展业助手APP上的营销话术方案的展示。</li>
          <li>驻点人员可提前了解该产品在实际营销中的常见问题及合理答复。</li>
        </ul>
      </div>
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          label="产品名称"
          :label-width="formLabelWidth"
          prop="headings"
        >
          <el-input
            maxlength="20"
            v-model="form.headings"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="产品配套话术"
          :label-width="formLabelWidth"
          prop="content"
        >
          <el-input
            type="textarea"
            maxlength="5000"
            v-model="form.content"
            class="input-box"
            autocomplete="off"
          ></el-input>
          <div class="insert-btn" @click="insertTxtAndSetcursor('##')">
            #高亮文本#
          </div>
        </el-form-item>
        <el-form-item label-width="120px" label="录音附件:" prop="imageList">
          <div v-if="form.audio">
            <div class="audio-name">
              <audio :key="audioKey" controls>
                <source :src="form.audio" type="audio/mpeg" />
              </audio>
              <el-button size="small" type="text" class="import-button">
                重新上传
                <input
                  type="file"
                  ref="fileUpload"
                  class="import-input"
                  accept=".mp3,.aac,.m4a"
                  @change="importFile($event)"
                />
              </el-button>
            </div>
          </div>
          <el-button size="small" class="import-button" v-else>
            <i class="el-icon-plus"></i>
            上传文件
            <input
              type="file"
              ref="fileUpload"
              class="import-input"
              accept=".mp3,.aac,.m4a"
              @change="importFile($event)"
            />
          </el-button>
          <p class="photo-top-tips">支持mp3,大小不超过200M</p>
        </el-form-item>
        <!-- <el-form-item label-width="120px" label="录音附件:" prop="imageList">
          <div v-if="form.audio">
            <div class="audio-name">{{ form.audio | format }}</div>
            <div class="audio-btn">重新上传</div>
          </div>
          <el-button size="small" class="import-button" v-else>
            <i class="el-icon-plus"></i>
            上传文件
            <input
              type="file"
              ref="fileUpload"
              multiple
              class="import-input"
              accept=".mp3,.aac,.m4a"
              @change="importFile($event)"
            />
          </el-button>
          <p class="photo-top-tips">支持mp3,大小不超过200M</p>
        </el-form-item> -->
        <el-form-item label-width="120px" label="内容:">
          <glsx-preview
            :pic-options="picOptions"
            @beforeUpload="beforeUpload"
            @afterUpload="afterUpload"
            :image-url-arr="form.pictureList"
            limit="9"
          ></glsx-preview>
          <p
            class="photo-top-tips"
            v-if="form.pictureList && form.pictureList.length"
          >
            图片按照以上排序进行展示,可拖动调整排序
          </p>
          <p class="photo-top-tips" v-else>
            推荐尺寸:1173*768,单个文件不超过10M,最多上传9张
          </p>
        </el-form-item>
      </el-form>
      <div class="drawer__footer">
        <el-button @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import preview from "../preview"
import uploadPdf from "../uploadPdf"
import * as productApi from "@api/contentManager"
import { getAliOSSToken } from "@api/serviceManage"
import OSS from "ali-oss"
export default {
  name: "AddDrawer",
  components: {
    glsxPreview: preview,
    uploadPdf
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    form: {
      type: Object,
      default: () => {}
    }
  },
  mounted() {
    this.init()
    setTimeout(() => {
      console.log(this.form)
      if (!this.form.pictureList) {
        this.form.pictureList = []
      }
    }, 1000)
  },
  data() {
    return {
      audioKey: new Date().getTime(),
      title: "",
      isFocs: false,
      picOptions: {
        width: "88px",
        height: "88px",
        isClose: true,
        limitSize: 10,
        type: "img",
        isDrag: true
      },
      formLabelWidth: "120px",
      rules: {
        headings: [{ required: true, message: "请产品名称", trigger: "blur" }],
        content: [
          { required: true, message: "产品配套话术", trigger: "change" }
        ]
      }
    }
  },
  filters: {
    format(val) {
      return val.split("/").pop()
    }
  },
  methods: {
    async init() {
      try {
        const res = await getAliOSSToken()
        if (res)
          this.client = new OSS({
            // yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
            region: "oss-cn-shenzhen",
            // 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
            accessKeyId: res.lsaccessKeyId,
            accessKeySecret: res.lsaccessKeySecret,
            stsToken: res.securityToken, // 从STS服务获取的安全令牌(SecurityToken)。
            // 填写Bucket名称。
            bucket: "glsk-oss",
            secure: true
          })
        else this.$message.error("获取阿里云配置失败!")
      } catch (error) {}
    },
    // 输入文本
    insertTxtAndSetcursor(text) {
      let element = document.querySelector(".input-box textarea") // 获取到指定标签
      let startPos = element.selectionStart // 获取光标开始的位置
      let endPos = element.selectionEnd // 获取光标结束的位置
      let oldTxt = element.value // 获取输入框的文本内容
      let result =
        oldTxt.substring(0, startPos) + text + oldTxt.substring(endPos) // 将文本插入
      console.log(result)
      this.form.content = result // 将拼接好的文本设置为输入框的值
      element.focus() // 重新聚焦输入框
      setTimeout(() => {
        const range = startPos + 1
        element.selectionStart = range // 设置光标开始的位置
        element.selectionEnd = range // 设置光标结束的位置
      })
    },
    async importFile(event) {
      try {
        if (!this.client) {
          this.$refs.fileUpload.value = ""
          this.init()
          return this.$message.error("请重试!")
        }

        const data = event.srcElement.files
        console.log(data[0], data[0].type)
        if (!data[0] || !data[0].type) {
          this.$message.error(`请上传mp3、aac、m4a的录音格式!`)
          this.$refs.fileUpload.value = ""
          return
        }

        const allowedTypes = /audio\/mpeg|aac|m4a/i
        if (!allowedTypes.test(data[0].type)) {
          this.$message.error(`请上传mp3、aac、m4a的录音格式!`)
          this.$refs.fileUpload.value = ""
          return
        }
        for (let i = 0; i < data.length; i++) {
          await this.putObject(data[i])
        }
        this.loading = true
        this.loading = false
      } catch (error) {
        this.loading = false
      }
    },
    async putObject(data) {
      let date = "",
        [originalString] = data.name.split("."),
        [_day, _time] = originalString.split("_")
      if (_day && _time) {
        const rex = /^\d+$/
        let year = originalString.slice(0, 4),
          month = originalString.slice(4, 6),
          day = originalString.slice(6, 8),
          time = originalString.slice(9, 13)
        if (
          rex.test(year) &&
          rex.test(month) &&
          rex.test(day) &&
          rex.test(time)
        )
          date = `${year}-${month}-${day} ${time.slice(0, 2)}:${time.slice(2)}`
      }
      try {
        const result = await this.client.put(`quality/${data.name}`, data)
        this.form.audio = result.url
        this.audioKey = new Date().getTime()
      } catch (error) {
        console.log(error)
        this.init()
        this.$message.error("上传失败")
      }
    },
    beforeUpload() {
      this.uploading = true
    },
    afterUpload() {
      this.uploading = false
    },
    handleSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.confirmAdd()
        }
      })
    },
    confirmAdd() {
      console.log(this.form)
      let api = "productSave"

      if (this.form.id) {
        api = "productUpdate"
      }
      productApi[api](this.form).then((res) => {
        if (res) {
          this.cancelForm()
        }
      })
    },
    cancelForm() {
      this.$emit("close")
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-drawer__header {
  border-bottom: 1px solid #e5e5e5;
  margin-bottom: 0px;
  padding-bottom: 20px;
  color: #333333;
}
.drawer__content {
  padding: 20px;
  .tips-box {
    height: 124px;
    border-radius: 4px;
    opacity: 1;
    background: #fafafa;
    padding: 14px 24px;
    box-sizing: border-box;
    margin-bottom: 20px;
    .tips-title {
      color: #333333;
      font-size: 14px;
      i {
        margin-right: 6px;
      }
    }
    ul {
      padding-left: 20px;
      li {
        color: #999999;
        line-height: 22px;
        font-size: 12px;
      }
    }
  }
  .el-form {
    height: calc(100vh - 300px);
    overflow: auto;
  }
}
.drawer__footer {
  text-align: right;
}
.insert-btn {
  width: 79px;
  height: 21px;
  border-radius: 4px;
  opacity: 1;
  background: #185efe1a;
  color: #185efe;
  font-size: 12px;
  line-height: 21px;
  text-align: center;
  margin-top: 10px;
}
.import-button {
  position: relative;
  .import-input {
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
}
.audio-name {
  display: flex;
  audio {
    height: 36px;
  }
  .import-button {
    margin-left: 20px;
  }
}
.photo-top-tips {
  color: #999999;
}
</style>