usageLimitDialog.vue 6.53 KB
<template>
  <baseDialog :is-show.sync="dialogShow" :dialog-config="dialogConfig.config" class="detail-content-warn"
    @handleClick="submit">
    <div class="usage-box flex">
      <div class="progress-box">
        <el-steps direction="vertical" :active="3">
          <el-step title="请阅读功能使用注意事项"></el-step>
          <el-step title="请填写当期使用额度"></el-step>
          <el-step title="请填写预警通知邮箱"></el-step>
        </el-steps>
      </div>
      <div class="form">
        <div class="tips">
          每次“新增使用额度”都会重置上次限额,且上次未使用额度不会延续叠加使用(仅当期有效);<br>
          预警通知触发条件:当额度使用率达到70%、80%、90%、95%时,各触发一次预警;<br>
          “使用额度”仅控制二手车业务自动付款,超过当期额度后无法自动付款。
        </div>
        <el-form label-width="150px" size="small" :model="infoForm" ref="editRef" :rules="formRules">
          <el-form-item label="当期使用额度:" class="usage-limit" prop="usageLimit">
            <el-input v-model.number="infoForm.usageLimit" class="input-width" :maxlength="15"
              @input="handleUsageLimitNum" @blur="handleBlur" placeholder="请输入当前使用额度">

            </el-input>
            <span class="unit">元</span>
          </el-form-item>
          <el-form-item label="预警通知邮箱:" prop="warningEmail">
            <el-input v-model="infoForm.warningEmail" class="input-width" :maxlength="200"
              placeholder="请输入预警通知邮箱(多邮箱用逗号隔开,最多五个)">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </basedialog>
</template>

<script>
import { addFundUsageLimit } from "@api/usedCarManagement"
import { limitPrice } from '@/common/format'
import { deepClone } from "@/common/util"
export default {
  name: "UsageLimitDialog",
  inject: ['userInfo'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('dialogClose', { flag: this.refreshFlag })
      }
    },
    authPhone() {
      return this.userInfo.loginAccount.phone
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.oldVal = ''
          this.infoForm = this.$options.data.call(this).infoForm
          this.$refs?.editRef?.resetFields()
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {
  },
  data() {
    return {
      dialogConfig: {
        config: {
          title: "新增使用额度",
          width: "840px"
        }
      },
      infoForm: {
        usageLimit: '',
        warningEmail: ''
      },
      oldVal: '',
      formRules: {
        usageLimit: [
          { required: true, message: "请填写当期使用额度", trigger: "blur" }
        ],
        warningEmail: [{
          required: true,
          validator: (rule, value, callback) => {
            if (!value) {
              return callback(new Error(`请填写预警通知邮箱`))
            }
            // 去除首尾空格,并按逗号分割
            const emailArray = value.split(',').map(email => email.trim())
            if (emailArray.length > 5) {
              return callback(new Error(`最多只能填写5个邮箱`))
            }

            // 邮箱格式正则(基础版)
            const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

            // 检查每个邮箱是否合法
            const invalidEmails = emailArray.filter(email => {
              return email && !emailRegex.test(email) // 过滤非法邮箱
            })

            if (invalidEmails.length > 0) {
              return callback(new Error(`以下邮箱格式不正确: ${invalidEmails.join(', ')}`))
            }
            // 可选:检查是否有重复邮箱
            const unique = [...new Set(emailArray)]
            if (unique.length !== emailArray.length) {
              return callback(new Error('存在重复的邮箱地址'))
            }
            callback() // 验证通过
          },
          trigger: "blur"
        }]
      },

    }
  },
  methods: {
    handleBlur(event) {
      let { usageLimit } = this.infoForm
      this.infoForm.usageLimit = usageLimit <= 0 ? '' : usageLimit
    },
    handleUsageLimitNum(event) {
      this.infoForm.usageLimit = limitPrice(event, this.oldVal, 15, 2)
      this.oldVal = this.infoForm.usageLimit
    },
    submit() {
      this.$refs.editRef.validate(async (valid) => {
        if (valid) {
          let { infoForm, authPhone } = this,
            params = deepClone(infoForm)
          // 提交前清理末尾逗号
          params.warningEmail = params.warningEmail.trim().replace(/,+$/, '')
          const data = await addFundUsageLimit({
            ...params,
            authPhone
          })
          if (data) {
            this.notice.okTips('新增使用额度成功')
            this.$emit('addUsageLimitSuccess')
          } else this.notice.errorTips('新增使用额度失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  align-items: center;
}

.detail-content-warn {
  ::v-deep .el-dialog {
    height: 428px;

    .el-dialog__body {
      // border-bottom: 1px solid #e6e6e6;
    }
  }

  .usage-box {
    height: 263px;

    .progress-box {
      width: 210px;
      height: 244px;

      ::v-deep .el-step__main {
        padding: 4px 0 0 18px;

        .el-step__title {
          font-size: 14px;
        }
      }

      ::v-deep .el-step__line {
        left: 15px;
        background: none;

        .el-step__line-inner {
          height: 29px !important;
          position: absolute;
          top: 54px;
        }
      }

      ::v-deep .el-step__icon {
        width: 32px;
        height: 32px;
      }
    }

    .form {
      width: calc(100% - 210px);
      height: 263px;

      .tips {
        border-radius: 4px;
        opacity: 1;
        border: 1px solid #e5e5e5;
        background: #fafafa;
        color: #666666;
        font-size: 13px;
        line-height: 20px;
        padding: 8px 16px;
        margin-bottom: 41px;
      }

      .usage-limit {
        margin-bottom: 70px;
        position: relative;

        .unit {
          position: absolute;
          right: 8px;
          top: 6px;
          color: #333333;
          font-size: 14px;
          line-height: 20px;
        }
      }
    }
  }
}
</style>