fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
    <div>
       <el-dialog :visible.sync="dialogVisible" :title="title" :close-on-click-modal="false" :modal-append-to-body="false" v-if="dialogVisible">
            <el-form size="mini" :model="form" label-width="150px" ref="form" :rules="formRules">
                 <el-form-item label="商品主编码:" prop="spuNum">
                     <el-input v-model="form.spuNum" placeholder="请输入商品主编码" clearable></el-input>
                 </el-form-item>
                 <el-form-item label="存货辅助核算编码:" prop="auxiliaryCode">
                     <el-input v-model="form.auxiliaryCode" placeholder="请输入存货辅助核算编码" clearable></el-input>
                 </el-form-item>
                 <el-form-item label="商品名称:" prop="spuName">
                     <el-input v-model="form.spuName" placeholder="请输入商品名称" clearable></el-input>
                 </el-form-item>
                 <el-form-item label="规格型号:" prop="model">
                     <el-input v-model="form.model" placeholder="请输入规格型号" clearable></el-input>
                 </el-form-item>
                 <el-form-item label="单位:" prop="unit">
                    <el-input v-model="form.unit" placeholder="请输入单位" clearable></el-input>
                 </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click="submit" type="primary"  :loading="loading">保存</el-button>
                <el-button size="mini" @click="dialogVisible = false">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import invoiceProdInfoApi from '@/api/invoiceProdInfo'
import { objectCopy } from '@/utils/objectCopyHelper'
import { limitNumType } from '@/utils/validator'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '添加品牌'
    },
    row: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        spuName: null,
        auxiliaryCode: null,
        spuNum: null,
        model: null,
        unit: null
      },
      formRules: {
        spuName: [{ required: true, message: '请输入商品名称', trigger: 'change' },
          { max: 128, message: '商品名称最多只能输入 128 个字' }
        ],
        auxiliaryCode: [{ required: true, message: '请输入存货辅助核算编码' },
          { validator: limitNumType },
          { max: 256, message: '存货辅助核算编码最多只能输入 256 个字' },
          { min: 5, message: '存货辅助核算编码最少输入 5 个字' }
        ],
        spuNum: [{ required: true, message: '请输入商品主编码', trigger: 'change' },
          { validator: limitNumType },
          { max: 32, message: '商品主编码最多只能输入 32 个字' }
        ],
        model: [{ required: true, message: '请输入规格型号', trigger: 'change' },
          { max: 64, message: '规格型号最多只能输入 64 个字' }
        ],
        unit: [{ required: true, message: '请输入单位', trigger: 'change' },
          { max: 16, message: '单位最多只能输入 16 个字' }
        ]
      },
      loading: false,
      fileList: [] // 图片数组集合
    }
  },
  watch: {
    row: {
      handler(newValue, oldValue) {
        if (newValue.id) {
          this.form = objectCopy(this.row)
        }
      },
      deep: true
    },
    /**
     * 监控外部显示变量变化
     * 传递到dialog组件
     */
    show: function(newShow, oldShow) {
      this.dialogVisible = newShow
    },
    /**
     * 监控内部显示属性变化
     * 传递到外部调用变量
     */
    dialogVisible: function(newDialogShow, oldDialogShow) {
      if (!newDialogShow) {
        for (const i in this.form) {
          this.form[i] = null
        }
      }
      this.$emit('update:show', newDialogShow)
    }
  },
  methods: {
    // 编辑
    async editInfo() {
      try {
        const res = await invoiceProdInfoApi.updateInfo(this.form)
        if (res.code === '0') {
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
          this.dialogVisible = false
          this.loading = false
          this.$parent.queryData()
        } else {
          this.loading = false
        }
      } catch (error) {
        this.loading = false
      }
    },
    // 新增
    async addInfo() {
      try {
        const res = await invoiceProdInfoApi.addInfo(this.form)
        if (res.code === '0') {
          this.$message({
            message: '新增成功',
            type: 'success'
          })
          this.loading = false
          this.dialogVisible = false
          this.$parent.queryData()
        } else {
          this.loading = false
        }
      } catch (error) {
        this.loading = false
      }
    },
    /**
       * 提交
       */
    submit() {
      this.$refs.form.validate().then(res => {
        this.loading = true
        if (this.form.id) {
          this.editInfo()
        } else {
          this.addInfo()
        }
      })
    }
  }
}
</script>