DeviceType.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <div class="DeviceType">
  3. <header id="header">
  4. <el-button @click="add_catalog" type="primary">添加目录</el-button>
  5. </header>
  6. <hr style="border-top: 2px solid #eee;margin: 10px 0;" />
  7. <section id="section">
  8. <el-tree
  9. :data="dataTree"
  10. node-key="id"
  11. default-expand-all
  12. :expand-on-click-node="false"
  13. :props="defaultProps"
  14. >
  15. <span class="custom-tree-node" slot-scope="{ node, data }">
  16. <span>{{ node.label }}</span>
  17. <span class="right">
  18. <el-tooltip effect="dark" content="添加子所属分类" placement="top-start">
  19. <i class="el-icon-folder-add icon" @click="() => add(data)"></i>
  20. </el-tooltip>
  21. <el-tooltip effect="dark" content="编辑此分类" placement="top-start">
  22. <i class="el-icon-edit icon" @click="() => edit(data)" alt="编辑"></i>
  23. </el-tooltip>
  24. <el-tooltip effect="dark" content="删除此分类" placement="top-start">
  25. <i class="el-icon-delete icon" @click="() => del(data)"></i>
  26. </el-tooltip>
  27. </span>
  28. </span>
  29. </el-tree>
  30. </section>
  31. <el-dialog :title="title.title" :visible.sync="showDialog">
  32. <el-row type="flex">
  33. <el-col :span="14">
  34. <el-form ref="form" :model="form" :rules="rules" label-width="140px">
  35. <el-form-item :label="title.nodeName" prop="nodeName">
  36. <el-input v-model="form.nodeName"></el-input>
  37. </el-form-item>
  38. <el-form-item label="配置信息:" prop="meta">
  39. <el-input
  40. v-model="form.meta"
  41. type="textarea"
  42. placeholder="请输入内容,JSON格式"
  43. :autosize="{ minRows: 4 }"
  44. ></el-input>
  45. </el-form-item>
  46. <el-form-item :label="title.code" prop="code">
  47. <el-input v-model="form.code" placeholder="要为这个子节点起个名字吗?"></el-input>
  48. </el-form-item>
  49. <el-form-item>
  50. <el-button @click="showDialog=false">取 消</el-button>
  51. <el-button type="primary" @click="submitForm('form')">保 存</el-button>
  52. </el-form-item>
  53. </el-form>
  54. </el-col>
  55. </el-row>
  56. </el-dialog>
  57. </div>
  58. </template>
  59. <script>
  60. import { mapActions } from "vuex";
  61. // 表单验证规则
  62. const rules = {
  63. nodeName: [
  64. { required: true, message: "请输入节点名称", trigger: "blur" }
  65. ],
  66. meta: [{ required: true, message: "请输入配置信息", trigger: "blur" }]
  67. };
  68. // tree组件 配置
  69. const defaultProps = { children: "childs", label: "nodeName" };
  70. const formDataInit = {
  71. nodeName: "",
  72. meta: "",
  73. code: ""
  74. }
  75. export default {
  76. name: "DeviceType",
  77. data() {
  78. return {
  79. options: [],
  80. id: "",
  81. dataTree: [],
  82. defaultProps,
  83. showDialog: false,
  84. form: formDataInit,
  85. /* 是添加还是编辑状态 { 1:节点后的添加, 2:节点后的编辑, 6:添加目录} */
  86. dialogStatus: null,
  87. addOrEditThis: {}, // 保存被点击的 this
  88. title: {
  89. title: "编辑/添加",
  90. nodeName: "节点名称",
  91. code: "节点代号"
  92. },
  93. rules
  94. };
  95. },
  96. created() {
  97. // 获取区域管理 树
  98. this.getTreeByCode("device-type");
  99. },
  100. methods: {
  101. ...mapActions(["fetch"]),
  102. submitForm(formName) {
  103. this.$refs[formName].validate(valid => {
  104. if (valid) {
  105. console.log(this.dyForm);
  106. switch (this.dialogStatus) {
  107. case 1 /* 添加 */:
  108. this.form.parentId = this.addOrEditThis.id;
  109. this.reqSave("/publics/treenode/addNode");
  110. break;
  111. case 2 /* 编辑 */:
  112. this.reqSave("/publics/treenode/updateNode");
  113. break;
  114. case 6 /* 编辑 分类*/:
  115. this.form.parentId = this.id;
  116. this.reqSave("/publics/treenode/addNode");
  117. break;
  118. default:
  119. this.$message.error(
  120. "操作错误请刷新!"
  121. );
  122. }
  123. } else {
  124. return false;
  125. }
  126. });
  127. },
  128. // 通过code获取树
  129. getTreeByCode(code) {
  130. this.fetch({
  131. api: "/publics/treenode/getTreeByCode",
  132. method: "POST",
  133. data: { code }, //目前只有一个组织,其他参数调整钟
  134. success: res => {
  135. this.id = res.id;
  136. this.getSubNodeList(res.id);
  137. },
  138. fail: err => {
  139. console.log(err);
  140. if (err.errCode == "request_not_authorize") {
  141. this.$message({
  142. message: "请重新登录",
  143. type: "warning"
  144. });
  145. }
  146. }
  147. });
  148. },
  149. // 保存事件,包括新增字节点和编辑当前节点
  150. reqSave(api) {
  151. if (!this.form.code) delete this.form.code;
  152. this.fetch({
  153. api,
  154. method: "POST",
  155. data: this.form,
  156. success: res => {
  157. this.getSubNodeList(this.id);
  158. this.$message.success("编辑节点成功!");
  159. // this.showDialog = false;
  160. },
  161. fail: err => {
  162. if (err.errMsg) this.$message.error(err.errMsg);
  163. else this.$message.error("服务器发生异常");
  164. }
  165. });
  166. },
  167. // 请求 拿到树列表
  168. getSubNodeList(parentId) {
  169. this.fetch({
  170. api: "/publics/treenode/listNodeByParent",
  171. method: "POST",
  172. data: {
  173. parentId,
  174. hasSub: true
  175. },
  176. success: res => {
  177. this.dataTree = res;
  178. },
  179. fail: err => {
  180. if (err.errMsg) this.$message.error(err.errMsg);
  181. else this.$message.error("服务器发生异常");
  182. }
  183. });
  184. },
  185. // 添加目录
  186. add_catalog() {
  187. this.showDialog = true;
  188. this.dialogStatus = 6;
  189. this.title = {
  190. title: "添加目录",
  191. nodeName: "节点名称",
  192. code: "节点代号"
  193. };
  194. },
  195. // 节点后的添加
  196. add(data) {
  197. console.log(data);
  198. this.showDialog = true;
  199. this.dialogStatus = 1;
  200. this.title = {
  201. title: data.nodeName + ":添加目录",
  202. nodeName: "子节点名称",
  203. code: "子节点代号"
  204. };
  205. this.form.parentId = data.id;
  206. this.addOrEditThis = data; // 保存当前点击者状态
  207. },
  208. // 节点后的编辑
  209. edit(data) {
  210. console.log(data);
  211. this.showDialog = true;
  212. this.dialogStatus = 2;
  213. this.title = {
  214. title: data.nodeName + ":编辑节点",
  215. nodeName: "子节点名称",
  216. code: "子节点代号"
  217. };
  218. this.addOrEditThis = data; // 保存当前点击者状态 此处只为 dialog 的title显示用
  219. delete this.form.parentId;
  220. this.form = {
  221. id: data.id,
  222. nodeName: data.nodeName,
  223. meta: JSON.stringify(data.meta, null, 2),
  224. code: data.code
  225. };
  226. },
  227. // 节点后的删除
  228. del(data) {
  229. this.fetch({
  230. api: "/publics/treenode/deleteNode",
  231. method: "POST",
  232. data: { id: data.id },
  233. success: res => {
  234. this.getSubNodeList(this.id);
  235. console.log(res);
  236. this.$message.success("删除成功!");
  237. },
  238. fail: err => {
  239. console.log(err);
  240. if (err.errMsg) this.$message.error(err.errMsg);
  241. else this.$message.error("服务器发生异常");
  242. }
  243. });
  244. }
  245. }
  246. };
  247. </script>
  248. <style lang="scss" scoped>
  249. .DeviceType {
  250. #header {
  251. margin-bottom: 15px;
  252. }
  253. #section {
  254. .hint {
  255. color: rgb(240, 34, 34);
  256. }
  257. }
  258. }
  259. .custom-tree-node {
  260. flex: 1;
  261. display: flex;
  262. align-items: center;
  263. font-size: 14px;
  264. padding-right: 8px;
  265. .right {
  266. margin-left: 40px;
  267. .icon {
  268. margin-right: 10px;
  269. }
  270. }
  271. }
  272. </style>