Skip to content

Commit

Permalink
代码分离,功能优化 (#204)
Browse files Browse the repository at this point in the history
* feat: 代码分离,功能优化

1. 代码迁移到 ts.
2. 分离 axios 部分代码.
3. 增加 pinia 支持,全局状态代码迁移到相对应的 store.
4. 代码格式优化, 用 === 代替 ==.
5. 代码声明更改,用 const 代替 var 声明.
6. Header 使用 Router 导航.
7. v-for 增加 key.

* fix[fe]: 移除过时的 prop 引用

* fix[fe]: 移除过时的 prop 引用

* fix[fe]: 修复 logo 上面有横线的问题

* fix[fe]: 修复 logo 上面有横线的问题

---------

Co-authored-by: zhe28 <huangze28@foxmail.com>
  • Loading branch information
Zhe28 and zhe28 authored Sep 23, 2024
1 parent dbb671d commit ad0167f
Show file tree
Hide file tree
Showing 20 changed files with 1,676 additions and 1,674 deletions.
4 changes: 3 additions & 1 deletion fe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"element-plus": "^2.3.6",
"pinia": "^2.0.36",
"vue": "^3.3.2",
"vue-icons-plus": "^0.1.6",
"vue-router": "^4.2.0"
},
"devDependencies": {
Expand All @@ -23,6 +24,7 @@
"eslint-plugin-vue": "^9.11.0",
"unplugin-auto-import": "^0.16.4",
"unplugin-vue-components": "^0.25.0",
"vite": "^4.3.5"
"vite": "^4.3.5",
"vue-tsc": "^2.1.6"
}
}
22 changes: 11 additions & 11 deletions fe/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<script setup>
import { RouterView } from 'vue-router'
import {RouterView, useRoute} from 'vue-router'
import HomeHeader from '@/components/HomeHeader.vue'
import HomeAside from '@/components/HomeAside.vue';
import { watch, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {ref, watch} from 'vue'
const route = useRoute()
const pageName = ref(route.name)
watch(
() => route.fullPath,
(n, o) => {
pageName.value = route.name
}
() => route.fullPath,
() => {
pageName.value = route.name
}
)
</script>

<template>
<div id="main">
<HomeHeader />
<HomeHeader/>
<div id="content">
<div id="aside" v-if="pageName != 'login' && pageName != 'setup'">
<HomeAside />
<div id="aside" v-if="pageName !== 'login' && pageName !== 'setup'">
<HomeAside/>
</div>
<div id="body">
<RouterView />
<RouterView/>
</div>
</div>
</div>
Expand Down
185 changes: 105 additions & 80 deletions fe/src/components/GroupSettings.vue
Original file line number Diff line number Diff line change
@@ -1,98 +1,123 @@
<template>
<div id="main">
<el-tree :expand-on-click-node="false" :data="data" :props="defaultProps" :defaultExpandAll="true" :class="node">
<template #default="{ node, data }">
<div>
<span v-if="data.id != -1"> {{ data.label }}</span>
<el-input v-if="data.id == -1" v-model="data.label" @blur="onInputBlur(data)"></el-input>
<el-button v-if="data.id != 0" @click="del(node, data)" size="small" type="danger" circle> -
</el-button>
<el-button v-if="data.id != 0" @click="add(data)" size="small" type="primary" circle> + </el-button>
</div>
</template>
</el-tree>
<div id="main">
<el-tree
:expand-on-click-node="false"
:data="data"
:defaultExpandAll="true"
>
<template #default="{ node, data }">
<div>
<span v-if="data.id !== -1"> {{ data.label }}</span>
<el-input
v-if="data.id === -1"
v-model="data.label"
@blur="onInputBlur(data)"
></el-input>
<el-button
v-if="data.id !== 0"
@click="del(node, data)"
size="small"
type="danger"
circle
>
-
</el-button>
<el-button
v-if="data.id !== 0"
@click="add(data)"
size="small"
type="primary"
circle
>
+
</el-button>
</div>
</template>
</el-tree>

<el-button @click="addRoot">{{ lang.add_group }}</el-button>
</div>
<el-button @click="addRoot">{{ lang.add_group }}</el-button>
</div>
</template>

<script setup>
import { reactive, ref, getCurrentInstance } from 'vue'
import lang from '../i18n/i18n';
import { reactive } from "vue";
import lang from "../i18n/i18n";
import { http } from "@/utils/axios";
import { ElMessage } from "element-plus";
const data = reactive([])
const app = getCurrentInstance()
const $http = app.appContext.config.globalProperties.$http
const data = reactive([]);
$http.get('/api/group').then(res => {
data.push(...res.data)
})
http.get("/api/group").then((res) => {
data.push(...res.data);
});
const del = function (node, data) {
if (data.id != -1) {
this.$axios.post("/api/group/del", { "id": data.id }).then(res => {
if (res.errorNo != 0) {
ElMessage({
message: res.errorMsg,
type: 'error',
})
} else {
const pc = node.parent.childNodes
for (let i = 0; i < pc.length; i++) {
if (pc[i].id == node.id) {
pc.splice(i, 1)
return
}
}
}
})
} else {
const pc = node.parent.childNodes
if (data.id !== -1) {
this.$axios.post("/api/group/del", { id: data.id }).then((res) => {
if (res.errorNo !== 0) {
ElMessage({
message: res.errorMsg,
type: "error",
});
} else {
const pc = node.parent.childNodes;
for (let i = 0; i < pc.length; i++) {
if (pc[i].id == node.id) {
pc.splice(i, 1)
return
}
if (pc[i].id === node.id) {
pc.splice(i, 1);
return;
}
}
}
});
} else {
const pc = node.parent.childNodes;
for (let i = 0; i < pc.length; i++) {
if (pc[i].id === node.id) {
pc.splice(i, 1);
return;
}
}
}
}
};
const add = function (item) {
if (item.children == null) {
item.children = []
}
item.children.push({
"children": [],
"label": "",
"id": "-1",
"parent_id": item.id
})
}
if (item.children == null) {
item.children = [];
}
item.children.push({
children: [],
label: "",
id: "-1",
parent_id: item.id,
});
};
const addRoot = function () {
data.push({
"children": [],
"label": "",
"id": "-1",
"parent_id": 0
})
}
data.push({
children: [],
label: "",
id: "-1",
parent_id: 0,
});
};
const onInputBlur = function (item) {
if (item.label != "") {
this.$axios.post("/api/group/add", { "name": item.label, "parent_id": item.parent_id }).then(res => {
if (res.errorNo != 0) {
ElMessage({
message: res.errorMsg,
type: 'error',
})
} else {
this.$axios.get('/api/group').then(res => {
data.splice(0, data.length)
data.push(...res.data)
})
}
})
}
}
</script>
if (item.label !== "") {
http
.post("/api/group/add", { name: item.label, parent_id: item.parent_id })
.then((res) => {
if (res.errorNo !== 0) {
ElMessage({
message: res.errorMsg,
type: "error",
});
} else {
this.$axios.get("/api/group").then((res) => {
data.splice(0, data.length);
data.push(...res.data);
});
}
});
}
};
</script>
59 changes: 25 additions & 34 deletions fe/src/components/HomeAside.vue
Original file line number Diff line number Diff line change
@@ -1,58 +1,49 @@
<template>
<div id="main">
<input id="search" :placeholder="lang.search">
<el-tree :data="data" :props="defaultProps" :defaultExpandAll="true" @node-click="handleNodeClick" :class="node" />
<input id="search" :placeholder="lang.search" />
<el-tree
:data="data"
:defaultExpandAll="true"
@node-click="handleNodeClick"
/>
</div>
</template>


<script setup>
import { useRouter } from 'vue-router'
import { reactive, ref } from 'vue'
import useGroupStore from '../stores/group'
import lang from '../i18n/i18n';
import { getCurrentInstance } from 'vue'
const app = getCurrentInstance()
const $http = app.appContext.config.globalProperties.$http
const groupStore = useGroupStore()
const router = useRouter()
const data = ref([])
$http.get('/api/group').then(res => {
data.value = res.data
})
import { useRouter } from "vue-router";
import { ref } from "vue";
import useGroupStore from "../stores/group";
import lang from "../i18n/i18n";
import { http } from "@/utils/axios";
const groupStore = useGroupStore();
const router = useRouter();
const data = ref([]);
http.get("/api/group").then((res) => {
if (res.data) data.value = res.data;
});
const handleNodeClick = function (data) {
if (data.tag != null) {
groupStore.name = data.label
groupStore.tag = data.tag
groupStore.name = data.label;
groupStore.tag = data.tag;
router.push({
name: "list",
})
});
}
}
};
</script>


<style scoped>
#main {
width: 243px;
background-color: #F1F1F1;
background-color: #f1f1f1;
height: 100%;
}
#search {
background-color: #D6E7F7;
background-color: #d6e7f7;
width: 100%;
height: 40px;
padding-left: 10px;
Expand All @@ -62,10 +53,10 @@ const handleNodeClick = function (data) {
}
.el-tree {
background-color: #F1F1F1;
background-color: #f1f1f1;
}
.add_group{
.add_group {
font-size: 14px;
text-align: left;
padding-left: 15px;
Expand Down
Loading

0 comments on commit ad0167f

Please sign in to comment.