gaoluyang
3 天以前 0333d66e4b397c161c6a44ce1e2a121c2cc41082
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<template>
  <view class="basic-parameters-edit">
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <up-form ref="formRef"
             :model="form"
             :rules="rules"
             :errorType="['none']"
             label-width="110">
      <up-form-item label="参数编码"
                    prop="paramCode">
        <up-input v-model="form.paramCode"
                  disabled
                  placeholder="自动生成" />
      </up-form-item>
      <up-form-item label="参数名称"
                    prop="paramName"
                    required>
        <up-input v-model="form.paramName"
                  placeholder="请输入参数名称"
                  clearable />
      </up-form-item>
      <up-form-item label="参数类型"
                    prop="paramType"
                    required>
        <up-input v-model="paramTypeText"
                  placeholder="请选择参数类型"
                  readonly
                  @click="showParamTypeSheet = true" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showParamTypeSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="单位"
                    prop="unit"
                    :required="form.paramType === 1">
        <up-input v-model="form.unit"
                  placeholder="请输入单位"
                  clearable />
      </up-form-item>
      <up-form-item label="取值格式"
                    v-if="form.paramType === 1 || form.paramType === 2"
                    prop="paramFormat">
        <up-input v-model="form.paramFormat"
                  placeholder="请输入取值格式"
                  clearable />
      </up-form-item>
      <up-form-item label="下拉字典"
                    v-else-if="form.paramType === 3"
                    prop="paramFormat">
        <up-input v-model="dictTypeText"
                  placeholder="请选择下拉字典"
                  readonly
                  @click="showDictTypeSheet = true" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showDictTypeSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="时间格式"
                    v-else-if="form.paramType === 4"
                    prop="paramFormat">
        <up-input v-model="form.paramFormat"
                  placeholder="请选择时间格式"
                  readonly
                  @click="showTimeFormatSheet = true" />
        <template #right>
          <up-icon name="arrow-right"
                   @click="showTimeFormatSheet = true"></up-icon>
        </template>
      </up-form-item>
      <up-form-item label="是否必填"
                    prop="isRequired">
        <view style="display: flex; justify-content: flex-end; width: 100%;">
          <up-switch v-model="form.isRequired"
                     :activeValue="1"
                     :inactiveValue="0" />
        </view>
      </up-form-item>
      <up-form-item label="备注"
                    prop="remark">
        <up-textarea v-model="form.remark"
                     placeholder="请输入备注"
                     autoHeight />
      </up-form-item>
    </up-form>
    <FooterButtons :loading="loading"
                   :confirmText="paramId ? '保存' : '新增'"
                   @cancel="goBack"
                   @confirm="handleSubmit" />
    <!-- 参数类型选择 -->
    <up-action-sheet :show="showParamTypeSheet"
                     title="选择参数类型"
                     :actions="paramTypeActions"
                     @select="onSelectParamType"
                     @close="showParamTypeSheet = false" />
    <!-- 下拉字典选择 -->
    <up-action-sheet :show="showDictTypeSheet"
                     title="选择下拉字典"
                     :actions="dictTypeActions"
                     @select="onSelectDictType"
                     @close="showDictTypeSheet = false" />
    <!-- 时间格式选择 -->
    <up-action-sheet :show="showTimeFormatSheet"
                     title="选择时间格式"
                     :actions="timeFormatActions"
                     @select="onSelectTimeFormat"
                     @close="showTimeFormatSheet = false" />
  </view>
</template>
 
<script setup>
  import { computed, nextTick, onMounted, ref } from "vue";
  import { onLoad, onReady } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
  import PageHeader from "@/components/PageHeader.vue";
  import {
    addBaseParam,
    editBaseParam,
  } from "@/api/basicData/parameterMaintenance";
  import { listType } from "@/api/system/dict/type";
 
  const formRef = ref();
  const loading = ref(false);
  const paramId = ref("");
  const showParamTypeSheet = ref(false);
  const showDictTypeSheet = ref(false);
  const showTimeFormatSheet = ref(false);
  const dictTypes = ref([]);
 
  const form = ref({
    id: null,
    paramCode: "",
    paramName: "",
    paramType: "",
    unit: "",
    remark: "",
    isRequired: 0,
    paramFormat: "",
  });
 
  const rules = {
    paramName: [{ required: true, message: "请输入参数名称" }],
    paramType: [{ required: true, message: "请选择参数类型" }],
    unit: [
      {
        validator: (rule, value, callback) => {
          if (form.value.paramType === 1 && !value) {
            callback(new Error("数值类型必须填写单位"));
          } else {
            callback();
          }
        },
      },
    ],
  };
 
  const paramTypeActions = [
    { name: "数值格式", value: 1 },
    { name: "文本格式", value: 2 },
    { name: "下拉选项", value: 3 },
    { name: "时间格式", value: 4 },
  ];
 
  const timeFormatActions = [
    { name: "YYYY-MM-DD", value: "YYYY-MM-DD" },
    { name: "YYYY-MM-DD HH:mm:ss", value: "YYYY-MM-DD HH:mm:ss" },
  ];
 
  const dictTypeActions = computed(() => {
    return dictTypes.value.map(item => ({
      name: item.dictName,
      value: item.dictType,
    }));
  });
 
  const pageTitle = computed(() => (paramId.value ? "编辑参数" : "新增参数"));
 
  const paramTypeText = computed(() => {
    const action = paramTypeActions.find(
      item => item.value === form.value.paramType
    );
    return action ? action.name : "";
  });
 
  const dictTypeText = computed(() => {
    const action = dictTypes.value.find(
      item => item.dictType === form.value.paramFormat
    );
    return action ? action.dictName : form.value.paramFormat || "";
  });
 
  const goBack = () => {
    uni.navigateBack();
  };
 
  const getDictTypes = () => {
    listType({ pageNum: 1, pageSize: 1000 }).then(res => {
      dictTypes.value = res.rows || [];
    });
  };
 
  const onSelectParamType = action => {
    form.value.paramType = action.value;
    if (action.value === 1) {
      form.value.paramFormat = "#.00000";
    } else if (action.value === 4) {
      form.value.paramFormat = "YYYY-MM-DD HH:mm:ss";
    } else {
      form.value.paramFormat = "";
    }
    showParamTypeSheet.value = false;
  };
 
  const onSelectDictType = action => {
    form.value.paramFormat = action.value;
    showDictTypeSheet.value = false;
  };
 
  const onSelectTimeFormat = action => {
    form.value.paramFormat = action.value;
    showTimeFormatSheet.value = false;
  };
 
  const handleSubmit = () => {
    formRef.value
      .validate()
      .then(() => {
        if (form.value.paramType === 3 && !form.value.paramFormat) {
          uni.showToast({ title: "请选择下拉字典", icon: "none" });
          return;
        }
 
        loading.value = true;
        const action = paramId.value ? editBaseParam : addBaseParam;
        action({ ...form.value, id: paramId.value || undefined })
          .then(() => {
            uni.showToast({ title: "保存成功", icon: "success" });
            setTimeout(() => {
              goBack();
            }, 1500);
          })
          .catch(() => {
            uni.showToast({ title: "保存失败", icon: "none" });
          })
          .finally(() => {
            loading.value = false;
          });
      })
      .catch(errors => {
        if (errors && errors.length > 0) {
          uni.showToast({
            title: errors[0].message,
            icon: "none",
          });
        }
      });
  };
 
  onReady(() => {
    if (formRef.value) {
      formRef.value.setRules(rules);
    }
  });
 
  onMounted(() => {
    getDictTypes();
  });
 
  onLoad(options => {
    if (options?.item) {
      const item = JSON.parse(decodeURIComponent(options.item));
      paramId.value = item.id;
      if (item.paramType) {
        item.paramType = Number(item.paramType);
      }
      Object.assign(form.value, item);
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
 
  .basic-parameters-edit {
    min-height: 100vh;
    background: #f5f5f5;
  }
</style>