feat: 协议管理

This commit is contained in:
jackhoo_98 2023-02-02 18:08:26 +08:00
parent ecd941c84c
commit c54894a20f
5 changed files with 268 additions and 34 deletions

View File

@ -1,10 +1,17 @@
import server from '@/utils/request';
import { BASE_API_PATH } from '@/utils/variable';
export const PROTOCOL_UPLOAD = `${BASE_API_PATH}/file/upload`;
export const detail = (id: string) => server.get(`/gateway/device/${id}`);
export const save = (data: Object) => server.post(`/gateway/device`, data);
export const list = (data: Object) =>
server.post(`/protocol/_query`, data);
export const update = (data: Object) => server.patch(`/gateway/device`, data);
export const list = (data: Object) => server.post(`/protocol/_query`, data);
export const remove = (id: string) => server.remove(`/gateway/device/${id}`);
export const querySystemApi = (data: Object) =>
server.post(`/system/config/scopes`, data);

View File

@ -0,0 +1,91 @@
<template>
<a-spin :spinning="loading">
<a-input
placeholder="请上传文件"
v-model:value="value"
style="width: calc(100% - 100px)"
:disabled="true"
/>
<a-upload
name="file"
accept=".jar, .zip"
:multiple="true"
:action="PROTOCOL_UPLOAD"
:headers="{
[TOKEN_KEY]: LocalStore.get(TOKEN_KEY),
}"
@change="handleChange"
:showUploadList="false"
class="upload-box"
:before-upload="beforeUpload"
>
<a-button type="primary">上传jar包</a-button>
</a-upload>
</a-spin>
</template>
<script setup lang="ts" name="FileUpload">
import { LocalStore } from '@/utils/comm';
import { TOKEN_KEY } from '@/utils/variable';
import { PROTOCOL_UPLOAD, querySystemApi } from '@/api/link/protocol';
import { message } from 'ant-design-vue';
import type { UploadChangeParam, UploadProps } from 'ant-design-vue';
import { notification as Notification } from 'ant-design-vue';
const emit = defineEmits(['update:modelValue', 'change']);
const props = defineProps({
modelValue: {
type: String,
default: () => '',
},
});
const value = ref(props.modelValue);
const loading = ref(false);
const beforeUpload: UploadProps['beforeUpload'] = (file) => {
const arr = file.name.split('.');
const isFile = ['jar', 'zip'].includes(arr[arr.length - 1]); // file.type === 'application/zip' || file.type === 'application/java-archive'
if (!isFile) {
message.error('请上传.zip.jar格式的文件');
loading.value = false;
}
return isFile;
};
const handleChange = async (info: UploadChangeParam) => {
loading.value = true;
if (info.file.status === 'done') {
loading.value = false;
const result = info.file.response?.result;
const api = await querySystemApi(['paths']);
const path = api.result[0]?.properties
? api.result[0]?.properties['base-path']
: '';
const f = `${path}/file/${result.id}?accessKey=${result.others.accessKey}`;
message.success('上传成功!');
value.value = f;
emit('update:modelValue', f);
emit('change', f);
} else {
if (info.file.error) {
Notification.error({
// key: '403',
message: '系统提示',
description: '系统未知错误,请反馈给管理员',
});
loading.value = false;
} else if (info.file.response) {
loading.value = false;
}
}
};
</script>
<style lang="less" scoped>
.upload-box {
:deep(.ant-btn) {
width: 100px;
}
}
</style>

View File

@ -4,16 +4,69 @@
ok-text="确认"
cancel-text="取消"
:visible="true"
width="600px"
width="700px"
:confirm-loading="loading"
@cancel="handleCancel"
@ok="handleOk"
>
123
<a-form
class="form"
layout="vertical"
:model="formData"
name="basic"
autocomplete="off"
>
<a-form-item label="名称" v-bind="validateInfos.name">
<a-input
placeholder="请输入名称"
v-model:value="formData.name"
/>
</a-form-item>
<a-form-item label="类型" v-bind="validateInfos.type">
<RadioCard
:disabled="!!id"
layout="horizontal"
:checkStyle="true"
:options="options"
v-model="formData.type"
/>
</a-form-item>
<a-form-item
label="文件地址"
v-bind="validateInfos['configuration.location']"
>
<a-input
v-if="formData.type === 'local'"
placeholder="请输入文件地址"
v-model:value="formData.configuration.location"
/>
<FileUpload
v-else
v-model:modelValue="formData.configuration.location"
/>
</a-form-item>
<a-form-item label="说明" v-bind="validateInfos.description">
<a-textarea
placeholder="请输入说明"
v-model:value="formData.description"
:maxlength="200"
:rows="3"
showCount
/>
</a-form-item>
</a-form>
</a-modal>
</template>
<script lang="ts" setup>
import { message, Form } from 'ant-design-vue';
import { getImage } from '@/utils/comm';
import type { UploadChangeParam } from 'ant-design-vue';
import FileUpload from './FileUpload.vue';
import { save, update } from '@/api/link/protocol';
const loading = ref(false);
const fileLoading = ref(false);
const useForm = Form.useForm;
const props = defineProps({
data: {
@ -24,14 +77,111 @@ const props = defineProps({
const emit = defineEmits(['change']);
const id = props.data.id;
const options = [
{
label: 'Jar',
value: 'jar',
logo: getImage('/jar.png'),
},
{
label: 'Local',
value: 'local',
logo: getImage('/local.png'),
},
];
const formData = ref({
type: 'jar',
name: '',
configuration: {
location: '',
},
description: '',
});
const { resetFields, validate, validateInfos } = useForm(
formData,
reactive({
type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
name: [
{ required: true, message: '请输入名称', trigger: 'blur' },
{ max: 64, message: '最多可输入64个字符' },
],
'configuration.location': [
{ required: true, message: '请输入文件地址', trigger: 'blur' },
],
description: [{ max: 200, message: '最多可输入200个字符' }],
}),
);
const onSubmit = () => {
validate()
.then(async (res) => {
const params = toRaw(formData.value);
loading.value = true;
const response = !id
? await save(params)
: await update({ ...props.data, ...params });
if (response.status === 200) {
message.success('操作成功');
emit('change', true);
}
loading.value = false;
})
.catch((err) => {
loading.value = false;
});
};
const handleChange = (info: UploadChangeParam) => {
fileLoading.value = true;
if (info.file.status === 'done') {
message.success('上传成功!');
const result = info.file.response?.result;
formData.value.configuration.location = result;
fileLoading.value = false;
}
};
const handleOk = () => {
console.log(2);
emit('change', true);
onSubmit();
};
const handleCancel = () => {
console.log(1);
emit('change', false);
};
watch(
() => formData.value.type,
() => {
formData.value.configuration.location = '';
},
);
watch(
() => props.data,
(value) => {
if (value.id) formData.value = value;
},
{ immediate: true, deep: true },
);
</script>
<style lang=""></style>
<style lang="less" scoped>
.form {
.form-radio-button {
width: 148px;
height: 80px;
padding: 0;
img {
width: 100%;
height: 100%;
}
}
.form-upload-button {
margin-top: 10px;
}
.form-submit {
background-color: @primary-color !important;
}
}
</style>

View File

@ -26,7 +26,7 @@
>
<template #img>
<slot name="img">
<img :src="getImage('/device-access.png')" />
<img :src="getImage('/protocol.png')" />
</slot>
</template>
<template #content>
@ -232,7 +232,7 @@ const getActions = (
},
icon: 'EditOutlined',
onClick: () => {
handlEdit(data.id);
handlEdit(data);
},
},
{
@ -241,15 +241,13 @@ const getActions = (
popConfirm: {
title: '确认删除?',
onConfirm: async () => {
console.log(11, data.id);
// const res = await remove(data.id);
// if (res.success) {
// message.success('');
// tableRef.value.reload();
// } else {
// message.error('');
// }
const res = await remove(data.id);
if (res.success) {
message.success('操作成功');
tableRef.value.reload();
} else {
message.error('操作失败!');
}
},
},
icon: 'DeleteOutlined',
@ -259,16 +257,11 @@ const getActions = (
};
const handlAdd = () => {
console.log(11);
current.value = {};
visible.value = true;
};
const handlEdit = (id: string) => {
// router.push(`/link/accessConfig/detail/edit/${id}`);
// router.push({
// path: `/iot/link/accessConfig/detail/${id}`,
// query: { view: false },
// });
console.log(id);
const handlEdit = (data: object) => {
current.value = data;
visible.value = true;
};

View File

@ -1,7 +0,0 @@
import type { BaseItem } from '@/utils/typings';
type ProtocolItem = {
state: number;
type: string;
configuration: Record<string, any>;
} & BaseItem;