tag.js 6.99 KB
import { getCustomersList, exportLabelList } from '@/api/modules/tagsManage'
import { getStaffList } from '@/api/modules/clientManage'
export default {
  data() {
    return {
      tagHeight: 0,
      tagHeightStatus: false,
      tagCheckItem: [],
      allListParams: [],
      crowdForm: {
        crowdName: '',
        crowdDesc: ''
      }
    }
  },
  mounted() {
    this.getStaffList()
    this.getCustomersListData()
  },
  watch: {
    tagCheckItem: {
      handler() {
        if (this.tagCheckItem.length == 0) {
          this.tagHeight = 0
        }
        this.allListParams = []
        const tagNode = this.$common.utils.deepClone(this.tagCheckItem)
        tagNode.forEach(group_item => {
          group_item.children.forEach(label_item => {
            if (label_item) {
              this.allListParams.push({
                groupId: group_item.id,
                groupName: group_item.name,
                groupType: group_item.labelType,
                labelId: label_item.id,
                labelName: label_item.name
              })
            }
          })
        })
        this.getCustomersListData()
      },
      deep: true
    },
    tagHeight: {
      handler(val) {
        if (val == 0) {
          setTimeout(() => {
            this.tagHeightStatus = false
          }, 300)
        } else {
          this.tagHeightStatus = true
        }
      },
      deep: true
    }
  },
  methods: {
    //获取跟进员工
    getStaffList() {
      getStaffList({}).then(res => {
        if (res.length == 0) {
          return ''
        }
        const id = this.tagsPartFormList.findIndex(item => {
          return item.name == 'followStaff'
        })
        const staff = res.map(item => {
          return {
            label: item.staffName,
            value: item.staffName
          }
        })
        staff.unshift({ label: '全部', value: '' })

        this.$nextTick(() => {
          this.tagsPartFormList[id].optionList = staff
        })
        console.log(this.tagsPartFormList[id])
      })
    },
    tagLength(children) {
      return children.filter(item => item).length
    },
    goDetail(scope) {
      this.$router.push('/clientManage/clientDetail?id=' + scope.oneId)
    },
    jumpGroup() {
      this.$router.push({
        path: '/crowdManage/index',
        query: {
          selectTab: 1
        }
      })
    },
    //删除标签组
    removeItem(index) {
      this.tagCheckItem.splice(index, 1)
      this.$refs.tagTree.getTreeNode(this.tagCheckItem)
    },
    treeScroll(id) {
      const h = document.querySelector('.node_' + id).offsetTop
      this.$refs.tagTree.handleScroll(h)
    },
    //获取选中的标签并排序
    getCheckItem(item) {
      const tree_id = []
      const labelTree = this.$common.utils.deepClone(this.$refs.tagTree.labelTree)
      labelTree.forEach(tree_node => {
        tree_node.children.forEach(t_node => {
          tree_id.push(t_node.id)
        })
      })
      this.tagCheckItem = []
      let group_all = item.getCheckedNodes().filter(item => item.labelType)
      let group_Label = item.getCheckedNodes().filter(item => !item.children)
      let label_id = []
      group_Label.forEach(item => {
        label_id.push(item.id)
      })
      let group_half = item.getHalfCheckedNodes().filter(item => item.labelType)
      let group = [...group_half, ...group_all].sort((a, b) => {
        return tree_id.indexOf(a.id) - tree_id.indexOf(b.id)
      })
      this.tagCheckItem = this.$common.utils.deepClone(group)
      group.forEach((tag, idx) => {
        tag.children.forEach((item, index) => {
          if (!label_id.includes(item.id)) {
            this.tagCheckItem[idx].children[index] = null
          }
        })
      })
      this.tagCheckItem.forEach(tag => {
        tag.children = tag.children.filter(item => item)
      })
    },
    //导出
    handleExport() {
      const params = {
        ...JSON.parse(JSON.stringify({ tagLists: this.allListParams })),
        combineType: 2
      }
      exportLabelList(params).then(res => {
        this.$Utils.exportFn(res)
      })
    },
    //客户列表
    getCustomersListData() {
      if (this.searchForm.createTime) {
        this.searchForm.beginTime = new Date(this.searchForm.createTime[0]).toLocaleDateString().replace(/-/g, '/')
        this.searchForm.endTime = new Date(this.searchForm.createTime[1]).toLocaleDateString().replace(/-/g, '/')
      }
      delete this.searchForm.createTime
      const tagLists = this.$common.utils.deepClone(this.allListParams)
      this.userLoading = true
      const params = {
        ...JSON.parse(JSON.stringify({ tagLists })),
        ...JSON.parse(JSON.stringify(this.searchForm)),
        combineType: 2,
        pageSize: this.$refs.viewCustomers.page.pageSize,
        pageNum: this.$refs.viewCustomers.page.pageNum
      }
      getCustomersList(params)
        .then(res => {
          this.userLoading = false
          this.tableData = res.data
          this.tableTotal = res.count
          this.$nextTick(() => {
            if (tagLists.length == 0) {
              document.querySelector('.noDataText').innerText = '当前未选择标签,快去选择标签查看客户吧!'
            } else {
              document.querySelector('.noDataText') ? (document.querySelector('.noDataText').innerText = '暂无数据') : ''
            }
          })
          if (this.tableData == 0) {
            this.$refs.viewCustomers.setTableHeightFun()
          } else {
            this.$refs.viewCustomers.tableHeightValues = ''
          }
        })
        .catch(err => {
          this.userLoading = false
        })
    },
    //创建人群
    groupConfirmDialog() {
      console.log(this.allListParams)
      if (this.tagCheckItem.length == 0) {
        this.$message.error('必须选择一个标签')
        return
      }
      if (!this.crowdForm.crowdName) {
        this.$message.error('请输入人群名称')
        return
      }
      const tagCombine = this.allListParams.reduce((prev, cur) => {
        const index = prev.findIndex(group => cur.groupId == group.groupId)
        if (index == -1) {
          return prev.concat({ groupId: cur.groupId, labelIdList: [cur.labelId] })
        } else {
          prev[index].labelIdList.push(cur.labelId)
          return prev
        }
      }, [])
      const params = {
        'req.merchantId': this.merchantId,
        'req.crowdType': 2, //类型 1-推荐 2-自定义
        'req.crowdName': this.crowdForm.crowdName,
        'req.crowdDesc': this.crowdForm.crowdDesc,
        'req.tagCombine': JSON.stringify(tagCombine)
      }
      this.$apis.crowdManage
        .addCrowd(params)
        .then(res => {
          if (res.data.code == 0) {
            this.groupVisible = false
            this.successVisible = true
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            })
          }
        })
        .catch(err => {
          this.$message({
            type: 'error',
            message: err.message
          })
        })
    }
  }
}