Commit 3e57e0f6 by 周田

perf:优化请求次数,修复一些小问题

parent 32693b81
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<div class="container"> <div class="container">
<div class="left"> <div class="left">
<div class="mr-4">协议版本</div> <div class="mr-4">协议版本</div>
<el-select v-model="value"> <el-select v-model="currentVersion">
<!-- TODO: 选择协议版本 --> <!-- TODO: 选择协议版本 -->
<el-option <el-option
v-for="option in options" v-for="option in options"
...@@ -77,14 +77,18 @@ ...@@ -77,14 +77,18 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref,onMounted } from 'vue' import { ref,onMounted } from 'vue'
import { ElMessage, ElMessageBox } from "element-plus"; import { ElMessage, ElMessageBox } from "element-plus";
import { GetProtocolVersion,ProtocolInit } from "@/dao/protocol" import { ProtocolInit } from "@/dao/protocol"
import ProtocolTable from "./ProtocolTable.vue"; import ProtocolTable from "./ProtocolTable.vue";
import type { DeviceProtocolResponse, ProtocolCmdResponse } from './types'; import type { DeviceProtocolResponse, ProtocolCmdResponse } from './types';
import { useProtocolVersionStore } from '@/stores/allProtocolVersion';
const addCmd = ref<boolean>(false) const addCmd = ref<boolean>(false)
const value = ref('') const currentVersion = ref('')
const options = ref<Array>([]) type OptionType = {
const protocol_names = ref<Array>([]) value: string
label: string
}
const options = ref<OptionType[]>([])
const formLabelWidth = '140px' const formLabelWidth = '140px'
type propsType = { type propsType = {
...@@ -118,37 +122,46 @@ const open = () => { ...@@ -118,37 +122,46 @@ const open = () => {
}) })
} }
async function getVersion(){
await GetProtocolVersion()
.then((res) => {
for (const item of res){
protocol_names.value.push(item.protocol_name)
if (item.protocol_name === props.name){
let version_paths = JSON.parse(item.version_paths)
for (const item2 of version_paths){
options.value.push({value: item2.version, label: item2.version})
}
}
}
})
.catch((err) => {
console.log(err);
})
}
function Init(){ function Init(){
ProtocolInit({protocol_name: props.name}) ProtocolInit({protocol_name: props.name})
.then((res) => { .then((_) => {
options.value.push({value: "init", label: "init"}) options.value.push({value: "init", label: "init"})
currentVersion.value = "init"
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
}) })
} }
onMounted(async () => { const store = useProtocolVersionStore()
await getVersion() onMounted(() => {
if (protocol_names.value.indexOf(props.name) === -1){ // 判断是否需要初始化
let flag = true
for (let protocolVersion of store.protocolVersions){
if (protocolVersion.protocol_name === props.name) {
let version_paths = JSON.parse(protocolVersion.version_paths)
for (let version_path of version_paths){
options.value.push({value: version_path.version, label: version_path.version})
}
flag = false
break
}
}
if (!flag) {
for (let item of store.currentVersions){
if (item.protocol_name === props.name) {
currentVersion.value = item.version
}
}
}
// 如果需要初始化
if (flag){
Init() Init()
} }
}) })
......
import axios from "axios" import axios from "axios"
const baseURL = 'http://192.168.0.214:8000/op' // const baseURL = 'http://192.168.0.214:8000/op'
export function GetProtocolVersion(){ export function GetProtocolVersion(){
return axios.get('/api/all_protocol_version/').then( return axios.get('/api/all_protocol_version/').then(
...@@ -11,9 +11,18 @@ export function GetProtocolVersion(){ ...@@ -11,9 +11,18 @@ export function GetProtocolVersion(){
} }
export function ProtocolInit(protocol_name: any){ export function ProtocolInit(protocol_name: any){
return axios.post('api/protocol_version_manage/init/', protocol_name).then( return axios.post('/api/protocol_version_manage/init/', protocol_name).then(
function (response){ function (response){
return response.data return response.data
} }
) )
} }
export function GetCurrentVersion() {
return axios.get('/api/current_versions/').then(
function (response) {
return response.data
}
)
}
import { defineStore } from 'pinia'
import { ref } from 'vue'
type ProtocolVersion = {
protocol_name: string
version_paths: string
}
type CurrentVersion = {
protocol_name: string
version: string
}
export const useProtocolVersionStore = defineStore('protocolVersion', () => {
const protocolVersions = ref<ProtocolVersion[]>([])
const currentVersions = ref<CurrentVersion[]>([])
return {
protocolVersions,
currentVersions
}
})
\ No newline at end of file
...@@ -27,16 +27,30 @@ ...@@ -27,16 +27,30 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue'; import { ref, onMounted } from 'vue';
import KitCollapse from "@/components/protocol/KitCollapse"; import KitCollapse from "@/components/protocol/KitCollapse";
import { GetProtocolVersion, GetCurrentVersion } from '@/dao/protocol';
import { useProtocolVersionStore } from '@/stores/allProtocolVersion';
const isShow = ref<boolean>(false) const isShow = ref<boolean>(false)
const formLabelWidth = '140px' const formLabelWidth = '140px'
const store = useProtocolVersionStore()
onMounted(() => {
GetProtocolVersion()
.then(res => {
store.protocolVersions.push(...res)
})
GetCurrentVersion()
.then(res => {
store.currentVersions = res
})
})
// TODO:添加协议 // TODO:添加协议
const addProtocol = () => { const addProtocol = () => {
isShow.value = false isShow.value = false
console.log('addProtocol') console.log('addProtocol')
} }
</script> </script>
......
from django.urls import re_path from django.urls import re_path
from rest_framework.routers import DefaultRouter
from . import views from . import views
router = DefaultRouter()
router.register(r'current_versions', views.CurrentDevVersionViewSet)
urlpatterns = [ urlpatterns = [
re_path(r'^protocol_version_manage/init/$', views.init), re_path(r'^protocol_version_manage/init/$', views.init),
...@@ -13,3 +16,5 @@ urlpatterns = [ ...@@ -13,3 +16,5 @@ urlpatterns = [
views.raw_file_download), views.raw_file_download),
re_path(r'^all_protocol_version/$', views.AllProtocolVersionViewSet.as_view({'get': 'list'})), re_path(r'^all_protocol_version/$', views.AllProtocolVersionViewSet.as_view({'get': 'list'})),
] ]
urlpatterns += router.urls
...@@ -193,3 +193,8 @@ def raw_file_download(request, protocol_name, version): ...@@ -193,3 +193,8 @@ def raw_file_download(request, protocol_name, version):
response['Content-Type'] = 'multipart/form-data' response['Content-Type'] = 'multipart/form-data'
response['filename'] = file_name response['filename'] = file_name
return response return response
class CurrentDevVersionViewSet(GenericViewSet, ListModelMixin):
queryset = CurrentDevVersion.objects.all()
serializer_class = CurrentDevVersionSerializer
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment