add.vue 19.6 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
<template>
  <div class="addCoupon">
    <TitleWithCircle title="基本信息" style="padding: 10px 0;padding-left: 30px;padding-bottom: 20px;"/>
    <!-- 新增用户 -->
    <div class="addCouponBox">
      <el-form ref="ruleForm" class="formBox" :model="addForm" label-width="150px" :rules="couponRules">
        <el-form-item label="优惠券类型">
          <el-radio v-model="addForm.couponType" label="1">满减券</el-radio>
          <el-radio v-model="addForm.couponType" label="2">折扣券</el-radio>
        </el-form-item>
        <el-form-item label="优惠券名称" prop="couponName">
          <el-input v-model="addForm.couponName" placeholder="请输入优惠券名称" onblur="value=value.replace(/(^\s*)|(\s*$)/g, '')"  />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="addForm.remark" placeholder="请输入备注"  />
        </el-form-item>
        <el-form-item class="applyType" label="适用商品" prop="applyType">
          <!-- <el-radio v-model="addForm.applyType" label="1" >全部商品</el-radio> -->
          <el-radio
            v-model="addForm.applyType"
            label="2"

            @change="chooseProduct(type = '2')"
          >指定商品可用</el-radio>
          <span class="selectBtn" @click="chooseProduct(type = '2')">
            请选择
            <i v-if="addForm.applyType === '2' && idList.length !== 0" class="selectNum">
              {{ idList.length }}
            </i>
          </span>
          <el-radio
            v-model="addForm.applyType"
            label="3"

            @change="chooseProduct(type = '3')"
          >指定商品不可用</el-radio>
          <span class="selectBtn" @click="chooseProduct(type = '3')">
            请选择
            <i v-if="addForm.applyType === '3' && idList.length !== 0" class="selectNum">
              {{ idList.length }}
            </i>
          </span>
        </el-form-item>
        <el-form-item class="inputW" label="使用门槛" prop="threshold">
          <!-- 订单满<el-input v-model="addForm.threshold" :disabled="isThreshold" type="number" oninput="value=value.replace(/-/, '')" />元 -->
          订单满 <el-input-number v-model="addForm.threshold" :disabled="isThreshold" :controls="false" :min="0" :precision="2" :step="0.01" /> 元
          <el-checkbox v-model="isThreshold" @change="changeThreshold" >无门槛</el-checkbox>
        </el-form-item>
        <el-form-item v-if="addForm.couponType === '1'" class="inputW" label="优惠内容" prop="couponContent">
          <!-- 减<el-input v-model="addForm.couponContent" type="number" />元 -->
          减 <el-input-number v-model="addForm.couponContent" :controls="false" :min="0" :precision="2" :step="0.01"  /> 元
        </el-form-item>
        <el-form-item v-else class="inputW discount" label="优惠内容" prop="couponContent">
          <!-- 打<el-input v-model="addForm.couponContent" type="number" />折 -->
          打 <el-input-number v-model="addForm.couponContent" :controls="false" :min="0.1" :max="9.9" :precision="1" :step="0.1"  /> 折
          <label>输入值要大于0 小于10,可保留一位小数</label>
        </el-form-item>
        <el-form-item class="timeDataBox" label="用券时间">
          <el-radio v-model="addForm.timeType" label="1" >固定时间</el-radio>
          <div class="dateBox">
            <el-form-item>
              <el-date-picker
                v-model="dateInfo"
                :disabled="addForm.timeType === '2'"
                type="datetimerange"

                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </div>
        </el-form-item>
        <el-form-item class="boxWidth">
          <el-radio v-model="addForm.timeType" label="2" >领券当日起</el-radio>
          <el-input v-model="addForm.effectiveDay" :disabled="addForm.timeType !== '2'" oninput="value=value.replace(/[^\d]/g,'')" type="number"  />天内可用
        </el-form-item>
        <el-form-item class="boxWidth" label="发放数量" prop="number">
          <el-input v-model="addForm.number" type="number" oninput="value=value.replace(/[^\d]/g,'')"  />张
        </el-form-item>
        <el-form-item class="boxWidth" label="每人限领次数">
          <el-radio v-model="addForm.receiveType" label="1" >无限制</el-radio>
          <el-radio v-model="addForm.receiveType" label="2" >限制</el-radio>
          <el-input v-model="addForm.frequency" :disabled="addForm.receiveType === '1'" oninput="value=value.replace(/[^\d]/g,'')" type="number"  />次
        </el-form-item>
        <!-- <el-form-item label="叠加平台优惠">
          <el-radio-group v-model="addForm.ifAdd" >
            <el-radio label="0">不叠加</el-radio>
            <el-radio label="1">叠加</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button  @click="addCouponFn('ruleForm')" style="background-color: #3F9B6A;color: #fff" >保 存</el-button>
      <el-button @click="goToCoupon"   class="buttonHover"
              style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;margin-right: 20px;">取 消</el-button>
    </span>
    <!-- 新建分组弹框 -->
    <el-dialog
      title="选择商品"
      :visible.sync="isVisible"
      width="70%"
      top="120px"
      class="group-dialog"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      :modal="false"
    >
      <!-- 表格 -->
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="tableData"
          border
        :header-cell-style="{fontSize: '12px', backgroundColor: '#FAFAFA',color:'#000',fontWeight: 'normal'}"
          tooltip-effect="dark"
          style="width: 100%"
          :row-key="getRowKeys"
          max-height="600"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            :reserve-selection="true"
            width="55"
          />
          <el-table-column label="产品主图" width="220" align="center">
            <template slot-scope="scope">
              <img height="80" width="80" :src="$baseURL+scope.row.image " alt srcset>
            </template>
          </el-table-column>
          <el-table-column prop="productName" label="产品名称" width="220" />
          <el-table-column prop="productId" label="产品id" show-overflow-tooltip />
          <!--          <el-table-column prop="discountPrice" label="售价(元)" show-overflow-tooltip />-->
          <el-table-column prop="originalPrice" label="原价(元)" show-overflow-tooltip />
          <el-table-column prop="stockNumber" label="库存(件)" show-overflow-tooltip />
          <!--          <el-table-column prop="volume" label="销量" show-overflow-tooltip />-->
        </el-table>
        <!-- <div class="fenye">
          <el-pagination
            :current-page="proOption.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="proOption.pageSize"
            background
            small
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div> -->
        <div style="display: flex;justify-content: space-between;margin-top: 30px;" class="bom">
            <div style="font-size: 14px;">共 <span style="color: #3F9B6A;">{{total}}</span> 项数据</div>
            <el-pagination :current-page="proOption.page" :page-sizes="[10, 20, 50, 100]" :page-size="proOption.pageSize" background
              small layout="prev, pager, next" :total="total" @size-change="handleSizeChange"
              @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <div class="footBtnBox">
          <span slot="footer">
            <el-button type="primary" @click="saveIdList" >确 定</el-button>
            <el-button @click="closeSelect" >取 消</el-button>
          </span>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCoupon, getProducts, couponDetail, updateCoupon } from '@/api/marketing'
import TitleWithCircle from '@/components/top/index';
function InitCouponForm() {
  this.type = 1
  this.applyType = '2' //  适用商品 1-全部商品 2-指定商品可用 3-指定商品不可用
  this.couponContent = null // 优惠内容减多少元
  this.couponName = '' //  优惠券名称
  this.couponType = '1' //  优惠券类型 1-满减券 2-折扣券
  this.effectiveDay = null //  领券当日几天内(天数)
  this.effectiveStart = '' //  用券开始时间
  this.effectiveEnd = '' //  用券结束时间
  this.frequency = null //  限制次数
  this.ids = [] //  选中的商品id数组
  this.ifAdd = '0' //  是否叠加平台优惠 1-是 0-否
  this.number = null //  发放数量
  this.receiveType = '1' //  每人限领次数 1-无限次 2-限制几次
  this.remark = '' //  备注
  this.shopCouponId = null //  店铺优惠券id
  this.threshold = '0' //  使用门槛满多少元,无门槛为0
  this.timeType = '1' //  用券时间 1-固定时间 2-领券当日起几天内可用
}
export default {
  name: 'AddCoupon',
  props: {
    couponId: {
      type: Number,
      default: 0
    }
  },
  components: {
      TitleWithCircle
    },
  data() {
    return {
      // 新增
      getRowKeys(row) {
        return row.productId
      },
      addForm: new InitCouponForm(),
      proOption: {
        page: 1,
        pageSize: 10
      },
      dateInfo: [],
      idList: [],
      isThreshold: true,
      total: 0,
      tableData: [],
      currentType: '1',
      shopCouponId: '', // 优惠券ID
      couponRules: {
        couponName: [
          { required: true, message: '请输入优惠券名称', trigger: 'blur' }
        ],
        couponContent: [{ required: true, message: '请输入优惠金额' }],
        dateInfo: [{ type: 'date', required: true, message: '请选择时间' }],
        number: [{ required: true, message: '请输入发放数量', trigger: 'blur' }]
      },
      multipleSelection: [],
      roleList: [],
      isVisible: false // 选择商品弹窗
    }
  },
  watch: {
    couponId: {
      handler(nVal, oVal) {
        this.shopCouponId = nVal
        console.log('this.shopCouponId', this.shopCouponId)
        if (!nVal) {
          this.addForm = new InitCouponForm()
        } else {
          this.getCouponInfo()
        }
        this.$nextTick(() => {
          console.log('multipleTable', this.$refs.multipleTable)
        })
      }
    }
  },
  mounted() {
    console.log('mounted-------')
    this.shopCouponId = this.couponId
    if (this.shopCouponId) {
      this.getCouponInfo()
    }
  },
  methods: {
    resetData() {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection()
        this.idList = []
      }
    },
    proving1(e) {
      var keynum = window.event ? e.keyCode : e.which // 获取键盘码
      // var keychar = String.fromCharCode(keynum) // 获取键盘码对应的字符
      if (
        e.key
          .replace(/[^\d^\.]+/g, '')
          .replace('.', '$#$')
          .replace(/\./g, '')
          .replace('$#$', '.') === '' &&
        keynum !== 8
      ) {
        this.$message.warning('禁止输入中文或空')
        e.target.value = ' '
      }
    },
    addCouponFn(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.addForm.timeType === '1' && this.dateInfo.length === 0) {
            this.$message({
              message: '请选择用券时间开始和结束日期',
              type: 'warning'
            })
            return false
          }
          if (this.addForm.timeType === '2' && this.addForm.effectiveDay === null) {
            this.$message({
              message: '请填写领券多久内可以使用时间',
              type: 'warning'
            })
            return false
          }
          if (this.addForm.receiveType === ' 2' && this.addForm.frequency === null) {
            this.$message({
              message: '请填写每人限领次数',
              type: 'warning'
            })
            return false
          }
          if (this.addForm.couponType === '2') {
            var regExp = /^([1-9]{1})(.\d{1})?$/
            if (!regExp.test(this.addForm.couponContent)) {
              this.$message({
                message: '格式输入不正确',
                type: 'warning'
              })
              return false
            }
          }
          if ([2, 3].includes(parseInt(this.addForm.applyType))) {
            if (!this.addForm.ids || !this.addForm.ids.length || this.addForm.ids.length === 0) {
              this.$message({
                message: '请选择指定商品',
                type: 'warning'
              })
              return false
            }
          }
          this.addForm.effectiveStart = this.dateInfo[0]
          this.addForm.effectiveEnd = this.dateInfo[1]
          if (this.shopCouponId) {
            updateCoupon(this.addForm).then(res => {
              if (res.code === '') {
                this.$message.success('修改成功')
                this.addForm = new InitCouponForm()
                this.$emit('reset')
              } else {
                this.$message.error(res.message)
              }
            })
          } else {
            addCoupon(this.addForm).then(res => {
              if (res.code === '') {
                this.$message.success('提交成功')
                this.addForm = new InitCouponForm()
                this.$emit('reset')
              } else {
                this.$message.error(res.message)
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSizeChange(val) {
      this.proOption.pageSize = val
      this.getProList()
    },
    handleCurrentChange(val) {
      this.proOption.page = val
      this.getProList()
    },
    // 选择商品
    chooseProduct(type) {
      this.isVisible = true
      if (this.currentType !== type) {
        this.idList = []
        if (this.multipleSelection.length !== 0) {
          this.$refs.multipleTable.clearSelection()
        }
      }
      this.currentType = type
      this.getProList()
    },
    getProList() {
      getProducts(this.proOption).then(res => {
        if (res.code === '') {
          this.tableData = res.data.list
          this.total = res.data.total
          // 点击编辑时回显表格勾选
          if (this.shopCouponId) {
            for (let i = 0; i < this.tableData.length; i++) {
              if (this.idList.includes(this.tableData[i].productId)) {
                this.$refs.multipleTable.toggleRowSelection(this.tableData[i])
              }
            }
          }
        }
      })
    },
    // 选中商品
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 保存选择商品ID
    saveIdList() {
      const idList = []
      this.multipleSelection.forEach(i => {
        idList.push(i.productId)
      })
      this.idList = idList
      console.log('idList.length', idList.length)
      this.addForm.ids = idList
      this.addForm.applyType = this.currentType
      this.isVisible = false
    },
    // 取消选择
    closeSelect() {
      this.isVisible = false
    },
    // 跳转优惠券列表
    goToCoupon() {
      this.$emit('reset')
    },
    async getCouponInfo() {
      const res = await couponDetail({ shopCouponId: this.shopCouponId })
      this.addForm = res.data
      this.addForm.applyType = res.data.applyType.toString()
      this.addForm.couponContent = res.data.couponContent
      this.addForm.couponName = res.data.couponName
      this.addForm.couponType = res.data.couponType.toString()
      this.addForm.effectiveDay = res.data.effectiveDay
      this.addForm.effectiveStart = res.data.effectiveStart
      this.addForm.effectiveEnd = res.data.effectiveEnd
      this.addForm.frequency = res.data.frequency
      this.addForm.ids = res.data.ids
      this.addForm.ifAdd = res.data.ifAdd.toString()
      this.addForm.number = res.data.number
      this.addForm.receiveType = res.data.receiveType.toString()
      this.addForm.remark = res.data.remark
      this.addForm.shopCouponId = res.data.shopCouponId
      this.addForm.threshold = res.data.threshold.toString()
      this.addForm.timeType = res.data.timeType.toString()
      this.dateInfo = [this.addForm.effectiveStart, this.addForm.effectiveEnd]
      if (this.addForm.applyType === '2') {
        this.addForm.products.forEach(item => {
          this.idList.push(item.productId)
        })
      } else if (this.addForm.applyType === '3') {
        this.addForm.excludeIds.forEach(item => {
          this.idList.push(item)
        })
      } else {
        this.idList = []
      }
      this.currentType = res.data.applyType.toString()
    },
    changeThreshold(val) { // 输入框值改变
      if (val) {
        this.addForm.threshold = 0
      }
    },
  }
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
@import url("../../../styles/elDialog.scss");
.addCoupon {
  background: #FFFFFF;
  .dialog-footer {
    // margin-left: 300px;
    display: flex;
    flex-direction: row-reverse;
    margin-top: 30px;
  }
  .formBox {
    .flexBox {
      display: flex;
    }
    .applyType {
      span {
        width: 100px;
        height: 30px;
        line-height: 30px;
        background: #3F9B6A;
        color: #FFFFFF;
        text-align: center;
        display: inline-block;
        font-size: 14px;
        margin-right: 30px;
        border-radius: 4px;
        cursor: pointer;
        position: relative;
        i {
          position: absolute;
          right: -10px;
          top: -10px;
          width: 25px;
          height: 25px;
          line-height: 25px;
          background: #FFFFFF;
          border-radius: 50%;
          border: 1px solid #3F9B6A;
          text-align: center;
          color: #3F9B6A;
          font-style: normal;
          font-size: 12px;
        }
      }
    }
  }
.footBtnBox {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-top: 50px;
}
  .dateBox {
    display: flex;
    align-items: center;
    .description {
      width: 59px;
      text-align: center;
      display: block;
      font-size: 14px;
      color: #999999;
    }
  }
  .discount label {
    color: #cccccc;
    font-weight: 500;
    padding-left: 30px;
  }
}

</style>
<style scoped>
.flexBox /deep/ .el-input {
  width: 300px;
}
.inputW /deep/ .el-input, .el-input-number {
  width: 100px;
  /* margin: 0 8px; */
}
.inputW /deep/ .el-input .el-input__inner {
  text-align: center;
}
.inputW /deep/ .el-form-item__error {
  padding-left: 25px;
}
.inputW /deep/ .el-checkbox {
  margin-left: 20px;
}
.inputW /deep/ .el-radio {
  margin-left: 30px;
}
.boxWidth /deep/ .el-input {
  width: 100px;
  margin-right: 15px;
}
.boxWidth /deep/ .el-input .el-input__inner {
  text-align: center;
}
.addCoupon /deep/ .el-button--primary {
  background: #3F9B6A;
  border-color: #3F9B6A;
}
.addCoupon /deep/ .el-dialog__header {
  background-color: #3F9B6A;
}
.addCoupon /deep/ .el-dialog__headerbtn .el-dialog__close {
  color: #FFFFFF;
}
.addCoupon .timeDataBox /deep/ .el-form-item__content {
  display: flex;
  align-items: center;
}
.addCoupon .timeDataBox /deep/ .el-form-item__label {}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
input[type="number"]{
  -moz-appearance: textfield;
}

</style>