From b22a3663fbb8b9d946e8d6f0e895ff197b63ed74 Mon Sep 17 00:00:00 2001 From: song Date: Tue, 24 Aug 2021 14:37:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=AE=9A=E6=9C=9F=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E7=BC=93=E5=AD=98=E5=92=8Capi=E8=B5=8B?= =?UTF-8?q?=E5=80=BC,=E6=9C=AA=E5=AE=8C=E6=88=90):=20=E5=AE=9A=E6=9C=9F?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=9C=AC=E5=9C=B0=E7=BC=93=E5=AD=98=E5=92=8C?= =?UTF-8?q?api=E8=B5=8B=E5=80=BC,=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- src/main.js | 2 + src/pages/project/project.vue | 110 ++++++++++++++++++++-------------- src/utils/cache.js | 81 ++++++++++++++++--------- src/utils/cacheAndRequest.js | 5 +- src/utils/storage.js | 2 - src/utils/time.js | 2 +- 7 files changed, 128 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b777d5c..f2ab023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.1.0 (2021-08-23) +# 0.1.0 (2021-08-24) ### 🌟 新功能 范围|描述|commitId @@ -85,6 +85,7 @@ 范围|描述|commitId --|--|-- - | 1.时间轴数据渲染 2.时间基准线 | [d643af2](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/d643af2) + - | api 存storage | [81032ba](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/81032ba) ID1000343 | 解决向下预加载查询参数时间没+1颗粒度;以及滚动加载颗粒度写死的问题 | [940603a](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/940603a), closes [#ID1000343](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/issues/ID1000343) plugin | 插件解析机制完善 | [0f5a27d](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/0f5a27d) project title | 项目标题修改; 切换角色移除script | [5c20017](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/5c20017) diff --git a/src/main.js b/src/main.js index 0e38fcc..9e10deb 100644 --- a/src/main.js +++ b/src/main.js @@ -21,6 +21,8 @@ Vue.prototype.$moment = dayjs; Vue.use(uView); Vue.use(Tall); +uni.$moment = dayjs; + dayjs.locale('zh-cn'); App.mpType = 'app'; diff --git a/src/pages/project/project.vue b/src/pages/project/project.vue index 7ee3997..9fefdc2 100644 --- a/src/pages/project/project.vue +++ b/src/pages/project/project.vue @@ -23,7 +23,7 @@ import mixin from '@/mixins/timeline'; export default { mixins: [mixin], data() { - return { height: '' }; + return { height: '', show: false }; }, computed: { @@ -75,6 +75,7 @@ export default { onUnload() { this.clearEndFlag(); this.clearTasks(); + this.setRoleId(); }, methods: { @@ -216,12 +217,14 @@ export default { } // 根据时间基准点和角色查找定期任务 this.getTasks({ queryType: 0 }); + // 根据项目id获取角色列表 this.getTasks({ queryType: 1 }, fn); }, // 设置 初始显示角色信息 setInitialRoleId(visibleList) { + if (!visibleList || !visibleList.length) return; const index = visibleList.findIndex(item => +item.mine === 1); const currentRole = index > 0 ? visibleList[index] : visibleList[0]; const storageRoleId = this.$t.storage.getStorageSync('roleId'); @@ -254,22 +257,26 @@ export default { }; that.$t.$q.getRegularTask(params, function (err, data) { - if (err) { - that.setShowSkeleton(false); - console.error('err: ', err); - } else { - console.log('++++'); - that.setShowSkeleton(false); - // 0 -> 向上 1 -> 向下 - if (data && data.length) { - console.log(1); - that.replacePrevData(data, params.queryType); + console.log('data: ', data); + console.log('that.show: ', that.show); + if (!that.show) { + if (err) { + that.setShowSkeleton(false); + console.error('err: ', err); } else { - params.queryType === 0 ? that.setPrevTasks() : that.setNextTasks(); - } - if (that.tasks.length && fn) { - console.log(2); - fn(that); + that.show = true; + that.setShowSkeleton(false); + // 0 -> 向上 1 -> 向下 + if (data && data.length) { + that.replacePrevData(data, params.queryType); + that.show = false; + } else { + params.queryType === 0 ? that.setPrevTasks() : that.setNextTasks(); + that.show = false; + } + if (that.tasks.length && fn) { + fn(that); + } } } }); @@ -311,44 +318,57 @@ export default { // 筛选相同的时间替换数据 replacePrevData(data, type) { let newTasks = [...this.tasks]; - for (let i = 0; i < newTasks.length; i++) { - const task = newTasks[i]; - let arr = []; - for (let j = 0; j < data.length; j++) { - const item = data[j]; - // 查找有没有超出时间刻度的时间 - if (+newTasks[0].planStart > +data[0].planStart) { - this.setPrevTasks(); - newTasks = [...this.tasks]; - i--; - break; - } else if (+data[data.length - 1].planStart > +newTasks[newTasks.length - 1].planStart) { - this.setNextTasks(); - newTasks = [...this.tasks]; - i--; - break; - } else { - // 筛选相同的时间替换数据 - const taskItem = this.$t.time.isSame(+task.planStart, +item.planStart, this.timeGranularity); - if (taskItem) { - arr.push(item); - if (task.detailId) { - newTasks.splice(i, 0, item); - } else { - if (arr.length === 1) { - newTasks.splice(i, 1, item); - } - if (arr.length > 1) { + let newDate = this.$u.deepClone(data); + + console.log('newDate: ', newDate); + + if (newDate && newDate.length) { + for (let i = 0; i < newTasks.length; i++) { + // const task = newTasks[i]; + let arr = []; + for (let j = 0; j < newDate.length; j++) { + const item = newDate[j]; + if (newTasks[i].id === item.id) { + console.log('j', j, i); + newTasks[i] = item; + break; + } + // 查找有没有超出时间刻度的时间 + if (+newTasks[0].planStart > +newDate[0].planStart) { + this.setPrevTasks(); + newTasks = [...this.tasks]; + i--; + break; + } else if (+newDate[newDate.length - 1].planStart > +newTasks[newTasks.length - 1].planStart) { + this.setNextTasks(); + newTasks = [...this.tasks]; + i--; + break; + } else { + // 筛选相同的时间替换数据 + const taskItem = this.$t.time.isSame(+newTasks[i].planStart, +item.planStart, this.timeGranularity); + if (taskItem) { + arr.push(item); + if (newTasks[i].detailId) { newTasks.splice(i, 0, item); + } else { + if (arr.length === 1) { + newTasks.splice(i, 1, item); + } + if (arr.length > 1) { + newTasks.splice(i, 0, item); + } } + i++; } - i++; } } } } this.clearTasks(); type === 0 ? this.setUpTasks(newTasks) : this.setDownTasks(newTasks); + console.log('newTasks: ', newTasks.length); + console.log('end'); }, }, }; diff --git a/src/utils/cache.js b/src/utils/cache.js index f715275..c22e6c5 100644 --- a/src/utils/cache.js +++ b/src/utils/cache.js @@ -27,16 +27,62 @@ export const filter = { * @param {number} timeNode 时间基准点 ms * @param {number} queryNum 颗粒度数量 * @param {number} timeUnit 时间颗粒度 + * @param {number} queryType 0向上查找 1向下查找(默认) 下查包含自己,上查不包含 * @returns */ - planTask(data, timeNode, queryNum, timeUnit) { + planTask(data, timeNode, queryNum, timeUnit, queryType) { if (!data || !data.length) return []; - // 计算颗粒度 对应的 dayjs add 的单位 - const target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit); - // TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签 - const start = uni.$t.time.add(timeNode, -queryNum, target.granularity).valueOf(); - const end = uni.$t.time.add(timeNode, +queryNum - 1, target.granularity).valueOf(); - return data.filter(item => start <= +item.endTime && end >= +item.startTime); + if (queryType === 0) { + // 计算颗粒度 对应的 dayjs add 的单位 + let target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit); + // TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签 + let start = uni.$t.time.add(+timeNode, -queryNum, target.granularity).valueOf(); + let arr = []; + arr = data.filter(item => start <= +item.planStart && +timeNode > +item.planEnd); + + if (!arr || !arr.length) { + // 开始时间往前推 + let resultS = []; + let againStart = uni.$t.time.add(start, -1, target.granularity).valueOf(); + let againArr = data.filter(item => againStart >= +item.planStart); + if (againArr && againArr.length) { + let sTime = uni.$t.time.setTimestampToStr(+againArr[0].planStart); + data.forEach(item => { + if (uni.$t.time.isSame(uni.$moment(sTime.date).valueOf(), +item.planStart, target.granularity)) { + resultS.push(item); + } + }); + } + return resultS; + } else { + return arr; + } + } else { + // 计算颗粒度 对应的 dayjs add 的单位 + let target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit); + // TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签 + let end = uni.$t.time.add(timeNode, +queryNum - 1, target.granularity).valueOf(); + let arr = []; + arr = data.filter(item => end >= +item.planEnd && +timeNode <= +item.planStart); + + if (!arr || !arr.length) { + // 结束时间往后推 + let resultE = []; + let againEnd = uni.$t.time.add(end, 1, target.granularity).valueOf(); + let againEndArr = data.filter(item => againEnd <= +item.planStart); + if (againEndArr) { + let eTime = uni.$t.time.setTimestampToStr(+againEndArr[againEndArr.length - 1].planStart); + data.forEach(item => { + if (uni.$t.time.isSame(uni.$moment(eTime.date).valueOf(), +item.planEnd, target.granularity)) { + resultE.push(item); + } + }); + } + return resultE; + } else { + return arr; + } + } }, /** @@ -114,7 +160,6 @@ export default { } }); } - uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage('projects', locals); } catch (error) { console.error('error: ', error); @@ -172,7 +217,6 @@ export default { } }); } - uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`roles_${projectId}`, locals); } catch (error) { console.error('error: ', error); @@ -189,7 +233,7 @@ export default { async getStorageRegularTask(params) { try { const data = await uni.$t.storage.getStorage(`plan_task_${params.projectId}_${params.roleId}`); - return filter.planTask(JSON.parse(data), params.timeNode, params.queryNum, params.timeUnit); + return filter.planTask(JSON.parse(data), params.timeNode, params.queryNum, params.timeUnit, params.queryType); } catch (error) { console.error('error: ', error); return []; @@ -221,7 +265,6 @@ export default { } }); } - uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, locals); } catch (error) { console.error('error: ', error); @@ -271,7 +314,6 @@ export default { } }); } - uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}_${params.roleId}`, locals); } catch (error) { console.error('error: ', error); @@ -319,7 +361,6 @@ export default { } }); } - uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`variable_tasks_${params.projectId}_${params.roleId}`, locals); } catch (error) { console.error('error: ', error); @@ -359,24 +400,10 @@ export default { // 本地有数据 locals = data; } - uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`plugin_${pluginId}`, locals); } catch (error) { console.error('error: ', error); uni.$t.storage.setStorage(`plugin_${pluginId}`, null); } }, - - // 检测local Storage容量 - checkCapacity() { - // if (window.localStorage) { - // console.log('浏览器不支持localStorage '); - // const capacity = JSON.stringify(localStorage).length; - // console.log('capacity: ', capacity); - // let max = 1024 * 1024 * 5; - // if (capacity >= max) { - // uni.$t.storage.clearStorage(); - // } - // } - }, }; diff --git a/src/utils/cacheAndRequest.js b/src/utils/cacheAndRequest.js index f952dca..bc35088 100644 --- a/src/utils/cacheAndRequest.js +++ b/src/utils/cacheAndRequest.js @@ -80,6 +80,7 @@ export default { uni.$t.cache .getStorageRegularTask(params) .then(data => { + console.log('cache data: ', data); !remote && fn(null, data); }) .catch(err => !remote && fn(err)); @@ -89,8 +90,10 @@ export default { uni.$u.api .getRegularTask(params) .then(data => { + console.log('api data: ', uni.$u.deepClone(data)); remote = true; - fn(null, data); + + fn(null, uni.$u.deepClone(data)); // 存api到cache里 uni.$t.cache.putStorageRegularTask(params, data); }) diff --git a/src/utils/storage.js b/src/utils/storage.js index cbcdb49..4ab40e6 100644 --- a/src/utils/storage.js +++ b/src/utils/storage.js @@ -106,10 +106,8 @@ export default { return; } const capacity = JSON.stringify(localStorage).length; - console.log('capacity: ', capacity); let max = 1024 * 1024 * 5; if (capacity >= max) { - console.log('清空'); uni.$t.storage.clearStorage(); } }, diff --git a/src/utils/time.js b/src/utils/time.js index 6f1fae2..c20cd2b 100644 --- a/src/utils/time.js +++ b/src/utils/time.js @@ -82,7 +82,7 @@ const setTimestampToStr = timestamp => { const day = formatNumber(timeObj.getDate()); const hour = formatNumber(timeObj.getHours()); const minute = formatNumber(timeObj.getMinutes()); - const date = `${year}/${month}/${day}`; + const date = `${year}-${month}-${day}`; const time = `${hour}:${minute}`; return { date,