add.vue 17.5 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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
<template>
  <div class="add-wrap" v-loading="loading">
    <div class="container">
      <div class="title">营销配置</div>
      <div style="display: flex">
        <div class="gl-form-page">
          <el-form ref="configForm" label-width="154px" :model="addForm" :rules="addRules">
            <el-form-item label="营销名称:" prop="imName">
              <el-input type="text" size="small" placeholder="请输入营销名称" v-model.trim="addForm.imName" maxlength="50"></el-input>
            </el-form-item>
            <div class="config-form-item el-form-item">
              <label class="is-require">营销时间:</label>
              <div style="position: relative">
                <el-col :span="11">
                  <el-date-picker type="date" :picker-options="pickerOptions" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" v-model="addForm.date1" style="width: 100%"></el-date-picker>
                </el-col>
                <!-- <el-col class="line" :span="1">-</el-col> -->
                <el-col :span="11" style="margin-left: 12px">
                  <el-time-picker placeholder="选择时间" value-format="HH:mm" format="HH:mm" v-model="addForm.date2" style="width: 100%"></el-time-picker>
                </el-col>
                <div class="el-form-item__error" v-if="isTime">请选择营销时间</div>
              </div>
            </div>
            <div class="config-form-item el-form-item">
              <label class="is-require">导入客户:</label>
              <div style="position: relative">
                <div class="upload-box">
                  <div class="upload-file-panel flex">
                    <div class="image-uploader">
                      <div class="upload-btn">
                        <i class="el-icon-plus"></i>
                        上传文件
                      </div>
                      <input type="file" style="opacity: 0" ref="file" accept="*" @change="uploadFile" />
                    </div>
                    <el-popover placement="bottom" width="455" trigger="hover">
                      <img src="@/images/table.png" class="preview" />
                      <span slot="reference" class="download-url">模板预览</span>
                    </el-popover>

                    <a class="download-url" href="https://img01.glsx.com.cn/weapp/resource/saas/scrm/xlsx/营销短信客户模板.xlsx">下载模版</a>
                  </div>
                  <div class="upload-result" v-if="importName">
                    <div class="upload-result-input">{{ importName }}</div>
                    <i class="el-icon-delete" @click="clearFile"></i>
                  </div>
                </div>
                <div class="el-form-item__error" v-if="isUplaod">{{ uploadErr }}</div>
              </div>
            </div>
            <div class="config-form-item el-form-item">
              <label class="is-require">短信内容:</label>
              <div style="position: relative">
                <wTextarea ref="textContent" class="textarea" v-model="addForm.messageContext" @updateText="updateText" :style="{ 'min-height': boxHeight + 'px' }"></wTextarea>
                <div class="select-panel">
                  <div @click="handleSelectMessage" class="flex">
                    <img src="@/images/link.png" class="link" />
                    <span class="">短链</span>
                  </div>
                </div>
                <div class="form-tips">70字內(含70字)计一条,超过70字,按70字/条计费。</div>
                <div class="form-tips">提示:当前短信已有{{ addForm.messageContext.length }}字,按{{ content.length }}条短信发送且计费。</div>
                <div class="el-form-item__error" v-if="isLink">请填写短信内容</div>
                <div class="el-form-item__error" v-if="isLink && count > 1">只允许插入一次短链</div>
              </div>
            </div>
          </el-form>
        </div>
        <div class="form-image">
          <img src="@/images/phone.png" />
          <div class="preview-content">
            <div class="preview-info">
              <div class="preview-text text" v-if="addForm.messageContext">【短信签名】{{ addForm.messageContext }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <GLFooter>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSave">提交</el-button>
    </GLFooter>
    <GLDialog title="提示" @onOk="handleSure" @onCancel="dialogVisible = false" type="warning" content="当前有内容信息未保存,是否继续返回?" :visible.sync="dialogVisible" mode="small"></GLDialog>
    <GLDialog title="操作结果" :hasFooter="true" mode="small" :visible.sync="importVisible">
      <div class="dialog-body-panel">
        <div class="import-result">
          <div class="success">
            <div class="number">{{ ImportResult.totalCount }}</div>
            <div>有效数据</div>
          </div>
          <div class="fail">
            <div class="number">{{ ImportResult.failCount }}</div>
            <div>异常数据</div>
          </div>
        </div>

        <div class="import-reason">
          <div class="import-tips">数据异常的可能原因:</div>
          <div class="import-reason-content">1、存在格式不正确的手机号数据</div>
          <div class="import-reason-content">2、存在重复的手机号数据</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="importVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="handleImport">导出错误数据</el-button>
      </span>
    </GLDialog>
  </div>
</template>
<script>
import wTextarea from './tag-textarea'
import util from '@/utils/util'
import * as XLSX from 'xlsx'
export default {
  data() {
    return {
      addForm: {
        imName: '',
        sendTime: '',
        exportUrl: '',
        messageContext: '',
        date1: '',
        date2: ''
      },
      uploadErr: '',
      content: [],
      tabelHeaderModel: ['短链', '短信签名', '商户名称', '手机号', '客户名称'],
      addRules: {
        imName: [{ required: true, message: '请输入营销名称', trigger: 'blur' }]
      },
      textValue: '',
      importName: '',
      loading: false,
      isTime: false,
      isUplaod: false,
      isLink: false,
      dialogVisible: false,
      importVisible: false,
      count: 0,
      ImportResult: {},
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 24 * 60 * 60 * 1000
        }
      }
    }
  },
  props: {
    boxHeight: {
      type: Number,
      default: 200
    }
  },
  components: {
    wTextarea
  },
  watch: {
    'addForm.messageContext': {
      handler: function() {
        this.content = []
        let str = this.addForm.messageContext
        let num = 70
        let len = str.length
        for (let i = 0; i < len / num; i++) {
          let str1 = str.substring(i * num, (i + 1) * num)
          this.content.push(str1)
        }
      }
    },
    addForm: {
      handler() {
        this.saveStatus = true
      },
      deep: true
    }
  },
  methods: {
    updateText(html) {
      this.addForm.messageContext = html
      this.textValue = html
    },
    handleSelectMessage() {
      if (this.$refs.textContent.currentText.indexOf('#短链#') != -1) {
        return
      }
      this.$refs.textContent.addTag('#短链#')
      this.addForm.messageContext = this.$refs.textContent.currentText
    },
    clearFile() {
      this.importName = ''
      this.addForm.exportUrl = ''
      this.$refs.file.value = ''
    },
    handleSave() {
      this.isUplaod = false
      this.isTime = false
      this.isLink = false
      this.count = 0
      if (this.addForm.date1 == '' || this.addForm.date2 == '') {
        this.isTime = true
        return
      }
      if (this.addForm.exportUrl == '') {
        this.isUplaod = true
        this.uploadErr = '请上传文件'
        return
      }
      if (this.addForm.messageContext == '') {
        this.isLink = true
        return
      } else {
        let array = this.addForm.messageContext.match(/#短链#/g) && this.addForm.messageContext.match(/#短链#/g).length
        this.count = array ? array.length : 0
      }
      if (this.count > 1) {
        this.isLink = true
      }
      if (this.ImportResult.totalCount == 0) {
        this.$message.error('导入客户有效数为0,请重新上传')
        return
      }
      this.$refs.configForm.validate(valid => {
        if (valid) {
          let params = {
            imName: this.addForm.imName,
            messageContext: this.addForm.messageContext,
            exportUrl: this.addForm.exportUrl,
            sendTime: this.addForm.date1 + ' ' + this.addForm.date2,
            exportName: this.importName
          }
          this.loading = true
          this.$apis.marketing.createMessageInfo(params).then(res => {
            if (res.code == 0) {
              this.loading = false
              this.$router.go(-1)
            }

            this.$message({
              message: res.code == '0' ? '创建成功' : res.message,
              type: res.code == '0' ? 'success' : 'error'
            })
          })
        }
      })
    },
    handleSure() {
      this.$router.push('/marketing/index')
    },
    handleCancel() {
      if (this.saveStatus) {
        this.dialogVisible = true
      } else {
        this.$router.push('/marketing/index')
      }
    },
    async uploadFile(e) {
      const file = e.srcElement.files[0]
      let AllImgExt = '.xls|.xlsx'
      let extName = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
      if (AllImgExt.indexOf(extName) === -1) {
        this.$message.error('上传格式不正确')
        return false
      }
      const header = await this.compareHeader(file)
      if (util.getArrayIsSubset(header, this.tabelHeaderModel)) {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = event => {
          const imgFile = event.target.result
          const arrBase64 = imgFile.split(',')
          const fileUrl = file.name.split('.')[1] + '@' + arrBase64[1]
          this.$apis.common.pictureUpload({ file: fileUrl }).then(res => {
            this.importName = file.name
            this.addForm.exportUrl = res
            this.isUplaod = false
            this.$refs.file.value = ''
            this.uploadErr = ''
            this.handleSaveImport()
          })
        }
      } else {
        this.importName = ''
        this.isUplaod = true
        this.uploadErr = '文件格式错误,请更换文件'
        this.$refs.file.value = ''
        this.addForm.exportUrl = ''
      }
    },
    compareHeader(file) {
      return new Promise(resolve => {
        const reader = new FileReader()
        reader.onload = event => {
          /* 解析数据 */
          const bstr = event.target.result
          const wb = XLSX.read(bstr, { type: 'binary' })
          /* 获取文件的第一个工作表(WorkSheet) */
          const wsname = wb.SheetNames[0]
          const ws = wb.Sheets[wsname]
          if (!ws['!ref']) {
            this.$message.error('格式错误')
            return
          }
          const data = this.getHeaderRow(ws)
          resolve(data)
        }
        reader.readAsBinaryString(file)
      })
    },
    getHeaderRow(sheet) {
      const headers = []
      /* sheet['!ref']表示所有单元格的范围,例如从A1到F8则记录为 A1:F8*/
      const range = XLSX.utils.decode_range(sheet['!ref'])
      let C,
        R = range.s.r /* 从第一行开始 */
      /* 按列进行数据遍历 */
      for (C = range.s.c; C <= range.e.c; ++C) {
        /* 查找第一行中的单元格 */
        const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]

        let hdr = 'UNKNOWN ' + C // <-- 进行默认值设置
        if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
        if (!hdr.includes('UNKNOWN')) {
          headers.push(hdr)
        }
      }
      return headers
    },
    handleSaveImport() {
      let params = {
        exportUrl: this.addForm.exportUrl
      }
      this.$apis.marketing.createMessageInfo(params).then(res => {
        if (res.code == 0) {
          this.importVisible = true
          this.ImportResult = res.data
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
          this.clearFile()
        }
      })
    },
    handleImport() {
      if (this.ImportResult.errorUrl) {
        util.openUrl(this.ImportResult.errorUrl.replace(/https?:/, ''))
      } else {
        this.$message({
          message: '暂无错误数据',
          type: 'error'
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  background: #ffffff;
  margin-bottom: 16px;
  padding: 0;
  .title {
    height: 56px;
    line-height: 56px;
    border-bottom: 1px solid #e5e5e5;
    padding-left: 20px;
    box-sizing: border-box;
  }
  .gl-form-page {
    flex: 1;
    padding: 20px;

    .config-form-item {
      display: flex;
      margin-top: 20px;
      /deep/.el-form-item__content {
        width: 460px;
      }
      /deep/.el-input--prefix .el-input__inner {
        line-height: 32px;
        height: 32px;
      }
    }
    /deep/.el-input__prefix {
      line-height: 32px;
      .el-input__icon {
        line-height: 32px;
      }
    }
    label {
      font-size: 14px;
      text-align: right;
      color: #666;
      width: 154px;
      // margin-right: 12px;
      height: 36px;
      line-height: 36px;
      img {
        width: 14px;
        height: 14px;
        vertical-align: middle;
        margin-left: 5px;
      }
      &.is-require {
        &::before {
          content: '*';
          color: #f56c6c;
          margin-right: 4px;
        }
      }
    }
    .line {
      color: #fff;
    }
    .download-url {
      margin-left: 11px;
      font-size: 12px;
      font-weight: 400;
      color: #5850ff;
      line-height: 17px;
    }
    .link {
      width: 18px;
      height: 18px;
      margin-right: 4px;
    }
    .upload-box {
      //   margin-bottom: 10px;
      .upload-btn {
        background: #ffffff;
        border: 1px solid #e5e5e5;
        border-radius: 3px;
        text-align: center;
        font-weight: 400;
        font-size: 14px;
        color: #666666;
        box-sizing: border-box;
        padding: 6px 17px;
        line-height: 20px;
      }
      .upload-result {
        display: flex;
        align-items: center;
        margin-top: 6px;
        &-input {
          width: 320px;
          height: 26px;
          background: #f5f7fa;
          border-radius: 4px;
          font-size: 14px;
          padding-left: 12px;
          color: #333333;
          line-height: 26px;
        }
        > i {
          cursor: pointer;
          color: #c6c6c6;
          font-size: 14px;
          margin-left: 10px;
        }
      }
      .upload-tips {
        margin-top: 6px;
        font-size: 14px;
        font-weight: 400;
        color: #999999;
        line-height: 20px;
      }
    }
    .textarea {
      width: 580px !important;
    }
    .select-panel {
      padding-left: 20px;
      background: #fafbfe;
      display: flex;
      font-size: 14px;
      font-weight: 400;
      color: #666666;
      line-height: 40px;
      border: 1px solid #e5e5e5;
      width: 580px !important;
      border-top: 0px;
      box-sizing: border-box;
      border-radius: 0 0 4px 4px;
      cursor: pointer;
    }
    .image-uploader {
      display: inline-block;
      width: 120px;
      position: relative;
      input {
        cursor: pointer;
        position: absolute;
        top: 0px;
        width: 100%;
        height: 100%;
        font-size: 0px;
      }
      /deep/ {
        .el-upload-list {
          display: none;
        }
      }
    }
    .form-tips {
      font-size: 12px;
      color: #999999;
      line-height: 20px;
      margin-top: 4px;
      white-space: nowrap;
    }
  }
  .form-image {
    width: 285px;
    height: 546px;
    margin: 20px 80px 20px 0;
    position: relative;
    .preview-content {
      position: absolute;
      width: 250px;
      height: 320px;
      overflow-y: auto;
      top: 125px;
      left: 50px;
      .preview-text {
        padding: 10px;
        position: relative;
        width: 160px;
        color: #333;
        background-color: #f4f4f4;
        border-radius: 4px;
        word-wrap: break-word;
        margin-bottom: 10px;
        line-height: 15px;
      }
    }
    img {
      width: 100%;
      height: auto;
    }
  }
}
.dialog-body-panel {
  .import-result {
    font-size: 14px;
    color: #333333;
    line-height: 22px;
    display: flex;
    align-items: center;
    .success {
      text-align: center;
      flex: 1;
      border-right: 1px solid #e7e7e7;
      .number {
        color: #2cca74;
        font-size: 26px;
        font-weight: 600;
        margin-bottom: 4px;
      }
    }
    .fail {
      text-align: center;
      flex: 1;
      .number {
        color: #eb4e4e;
        font-size: 26px;
        font-weight: 600;
        margin-bottom: 4px;
      }
    }
  }

  .import-reason {
    height: 80px;
    background: #f5f5f5;
    padding: 8px 16px;
    box-sizing: border-box;
    margin-top: 16px;
    .import-tips {
      font-size: 14px;
      font-weight: 400;
      color: #666666;
      line-height: 20px;
      margin-bottom: 4px;
    }
    &-content {
      font-size: 14px;
      font-weight: 400;
      color: #333333;
      line-height: 20px;
    }
  }
}
</style>