|
@@ -0,0 +1,187 @@
|
|
|
+<template>
|
|
|
+ <n-modal v-model:show="modalVisible" preset="card" :title="title" class="w-700px">
|
|
|
+ <n-form ref="formRef" label-placement="left" :label-width="80" :model="formModel" :rules="rules">
|
|
|
+ <n-grid :cols="24" :x-gap="18">
|
|
|
+ <n-form-item-grid-item :span="12" label="牧场名称" path="pasture_id">
|
|
|
+ <n-select v-model:value="formModel.pasture_id" :options="TopicAccessOptions" />
|
|
|
+ </n-form-item-grid-item>
|
|
|
+ <n-form-item-grid-item :span="12" label="客户端ID" path="client_id">
|
|
|
+ <n-input v-model:value="formModel.client_id" />
|
|
|
+ </n-form-item-grid-item>
|
|
|
+ <n-form-item-grid-item :span="12" label="用户名称" path="user_name">
|
|
|
+ <n-input v-model:value="formModel.user_name" />
|
|
|
+ </n-form-item-grid-item>
|
|
|
+ <n-form-item-grid-item :span="12" label="用户密码" path="user_name">
|
|
|
+ <n-input v-model:value="formModel.password" type="password" />
|
|
|
+ </n-form-item-grid-item>
|
|
|
+ <n-form-item-grid-item :span="12" label="主题名称(topic)" path="topic_id">
|
|
|
+ <n-select v-model:value="formModel.topic_id" :options="TopicAccessOptions" />
|
|
|
+ </n-form-item-grid-item>
|
|
|
+ <n-form-item-grid-item :span="12" label="主题动作" path="access">
|
|
|
+ <n-radio-group v-model:value="formModel.access">
|
|
|
+ <n-radio v-for="item in TopicAccessOptions" :key="item.value" :value="item.value">{{ item.label }}</n-radio>
|
|
|
+ </n-radio-group>
|
|
|
+ </n-form-item-grid-item>
|
|
|
+ </n-grid>
|
|
|
+ <n-space class="w-full pt-16px" :size="24" justify="end">
|
|
|
+ <n-button class="w-72px" @click="closeModal">取消</n-button>
|
|
|
+ <n-button class="w-72px" type="primary" @click="handleSubmit">确定</n-button>
|
|
|
+ </n-space>
|
|
|
+ </n-form>
|
|
|
+ </n-modal>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup lang="ts">
|
|
|
+import { computed, ref, reactive, watch } from 'vue';
|
|
|
+import type { FormInst, FormItemRule } from 'naive-ui';
|
|
|
+import { TopicAccessOptions } from '@/constants';
|
|
|
+import { createRequiredFormRule } from '@/utils';
|
|
|
+import { mqttUserAdd, mqttUserEdit } from '@/service/api/mqtt';
|
|
|
+import { MD5 } from '@/utils/crypto';
|
|
|
+export interface Props {
|
|
|
+ /** 弹窗可见性 */
|
|
|
+ visible: boolean;
|
|
|
+ /**
|
|
|
+ * 弹窗类型
|
|
|
+ * add: 新增
|
|
|
+ * edit: 编辑
|
|
|
+ */
|
|
|
+ type?: 'add' | 'edit';
|
|
|
+ /** 编辑的表格行数据 */
|
|
|
+ editData?: Mqtt.User | null;
|
|
|
+}
|
|
|
+
|
|
|
+export type ModalType = NonNullable<Props['type']>;
|
|
|
+
|
|
|
+defineOptions({ name: 'TableActionModal' });
|
|
|
+
|
|
|
+const props = withDefaults(defineProps<Props>(), {
|
|
|
+ type: 'add',
|
|
|
+ editData: null
|
|
|
+});
|
|
|
+
|
|
|
+interface Emits {
|
|
|
+ (e: 'update:visible', visible: boolean): void;
|
|
|
+}
|
|
|
+
|
|
|
+const emit = defineEmits<Emits>();
|
|
|
+
|
|
|
+const modalVisible = computed({
|
|
|
+ get() {
|
|
|
+ return props.visible;
|
|
|
+ },
|
|
|
+ set(visible) {
|
|
|
+ emit('update:visible', visible);
|
|
|
+ }
|
|
|
+});
|
|
|
+const closeModal = () => {
|
|
|
+ modalVisible.value = false;
|
|
|
+};
|
|
|
+const titles: Record<ModalType, string> = {
|
|
|
+ add: '添加用户',
|
|
|
+ edit: '编辑用户'
|
|
|
+};
|
|
|
+
|
|
|
+const title = computed(() => {
|
|
|
+ return titles[props.type];
|
|
|
+});
|
|
|
+
|
|
|
+const formRef = ref<HTMLElement & FormInst>();
|
|
|
+
|
|
|
+type FormModel = Pick<
|
|
|
+ Mqtt.User,
|
|
|
+ | 'id'
|
|
|
+ | 'pasture_id'
|
|
|
+ | 'user_name'
|
|
|
+ | 'password'
|
|
|
+ | 'client_id'
|
|
|
+ | 'topic_id'
|
|
|
+ | 'access'
|
|
|
+ | 'is_show'
|
|
|
+ | 'created_at_format'
|
|
|
+ | 'updated_at_format'
|
|
|
+>;
|
|
|
+
|
|
|
+const formModel = reactive<FormModel>(createDefaultFormModel());
|
|
|
+
|
|
|
+const rules: Record<keyof FormModel, FormItemRule | FormItemRule[]> = {
|
|
|
+ pasture_id: createRequiredFormRule('请选择牧场'),
|
|
|
+ user_name: createRequiredFormRule('请输入用户名称'),
|
|
|
+ password: createRequiredFormRule('请输入用户密码'),
|
|
|
+ client_id: createRequiredFormRule('请输入客户端id'),
|
|
|
+ topic_id: createRequiredFormRule('请选择topic模板'),
|
|
|
+ access: createRequiredFormRule('请选择topic权限'),
|
|
|
+ is_show: createRequiredFormRule(),
|
|
|
+ created_at_format: createRequiredFormRule(),
|
|
|
+ updated_at_format: createRequiredFormRule(),
|
|
|
+ id: createRequiredFormRule()
|
|
|
+};
|
|
|
+
|
|
|
+function createDefaultFormModel(): FormModel {
|
|
|
+ return {
|
|
|
+ id: 0,
|
|
|
+ pasture_id: null,
|
|
|
+ user_name: '',
|
|
|
+ password: '',
|
|
|
+ client_id: '',
|
|
|
+ topic_id: null,
|
|
|
+ access: 0,
|
|
|
+ is_show: 1,
|
|
|
+ created_at_format: '',
|
|
|
+ updated_at_format: ''
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+function handleUpdateFormModel(model: Partial<FormModel>) {
|
|
|
+ Object.assign(formModel, model);
|
|
|
+}
|
|
|
+
|
|
|
+function handleUpdateFormModelByModalType() {
|
|
|
+ const handlers: Record<ModalType, () => void> = {
|
|
|
+ add: () => {
|
|
|
+ const defaultFormModel = createDefaultFormModel();
|
|
|
+ handleUpdateFormModel(defaultFormModel);
|
|
|
+ },
|
|
|
+ edit: () => {
|
|
|
+ if (props.editData) {
|
|
|
+ handleUpdateFormModel(props.editData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ handlers[props.type]();
|
|
|
+}
|
|
|
+
|
|
|
+async function handleSubmit() {
|
|
|
+ await formRef.value?.validate();
|
|
|
+ if (props.type === 'add') {
|
|
|
+ formModel.password = MD5(formModel.password).toString();
|
|
|
+ const data = mqttUserAdd(formModel);
|
|
|
+ data.then(res => {
|
|
|
+ if (res.data?.success) {
|
|
|
+ window.$message?.success(`${titles[props.type]}成功!`);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (props.type === 'edit') {
|
|
|
+ const data = mqttUserEdit(formModel);
|
|
|
+ data.then(res => {
|
|
|
+ if (res.data?.success) {
|
|
|
+ window.$message?.success(`${titles[props.type]}成功!`);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ closeModal();
|
|
|
+}
|
|
|
+
|
|
|
+watch(
|
|
|
+ () => props.visible,
|
|
|
+ newValue => {
|
|
|
+ if (newValue) {
|
|
|
+ handleUpdateFormModelByModalType();
|
|
|
+ }
|
|
|
+ }
|
|
|
+);
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped></style>
|