Browse Source

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

tall
song 4 years ago
parent
commit
b22a3663fb
  1. 3
      CHANGELOG.md
  2. 2
      src/main.js
  3. 42
      src/pages/project/project.vue
  4. 77
      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
@ -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)

2
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';

42
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,24 +257,28 @@ export default {
};
that.$t.$q.getRegularTask(params, function (err, data) {
console.log('data: ', data);
console.log('that.show: ', that.show);
if (!that.show) {
if (err) {
that.setShowSkeleton(false);
console.error('err: ', err);
} else {
console.log('++++');
that.show = true;
that.setShowSkeleton(false);
// 0 -> 1 ->
if (data && data.length) {
console.log(1);
that.replacePrevData(data, params.queryType);
that.show = false;
} else {
params.queryType === 0 ? that.setPrevTasks() : that.setNextTasks();
that.show = false;
}
if (that.tasks.length && fn) {
console.log(2);
fn(that);
}
}
}
});
},
@ -311,28 +318,38 @@ export default {
//
replacePrevData(data, type) {
let newTasks = [...this.tasks];
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];
// const task = newTasks[i];
let arr = [];
for (let j = 0; j < data.length; j++) {
const item = data[j];
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 > +data[0].planStart) {
if (+newTasks[0].planStart > +newDate[0].planStart) {
this.setPrevTasks();
newTasks = [...this.tasks];
i--;
break;
} else if (+data[data.length - 1].planStart > +newTasks[newTasks.length - 1].planStart) {
} 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(+task.planStart, +item.planStart, this.timeGranularity);
const taskItem = this.$t.time.isSame(+newTasks[i].planStart, +item.planStart, this.timeGranularity);
if (taskItem) {
arr.push(item);
if (task.detailId) {
if (newTasks[i].detailId) {
newTasks.splice(i, 0, item);
} else {
if (arr.length === 1) {
@ -347,8 +364,11 @@ export default {
}
}
}
}
this.clearTasks();
type === 0 ? this.setUpTasks(newTasks) : this.setDownTasks(newTasks);
console.log('newTasks: ', newTasks.length);
console.log('end');
},
},
};

77
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 [];
if (queryType === 0) {
// 计算颗粒度 对应的 dayjs add 的单位
const target = uni.$t.timeConfig.timeUnits.find(item => item.id === timeUnit);
let 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);
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();
// }
// }
},
};

5
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);
})

2
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();
}
},

2
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,

Loading…
Cancel
Save