library.vue 6.7 KB
<template>
  <div class="layout">
    <div class="sticky">
      <template v-if="!guestMode">
        <tab
          :list="tabList"
          @change="change"
        >
        </tab>
      </template>
      <search-box
        v-show="tabIndex == 1"
        @search="search"
        placeholder="输入关键词搜索"
        :showDropdown="false"
      ></search-box>
    </div>
    <chat-gpt :show="tabIndex == 0"></chat-gpt>
    <template v-if="tabIndex == 1">
      <template v-if="flag">
        <template v-if="list.length">
          <scroll-view
            class="list-box"
            @ReachBottom="onReachBottom"
            ref="scrollView"
          >
            <div class="box">
              <template v-for="(i,index) in list">
                <div
                  class="item"
                  :key="index"
                >
                  <div
                    v-html="decodeURIComponent(i.text)"
                    :class="['content',{'line-clamp':i.moreState}]"
                  ></div>
                  <div class="chat-btns flex">
                    <template v-if="i.moreBtn">
                      <div
                        class="more"
                        @click="i.moreState = !i.moreState"
                      >{{ i.moreState ? '展开' :'收起' }}</div>
                    </template>
                    <van-button
                      class="send"
                      block
                      round
                      color="#3975C6"
                      :disabled="sendChatMessageDisabled"
                      @click="sendChatMessage(i.scriptContent)"
                    >发送</van-button>
                  </div>
                </div>
              </template>
            </div>
          </scroll-view>
        </template>
        <template v-else>
          <nothing
            class="nothing-box"
            image="https://img01.glsx.com.cn/weapp/resource/scrm-mobile/psd/list-nothing.png"
          >暂无可用话术</nothing>
        </template>
      </template>
    </template>
  </div>
</template>

<script type='text/ecmascript-6'>
import { Notify } from 'vant'
import { wxConfig, marketingApi } from '@/api'
export default {
  name: 'library',
  data () {
    return {
      tabList: ['智能AI话术', '企业话术'],
      flag: false,
      list: [],
      pageNum: 1,
      sendChatMessageDisabled: false,//发送按钮权限
      params: {
        scriptGroup: '',
        scriptContent: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      tabIndex: 0
    }
  },
  computed: {
    guestMode () {
      return this.$store.state.app.guestMode
    }
  },
  async mounted () {
    if (!this.guestMode) try {
      await wxConfig.init(['sendChatMessage'])
    } catch (error) {
      this.sendChatMessageDisabled = true
    }
  },
  methods: {
    /**发送 */
    sendChatMessage (content) {
      this.showLoading()
      wxConfig
        .sendChatMessage({
          msgtype: 'text', //消息类型,必填
          enterChat: true, //为true时表示发送完成之后顺便进入会话,仅移动端3.1.10及以上版本支持该字段
          text: {
            content
          },
        })
        .then(res => {
          this.hideLoading()
        }).catch(err => {
          console.log()
          if (!err?.err_Info?.startsWith('cancel')) Notify(String(err))
          this.hideLoading()
        })
    },

    /**清空 */
    clearData () {
      this.list = []
      this.flag = false
      this.params.pageNum = 1
      this.getChatLibraryList()
    },
    /**搜索 */
    search (val) {
      this.params.scriptContent = val
      this.clearData()
    },
    /**切换tab */
    change (index) {
      this.tabIndex = index
      if (index == 1) this.clearData()
      // const { groupList, params: { scriptGroup } } = this
      // if (scriptGroup == groupList[index] || (index == 0 && scriptGroup == '')) return
      // this.params.scriptGroup = index == 0 ? '' : groupList[index]
      // this.clearData()
    },
    /**话术组 */
    // async getChatGroupList () {
    //   let res = await marketingApi.getChatGroupList() || [],
    //     maxLength = 10
    //   if (res.length > maxLength) res = res.splice(0, maxLength)
    //   this.groupList = ['全部', ...res]
    // },
    /**话术 */
    async getChatLibraryList () {
      const { params, list, $refs: {
        scrollView
      } } = this
      this.showLoading()
      try {
        const { data, count } = await marketingApi.getChatLibraryList(params),
          val = params.scriptContent || '',
          rex = new RegExp(val, 'g')
        data.length && data.map(e => {
          let txt = e.scriptContent
          if (e.scriptContent.length * 12 >= 1248) {
            e.moreBtn = true
            e.moreState = true
          }
          if (val) txt = txt.replace(rex, `<span>${val}</span>`)
          e.text = encodeURIComponent(txt)
        })
        this.list = list.concat(data)
        this.total = count
        this.hideLoading()
      } catch (error) {
        this.hideLoading()
      }
      if (scrollView) scrollView.loading = false
      this.flag = true
    },
    /**触底分页加载 */
    onReachBottom () {
      let { total, params: { pageNum }, list, $refs: {
        scrollView
      } } = this
      scrollView.loading = true
      if (list.length == total) {
        return scrollView.finished = true
      }
      if (Math.ceil(total / 10) > pageNum) {
        this.params.pageNum = pageNum + 1
        this.getChatLibraryList()
      } else {
        scrollView.finished = true
        scrollView.loading = false
      }
    },
  }
}
</script>

<style scoped lang='scss'>
.layout {
  .list-box {
    height: calc(100vh - 100px);
    box-sizing: border-box;
    .item {
      background: #ffffff;
      box-shadow: 0 2px 8px 0 #ced1de66, 0 2px 4px 0 #ced1de1f;
      border-radius: 4px;
      margin-bottom: 12px;
      font-size: 12px;
      color: #666666;
      word-break: break-all;
      position: relative;
      .content {
        padding: 14px 10px 0;
        line-height: 20px;
        /deep/span {
          color: #3582ee;
        }
      }
      .line-clamp {
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-line-clamp: 4;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .chat-btns {
        height: 36px;
        padding: 0 16px;
        border-top: 1px solid rgba(195, 195, 195, 0.3);
        margin-top: 13px;
        .more {
          color: #999999;
        }
        .van-button--block {
          width: 60px;
          height: 24px;
          font-weight: 500;
          font-size: 12px;
          color: #ffffff;
          margin-left: auto;
          box-shadow: 0 2px 6px 2px #3975c61c;
        }
      }
    }
  }
}
</style>