From 51ad1f2614bc1f976aad98254649bb3c207332ee Mon Sep 17 00:00:00 2001 From: 闫增涛 <1829501689@qq.com> Date: 星期三, 11 九月 2024 16:50:35 +0800 Subject: [PATCH] 转换树结构方法优化 --- packageBookService/pages/bookServices/detail/buyResource/index.js | 213 +----------------------------- packageBookService/pages/bookServices/detail/components/tree/index.js | 154 ++++++++++----------- packageBookService/pages/bookServices/detail/index.js | 32 +--- 3 files changed, 91 insertions(+), 308 deletions(-) diff --git a/packageBookService/pages/bookServices/detail/buyResource/index.js b/packageBookService/pages/bookServices/detail/buyResource/index.js index a074c5d..62d3e99 100644 --- a/packageBookService/pages/bookServices/detail/buyResource/index.js +++ b/packageBookService/pages/bookServices/detail/buyResource/index.js @@ -125,9 +125,9 @@ }, // 鎵佸钩鍖栨暟鎹浆鎹ree - getTreeList(rootList, pathLength, newArr, path) { + getTreeList(rootList, newArr, path) { for (const item of rootList) { - if ((pathLength - item.productLinkPath.length >= 0 && pathLength - item.productLinkPath.length <= 3) && item.productLinkPath.includes(path)) { + if (path == item.productLinkPath.replace('\\' + item.id, '')) { if (item.sysType == 'CmsItem') { if (item.selectType == 'webpage') { item.disabled = true @@ -140,7 +140,6 @@ if (item.fileMap[item.file].protectType == 'Private') item.disabled = true } } - // newArr.push(item) // 鍦ㄦ彃鍏ヨ繃绋嬩腑瀵规暟鎹繘琛屾帓搴� 骞朵笖鍘婚櫎宸茬粡棰嗗彇鍜岃喘涔扮殑 if (item.sysType == 'CmsFolder') { newArr = this.insertAndSortObjectsByProductLinkPath(newArr, item) @@ -152,7 +151,7 @@ //缁欐暟缁勯噷闈㈠啀娣诲姞涓�涓猚hildren鐨勭┖鏁扮粍 for (const i of newArr) { i.children = [] - this.getTreeList(rootList, pathLength + 6, i.children, i.productLinkPath) + this.getTreeList(rootList, i.children, i.productLinkPath) if (i.children.length == 0) { delete i.children } @@ -179,8 +178,7 @@ // 鎺掑簭鏁扮粍 鎸夌収productLinkPath insertAndSortObjectsByProductLinkPath(array, newObj) { // 鏌ユ壘鏂板璞″簲璇ユ彃鍏ョ殑浣嶇疆 - let insertIndex = array.findIndex(obj => Number(newObj.productLinkPath.substring(newObj.productLinkPath.length - 6, newObj.productLinkPath.length)) < Number(obj.productLinkPath.substring(obj.productLinkPath.length - 6, obj.productLinkPath.length))); - + let insertIndex = array.findIndex(obj => newObj.id < obj.id); // 濡傛灉娌℃湁鎵惧埌鍚堥�傜殑浣嶇疆锛屽垯鏀惧湪鏁扮粍鏈熬 if (insertIndex === -1) { insertIndex = array.length; @@ -190,16 +188,10 @@ // 娴嬭瘯6 姝e紡5 // 瀵规暟缁勮繘琛屾帓搴� array.sort((a, b) => { - if (Number(a.productLinkPath.substring(a.productLinkPath.length - 5, a.productLinkPath.length)) < Number(b.productLinkPath.substring(b.productLinkPath.length - 5, b.productLinkPath.length))) { - return -1; - } - if (Number(a.productLinkPath.substring(a.productLinkPath.length - 5, a.productLinkPath.length)) > Number(b.productLinkPath.substring(b.productLinkPath.length - 5, b.productLinkPath.length))) { - return 1; - } - // a must be equal to b + if (a.id < b.id) return -1; + if (a.id > b.id) return 1; return 0; }); - // 杩斿洖鏇存柊鍚庣殑鏁扮粍 return array; }, @@ -272,23 +264,12 @@ if (type.value == item.resourcesClassification) item.resourceClass = type.name; }); - if (this.data.tabValue == "jsek_teachingResources") {} else if (this.data.tabValue == "jsek_cloudLearning") { - item.isbuy = this.isShowNeedBuy(item); - item.isShopCar = this.isShoppingCart(item); - } + item.isbuy = this.isShowNeedBuy(item); + item.isShopCar = this.isShoppingCart(item); } }); // 鏄剧ず 闇�瑕佽喘涔扮殑鍜岄渶瑕佸厤璐归鍙栫殑 - // const arr = res.datas.cmsDatas[0].datas.filter(item => this.resourceIsBuy(item)) - // if (!arr.length) { - // return this.setData({ - // loading: false, - // haider: true, - // noData: true - // }) - // } - const num = query.cmsPath.length + 6 - this.getTreeList(res.datas.cmsDatas[0].datas, num, list, '\\') + this.getTreeList(res.datas.cmsDatas[0].datas, list, query.cmsPath) list = this.ensureTreeConsistency(list) list = this.changeResourceChildren(list) console.log('淇敼鍚庝簯', list); @@ -322,156 +303,6 @@ }) }, - // 鑾峰彇鏁欏璧勬簮 浜戝涔� 浜戞祴璇� - getResourceData(type) { - this.setData({ - loading: true, - noResources: false, - }); - let query = { - path: "*", - queryType: "*", - productId: this.data.bookId, - cmsPath: type.productLinkPath, - itemFields: { - SysType: "CmsFolder", - // 璧勬簮绫诲瀷锛岃瘯璇绘枃浠讹紝鏄惁鍏佽涓嬭浇绛夊弬鏁� - selectType: [], - freeFile: [], - file: [], - resourcesClassification: [], - isDownload: [], - jsek_resourceBrief: [], - jsek_link: [], - jsek_questionBank: [], - }, - pading: { - start: 0, - size: 999, - }, - }; - app.MG.store - .getProductDetail(query) - .then(async (res) => { - if (!res.datas.cmsDatas[0].datas.length) { - return this.setData({ - noResources: true, - loading: false, - }); - } - //鏁欏璧勬簮 浜戝涔� - if ( - type.refCode == "jsek_teachingResources" || - type.refCode == "jsek_cloudLearning" - ) { - if (res.datas.cmsDatas[0].datas.length > 0) { - res.datas.cmsDatas[0].datas.forEach((item) => { - (item.checked = false), (item.checkAll = true); - }); - const list = await this.getAllResource(res.datas.cmsDatas[0].datas); - if (type.refCode == "jsek_teachingResources") { - this.setData({ - teach: list, - loading: false, - }); - this.findChildIds(this.data.teach, (this.data.openTeachids = [])); - } else if (type.refCode == "jsek_cloudLearning") { - const result = []; - res.datas.cmsDatas[0].datas.forEach((item) => { - item.checked = false; - }); - this.findChildIds(list, result); - this.setData({ - learn: list, - loading: false, - openLearnids: result, - }); - } - console.log(list, '浜戝缈�'); - } else { - // 鏃犳暟鎹� - } - } else if (type.refCode == "jsek_questionBank") { - // const list = await this.getAllResource(res.datas.cmsDatas[0].datas) - this.setData({ - test: res.datas.cmsDatas[0].datas, - loading: false, - }); - } else { - console.log("鍏朵粬"); - } - }) - .catch((e) => { - console.log(e); - }); - }, - // 鑾峰彇tag涓嬫墍鏈夎祫婧� - async getAllResource(data) { - if (!data.length) return false; - for (let i = 0; i < data.length; i++) { - let item = data[i]; - if (item.sysType == "CmsFolder" && item.childrenCount > 0) { - item.children = []; - item.children = await this.getFolderItem(item.productLinkPath); - await this.getAllResource(item.children); - } - } - return data; - }, - // 鑾峰彇璧勬簮鎺ュ彛 - async getFolderItem(path) { - let query = { - path: "*", - queryType: "*", - productId: this.data.bookId, - cmsPath: path, - itemFields: { - SysType: "CmsFolder", - // 璧勬簮绫诲瀷锛岃瘯璇绘枃浠讹紝鏄惁鍏佽涓嬭浇绛夊弬鏁� - selectType: [], - freeFile: [], - file: [], - protectedFile: [], - resourcesClassification: [], - isDownload: [], - jsek_resourceBrief: [], - jsek_link: [], - accessType: [], - learnSelectType: [], - }, - pading: { - start: 0, - size: 999, - }, - }; - const data = await app.MG.store.getProductDetail(query); - data.datas.cmsDatas[0].datas.forEach((item) => { - this.data.resourceClassList.forEach((type) => { - if (type.value == item.resourcesClassification) - item.resourceClass = type.name; - }); - // 缃戦〉 涓嶈兘涓嬭浇 绉佹湁鏂囦欢 - if (item.sysType == "CmsItem") { - if (item.selectType == "webpage") { - item.disabled = true; - } else { - if (item.isDownload != 1) { - item.disabled = true; - } - } - if (item.file) { - if (item.fileMap[item.file].protectType == "Private") - item.disabled = true; - } - } - }); - data.datas.cmsDatas[0].datas.forEach((item) => { - item.checked = false; - item.isbuy = this.isShowNeedBuy(item); - item.isShopCar = this.isShoppingCart(item); - }); - return data.datas.cmsDatas[0].datas; - }, async getBookInfo(id) { this.setData({ "mockData'.id": 0, @@ -515,38 +346,12 @@ bookDetail: res.datas, buyIdList: res.datas.purchasedSaleMethodIdList, }); - // this.getResourceData({ - // productLinkPath: this.data.productLinkPath, - // refCode: this.data.refCode, - // }); - // this.getResourceCode( - // res.datas.id, - // res.datas.rootCmsItemId - // ) this.getResourceDataList({ productLinkPath: this.data.productLinkPath, refCode: this.data.refCode, }) }); - }, - getResourceCode(bookId, cmsId) { - let query = { - path: '*', - queryType: '*', - productId: bookId, - cmsPath: cmsId, - itemFields: { - SysType: 'Cms' - }, - pading: { - start: 99, - size: 0 - } - } - app.MG.store.getProductDetail(query).then(res => { - console.log('鍥句功璧勬簮', res.datas.cmsDatas[0].datas); - }) }, // 鑾峰彇宸茶喘涔板晢鍝� getShoppingCartProductGet() { diff --git a/packageBookService/pages/bookServices/detail/components/tree/index.js b/packageBookService/pages/bookServices/detail/components/tree/index.js index fd3b476..4194ed3 100644 --- a/packageBookService/pages/bookServices/detail/components/tree/index.js +++ b/packageBookService/pages/bookServices/detail/components/tree/index.js @@ -137,88 +137,82 @@ goPlayer(e) { // 妫�鏌ョ櫥褰曠姸鎬� const token = wx.getStorageSync(app.config.tokenKey); - if (token) { - const item = e.currentTarget.dataset.item; - const parent = e.currentTarget.dataset.parent; - const parentProductLinkPath = parent ? - parent.productLinkPath : - wx.getStorageSync("teachResourcesPath"); - const parentName = parent ? parent.name : "鏁欏璧勬簮"; - if (this.properties.isShowCheck && this.resourceIsBuy(item)) { - return false; - } - - if ( - this.properties.tab == "jsek_cloudLearning" && - this.resourceIsBuy(item) - ) { - return wx.showToast({ - icon: "error", - title: item.saleMethod[0].Price > 0 ? - "璇峰厛璐拱璇ヨ祫婧�" : "璇峰厛鐐瑰嚮棰嗗彇鏌ョ湅鎸夐挳", - }); - } - - - if (this.properties.tab == "jsek_teachingResources" && (item.file || item.freeFile)) { - let file = item.fileMap[item.file] ? item.fileMap[item.file] : item.fileMap[item.freeFile] - if (file && file.protectType !== "Public") { - return wx.showToast({ - icon: "error", - title: "璧勬簮涓嶅彲鐢紝璇疯仈绯荤鐞嗗憳", - }); - } - } else if ((this.properties.tab == "jsek_teachingResources" || this.properties.tab == "jsek_cloudLearning") && (!item.file && !item.freeFile && !item.protectedFile)) { - return wx.showToast({ - icon: 'error', - title: '鏆傛棤璧勬簮', - }) - } - - if (item.selectType == 'zip') { - if (this.properties.applyState == 'Normal') { - return wx.showToast({ - icon: 'error', - title: '璇峰墠寰�PC绔笅杞�', - }) - } else { - return wx.showToast({ - icon: 'error', - title: '璇峰厛鐢宠涓嬭浇', - }) - } - } - - let url; - if (item.selectType == "webpage") { - // 缃戦〉绫诲瀷璺宠浆鍒扮綉鍧�澶嶅埗椤碉紝鍥犱负url浼犲弬涓嶈兘浼� 甯﹀弬鏁扮殑缃戠粶鍦板潃锛屾墍浠ユ澶勮鎶婄綉缁滃湴鍧�閲岀殑浼犲弬鎽樺嚭鏉ワ紝鏀惧埌url閲� - let urlData = '' - const flags = item.jsek_link.indexOf("?") - if (flags > -1) { - urlData = item.jsek_link.slice(flags + 1, item.jsek_link.length) - } - url = '/packageBookService/pages/bookServices/linkPage/index' - return wx.navigateTo({ - url: urlData ? url + `?path=${item.jsek_link.slice(0,flags)}` + '&' + urlData : url + `?path=${item.jsek_link}` - }) - } - - if (item.selectType == "video" || item.learnSelectType == "video") { - url = "/packageDomain/pages/resourceDetails/myVideo/index"; - } - if ( - item.selectType == "audio" || - item.learnSelectType == "audio" - ) { - url = "/packageDomain/pages/resourceDetails/myAudio/index"; - } - if (item.selectType == "picture") { - url = "/packageDomain/pages/resourceDetails/document/index"; - } - wx.navigateTo({ - url: `${url}?productLinkPath=${item.productLinkPath}&parentProductLinkPath=${parentProductLinkPath}&parentName=${parentName}&activeId=${item.id}&bookId=${this.properties.bookInfo.id}&bookName=${this.properties.bookInfo.name}&cmsId=${this.properties.bookInfo.rootCmsItemId}&formPath=${this.properties.tab}&applyState=${this.properties.applyState}&deadline=${this.properties.deadline}`, + if (!token) return false + const item = e.currentTarget.dataset.item; + const parent = e.currentTarget.dataset.parent; + const parentProductLinkPath = parent ? + parent.productLinkPath : + wx.getStorageSync("teachResourcesPath"); + const parentName = parent ? parent.name : "鏁欏璧勬簮"; + // 璧勬簮璐拱椤甸潰(鍙睍绀烘湭璐拱璧勬簮) 鐐瑰嚮涓嶈烦杞� + if (this.properties.isShowCheck) return false; + if ( + this.properties.tab == "jsek_cloudLearning" && + this.resourceIsBuy(item) + ) { + return wx.showToast({ + icon: "error", + title: item.saleMethod[0].Price > 0 ? + "璇峰厛璐拱璇ヨ祫婧�" : "璇峰厛鐐瑰嚮棰嗗彇鏌ョ湅鎸夐挳", }); } + if (this.properties.tab == "jsek_teachingResources" && (item.file || item.freeFile)) { + let file = item.fileMap[item.file] ? item.fileMap[item.file] : item.fileMap[item.freeFile] + if (file && file.protectType !== "Public") { + return wx.showToast({ + icon: "error", + title: "璧勬簮涓嶅彲鐢紝璇疯仈绯荤鐞嗗憳", + }); + } + } + if (!item.file && !item.freeFile && !item.protectedFile) { + return wx.showToast({ + icon: 'error', + title: '鏆傛棤璧勬簮', + }) + } + if (item.selectType == 'zip') { + if (this.properties.applyState == 'Normal') { + return wx.showToast({ + icon: 'error', + title: '璇峰墠寰�PC绔笅杞�', + }) + } else { + return wx.showToast({ + icon: 'error', + title: '璇峰厛鐢宠涓嬭浇', + }) + } + } + let url; + if (item.selectType == "webpage") { + // 缃戦〉绫诲瀷璺宠浆鍒扮綉鍧�澶嶅埗椤碉紝鍥犱负url浼犲弬涓嶈兘浼� 甯﹀弬鏁扮殑缃戠粶鍦板潃锛屾墍浠ユ澶勮鎶婄綉缁滃湴鍧�閲岀殑浼犲弬鎽樺嚭鏉ワ紝鏀惧埌url閲� + let urlData = '' + const flags = item.jsek_link.indexOf("?") + if (flags > -1) { + urlData = item.jsek_link.slice(flags + 1, item.jsek_link.length) + } + url = '/packageBookService/pages/bookServices/linkPage/index' + return wx.navigateTo({ + url: urlData ? url + `?path=${item.jsek_link.slice(0,flags)}` + '&' + urlData : url + `?path=${item.jsek_link}` + }) + } + + if (item.selectType == "video" || item.learnSelectType == "video") { + url = "/packageDomain/pages/resourceDetails/myVideo/index"; + } + if ( + item.selectType == "audio" || + item.learnSelectType == "audio" + ) { + url = "/packageDomain/pages/resourceDetails/myAudio/index"; + } + if (item.selectType == "picture") { + url = "/packageDomain/pages/resourceDetails/document/index"; + } + wx.navigateTo({ + url: `${url}?productLinkPath=${item.productLinkPath}&parentProductLinkPath=${parentProductLinkPath}&parentName=${parentName}&activeId=${item.id}&bookId=${this.properties.bookInfo.id}&bookName=${this.properties.bookInfo.name}&cmsId=${this.properties.bookInfo.rootCmsItemId}&formPath=${this.properties.tab}&applyState=${this.properties.applyState}&deadline=${this.properties.deadline}`, + }); }, // 鎷垮埌鎵�鏈夐」 diff --git a/packageBookService/pages/bookServices/detail/index.js b/packageBookService/pages/bookServices/detail/index.js index 8d73b89..7dc1abf 100644 --- a/packageBookService/pages/bookServices/detail/index.js +++ b/packageBookService/pages/bookServices/detail/index.js @@ -873,10 +873,9 @@ }, // 鎵佸钩鍖栨暟鎹浆鎹ree - getTreeList(rootList, pathLength, newArr, path) { + getTreeList(rootList, newArr, path) { for (const item of rootList) { - // 姝ゅ鍘熸湰 item.productLinkPath.length == pathLength 浣� productLinkPath 闀垮害涓埆涔﹀瓨鍦�4銆�5浣嶄氦閿� - if ((pathLength - item.productLinkPath.length >= 0 && pathLength - item.productLinkPath.length <= 3) && item.productLinkPath.includes(path)) { + if (path == item.productLinkPath.replace('\\' + item.id, '')) { if (item.sysType == 'CmsItem') { if (item.selectType == 'webpage') { item.disabled = true @@ -897,11 +896,9 @@ } } //缁欐暟缁勯噷闈㈠啀娣诲姞涓�涓猚hildren鐨勭┖鏁扮粍 - // let addnum1 = pathLength - path.length == 6 ? 6 : 7 for (const i of newArr) { i.children = [] - let addnum1 = i.productLinkPath.length - path.length == 6 ? 6 : 7 - this.getTreeList(rootList, pathLength + addnum1, i.children, i.productLinkPath) + this.getTreeList(rootList, i.children, i.productLinkPath) if (i.children.length == 0) { delete i.children } @@ -928,26 +925,19 @@ // 鎺掑簭鏁扮粍 鎸夌収productLinkPath insertAndSortObjectsByProductLinkPath(array, newObj) { // 鏌ユ壘鏂板璞″簲璇ユ彃鍏ョ殑浣嶇疆 - let insertIndex = array.findIndex(obj => Number(newObj.productLinkPath.substring(newObj.productLinkPath.lastIndexOf('\\') + 1, newObj.productLinkPath.length)) < Number(obj.productLinkPath.substring(obj.productLinkPath.lastIndexOf('\\') + 1, obj.productLinkPath.length))); + let insertIndex = array.findIndex(obj => newObj.id < obj.id); // 濡傛灉娌℃湁鎵惧埌鍚堥�傜殑浣嶇疆锛屽垯鏀惧湪鏁扮粍鏈熬 if (insertIndex === -1) { insertIndex = array.length; } // 鎻掑叆鏂板璞″埌鏁扮粍 array.splice(insertIndex, 0, newObj); - // 娴嬭瘯6 姝e紡5 // 瀵规暟缁勮繘琛屾帓搴� array.sort((a, b) => { - if (Number(a.productLinkPath.substring(a.productLinkPath.lastIndexOf('\\') + 1, a.productLinkPath.length)) < Number(b.productLinkPath.substring(b.productLinkPath.lastIndexOf('\\') + 1, b.productLinkPath.length))) { - return -1; - } - if (Number(a.productLinkPath.substring(a.productLinkPath.lastIndexOf('\\') + 1, a.productLinkPath.length)) > Number(b.productLinkPath.substring(b.productLinkPath.lastIndexOf('\\') + 1, b.productLinkPath.length))) { - return 1; - } - // a must be equal to b + if (a.id < b.id) return -1; + if (a.id > b.id) return 1; return 0; }); - // 杩斿洖鏇存柊鍚庣殑鏁扮粍 return array; }, @@ -1039,9 +1029,7 @@ }) } } - const addNum = query.cmsPath.length > 10 ? 7 : query.cmsPath.length > 13 ? 8 : 6 - const num = query.cmsPath.length + addNum - this.getTreeList(res.datas.cmsDatas[0].datas, num, list, '\\') + this.getTreeList(res.datas.cmsDatas[0].datas, list, query.cmsPath) list = this.ensureTreeConsistency(list) list = this.changeResourceChildren(list) let result = []; @@ -1151,11 +1139,7 @@ } if (data.length > 0) { let list = [] - // 娴嬭瘯 6浣�// 姝e紡 5浣�// 娴嬭瘯璋冪敤浼�20锛屽唴閮�7 姝e紡璋冪敤浼�17 鍐呴儴浼�6 - - const addNum = query.cmsPath.length > 10 ? 7 : query.cmsPath.length > 13 ? 8 : 6 - const num = query.cmsPath.length + addNum - that.getTreeList(data, num, list, '\\') + that.getTreeList(data, list, query.cmsPath) list = that.ensureTreeConsistency(list) that.findChildIds(list[0].children, result); that.setData({ -- Gitblit v1.9.1