oneProduct.vue 10 KB

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