From 7d4edfc92b2d322852dd507b6a67fc440fc43eea Mon Sep 17 00:00:00 2001 From: song Date: Fri, 20 Aug 2021 18:36:06 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20api=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +- src/components/Roles/Roles.vue | 2 +- src/pages/index/index.vue | 24 +++-- src/pages/project/project.vue | 179 +++++++++++++++++++------------- src/store/project/actions.js | 17 +--- src/store/role/actions.js | 18 +--- src/store/task/actions.js | 44 +++----- src/store/task/state.js | 3 +- src/utils/cache.js | 180 ++++++++++++++++++++++++++++++--- src/utils/cacheAndRequest.js | 103 +++++++++++++++++-- 10 files changed, 405 insertions(+), 170 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53eb312..ba83601 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.1.0 (2021-08-18) +# 0.1.0 (2021-08-20) ### 🌟 新功能 范围|描述|commitId @@ -39,6 +39,7 @@ - | 标题栏变化 | [c0fcd9d](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/c0fcd9d) - | 标题栏角色栏全局任务组件新建 | [0500cb4](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/0500cb4) - | 模拟接口测试 | [69e7931](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/69e7931) + - | 添加子任务插件 子项目插件 | [7bda7e2](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/7bda7e2) - | 添加时间轴上下滚动 | [2b81bbc](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/2b81bbc) - | 点击日历日期查询项目列表 | [c458385](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/c458385) - | 缓存修改 | [63e1f0d](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/63e1f0d) @@ -171,6 +172,7 @@ - | 1.时间轴筛选相同的时间替换数据 2.整理代码 | [e082ccb](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/e082ccb) - | 修改代码格式 | [14123d7](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/14123d7) - | 修改定期任务骨架屏高度 | [909a734](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/909a734) + - | 小红点api缓存修改 | [e992343](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/e992343) - | 插件查询及展示 | [4dba770](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/4dba770) - | 整理代码 | [7a55315](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/7a55315) - | 日历的更改 | [7353ac8](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/7353ac8) @@ -204,3 +206,4 @@ - | style:index | [978f272](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/978f272) - | !2 基础模板v1.1.0 | [f5e61dd](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/f5e61dd) - | init | [c0f1deb](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/c0f1deb) + diff --git a/src/components/Roles/Roles.vue b/src/components/Roles/Roles.vue index 6bf350f..6aacb13 100644 --- a/src/components/Roles/Roles.vue +++ b/src/components/Roles/Roles.vue @@ -56,7 +56,7 @@ export default { }, methods: { - ...mapActions('task', ['handleRegularTask', 'getPermanent']), + ...mapActions('task', ['handleRegularTask']), ...mapMutations('role', ['setRoleId']), ...mapMutations('task', ['setPermanents', 'clearEndFlag']), diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 9337ec7..fcb8ce5 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -63,16 +63,20 @@ export default { }); }, - // 查询小红点 - handleFindPoint(start = this.$moment().startOf('day').valueOf(), end = this.$moment().endOf('day').valueOf()) { - // const data = await this.$u.api.findRedPoint(startTime, endTime); - this.$t.$q.findRedPoint(start, end, (err, data) => { - if (err) { - console.err('err: ', err); - } else { - this.setDotList(data); - } - }); + /** + * 查询小红点 + * @param { string } endTime 结束时间 + * @param { string } startTime 开始时间 + */ + async handleFindPoint(start, end) { + try { + const startTime = start || this.$moment().startOf('month').valueOf(); + const endTime = end || this.$moment().endOf('month').valueOf(); + const data = await this.$u.api.findRedPoint(startTime, endTime); + this.setDotList(data); + } catch (error) { + console.log('error: ', error); + } }, // 点击了某个日期 diff --git a/src/pages/project/project.vue b/src/pages/project/project.vue index 9ea4c29..ad150fe 100644 --- a/src/pages/project/project.vue +++ b/src/pages/project/project.vue @@ -29,8 +29,10 @@ export default { computed: { ...mapState('user', ['user', 'token']), ...mapState('role', ['visibleRoles', 'roleId']), - ...mapState('task', ['timeNode', 'timeUnit', 'tasks']), + ...mapState('task', ['timeNode', 'timeUnit', 'tasks', 'regularTask']), + ...mapState('project', ['project']), ...mapGetters('task', ['timeGranularity']), + ...mapGetters('project', ['projectId']), }, onLoad(options) { @@ -43,10 +45,10 @@ export default { * 重新根据时间和角色查询普通日常任务 * 永久日常任务不发生改变 */ - async timeNode(val) { + timeNode(val) { if (val && this.roleId) { // 根据时间和角色查找日常任务 - await this.initTasks(); + this.initTasks(); } }, @@ -60,7 +62,8 @@ export default { if (val) { this.setTimeNode(Date.now()); // 根据角色查找永久的日常任务 - this.getPermanent(val); + const params = { roleId: val, projectId: this.projectId }; + this.getPermanent(params); } }, }, @@ -76,8 +79,6 @@ export default { methods: { ...mapActions('user', ['getToken']), - ...mapActions('project', ['getProjectById']), - ...mapActions('role', ['getRoles']), ...mapActions('task', ['getRegulars', 'getPermanent', 'getGlobal']), ...mapMutations('user', ['setToken']), ...mapMutations('project', ['setProject', 'setProjectName']), @@ -99,37 +100,63 @@ export default { * 初始化 * @param {object | null} options */ - async init(options) { - try { - if (!this.token) { - // 不论有没有token都直接从userId获取token - // token有过期时间 从本地获取可能是过期 干脆直接从userId获取 - if (!options || !options.u) { - // 参数里没有u (userId)提示 - this.$t.ui.showToast('缺少用户信息参数'); - } else { - await this.getToken(options.u); - } + init(options) { + if (!this.token) { + // 不论有没有token都直接从userId获取token + // token有过期时间 从本地获取可能是过期 干脆直接从userId获取 + if (!options || !options.u) { + // 参数里没有u (userId)提示 + this.$t.ui.showToast('缺少用户信息参数'); + } else { + this.getToken(options.u); } + } - // 参数里有项目名称 就设置标题里的项目名称 - options && options.pname && this.setProjectName(options.pname); + // 参数里有项目名称 就设置标题里的项目名称 + options && options.pname && this.setProjectName(options.pname); - if (!options || !options.p) { - // 没有项目id参数 - this.$t.ui.showToast('缺少项目信息参数'); - } else { - // 根据项目id获取项目信息 - await this.getProjectById({ projectId: options.p }); - // 根据项目id获取角色列表 - await this.getRoles({ projectId: options.p }); - this.setInitialRoleId(this.visibleRoles); - } + if (!options || !options.p) { + // 没有项目id参数 + this.$t.ui.showToast('缺少项目信息参数'); + } else { + // 根据项目id获取项目信息 + this.getProjectById({ projectId: options.p }); + } + }, + + /** + * 通过项目id获取项目信息 + * @param {string} projectId + * @param {object} params 提交的参数 + */ + async getProjectById(params) { + try { + const data = await uni.$u.api.findProjectById(params); + this.setProject(data); + // 根据项目id获取角色列表 + this.getRoles(params); } catch (error) { - console.error('project init function:', error); + console.log('error: ', error || '获取项目信息失败'); } }, + /** + * 通过项目id获取角色信息 + * @param {string} projectId + * @param {object} params 提交的参数 + */ + getRoles(params) { + this.$t.$q.findShowRole(params, (err, data) => { + if (err) { + console.error('err: ', err || '获取角色信息失败'); + } else { + this.setInvisibleRoles(data ? data.invisibleList : []); + this.setVisibleRoles(data ? data.visibleList : []); + this.setInitialRoleId(data ? data.visibleList : []); + } + }); + }, + // 初始化 时间轴 async initTasks() { // 清空日常任务的数据 @@ -153,7 +180,9 @@ export default { }); // 向下加载空数据 this.setNextTasks(); + await this.getInitTasks(); + // 根据项目id获取角色列表 // 从详情页返回时跳转到之前的位置 storage有存储值 this.$nextTick(() => { if (storageTaskId) { @@ -163,28 +192,31 @@ export default { }, // 切换了 颗粒度 || 角色时候 获取初始定期任务 - async getInitTasks() { + getInitTasks() { // 根据时间基准点和角色查找定期任务 - await this.getTasks({ queryType: 0 }); - await this.getTasks({ queryType: 1 }); + this.getTasks({ queryType: 0 }); + // 根据项目id获取角色列表 + this.getTasks({ queryType: 1 }); // 预加载 上下的定期任务 - const detailId = this.tasks.findIndex(task => task.detailId); - if (detailId !== -1) { - this.$nextTick(async () => { - const { tasks, timeGranularity } = this; - await this.getTasks({ timeNode: +tasks[0].planStart, queryType: 0, queryNum: 6 }); // 向上拿数据 - // 向下查的时候要再最后一个任务的开始时间的基础上 多加一个时间颗粒度 - const nextQueryTime = +this.$t.time.add(+tasks[tasks.length - 1].planStart, 1, timeGranularity); - await this.getTasks({ timeNode: nextQueryTime, queryType: 1, queryNum: 6 }); // 向下拿数据 - }); - } else { - // 没有任务 上下显示时间刻度 - // 向上加载 - this.setPrevTasks(); - // 向下加载 - this.setNextTasks(); - } + // const detailId = this.tasks.findIndex(task => task.detailId); + // console.log('detailId: ', detailId); + // if (detailId !== -1) { + // this.$nextTick(() => { + // // 向上拿数据 + // const { tasks, timeGranularity } = this; + // this.getTasks({ timeNode: +tasks[0].planStart, queryType: 0, queryNum: 6 }); + // // 向下拿数据 + // const nextQueryTime = +this.$t.time.add(+tasks[tasks.length - 1].planStart, 1, timeGranularity); + // this.getTasks({ timeNode: nextQueryTime, queryType: 1, queryNum: 6 }); + // }); + // } else { + // // 没有任务 上下显示时间刻度 + // // 向上加载 + // this.setPrevTasks(); + // // 向下加载 + // this.setNextTasks(); + // } }, // 设置 初始显示角色信息 @@ -207,35 +239,38 @@ export default { * @param {string} query.queryNum 查找颗粒度数量 默认3个 * @param {string} query.queryType 0向上查找 1向下查找(默认) 下查包含自己,上查不包含 */ - async getTasks(query) { - try { - this.setShowSkeleton(true); - const { roleId, timeNode, timeUnit } = this; - const params = { - roleId, - timeNode: query.timeNode || timeNode, - timeUnit: query.timeUnit || timeUnit, - queryNum: query.queryNum || 3, - queryType: query.queryType, - }; - const res = await this.getRegulars(params); - this.setShowSkeleton(false); - // 0 -> 向上 1 -> 向下 - if (res && res.length) { - this.replacePrevData(res, params.queryType); + getTasks(query) { + this.setShowSkeleton(true); + const { roleId, timeNode, timeUnit, projectId } = this; + const params = { + roleId, + timeNode: query.timeNode || timeNode, + timeUnit: query.timeUnit || timeUnit, + queryNum: query.queryNum || 3, + queryType: query.queryType, + projectId, + }; + this.$t.$q.getRegularTask(params, (err, data) => { + if (err) { + this.setShowSkeleton(false); + console.error('err: ', err); } else { - params.queryType === 0 ? this.setPrevTasks() : this.setNextTasks(); + this.setShowSkeleton(false); + // 0 -> 向上 1 -> 向下 + if (data && data.length) { + console.log('data: ', data); + this.replacePrevData(data, params.queryType); + } else { + params.queryType === 0 ? this.setPrevTasks() : this.setNextTasks(); + } } - } catch (error) { - this.setShowSkeleton(false); - console.error('error: ', error); - } + }); }, // 获取可变全局任务 getGlobalData() { - const { roleId, timeNode, timeUnit } = this; - const param = { roleId, timeNode, timeUnit }; + const { roleId, timeNode, timeUnit, projectId } = this; + const param = { roleId, timeNode, timeUnit, projectId }; this.getGlobal(param); }, diff --git a/src/store/project/actions.js b/src/store/project/actions.js index 57b1c9b..5dfa3d4 100644 --- a/src/store/project/actions.js +++ b/src/store/project/actions.js @@ -1,18 +1,3 @@ -const actions = { - /** - * 通过项目id获取项目信息 - * @param {any} commit - * @param {object} params 提交的参数 - */ - async getProjectById({ commit }, params) { - try { - const data = await uni.$u.api.findProjectById(params); - commit('setProject', data); - return data; - } catch (error) { - throw error || '获取项目信息失败'; - } - }, -}; +const actions = {}; export default actions; diff --git a/src/store/role/actions.js b/src/store/role/actions.js index 7ceb712..5dfa3d4 100644 --- a/src/store/role/actions.js +++ b/src/store/role/actions.js @@ -1,19 +1,3 @@ -const actions = { - /** - * 通过项目id获取角色信息 - * @param {any} commit - * @param {object} params 提交的参数 - */ - async getRoles({ commit }, params) { - try { - const res = await uni.$u.api.findShowRole(params); - commit('setInvisibleRoles', res.invisibleList); - commit('setVisibleRoles', res.visibleList); - return res; - } catch (error) { - throw error || '获取角色信息失败'; - } - }, -}; +const actions = {}; export default actions; diff --git a/src/store/task/actions.js b/src/store/task/actions.js index b8dfbfb..40cdb31 100644 --- a/src/store/task/actions.js +++ b/src/store/task/actions.js @@ -4,13 +4,14 @@ const actions = { * @param {*} commit * @param {string} roleId 角色id */ - async getPermanent({ commit }, roleId) { - try { - const data = await uni.$u.api.getPermanent({ roleId }); - commit('setPermanents', data); - } catch (error) { - console.log('task actions getPermanent error: ', error); - } + getPermanent({ commit }, param) { + uni.$t.$q.getPermanent(param, (err, data) => { + if (err) { + console.error('err: ', err); + } else { + commit('setPermanents', data); + } + }); }, /** @@ -18,27 +19,14 @@ const actions = { * @param {*} commit * @param {object} param 请求参数 roleId, timeNode, timeUnit */ - async getGlobal({ commit }, param) { - try { - const data = await uni.$u.api.getGlobal(param); - commit('setDailyTasks', data); - } catch (error) { - console.log('task actions getGlobal error: ', error); - } - }, - - /** - * 根据时间基准点和角色查找定期任务 - * @param {object} param 查询参数 - * @param {number} param.queryType 必填 0 -> 向上 1 -> 向下 - */ - // eslint-disable-next-line - async getRegulars({ commit }, param) { - try { - return await uni.$u.api.getRegularTask(param); - } catch (error) { - throw error || '获取定期任务失败'; - } + getGlobal({ commit }, param) { + uni.$t.$q.getGlobal(param, (err, data) => { + if (err) { + console.error('err: ', err); + } else { + commit('setDailyTasks', data); + } + }); }, }; diff --git a/src/store/task/state.js b/src/store/task/state.js index c2ed496..a4b4fab 100644 --- a/src/store/task/state.js +++ b/src/store/task/state.js @@ -16,8 +16,7 @@ const state = { bottomEnd: false, // 时间轴向下查任务到底了 permanents: [], // 永久日常任务 dailyTasks: [], // 日常任务 - // 定期任务 - tasks: [], + tasks: [], // 所有的定期任务 showSkeleton: false, // 定期任务骨架屏 }; diff --git a/src/utils/cache.js b/src/utils/cache.js index 7bb7ee9..ad64da3 100644 --- a/src/utils/cache.js +++ b/src/utils/cache.js @@ -36,8 +36,8 @@ export default { putProjects(data) { try { if (!data || !data.length) return; // 服务端没数据不做操作 - - let locals = JSON.parse(uni.$t.storage.getStorageSync('projects')) || []; + let value = uni.$t.storage.getStorageSync('projects'); + let locals = value ? JSON.parse(value) : []; if (!locals || !locals.length) { // 本地没数据 locals = data || []; @@ -62,15 +62,72 @@ export default { }, /** - * 小红点 获取 + * 当前显示的角色信息 获取 + * @param {object} params + * @returns + */ + async getShowRole(projectId) { + try { + const data = await uni.$t.storage.getStorage(`roles_${projectId}`); + return JSON.parse(data); + } catch (error) { + console.error('error: ', error); + return null; + } + }, + + /** + * 当前显示的角色信息 存 + * @param {array} data + */ + putShowRole(projectId, data) { + try { + if (!data || !data.visibleList || !data.visibleList.length) return; // 服务端没数据不做操作 + let value = uni.$t.storage.getStorageSync(`roles_${projectId}`); + let locals = value ? JSON.parse(value) : null; + if (!locals || !locals.length) { + // 本地没数据 + locals = data || null; + } else { + // 本地有数据 + data.invisibleList.forEach(item => { + let invisibleListLocalData = locals.invisibleList.find(local => item.id === local.id); + if (invisibleListLocalData) { + // 有相同数据 就用新的data里的数据 + invisibleListLocalData = item; + } else { + // 没有就直接存本地 + locals.invisibleList.push(item); + } + }); + data.visibleList.forEach(item => { + let localData = locals.visibleList.find(local => item.id === local.id); + if (localData) { + // 有相同数据 就用新的data里的数据 + localData = item; + } else { + // 没有就直接存本地 + locals.visibleList.push(item); + } + }); + } + uni.$t.storage.setStorage(`roles_${projectId}`, locals); + } catch (error) { + console.error('error: ', error); + uni.$t.storage.setStorage(`roles_${projectId}`, []); + } + }, + + /** + * 定期任务 获取 * @param {number} startTime * @param {number} endTime * @returns */ - async getDotListByDay(startTime, endTime) { + async getStorageRegularTask(params) { try { - const data = await uni.$t.storage.getStorage('dotList'); - return filter.projects(JSON.parse(data), startTime, endTime); + const data = await uni.$t.storage.getStorage(`plan_task_${params.projectId}_${params.roleId}`); + return JSON.parse(data); } catch (error) { console.error('error: ', error); return []; @@ -78,33 +135,132 @@ export default { }, /** - * 小红点 存 + * 定期任务 存 * @param {array} data */ - putDotList(data) { + putStorageRegularTask(params, data) { try { if (!data || !data.length) return; // 服务端没数据不做操作 - let locals = JSON.parse(uni.$t.storage.getStorageSync('dotList')) || []; + let value = uni.$t.storage.getStorageSync(`plan_task_${params.projectId}_${params.roleId}`); + let locals = value ? JSON.parse(value) : []; if (!locals || !locals.length) { // 本地没数据 locals = data || []; } else { // 本地有数据 data.forEach(item => { - let localData = locals.find(local => item === local); + let localData = locals.find(local => item.id === local.id); + if (localData) { + // 有相同数据 就用新的data里的数据 + localData = item; + } else { + // 没有就直接存本地 + locals.push(item); + } + }); + } + uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, locals); + } catch (error) { + console.error('error: ', error); + uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, []); + } + }, + + /** + * 永久的日常任务 获取 + * @param {number} startTime + * @param {number} endTime + * @returns + */ + async getStoragePermanent(params) { + try { + const data = await uni.$t.storage.getStorage(`fixed_tasks_${params.projectId}`); + return JSON.parse(data); + } catch (error) { + console.error('error: ', error); + return []; + } + }, + + /** + * 永久的日常任务 存 + * @param {array} data + */ + putStoragePermanent(params, data) { + try { + if (!data || !data.length) return; // 服务端没数据不做操作 + let value = uni.$t.storage.getStorageSync(`fixed_tasks_${params.projectId}`); + let locals = value ? JSON.parse(value) : []; + if (!locals || !locals.length) { + // 本地没数据 + locals = data || []; + } else { + // 本地有数据 + data.forEach((item, index) => { + let localData = locals.find(local => item.detailId === local.detailId); + if (localData) { + // 有相同数据 就用新的data里的数据 + localData = item; + } else { + locals.splice(index, 1); + // 没有就直接存本地 + locals.push(item); + } + }); + } + uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}`, locals); + } catch (error) { + console.error('error: ', error); + uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}`, []); + } + }, + + /** + * 日常任务 获取 + * @param {number} startTime + * @param {number} endTime + * @returns + */ + async getDailyTask(params) { + try { + const data = await uni.$t.storage.getStorage(`variable_tasks_${params.projectId}`); + return JSON.parse(data); + } catch (error) { + console.error('error: ', error); + return []; + } + }, + + /** + * 日常任务 存 + * @param {array} data + */ + putDailyTask(params, data) { + try { + if (!data || !data.length) return; // 服务端没数据不做操作 + let value = uni.$t.storage.getStorageSync(`variable_tasks_${params.projectId}`); + let locals = value ? JSON.parse(value) : []; + if (!locals || !locals.length) { + // 本地没数据 + locals = data || []; + } else { + // 本地有数据 + data.forEach((item, index) => { + let localData = locals.find(local => item.detailId === local.detailId); if (localData) { // 有相同数据 就用新的data里的数据 localData = item; } else { + locals.splice(index, 1); // 没有就直接存本地 locals.push(item); } }); } - uni.$t.storage.setStorage('dotList', locals); + uni.$t.storage.setStorage(`variable_tasks_${params.projectId}`, locals); } catch (error) { console.error('error: ', error); - uni.$t.storage.setStorage('dotList', []); + uni.$t.storage.setStorage(`variable_tasks_${params.projectId}`, []); } }, }; diff --git a/src/utils/cacheAndRequest.js b/src/utils/cacheAndRequest.js index 8662055..1701342 100644 --- a/src/utils/cacheAndRequest.js +++ b/src/utils/cacheAndRequest.js @@ -24,7 +24,6 @@ export default { uni.$t.cache .getProjectsByDay(startTime, endTime) .then(data => { - // console.log('cache data: ', data); !remote && fn(null, data); }) .catch(err => !remote && fn(err)); @@ -34,7 +33,6 @@ export default { uni.$u.api .getProjects(startTime, endTime) .then(data => { - // console.log('api data: ', data); remote = true; fn(null, data); // 存api到cache里 @@ -45,17 +43,15 @@ export default { }, /** - * 查询小红点 - * @param { string } endTime 结束时间 - * @param { string } startTime 开始时间 + * 通过项目id获取角色信息 + * @param {object} params 提交的参数 */ - findRedPoint(startTime, endTime, fn) { + findShowRole(params, fn) { let remote = false; // 有缓存 且 服务端数据未返回 就先返回缓存 uni.$t.cache - .getDotListByDay(startTime, endTime) + .getShowRole(params.projectId) .then(data => { - // console.log('cache data: ', data); !remote && fn(null, data); }) .catch(err => !remote && fn(err)); @@ -63,13 +59,98 @@ export default { waitTokenRequest(() => { // 拿到api数据后 再用api的数据 uni.$u.api - .findRedPoint(startTime, endTime) + .findShowRole(params) .then(data => { - // console.log('api data: ', data); remote = true; fn(null, data); // 存api到cache里 - uni.$t.cache.putDotList(data); + uni.$t.cache.putShowRole(params.projectId, data); + }) + .catch(err => fn(err)); + }); + }, + + /** + * 根据时间基准点和角色查找定期任务 + * @param {object} params 提交的参数 + */ + getRegularTask(params, fn) { + let remote = false; + // 有缓存 且 服务端数据未返回 就先返回缓存 + uni.$t.cache + .getStorageRegularTask(params) + .then(data => { + console.log('cache data: ', data); + !remote && fn(null, data); + }) + .catch(err => !remote && fn(err)); + + waitTokenRequest(() => { + // 拿到api数据后 再用api的数据 + uni.$u.api + .getRegularTask(params) + .then(data => { + console.log('api data: ', data); + remote = true; + fn(null, data); + // 存api到cache里 + uni.$t.cache.putStorageRegularTask(params, data); + }) + .catch(err => fn(err)); + }); + }, + + /** + * 根据角色查找永久的日常任务 + * @param {object} params 提交的参数 + */ + getPermanent(params, fn) { + let remote = false; + // 有缓存 且 服务端数据未返回 就先返回缓存 + uni.$t.cache + .getStoragePermanent(params) + .then(data => { + !remote && fn(null, data); + }) + .catch(err => !remote && fn(err)); + + waitTokenRequest(() => { + // 拿到api数据后 再用api的数据 + uni.$u.api + .getPermanent(params) + .then(data => { + remote = true; + fn(null, data); + // 存api到cache里 + uni.$t.cache.putStoragePermanent(data); + }) + .catch(err => fn(err)); + }); + }, + + /** + * 根据时间和角色查找日常任务 + * @param {object} params 提交的参数 + */ + getGlobal(params, fn) { + let remote = false; + // 有缓存 且 服务端数据未返回 就先返回缓存 + uni.$t.cache + .getDailyTask(params) + .then(data => { + !remote && fn(null, data); + }) + .catch(err => !remote && fn(err)); + + waitTokenRequest(() => { + // 拿到api数据后 再用api的数据 + uni.$u.api + .getGlobal(params) + .then(data => { + remote = true; + fn(null, data); + // 存api到cache里 + uni.$t.cache.putDailyTask(data); }) .catch(err => fn(err)); }); From 81032bacb5e684dd0756ea87bb918be2870afec1 Mon Sep 17 00:00:00 2001 From: song Date: Mon, 23 Aug 2021 19:59:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20api=20=E5=AD=98storage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- src/components/Plugin/Plugin.vue | 43 ++++----- src/pages/project/project.vue | 68 +++++++------- src/utils/cache.js | 148 +++++++++++++++++++++++++++---- src/utils/cacheAndRequest.js | 34 ++++++- src/utils/storage.js | 16 ++++ 6 files changed, 241 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba83601..b777d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ -# 0.1.0 (2021-08-20) +# 0.1.0 (2021-08-23) ### 🌟 新功能 范围|描述|commitId --|--|-- + - | api封装 | [7d4edfc](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/7d4edfc) - | cache indexedDB处理 | [3388967](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/3388967) calendar, tall.js | 上下滑动切换日历的模式,tall.js中domain根据环境变量切换 | [364e25d](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/364e25d) - | db store | [6414c4f](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/6414c4f) diff --git a/src/components/Plugin/Plugin.vue b/src/components/Plugin/Plugin.vue index 20d7127..6136b8c 100644 --- a/src/components/Plugin/Plugin.vue +++ b/src/components/Plugin/Plugin.vue @@ -81,28 +81,31 @@ export default { // 获取插件信息 async getPlugin() { const { pluginId, styleType } = this; - const data = await this.$u.api.getOtherPlugin({ - pluginId, - styleType, - }); - if (!data || !data.id) return; - const reg = /data-root=["|']?(\w+)["|']?/gi; - let uuid = ''; - // FIXME: 没有兼容 只有js, 没有html的情况 - if (data.html) { - // 查有没有data-root=“xxx” 有的话 将xxx替换为 pluginTaskId - - if (reg.test(data.html)) { - uuid = RegExp.$1; - const str = data.html.replace(new RegExp(uuid, 'g'), `p${this.pluginTaskId}`); - this.pluginContent = str; + const params = { pluginId, styleType }; + this.$t.$q.getOtherPlugin(params, (err, data) => { + if (err) { + console.error('err: ', err); } else { - this.pluginContent = data.html; - } + if (!data || !data.id) return; + const reg = /data-root=["|']?(\w+)["|']?/gi; + let uuid = ''; + // FIXME: 没有兼容 只有js, 没有html的情况 + if (data.html) { + // 查有没有data-root=“xxx” 有的话 将xxx替换为 pluginTaskId - const str = data.js.replace(new RegExp(uuid, 'g'), `p${this.pluginTaskId}`); - this.handleDom(str); - } + if (reg.test(data.html)) { + uuid = RegExp.$1; + const str = data.html.replace(new RegExp(uuid, 'g'), `p${this.pluginTaskId}`); + this.pluginContent = str; + } else { + this.pluginContent = data.html; + } + + const str = data.js.replace(new RegExp(uuid, 'g'), `p${this.pluginTaskId}`); + this.handleDom(str); + } + } + }); // if (data.js) { // if (reg.test(data.js)) { diff --git a/src/pages/project/project.vue b/src/pages/project/project.vue index ad150fe..7ee3997 100644 --- a/src/pages/project/project.vue +++ b/src/pages/project/project.vue @@ -193,30 +193,31 @@ export default { // 切换了 颗粒度 || 角色时候 获取初始定期任务 getInitTasks() { + // 预加载 上下的定期任务 + function fn(that) { + const detailId = that.tasks.findIndex(task => task.detailId); + console.log('detailId: ', detailId); + if (detailId !== -1) { + that.$nextTick(() => { + // 向上拿数据 + const { tasks, timeGranularity } = that; + that.getTasks({ timeNode: +tasks[0].planStart, queryType: 0, queryNum: 6 }); + // 向下拿数据 + const nextQueryTime = +that.$t.time.add(+tasks[tasks.length - 1].planStart, 1, timeGranularity); + that.getTasks({ timeNode: nextQueryTime, queryType: 1, queryNum: 6 }); + }); + } else { + // 没有任务 上下显示时间刻度 + // 向上加载 + // that.setPrevTasks(); + // // 向下加载 + // that.setNextTasks(); + } + } // 根据时间基准点和角色查找定期任务 this.getTasks({ queryType: 0 }); // 根据项目id获取角色列表 - this.getTasks({ queryType: 1 }); - - // 预加载 上下的定期任务 - // const detailId = this.tasks.findIndex(task => task.detailId); - // console.log('detailId: ', detailId); - // if (detailId !== -1) { - // this.$nextTick(() => { - // // 向上拿数据 - // const { tasks, timeGranularity } = this; - // this.getTasks({ timeNode: +tasks[0].planStart, queryType: 0, queryNum: 6 }); - // // 向下拿数据 - // const nextQueryTime = +this.$t.time.add(+tasks[tasks.length - 1].planStart, 1, timeGranularity); - // this.getTasks({ timeNode: nextQueryTime, queryType: 1, queryNum: 6 }); - // }); - // } else { - // // 没有任务 上下显示时间刻度 - // // 向上加载 - // this.setPrevTasks(); - // // 向下加载 - // this.setNextTasks(); - // } + this.getTasks({ queryType: 1 }, fn); }, // 设置 初始显示角色信息 @@ -239,9 +240,10 @@ export default { * @param {string} query.queryNum 查找颗粒度数量 默认3个 * @param {string} query.queryType 0向上查找 1向下查找(默认) 下查包含自己,上查不包含 */ - getTasks(query) { - this.setShowSkeleton(true); - const { roleId, timeNode, timeUnit, projectId } = this; + getTasks(query, fn) { + const that = this; + that.setShowSkeleton(true); + const { roleId, timeNode, timeUnit, projectId } = that; const params = { roleId, timeNode: query.timeNode || timeNode, @@ -250,18 +252,24 @@ export default { queryType: query.queryType, projectId, }; - this.$t.$q.getRegularTask(params, (err, data) => { + + that.$t.$q.getRegularTask(params, function (err, data) { if (err) { - this.setShowSkeleton(false); + that.setShowSkeleton(false); console.error('err: ', err); } else { - this.setShowSkeleton(false); + console.log('++++'); + that.setShowSkeleton(false); // 0 -> 向上 1 -> 向下 if (data && data.length) { - console.log('data: ', data); - this.replacePrevData(data, params.queryType); + console.log(1); + that.replacePrevData(data, params.queryType); } else { - params.queryType === 0 ? this.setPrevTasks() : this.setNextTasks(); + params.queryType === 0 ? that.setPrevTasks() : that.setNextTasks(); + } + if (that.tasks.length && fn) { + console.log(2); + fn(that); } } }); diff --git a/src/utils/cache.js b/src/utils/cache.js index ad64da3..f715275 100644 --- a/src/utils/cache.js +++ b/src/utils/cache.js @@ -10,6 +10,66 @@ export const filter = { if (!data || !data.length) return []; return data.filter(item => start <= +item.endTime && end >= +item.startTime); }, + + /** + * 角色 过滤获取到的数据 根据开始截止时间 + * @param {object} data 缓存拿到的数据 + * @returns + */ + roles(data) { + if (!data || !data.length) return []; + return data; + }, + + /** + * 定期任务 过滤获取到的数据 根据开始截止时间 + * @param {object} data 缓存拿到的数据 + * @param {number} timeNode 时间基准点 ms + * @param {number} queryNum 颗粒度数量 + * @param {number} timeUnit 时间颗粒度 + * @returns + */ + planTask(data, timeNode, queryNum, timeUnit) { + 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); + }, + + /** + * 永久日常任务 过滤获取到的数据 根据开始截止时间 + * @param {object} data 缓存拿到的数据 + * @returns + */ + fixedTasks(data) { + if (!data || !data.length) return []; + return data; + }, + + /** + * 日常任务 过滤获取到的数据 根据开始截止时间 + * @param {object} data 缓存拿到的数据 + * @param {number} timeNode 时间基准点 ms + * @returns + */ + dailyTask(data, timeNode) { + if (!data || !data.length) return []; + // TODO: 缺少通过时间颗粒度筛选数据 任务没有返回时间颗粒度标签 + return data.filter(item => timeNode <= +item.endTime && timeNode >= +item.startTime); + }, + + /** + * 插件 过滤获取到的数据 根据插件id + * @param {object} data 缓存拿到的数据 + * @returns + */ + plugin(data) { + if (!data || !data.id) return null; + return data; + }, }; export default { @@ -54,6 +114,7 @@ export default { } }); } + uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage('projects', locals); } catch (error) { console.error('error: ', error); @@ -69,7 +130,7 @@ export default { async getShowRole(projectId) { try { const data = await uni.$t.storage.getStorage(`roles_${projectId}`); - return JSON.parse(data); + return filter.roles(JSON.parse(data)); } catch (error) { console.error('error: ', error); return null; @@ -111,6 +172,7 @@ export default { } }); } + uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`roles_${projectId}`, locals); } catch (error) { console.error('error: ', error); @@ -127,7 +189,7 @@ export default { async getStorageRegularTask(params) { try { const data = await uni.$t.storage.getStorage(`plan_task_${params.projectId}_${params.roleId}`); - return JSON.parse(data); + return filter.planTask(JSON.parse(data), params.timeNode, params.queryNum, params.timeUnit); } catch (error) { console.error('error: ', error); return []; @@ -159,6 +221,7 @@ export default { } }); } + uni.$t.cache.checkCapacity(); uni.$t.storage.setStorage(`plan_task_${params.projectId}_${params.roleId}`, locals); } catch (error) { console.error('error: ', error); @@ -174,8 +237,8 @@ export default { */ async getStoragePermanent(params) { try { - const data = await uni.$t.storage.getStorage(`fixed_tasks_${params.projectId}`); - return JSON.parse(data); + const data = await uni.$t.storage.getStorage(`fixed_tasks_${params.projectId}_${params.roleId}`); + return filter.fixedTasks(JSON.parse(data)); } catch (error) { console.error('error: ', error); return []; @@ -189,7 +252,7 @@ export default { putStoragePermanent(params, data) { try { if (!data || !data.length) return; // 服务端没数据不做操作 - let value = uni.$t.storage.getStorageSync(`fixed_tasks_${params.projectId}`); + let value = uni.$t.storage.getStorageSync(`fixed_tasks_${params.projectId}_${params.roleId}`); let locals = value ? JSON.parse(value) : []; if (!locals || !locals.length) { // 本地没数据 @@ -208,23 +271,23 @@ export default { } }); } - uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}`, locals); + uni.$t.cache.checkCapacity(); + uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}_${params.roleId}`, locals); } catch (error) { console.error('error: ', error); - uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}`, []); + uni.$t.storage.setStorage(`fixed_tasks_${params.projectId}_${params.roleId}`, []); } }, /** * 日常任务 获取 - * @param {number} startTime - * @param {number} endTime + * @param {number} timeNode * @returns */ async getDailyTask(params) { try { - const data = await uni.$t.storage.getStorage(`variable_tasks_${params.projectId}`); - return JSON.parse(data); + const data = await uni.$t.storage.getStorage(`variable_tasks_${params.projectId}_${params.roleId}`); + return filter.dailyTask(JSON.parse(data), params.timeNode); } catch (error) { console.error('error: ', error); return []; @@ -238,29 +301,82 @@ export default { putDailyTask(params, data) { try { if (!data || !data.length) return; // 服务端没数据不做操作 - let value = uni.$t.storage.getStorageSync(`variable_tasks_${params.projectId}`); + let value = uni.$t.storage.getStorageSync(`variable_tasks_${params.projectId}_${params.roleId}`); let locals = value ? JSON.parse(value) : []; if (!locals || !locals.length) { // 本地没数据 locals = data || []; } else { // 本地有数据 - data.forEach((item, index) => { + data.forEach(item => { let localData = locals.find(local => item.detailId === local.detailId); if (localData) { // 有相同数据 就用新的data里的数据 localData = item; } else { - locals.splice(index, 1); // 没有就直接存本地 locals.push(item); } }); } - uni.$t.storage.setStorage(`variable_tasks_${params.projectId}`, locals); + uni.$t.cache.checkCapacity(); + uni.$t.storage.setStorage(`variable_tasks_${params.projectId}_${params.roleId}`, locals); } catch (error) { console.error('error: ', error); - uni.$t.storage.setStorage(`variable_tasks_${params.projectId}`, []); + uni.$t.storage.setStorage(`variable_tasks_${params.projectId}_${params.roleId}`, []); } }, + + /** + * 插件信息 获取 + * @param {string} pluginId + * @returns + */ + async getPlugin(pluginId) { + try { + const data = await uni.$t.storage.getStorage(`plugin_${pluginId}`); + return filter.plugin(JSON.parse(data)); + } catch (error) { + console.error('error: ', error); + return null; + } + }, + + /** + * 插件信息 存 + * @param {string} pluginId + * @param {object} data + */ + putPlugin(pluginId, data) { + try { + if (!data || !data.id) return; // 服务端没数据不做操作 + let value = uni.$t.storage.getStorageSync(`plugin_${pluginId}`); + let locals = value ? JSON.parse(value) : null; + if (!locals || !locals.length) { + // 本地没数据 + locals = data || null; + } else { + // 本地有数据 + 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 1701342..f952dca 100644 --- a/src/utils/cacheAndRequest.js +++ b/src/utils/cacheAndRequest.js @@ -80,7 +80,6 @@ export default { uni.$t.cache .getStorageRegularTask(params) .then(data => { - console.log('cache data: ', data); !remote && fn(null, data); }) .catch(err => !remote && fn(err)); @@ -90,7 +89,6 @@ export default { uni.$u.api .getRegularTask(params) .then(data => { - console.log('api data: ', data); remote = true; fn(null, data); // 存api到cache里 @@ -122,7 +120,7 @@ export default { remote = true; fn(null, data); // 存api到cache里 - uni.$t.cache.putStoragePermanent(data); + uni.$t.cache.putStoragePermanent(params, data); }) .catch(err => fn(err)); }); @@ -150,7 +148,35 @@ export default { remote = true; fn(null, data); // 存api到cache里 - uni.$t.cache.putDailyTask(data); + uni.$t.cache.putDailyTask(params, data); + }) + .catch(err => fn(err)); + }); + }, + + /** + * 获取插件信息 + * @param {object} params 提交的参数 + */ + getOtherPlugin(params, fn) { + let remote = false; + // 有缓存 且 服务端数据未返回 就先返回缓存 + uni.$t.cache + .getPlugin(params.pluginId) + .then(data => { + !remote && fn(null, data); + }) + .catch(err => !remote && fn(err)); + + waitTokenRequest(() => { + // 拿到api数据后 再用api的数据 + uni.$u.api + .getOtherPlugin(params) + .then(data => { + remote = true; + fn(null, data); + // 存api到cache里 + uni.$t.cache.putPlugin(params.pluginId, data); }) .catch(err => fn(err)); }); diff --git a/src/utils/storage.js b/src/utils/storage.js index f5bc6cb..cbcdb49 100644 --- a/src/utils/storage.js +++ b/src/utils/storage.js @@ -39,6 +39,7 @@ export default { * @param {*} data */ setStorage(key, data) { + uni.$t.storage.checkCapacity(); return new Promise((resolve, reject) => { const value = typeof data === 'string' ? data : JSON.stringify(data); uni.setStorage({ @@ -97,4 +98,19 @@ export default { clearStorage() { uni.clearStorage(); }, + + // 检测local Storage容量 超出容量清空数据缓存 + checkCapacity() { + if (!window.localStorage) { + console.log('浏览器不支持localStorage '); + 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(); + } + }, }; From b22a3663fbb8b9d946e8d6f0e895ff197b63ed74 Mon Sep 17 00:00:00 2001 From: song Date: Tue, 24 Aug 2021 14:37:58 +0800 Subject: [PATCH 3/4] =?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, From 5a108563cb33c0a1ef3a1459e81a3c146c4a4ff1 Mon Sep 17 00:00:00 2001 From: song Date: Tue, 24 Aug 2021 14:41:34 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2ab023..1aa5ac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -110,6 +110,7 @@ - | 定期任务接口 | [aa4981c](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/aa4981c) - | 定期任务插件 | [92b3254](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/92b3254) - | 定期任务未加载时,显示空的时间轴并能上下滑动 | [ce38093](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/ce38093) + 定期任务本地缓存和api赋值,未完成 | 定期任务本地缓存和api赋值,未完成 | [b22a366](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/b22a366) - | 定期任务骨架屏修改 | [8ff72dd](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/8ff72dd) - | 平车演示临时去掉项目快捷方式的toast提示 | [e0b2c23](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/e0b2c23) - | 手动展开日常任务 | [0a4a622](https://dd.tall.wiki/gitea/ccsens_fe/TALL-MUI-3/commits/0a4a622)