addClustering.vue 13.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
<template>
  <el-dialog :close-on-click-modal="false" :title="isAdd ? '新建人群' : '修改人群'" width="80%" :visible.sync="visible">
    <div class="pending">
      <div class="addClustering pending">
        <div class="addCrowd">
          <el-form class="demo-form-inline" label-width="100px">
            <el-form-item class="inputWide" label="人群名称">
              <el-input v-model.trim="crowdData.crowdName" placeholder="请输入2-10个中文字符" maxlength="10" onblur="value=value.replace(/(^\s*)|(\s*$)/g, '')" show-word-limit />
            </el-form-item>
          </el-form>
          <div class="crowdOptions">
            <div v-for="(item, index) of crowdData.conditions" :key="item.type" class="condition">
              <div class="flexBox">
                <div class="leftTit" v-text="index === 0 ? '特征筛选' : ''" />
                <div class="rightInfo">
                  <el-select v-model="item.type" placeholder="请选择" @change="changeType(item, index)">
                    <el-option
                      v-for="typeItem in options"
                      v-show="!types.includes(typeItem.type)"
                      :key="typeItem.type"
                      :label="typeItem.name"
                      :value="typeItem.type"
                    />
                  </el-select>
                </div>
                <div v-if="item.type === 9 || item.type === 10" class="purchase">
                  <el-select v-model="item.calculation" placeholder="请选择">
                    <el-option
                      v-for="calculationItem in calculationList"
                      :key="calculationItem.value"
                      :label="calculationItem.label"
                      :value="calculationItem.value"
                    />
                  </el-select>
                </div>
                <div v-if="item.type === 11" class="tagList">
                  <el-select
                    v-model="item.labelIds"
                    multiple
                    collapse-tags
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="tagItem in tagList"
                      :key="tagItem.labelId"
                      :label="tagItem.labelName"
                      :value="tagItem.labelId"
                    />
                  </el-select>
                </div>
                <div v-if="item.type !== 11" class="valueBox">
                  <span v-if="item.type !== 9 && item.type !== 10">最近</span>
                  <el-input v-model="item.number" oninput="value=value.replace(/[^\d]/g,'')" type="number" :placeholder="item.type !== 9 && item.type !== 10 ? '请输入1-365' : ''" />
                  <span v-if="item.type !== 9 && item.type !== 10">天</span>
                  <span v-if="item.type === 9">次</span>
                  <span v-if="item.type === 10">元</span>
                </div>
                <div class="deleteIcon" @click="deleteOptions(item, index)"><i class="el-icon-remove" /></div>
              </div>
              <p v-if="item.type === 1">选定时间内,购买过本店商品的客户(以支付成功为准,不剔除退款)</p>
              <p v-if="item.type === 2">选定时间内,没有购买过本店商品的客户(以支付成功为准,不剔除退款)</p>
              <p v-if="item.type === 3">选定时间内,在店铺有下单行为的客户(包含未付款客户)</p>
              <p v-if="item.type === 4">选定时间内,在店铺没有下单行为的客户</p>
              <p v-if="item.type === 5">选定时间内,加购过本店商品的客户</p>
              <p v-if="item.type === 6">选定时间内,没有加购过本店商品的客户</p>
              <p v-if="item.type === 7">选定时间内,访问过本店的客户</p>
              <p v-if="item.type === 8">选定时间内,没有访问过本店的客户</p>
              <p v-if="item.type === 9">客户累计在店铺交易成功的订单数量(剔除退款的订单)</p>
              <p v-if="item.type === 10">客户累计在店铺交易成功的金额数量(剔除退款金额)</p>
              <p v-if="item.type === 11">当前拥有以上任意标签的客户</p>
            </div>
            <div v-if="crowdData.conditions.length !== 11" class="addCondition" @click="addCondition">
              <i class="el-icon-circle-plus" />
              <span>添加筛选条件</span>
            </div>
          </div>
          <div class="saveBox">
            <el-button type="primary" @click="saveClustering">保 存</el-button>
            <el-button @click="closeClustering">取 消</el-button>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getLabelData, addCrowd, getCrowdDetail, crowdUpdate } from '@/api/customer'
export default {
  name: 'AddClustering',
  data() {
    return {
      visible: false,
      isAdd: false,
      crowdName: '',
      shopCrowdId: '',
      crowdData: {
        conditions: [
          {
            calculation: null,
            ids: [],
            labelIds: [],
            number: null,
            type: 1,
            userIds: []
          }
        ],
        crowdName: '',
        shopCrowdId: null
      },
      tagList: [], // 标签列表
      types: [1],
      options: [
        {
          type: 1,
          name: '店铺有购买'
        },
        {
          type: 2,
          name: '店铺无购买'
        },
        {
          type: 3,
          name: '店铺有下单'
        },
        {
          type: 4,
          name: '店铺无下单'
        },
        {
          type: 5,
          name: '店铺有加购'
        },
        {
          type: 6,
          name: '店铺无加购'
        },
        {
          type: 7,
          name: '店铺有访问'
        },
        {
          type: 8,
          name: '店铺无访问'
        },
        {
          type: 9,
          name: '有效购买次数'
        },
        {
          type: 10,
          name: '有效购买金额'
        },
        {
          type: 11,
          name: '任意满足一个标签'
        }
      ],
      calculationList: [
        {
          label: '大于',
          value: 1
        },
        {
          label: '等于',
          value: 2
        },
        {
          label: '小于',
          value: 3
        }
      ]
    }
  },
  mounted() {
    this.getTagList()
  },
  methods: {
    show(id) {
      this.visible = true
      if (id) {
        this.isAdd = false
        this.shopCrowdId = id
        this.getDetail()
      } else {
        this.shopCrowdId = null
        this.isAdd = true
        this.crowdData = {
          conditions: [
            {
              calculation: null,
              ids: [],
              labelIds: [],
              number: null,
              type: 1,
              userIds: []
            }
          ],
          crowdName: '',
          shopCrowdId: null
        }
      }
    },
    // 获取编辑详情
    getDetail() {
      getCrowdDetail({ shopCrowdId: this.shopCrowdId }).then(res => {
        if (res.code === '') {
          console.log(res.data)
          this.crowdData = res.data
          this.crowdData.conditions.forEach(item => {
            if (item.labelIds.length > 0) {
              let ids = []
              ids = item.labelIds.map(Number)
              item.labelIds = ids
              console.log(item.labelIds, 'fsdfsd')
            }
          })
          this.types = []
          this.crowdData.conditions.forEach(i => {
            this.types.push(i.type)
          })
        } else {
          self.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    // 添加筛选
    addCondition() {
      for (let i = 1; i <= this.options.length; i++) {
        if (!this.types.includes(i)) {
          console.log(this.crowdData.conditions, 'conditions')
          this.crowdData.conditions.push(
            {
              calculation: i === 9 || i === 10 ? 1 : null,
              ids: [],
              labelIds: [],
              number: null,
              type: i,
              userIds: []
            }
          )
          this.types.push(i)
          break
        }
      }
    },
    // 保存提交新客户
    saveClustering() {
      var self = this
      var crowds = self.crowdData.conditions
      var options = self.options
      if (this.crowdData.crowdName === '') {
        self.$message({
          message: '人群名称不能为空',
          type: 'warning'
        })
        return false
      } else if (this.crowdData.crowdName.length < 2) {
        self.$message({
          message: '人群名称长度在2-10个字符之间',
          type: 'warning'
        })
        return false
      }
      // eslint-disable-next-line no-labels
      for (let i = 0; i < crowds.length; i++) {
        if (crowds[i].type === 11) {
          if (crowds[i].labelIds.length === 0) {
            self.$message({
              message: '请选择商家标签',
              type: 'warning'
            })
            return false
          }
        } else if (crowds[i].number === null || crowds[i].number === '') {
          for (let j = 0; j < options.length; j++) {
            if (crowds[i].type === options[j].type) {
              self.$message({
                message: `选项为${options[j].name}的内容不能为空!`,
                type: 'warning'
              })
              // eslint-disable-next-line no-labels
              return false
            }
          }
        }
      }
      if (this.shopCrowdId !== '' && this.shopCrowdId !== null) {
        // 修改人群
        crowdUpdate(self.crowdData).then(res => {
          if (res.code === '') {
            self.$message({
              message: '修改成功',
              type: 'success'
            })
            this.visible = false
            this.$emit('reset')
          } else {
            self.$message({
              message: res.message,
              type: 'error'
            })
          }
        })
      } else {
        // 新增人群
        addCrowd(self.crowdData).then(res => {
          if (res.code === '') {
            self.$message({
              message: '添加成功',
              type: 'success'
            })
            this.visible = false
            this.$emit('reset')
          } else {
            self.$message({
              message: res.message,
              type: 'error'
            })
          }
        })
      }
    },
    // 取消新增
    closeClustering() {
      this.visible = false
    },
    // 初始化标签数据
    async getTagList() {
      const res = await getLabelData()
      if (res.code === '') {
        this.tagList = res.data
      } else {
        this.$message({
          message: res.message,
          type: 'error'
        })
      }
    },
    // 更改当前选项
    changeType(item, index) {
      var crowdData = this.crowdData.conditions
      this.types = []
      crowdData.forEach(i => {
        this.types.push(i.type)
      })
      if (item.type === 9 || item.type === 10) {
        this.crowdData.conditions[index].calculation = 1
      }
    },
    // 删除当前筛选
    deleteOptions(item, index) {
      this.types.splice(index, 1)
      this.crowdData.conditions.splice(index, 1)
    }
  }
}
</script>

<style lang="scss" scoped>
//@import url(); 引入公共css类
@import url("../../styles/elDialog.scss");
.addClustering {
  background: #FFFFFF;
  h3 {
    font-size: 20px;
    color: #333333;
    font-weight: 500;
  }
  .addCrowd {
    margin-left: 200px;
    .crowdOptions {
      .condition {
        margin-top: 30px;
        p {
          font-size: 14px;
          color: #666666;
          margin-left: 100px;
          margin-top: 10px;
        }
        .flexBox {
          display: flex;
          align-items: center;
          .leftTit {
            width: 100px;
            text-align: right;
            font-size: 14px;
            color: #606260;
            font-weight: 700;
            padding-right: 12px;
          }
          .purchase {
            margin-left: 50px;
          }
          .tagList {
            margin-left: 50px;
          }
          .valueBox {
            display: flex;
            align-items: center;
            margin-left: 50px;
            span {
              display: block;
              width: 60px;
              text-align: center;
              color: #666666;
            }
          }
          .deleteIcon {
            margin-left: 100px;
            .el-icon-remove:before {
              color: #ff5c5c;
              font-size: 22px;
              cursor: pointer;
            }
          }
        }
      }
    }
    .saveBox {
      display: flex;
      margin: 120px 0 20px 200px;
    }
    .addCondition {
      margin: 60px 0 0 80px;
      display: flex;
      align-items: center;
      cursor: pointer;
      width: 160px;
      .el-icon-circle-plus:before {
        font-size: 24px;
        color: #999999;
      }
      span {
        margin-left: 10px;
      }
    }
  }
}
.pending {
  padding: 30px;
}
</style>
<style scoped>
.inputWide /deep/ .el-form-item__content {
  width: 300px;
}
.inputWide /deep/ .el-select {
  width: 100%;
}
.addCrowd /deep/ .demo-form-inline {
  margin-bottom: 20px;
}
</style>