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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<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="120px" ref="form" :rules="formRules">
                 <el-form-item label="一级分类名称:" v-show="form.pid" prop="publicityName">
                     <el-select :disabled="true" v-model="form.pid">
                       <el-option
                         v-for="item in publicityArr"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"
                       ></el-option>
                     </el-select>
                 </el-form-item>
                 <el-form-item :label="form.pid ? '二级分类名称:':'一级分类名称:'" prop="publicityName"
                  :rules="[{ required: true, message: `请输入${form.pid ? '二级分类名称' :'一级分类名称'}`, trigger: 'change' },
                    { max: form.pid ? 6:4, message: `${form.pid ? '二级分类名称最多只能输入 6 个字' :'一级分类名称最多只能输入 4 个字'}`, trigger: 'change' }
                  ]"
                 >
                     <el-input v-model="form.publicityName" :placeholder="`请输入${form.pid ? '二级分类名称' :'一级分类名称'}`" clearable></el-input>
                 </el-form-item>
                 <el-form-item label="排序" prop="sort" v-if="form.pid">
                     <el-input-number v-model="form.sort" :min="1"  :max="999" clearable></el-input-number>
                     <div>提示:若排序编号已存在则无法添加</div>
                 </el-form-item>
                <el-form-item label="描述:" prop="publicityDesc">
                     <el-input v-model="form.publicityDesc" type="textarea" :autosize="{minRows:2,maxRows:4}" 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 catPublicityApi from '@/api/proPublicityMgt/catPublicity'
import { objectCopy } from '@/utils/objectCopyHelper'
import { inputNumberValid } 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: {
        publicityName: null,
        publicityDesc: null,
        pid: null,
        sort: undefined
      },
      formRules: {
        culturalType: [
          { required: true, message: '请输入一级分类名称', trigger: 'change' },
          { max: 4, message: '一级分类名称最多只能输入 4 个字', trigger: 'change' }
        ],
        publicityDesc: [{ max: 200, message: '描述最多只能输入 200 个字' }],
        sort: [
          { required: true, message: '请输入排序' },
          { validator: inputNumberValid }
        ]
      },
      loading: false,
      publicityArr: []
    }
  },
  watch: {
    row: {
      handler (newValue, oldValue) {
        if (this.title === '新增二级分类') {
          this.form.pid = this.row.publicityId
          this.getCategory()
        } else if (newValue.publicityId) {
          this.form = objectCopy(this.row)
          this.form.sort = this.row.sort ? this.row.sort : undefined
        }
      },
      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 getCategory () {
      try {
        const res = await catPublicityApi.getNoDelete({ level: '1' })
        if (res.code === '0') {
          this.publicityArr = res.data.map(item => {
            return {
              id: item.publicityId,
              name: item.publicityName
            }
          })
        }
      } catch (error) {
      }
    },
    // 编辑
    async editInfo () {
      try {
        const res = await catPublicityApi.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 catPublicityApi.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.publicityId) {
          this.editInfo()
        } else {
          this.addInfo()
        }
      })
    }
  }
}
</script>