oneProduct.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <div class="ele-body">
  3. <el-card shadow="never">
  4. <ele-split-layout
  5. width="300px"
  6. allow-collapse
  7. :right-style="{ overflow: 'hidden' }"
  8. >
  9. <div class="ele-border-lighter sys-organization-list">
  10. <el-tree
  11. :data="treeList"
  12. :props="defaultProps"
  13. ref="treeRef"
  14. :default-expanded-keys="current && current.id ? [current.id] : []"
  15. :highlight-current="true"
  16. node-key="id"
  17. @node-click="handleNodeClick"
  18. ></el-tree>
  19. </div>
  20. <template v-slot:content>
  21. <div class="ele-border-lighter form-content" v-loading="loading">
  22. <div class="form-wrapper">
  23. <el-form size="mini" label-width="100px" class="ele-form-detail">
  24. <el-row :gutter="15" v-if="current.parentId == 0">
  25. <el-col v-bind="styleResponsive ? { sm: 6 } : { span: 6 }">
  26. <el-form-item label="类型编码:">
  27. <div class="ele-text-secondary">
  28. {{ current.code }}
  29. </div>
  30. </el-form-item>
  31. </el-col>
  32. <el-col v-bind="styleResponsive ? { sm: 6 } : { span: 6 }">
  33. <el-form-item label="类型名称:">
  34. <div class="ele-text-secondary">
  35. {{ current.name }}
  36. </div>
  37. </el-form-item>
  38. </el-col>
  39. <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
  40. <el-form-item label="描述:">
  41. <div class="ele-text-secondary">
  42. {{ current.remark }}
  43. </div>
  44. </el-form-item>
  45. </el-col>
  46. </el-row>
  47. <el-row :gutter="15" v-else>
  48. <el-col v-bind="styleResponsive ? { sm: 6 } : { span: 6 }">
  49. <el-form-item label="节点编码:">
  50. <div class="ele-text-secondary">
  51. {{ current.code }}
  52. </div>
  53. </el-form-item>
  54. </el-col>
  55. <el-col v-bind="styleResponsive ? { sm: 6 } : { span: 6 }">
  56. <el-form-item label="节点名称:">
  57. <div class="ele-text-secondary">
  58. {{ current.name }}
  59. </div>
  60. </el-form-item>
  61. </el-col>
  62. <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
  63. <el-form-item label="层级全览:">
  64. <div class="ele-text-secondary">
  65. {{ currentData.categoryLevelPath }}
  66. </div>
  67. </el-form-item>
  68. </el-col>
  69. </el-row>
  70. </el-form>
  71. </div>
  72. <IndexSearch @search="reload" />
  73. <IndexData
  74. ref="listData"
  75. v-if="current"
  76. :current-id="current.id"
  77. :data="current"
  78. :rootTreeId="rootTreeId"
  79. :currentData="currentData"
  80. :rootId="rootId"
  81. :oneProduct="true"
  82. >
  83. </IndexData>
  84. </div>
  85. </template>
  86. </ele-split-layout>
  87. </el-card>
  88. </div>
  89. </template>
  90. <script>
  91. import IndexData from './components/index-data.vue';
  92. import IndexSearch from './components/index-search.vue';
  93. import dictMixins from '@/mixins/dictMixins';
  94. import { getTreeByPid } from '@/api/classifyManage';
  95. import { getInfoById } from '@/api/classifyManage';
  96. import { deepClone } from '@/utils';
  97. export default {
  98. components: {
  99. IndexData,
  100. IndexSearch
  101. },
  102. mixins: [dictMixins],
  103. data() {
  104. return {
  105. loading: false,
  106. current: {},
  107. curNode: {},
  108. currentData: {},
  109. treeList: [],
  110. treeLoading: false,
  111. formData: {},
  112. rootTreeId: null,
  113. defaultProps: {
  114. children: 'children',
  115. label: 'name'
  116. }
  117. };
  118. },
  119. computed: {
  120. // 是否开启响应式布局
  121. styleResponsive() {
  122. return this.$store.state.theme.styleResponsive;
  123. },
  124. rootId() {
  125. return this.$route.query.id;
  126. },
  127. fullName() {
  128. let str = '';
  129. let codeStr = '';
  130. let node = this.curNode;
  131. while (node?.data?.name) {
  132. str = node.data.name + '-' + str;
  133. codeStr = node.data.code + '-' + codeStr;
  134. node = node?.parent;
  135. }
  136. return str.substring(str, str.length - 1);
  137. }
  138. },
  139. created() {
  140. this.getTreeData();
  141. },
  142. methods: {
  143. async getTreeData() {
  144. try {
  145. this.treeLoading = true;
  146. const res = await getTreeByPid(9);
  147. this.treeLoading = false;
  148. if (res?.code === '0') {
  149. this.treeList = res.data;
  150. if (!this.$route.query.categoryLevelId) {
  151. this.$nextTick(() => {
  152. //默认高亮第一级树节点;
  153. if (this.treeList[0]) {
  154. this.rootTreeId = this.treeList[0].id;
  155. this.getDetail(this.treeList[0].id);
  156. console.log(this.treeList[0].id, 'rootTreeId');
  157. }
  158. });
  159. } else {
  160. this.getDetail(this.$route.query.categoryLevelId);
  161. this.$nextTick(() => {
  162. this.$refs.treeRef.setCurrentKey(
  163. this.$route.query.categoryLevelId
  164. );
  165. });
  166. const objByid = this.findNodeById(
  167. this.treeList,
  168. this.$route.query.categoryLevelId
  169. );
  170. console.log(objByid, '当前选择的产品');
  171. this.publicfun(objByid);
  172. }
  173. return this.treeList;
  174. }
  175. } catch (error) {}
  176. this.treeLoading = false;
  177. },
  178. async getDetail(id) {
  179. this.loading = true;
  180. const res = await getInfoById(id);
  181. this.loading = false;
  182. if (res.code == '0') {
  183. this.current = res.data;
  184. this.$nextTick(() => {
  185. this.$refs.treeRef.setCurrentKey(this.current.id);
  186. });
  187. }
  188. },
  189. findNodeById(tree, targetId) {
  190. function traverse(nodes) {
  191. for (let node of nodes) {
  192. if (node.id === targetId) {
  193. return node;
  194. } else if (node.children && node.children.length > 0) {
  195. const foundNode = traverse(node.children);
  196. if (foundNode) {
  197. return foundNode;
  198. }
  199. }
  200. }
  201. return null;
  202. }
  203. return traverse(tree);
  204. },
  205. handleNodeClick(data, node) {
  206. this.$route.query.categoryLevelId = '';
  207. this.curNode = node;
  208. this.publicfun(data);
  209. this.getDetail(data.id);
  210. },
  211. publicfun(data) {
  212. this.pathList = this.findParent([], data, this.treeList);
  213. this.rootTreeId = null;
  214. if (this.pathList.length == 0) {
  215. this.rootTreeId = data.id;
  216. } else {
  217. this.rootTreeId =
  218. this.pathList[this.pathList.length - 1] &&
  219. this.pathList[this.pathList.length - 1].id;
  220. }
  221. let ruleCode = null;
  222. if (this.pathList.length == 0) {
  223. ruleCode = data.ruleCode;
  224. } else {
  225. ruleCode =
  226. this.pathList[this.pathList.length - 1] &&
  227. this.pathList[this.pathList.length - 1].ruleCode;
  228. }
  229. this.pathList.unshift(data);
  230. const PathInfo = {
  231. categoryLevelPath: '',
  232. categoryLevelPathId: [],
  233. // rootCategoryLevelId: null,
  234. categoryLevelId: '',
  235. categoryLevelName: '',
  236. ruleCode: ''
  237. };
  238. const pathName = [];
  239. this.pathList.map((item) => {
  240. pathName.unshift(item.name);
  241. PathInfo.categoryLevelPathId.unshift(item.id);
  242. // PathInfo.rootCategoryLevelId = item.rootCategoryLevelId;
  243. });
  244. PathInfo.categoryLevelPath = pathName.join('-');
  245. PathInfo.categoryLevelPathId = PathInfo.categoryLevelPathId.join(',');
  246. PathInfo.categoryLevelId = data.id;
  247. PathInfo.categoryLevelName = data.name;
  248. PathInfo.ruleCode = ruleCode;
  249. this.currentData = deepClone(PathInfo);
  250. console.log(this.currentData, '选中的产品分类');
  251. },
  252. /* 刷新表格 */
  253. reload(where) {
  254. this.$refs.listData.reload(where);
  255. },
  256. // parents:用于返回的数组,childNode:要查询的节点,treeList:json树形数据
  257. findParent(parents, childNode, treeList) {
  258. for (let i = 0; i < treeList.length; i++) {
  259. // 父节点查询条件
  260. if (treeList[i].id === childNode.parentId) {
  261. // 如果找到结果,保存当前节点
  262. parents.push(treeList[i]);
  263. // 用当前节点再去原数据查找当前节点的父节点
  264. this.findParent(parents, treeList[i], this.treeList);
  265. break;
  266. } else {
  267. if (treeList[i].children instanceof Array) {
  268. // 没找到,遍历该节点的子节点
  269. this.findParent(parents, childNode, treeList[i].children);
  270. }
  271. }
  272. }
  273. return parents;
  274. }
  275. }
  276. };
  277. </script>
  278. <style lang="scss" scoped>
  279. .sys-organization-list {
  280. height: calc(100vh - 165px);
  281. box-sizing: border-box;
  282. border-width: 1px;
  283. border-style: solid;
  284. overflow: auto;
  285. }
  286. .sys-organization-list :deep(.el-tree-node__content) {
  287. height: 30px;
  288. & > .el-tree-node__expand-icon {
  289. margin-left: 10px;
  290. }
  291. }
  292. .form-content {
  293. height: 50px;
  294. border: 1px solid;
  295. box-sizing: border-box;
  296. }
  297. .form-wrapper {
  298. padding: 12px 24px;
  299. }
  300. </style>