liuxiaolong
2019-05-06 19e47fa0e48ac76a951bbfaa0a3e95211567f5a1
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
<template>
  <div>
    <h4 class="font-weight-bold py-3 mb-2">
      <router-link to="/dic">
        <span class="text-muted font-weight-light">数据字典管理 /</span>
      </router-link>
      字典{{$route.query.id?'编辑':'添加'}}
    </h4>
    <b-card
      no-body
      class="pt30 pl20 pr20 pb30"
    >
      <b-form @submit="saveDic">
        <h5>字典{{$route.query.id?'编辑':'添加'}}</h5>
        <div class="row">
          <div class="col-md-12 col-lg-6">
            <b-form-group
              horizontal
              label-class="text-sm-right"
              :label-cols="4"
              label="字典类型:"
              label-for="type"
            >
              <Select
                v-model="form.type"
                required
                placeholder="请选择类型"
              >
                <Option
                  v-for="item in sys_type"
                  :key="item.value"
                  :label="item.lable"
                  :value="item.value"
                />
              </Select>
            </b-form-group>
            <b-form-group
              horizontal
              required
              label-class="text-sm-right"
              :label-cols="4"
              label="字典名称:"
              label-for="name"
              :invalid-feedback="requiredValid(form.lable)"
              :valid-feedback="requiredValid(form.lable)"
              :state="requiredState(form.lable)"
            >
              <b-form-input
                id="name"
                type="text"
                autocomplete="off"
                v-model="form.lable"
                :state="requiredState(form.lable)"
                :placeholder="dicTips&&dicTips.labelDefault?`输入字典名称(必须是 ${dicTips.labelDefault})`:'输入字典名称'"
              />
            </b-form-group>
            <b-form-group
              horizontal
              required
              label-class="text-sm-right"
              :label-cols="4"
              label="字典值:"
              label-for="value"
              :invalid-feedback="requiredValid(form.value)"
              :valid-feedback="requiredValid(form.value)"
              :state="requiredState(form.value)"
            >
              <b-form-input
                id="value"
                type="text"
                autocomplete="off"
                v-model="form.value"
                :state="requiredState(form.value)"
                :placeholder="dicTips&&dicTips.valueType?`输入字典值(必须是${dicTips.valueType})`:'输入字典值'"
              />
            </b-form-group>
            <b-form-group
              horizontal
              label-class="text-sm-right"
              :label-cols="4"
              label="字典附加字段(json格式):"
              label-for="description"
              :state="requiredState(form.revJson)"
            >
              <b-form-textarea
                id="value"
                type="text"
                autocomplete="off"
                v-model="form.revJson"
                :placeholder="dicTips&&dicTips.jsonDesc?dicTips.jsonDesc:'输入字典附加字段'"
                :rows="4"
                :max-rows="6"
              />
            </b-form-group>
          </div>
        </div>
        <div class="text-right mt-3">
          <LaddaBtn
            :loading="saveLoading"
            @click.native="saveDic"
            data-style="slide-down"
            class="btn btn-primary"
          >
            保存
          </LaddaBtn>
          &nbsp;
          <b-btn
            variant="default"
            @click="$router.push({path:'/dic'})"
          >
            返回
          </b-btn>
        </div>
      </b-form>
    </b-card>
  </div>
</template>
 
<script>
import LaddaBtn from '@/vendor/libs/ladda/Ladda'
import { Select, Option } from 'element-ui'
import {
  getGloDicts,
  SysDictsSave,
  sysDictsFindSysById,
  sysDictsUpdate
} from '../../server/dic.js'
 
export default {
  data: () => ({
    form: {
      type: '',
      lable: '',
      value: '',
      revJson: ''
    },
    saveLoading: false,
    sys_type: [],
    ischeckUp: false
  }),
  computed: {
    dicTips() {
      const tips = this.sys_type.find(item => item.value === this.form.type)
      if (tips && tips.revJson) {
        return JSON.parse(tips.revJson)
      }
      return null
    }
  },
  async mounted() {
    this.sys_type = await this.fetchtGloDicts('SYSTEM_DICT')
    this.fetchDicts()
  },
  methods: {
    // * 保存接口
    saveDic() {
      this.ischeckUp = true
      const isValid = this.valid()
      if (!isValid) return
      if (this.$route.query.id && this.$route.query.id !== '') {
        // * 编辑
        this.updateDic()
      } else {
        // * 添加
        this.addDic()
      }
    },
    // * 添加操作
    async addDic() {
      const res = await SysDictsSave({
        orgId: this.$store.getters.basicUserInfo.orgId,
        module: this.$store.state.menuName,
        scope: 1,
        ...this.form
      })
      if (res.code - 0 === 0) {
        this.$toast({
          type: 'success',
          message: '添加成功'
        })
        this.$router.push('/dic')
      } else {
        this.$toast({
          type: 'error',
          message: res.message
        })
      }
    },
    // * 编辑操作
    async updateDic() {
      const res = await sysDictsUpdate({
        orgId: this.$store.getters.basicUserInfo.orgId,
        module: this.$store.state.menuName,
        id: this.$route.query.id,
        scope: 1,
        ...this.form
      })
      if (res.code - 0 === 0) {
        this.$toast({
          type: 'success',
          message: '编辑成功'
        })
        this.$router.push('/dic')
      } else {
        this.$toast({
          type: 'error',
          message: res.message
        })
      }
    },
    // * 获取字典类型
    async fetchtGloDicts(type) {
      const res = await getGloDicts({
        type
      })
      if (res && res.code - 0 === 0) {
        return res.data
      }
    },
    // * 校验
    requiredValid(val) {
      return this.ischeckUp ? (val !== '' ? '' : '此字段为必填项') : ''
    },
    requiredState(val) {
      return this.ischeckUp ? val !== '' : null
    },
    valid() {
      if (!this.form.type) {
        this.$toast({
          type: 'error',
          message: '请选择字典类型'
        })
        return false
      }
      if (!this.form.lable) {
        this.$toast({
          type: 'error',
          message: '请填写字典名称'
        })
        return false
      }
      if (!this.form.value) {
        this.$toast({
          type: 'error',
          message: '请填写字典信息'
        })
        return false
      }
      return true
    },
    // * 初始化
    async fetchDicts() {
      if (!(this.$route.query.id && this.$route.query.id !== '')) return
      const id = this.$route.query.id
      const res = await sysDictsFindSysById({
        id
      })
      if (res && res.code - 0 === 0) {
        for (let name in this.form) {
          this.form[name] = res.data[name]
        }
      }
    }
  },
  components: {
    Select,
    Option,
    LaddaBtn
  }
}
</script>