Browse Source

fix(定期任务本地缓存和api赋值,未完成): 定期任务本地缓存和api赋值,未完成

tall
song 4 years ago
parent
commit
b22a3663fb
  1. 3
      CHANGELOG.md
  2. 2
      src/main.js
  3. 110
      src/pages/project/project.vue
  4. 81
      src/utils/cache.js
  5. 5
      src/utils/cacheAndRequest.js
  6. 2
      src/utils/storage.js
  7. 2
      src/utils/time.js

3
CHANGELOG.md

@ -1,4 +1,4 @@
# 0.1.0 (2021-08-23) # 0.1.0 (2021-08-24)
### 🌟 新功能 ### 🌟 新功能
范围|描述|commitId 范围|描述|commitId
@ -85,6 +85,7 @@
范围|描述|commitId 范围|描述|commitId
--|--|-- --|--|--
- | 1.时间轴数据渲染 2.时间基准线 | [d643af2](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/d643af2) - | 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) 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) 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) project title | 项目标题修改; 切换角色移除script | [5c20017](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/5c20017)

2
src/main.js

@ -21,6 +21,8 @@ Vue.prototype.$moment = dayjs;
Vue.use(uView); Vue.use(uView);
Vue.use(Tall); Vue.use(Tall);
uni.$moment = dayjs;
dayjs.locale('zh-cn'); dayjs.locale('zh-cn');
App.mpType = 'app'; App.mpType = 'app';

110
src/pages/project/project.vue

@ -23,7 +23,7 @@ import mixin from '@/mixins/timeline';
export default { export default {
mixins: [mixin], mixins: [mixin],
data() { data() {
return { height: '' }; return { height: '', show: false };
}, },
computed: { computed: {
@ -75,6 +75,7 @@ export default {
onUnload() { onUnload() {
this.clearEndFlag(); this.clearEndFlag();
this.clearTasks(); this.clearTasks();
this.setRoleId();
}, },
methods: { methods: {
@ -216,12 +217,14 @@ export default {
} }
// //
this.getTasks({ queryType: 0 }); this.getTasks({ queryType: 0 });
// id // id
this.getTasks({ queryType: 1 }, fn); this.getTasks({ queryType: 1 }, fn);
}, },
// //
setInitialRoleId(visibleList) { setInitialRoleId(visibleList) {
if (!visibleList || !visibleList.length) return;
const index = visibleList.findIndex(item => +item.mine === 1); const index = visibleList.findIndex(item => +item.mine === 1);
const currentRole = index > 0 ? visibleList[index] : visibleList[0]; const currentRole = index > 0 ? visibleList[index] : visibleList[0];
const storageRoleId = this.$t.storage.getStorageSync('roleId'); const storageRoleId = this.$t.storage.getStorageSync('roleId');
@ -254,22 +257,26 @@ export default {
}; };
that.$t.$q.getRegularTask(params, function (err, data) { that.$t.$q.getRegularTask(params, function (err, data) {
if (err) { console.log('data: ', data);
that.setShowSkeleton(false); console.log('that.show: ', that.show);
console.error('err: ', err); if (!that.show) {
} else { if (err) {
console.log('++++'); that.setShowSkeleton(false);
that.setShowSkeleton(false); console.error('err: ', err);
// 0 -> 1 ->
if (data && data.length) {
console.log(1);
that.replacePrevData(data, params.queryType);
} else { } else {
params.queryType === 0 ? that.setPrevTasks() : that.setNextTasks(); that.show = true;
} that.setShowSkeleton(false);
if (that.tasks.length && fn) { // 0 -> 1 ->
console.log(2); if (data && data.length) {
fn(that); 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) { replacePrevData(data, type) {
let newTasks = [...this.tasks]; let newTasks = [...this.tasks];
for (let i = 0; i < newTasks.length; i++) { let newDate = this.$u.deepClone(data);
const task = newTasks[i];
let arr = []; console.log('newDate: ', newDate);
for (let j = 0; j < data.length; j++) {
const item = data[j]; if (newDate && newDate.length) {
// for (let i = 0; i < newTasks.length; i++) {
if (+newTasks[0].planStart > +data[0].planStart) { // const task = newTasks[i];
this.setPrevTasks(); let arr = [];
newTasks = [...this.tasks]; for (let j = 0; j < newDate.length; j++) {
i--; const item = newDate[j];
break; if (newTasks[i].id === item.id) {
} else if (+data[data.length - 1].planStart > +newTasks[newTasks.length - 1].planStart) { console.log('j', j, i);
this.setNextTasks(); newTasks[i] = item;
newTasks = [...this.tasks]; break;
i--; }
break; //
} else { if (+newTasks[0].planStart > +newDate[0].planStart) {
// this.setPrevTasks();
const taskItem = this.$t.time.isSame(+task.planStart, +item.planStart, this.timeGranularity); newTasks = [...this.tasks];
if (taskItem) { i--;
arr.push(item); break;
if (task.detailId) { } else if (+newDate[newDate.length - 1].planStart > +newTasks[newTasks.length - 1].planStart) {
newTasks.splice(i, 0, item); this.setNextTasks();
} else { newTasks = [...this.tasks];
if (arr.length === 1) { i--;
newTasks.splice(i, 1, item); break;
} } else {
if (arr.length > 1) { //
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); 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(); this.clearTasks();
type === 0 ? this.setUpTasks(newTasks) : this.setDownTasks(newTasks); type === 0 ? this.setUpTasks(newTasks) : this.setDownTasks(newTasks);
console.log('newTasks: ', newTasks.length);
console.log('end');
}, },
}, },
}; };

81
src/utils/cache.js

@ -27,16 +27,62 @@ export const filter = {
* @param {number} timeNode 时间基准点 ms * @param {number} timeNode 时间基准点 ms
* @param {number} queryNum 颗粒度数量 * @param {number} queryNum 颗粒度数量
* @param {number} timeUnit 时间颗粒度 * @param {number} timeUnit 时间颗粒度
* @param {number} queryType 0向上查找 1向下查找(默认) 下查包含自己上查不包含
* @returns * @returns
*/ */
planTask(data, timeNode, queryNum, timeUnit) { planTask(data, timeNode, queryNum, timeUnit, queryType) {
if (!data || !data.length) return []; if (!data || !data.length) return [];
// 计算颗粒度 对应的 dayjs add 的单位 if (queryType === 0) {
const target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit); // 计算颗粒度 对应的 dayjs add 的单位
// TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签 let target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit);
const start = uni.$t.time.add(timeNode, -queryNum, target.granularity).valueOf(); // TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签
const end = uni.$t.time.add(timeNode, +queryNum - 1, target.granularity).valueOf(); let start = uni.$t.time.add(+timeNode, -queryNum, target.granularity).valueOf();
return data.filter(item => start <= +item.endTime && end >= +item.startTime); 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); uni.$t.storage.setStorage('projects', locals);
} catch (error) { } catch (error) {
console.error('error: ', error); console.error('error: ', error);
@ -172,7 +217,6 @@ export default {
} }
}); });
} }
uni.$t.cache.checkCapacity();
uni.$t.storage.setStorage(`roles_${projectId}`, locals); uni.$t.storage.setStorage(`roles_${projectId}`, locals);
} catch (error) { } catch (error) {
console.error('error: ', error); console.error('error: ', error);
@ -189,7 +233,7 @@ export default {
async getStorageRegularTask(params) { async getStorageRegularTask(params) {
try { try {
const data = await uni.$t.storage.getStorage(`plan_task_${params.projectId}_${params.roleId}`); 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) { } catch (error) {
console.error('error: ', error); console.error('error: ', error);
return []; return [];
@ -221,7 +265,6 @@ export default {
} }
}); });
} }
uni.$t.cache.checkCapacity();
uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, locals); uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, locals);
} catch (error) { } catch (error) {
console.error('error: ', 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); uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}_${params.roleId}`, locals);
} catch (error) { } catch (error) {
console.error('error: ', 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); uni.$t.storage.setStorage(`variable_tasks_${params.projectId}_${params.roleId}`, locals);
} catch (error) { } catch (error) {
console.error('error: ', error); console.error('error: ', error);
@ -359,24 +400,10 @@ export default {
// 本地有数据 // 本地有数据
locals = data; locals = data;
} }
uni.$t.cache.checkCapacity();
uni.$t.storage.setStorage(`plugin_${pluginId}`, locals); uni.$t.storage.setStorage(`plugin_${pluginId}`, locals);
} catch (error) { } catch (error) {
console.error('error: ', error); console.error('error: ', error);
uni.$t.storage.setStorage(`plugin_${pluginId}`, null); 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();
// }
// }
},
}; };

5
src/utils/cacheAndRequest.js

@ -80,6 +80,7 @@ export default {
uni.$t.cache uni.$t.cache
.getStorageRegularTask(params) .getStorageRegularTask(params)
.then(data => { .then(data => {
console.log('cache data: ', data);
!remote && fn(null, data); !remote && fn(null, data);
}) })
.catch(err => !remote && fn(err)); .catch(err => !remote && fn(err));
@ -89,8 +90,10 @@ export default {
uni.$u.api uni.$u.api
.getRegularTask(params) .getRegularTask(params)
.then(data => { .then(data => {
console.log('api data: ', uni.$u.deepClone(data));
remote = true; remote = true;
fn(null, data);
fn(null, uni.$u.deepClone(data));
// 存api到cache里 // 存api到cache里
uni.$t.cache.putStorageRegularTask(params, data); uni.$t.cache.putStorageRegularTask(params, data);
}) })

2
src/utils/storage.js

@ -106,10 +106,8 @@ export default {
return; return;
} }
const capacity = JSON.stringify(localStorage).length; const capacity = JSON.stringify(localStorage).length;
console.log('capacity: ', capacity);
let max = 1024 * 1024 * 5; let max = 1024 * 1024 * 5;
if (capacity >= max) { if (capacity >= max) {
console.log('清空');
uni.$t.storage.clearStorage(); uni.$t.storage.clearStorage();
} }
}, },

2
src/utils/time.js

@ -82,7 +82,7 @@ const setTimestampToStr = timestamp => {
const day = formatNumber(timeObj.getDate()); const day = formatNumber(timeObj.getDate());
const hour = formatNumber(timeObj.getHours()); const hour = formatNumber(timeObj.getHours());
const minute = formatNumber(timeObj.getMinutes()); const minute = formatNumber(timeObj.getMinutes());
const date = `${year}/${month}/${day}`; const date = `${year}-${month}-${day}`;
const time = `${hour}:${minute}`; const time = `${hour}:${minute}`;
return { return {
date, date,

Loading…
Cancel
Save