ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import type { FormSchema } from '@jnpf/ui/form';
 
import { computed, reactive } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
import { formValidate } from '@jnpf/utils';
 
import { create, getInfo, update } from '#/api/msgCenter/accountConfig';
import { $t } from '#/locales';
 
interface State {
  dataForm: any;
}
 
const emit = defineEmits(['register', 'reload']);
const state = reactive<State>({
  dataForm: {},
});
const schemas: FormSchema[] = [
  {
    field: 'fullName',
    label: '名称',
    component: 'Input',
    componentProps: { placeholder: '请输入', maxlength: 50 },
    rules: [{ required: true, trigger: 'blur', message: '必填' }],
  },
  {
    field: 'enCode',
    label: '编码',
    component: 'Input',
    componentProps: { placeholder: '请输入', maxlength: 50 },
    rules: [
      { required: true, trigger: 'blur', message: '必填' },
      { validator: formValidate('enCode'), trigger: 'blur' },
    ],
  },
  {
    field: 'webhookType',
    label: '类型',
    component: 'Select',
    componentProps: { placeholder: '请选择', onChange: onWebhookTypeChange },
    rules: [{ required: true, trigger: 'change', message: '必填' }],
  },
  {
    field: 'webhookAddress',
    label: 'WebHook地址',
    component: 'Input',
    componentProps: { placeholder: '请输入' },
    rules: [{ required: true, trigger: 'blur', message: '必填' }],
  },
  {
    field: 'approveType',
    label: '认证类型',
    component: 'Select',
    componentProps: {
      placeholder: '请选择',
      options: [
        { fullName: '无需认证', id: '1' },
        { fullName: 'bearer令牌', id: '2' },
      ],
    },
    rules: [{ required: true, trigger: 'change', message: '必填', type: 'string' }],
  },
  {
    ifShow: ({ values }) => values.approveType == '2',
    field: 'bearer',
    label: 'Bearer令牌',
    component: 'Input',
    helpMessage: '密钥',
    componentProps: { placeholder: '请输入' },
    rules: [{ required: true, trigger: 'blur', message: '必填' }],
  },
  {
    field: 'sortCode',
    label: '排序',
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: { min: '0', max: '999999', placeholder: '请输入' },
  },
  {
    field: 'enabledMark',
    label: '状态',
    component: 'Switch',
    defaultValue: 1,
  },
  {
    field: 'description',
    label: '说明',
    component: 'Textarea',
    componentProps: { rows: 3, placeholder: '请输入' },
  },
];
const getTitle = computed(() => (state.dataForm.id ? $t('common.editText') : $t('common.addText')));
const { createMessage } = useMessage();
const [registerForm, { setFieldsValue, validate, resetFields, updateSchema }] = useForm({ labelWidth: 120, schemas });
const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner(init);
 
function init(data) {
  resetFields();
  state.dataForm.id = data.id;
  updateSchema({ field: 'approveType', componentProps: { disabled: false } });
  if (data.categoryList) updateSchema({ field: 'webhookType', componentProps: { options: data.categoryList } });
  if (state.dataForm.id) {
    changeLoading(true);
    getInfo(state.dataForm.id).then((res) => {
      const data = res.data;
      state.dataForm = data;
      setFieldsValue(data);
      updateSchema({ field: 'approveType', componentProps: { disabled: data.webhookType == '2' } });
      changeLoading(false);
    });
  }
}
async function handleSubmit() {
  const values = await validate();
  if (!values) return;
  changeOkLoading(true);
  const query = {
    ...values,
    id: state.dataForm.id,
    type: 6,
  };
  if (query.approveType == '1') {
    query.bearer = '';
    query.userName = '';
    query.password = '';
  } else if (query.approveType == '2') {
    query.userName = '';
    query.password = '';
  } else {
    query.bearer = '';
  }
  const formMethod = state.dataForm.id ? update : create;
  formMethod(query)
    .then((res) => {
      createMessage.success(res.msg);
      changeOkLoading(false);
      closeModal();
      emit('reload');
    })
    .catch(() => {
      changeOkLoading(false);
    });
}
function onWebhookTypeChange(val) {
  if (val == 2) setFieldsValue({ approveType: '1', bearer: '' });
  updateSchema({ field: 'approveType', componentProps: { disabled: val == 2 } });
}
</script>
 
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" show-ok-btn @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>