complementCodeDialog.vue 5.5 KB
<!-- 补库存标签码弹窗 -->
<template>
  <GLDialog title="补库存标签码" :visible.sync="dialogVisible" class="complementCodeDialog" width="500px" mode="mid" :destroy-on-close="true" :close-on-click-modal="false" okText="下载" cancelText="取消" @onOk="handleConfirm" @onCancel="handleClose" :before-close="handleClose">
    <div v-loading="isLoading" class="complement-dialog-content">
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="产品名称:">
          <span>{{ row.upgradeGoodsName || '-' }}</span>
        </el-form-item>
        <el-form-item label="产品编码:">
          <span>{{ row.productCode || '-' }}</span>
        </el-form-item>
        <el-form-item label="产品ID:">
          <span>{{ row.upgradeCommodityCode || '-' }}</span>
        </el-form-item>
        <el-form-item v-if="row.isFilm" :error="errorMsg" label="库存数量:" prop="stockNum">
          <el-input v-model="form.stockNum" placeholder="请输入" style="width: 200px">
            <template #append>
              米
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
  </GLDialog>
</template>

<script>
import { limitPrice } from '@/common/format'

export default {
  name: 'ComplementCodeDialog',
  props: {
    visibleSync: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      form: {
        stockNum: ''
      },
      rules: {
        stockNum: [
          { required: true, message: '请输入库存数量' },
          { validator: this.validateStockNum, trigger: 'blur' },
          { validator: this.validateStockNum, trigger: 'change' }
        ]
      },
      isLoading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visibleSync
      },
      set(newVal) {
        this.$emit('update:visibleSync', newVal)
      }
    }
  },
  methods: {
    validateStockNum(rule, val, callback) {
      // 1. 必填校验
      if (!val) {
        return callback(new Error('请输入库存数量'))
      }

      // 2. 格式化输入:只能输入数字,保留2位小数
      let formatted = val.replace(/[^\d.]/g, '')
      formatted = limitPrice(formatted, this.form.stockNum, 10, 2, 0)

      // 使用 $nextTick 更新表单数据,保持视图同步
      this.$nextTick(() => {
        this.form.stockNum = formatted
      })

      const numVal = Number(formatted)

      // 3. 校验数值有效性
      if (isNaN(numVal)) {
        return callback(new Error('请输入有效的数字'))
      }

      if (numVal <= 0) {
        return callback(new Error('库存数量必须大于0'))
      }

      // 4. 校验是否超过当前库存上限
      const maxStock = Number(this.row.stock)
      if (!isNaN(maxStock) && numVal > maxStock) {
        return callback(new Error('输入值不能高于当前库存数量'))
      }

      // 校验通过
      callback()
    },
    handleClose(done) {
      this.dialogVisible = false
      if (typeof done === 'function') done()
    },

    handleConfirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isLoading) return

          if (!this.row.isFilm) {
            if (!this.row.stock || Number(this.row.stock) <= 0) {
              this.$message.error('当前产品库存必须大于0,才允许补库存标签码')
              return
            }
          }

          const params = {
            // merchantId: this.row.merchantId,
            upgradeGoodsId: this.row.upgradeGoodsId,
            goodsType: this.row.goodsType,
            merchantId: this.row.yqMerchantId
          }

          if (this.row.isFilm) {
            params.stockNum = this.form.stockNum
          }

          this.isLoading = true
          this.$apis.stockManage
            .labelCodeRecode({ dataString: JSON.stringify(params) })
            .then(res => {
              this.isLoading = false
              if (res && res.code == 0) {
                const url = res.data.labelImageUrl
                if (url) {
                  this.downloadFile(url)
                  this.dialogVisible = false
                  this.$message.success('补库存标签码成功并开始下载')
                }
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(() => {
              this.isLoading = false
            })
        }
      })
    },
    downloadFile(url, fileName) {
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.download = fileName
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    }
  }
}
</script>

<style scoped lang="scss">
.complement-dialog-content {
  padding: 20px;
}

.qrcode-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;
}

.qrcode-capture-box {
  width: 280px;
  padding: 20px;
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .info-section {
    width: 100%;
    margin-bottom: 15px;
    font-size: 14px;
    color: #333;
    line-height: 1.8;

    .info-item {
      word-break: break-all;
    }
  }

  .qrcode-img {
    margin-bottom: 10px;
  }

  .qr-text {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-top: 5px;
  }
}

::v-deep .el-form-item__error {
  position: relative;
  margin-top: 4px;
}
</style>