Browse Source

feat: api封装

pull/45/head
song 4 years ago
parent
commit
7d4edfc92b
  1. 5
      CHANGELOG.md
  2. 2
      src/components/Roles/Roles.vue
  3. 24
      src/pages/index/index.vue
  4. 179
      src/pages/project/project.vue
  5. 17
      src/store/project/actions.js
  6. 18
      src/store/role/actions.js
  7. 44
      src/store/task/actions.js
  8. 3
      src/store/task/state.js
  9. 180
      src/utils/cache.js
  10. 103
      src/utils/cacheAndRequest.js

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

2
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']),

24
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);
}
},
//

179
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) {
// tokenuserIdtoken
// token userId
if (!options || !options.u) {
// u (userId)
this.$t.ui.showToast('缺少用户信息参数');
} else {
await this.getToken(options.u);
}
init(options) {
if (!this.token) {
// tokenuserIdtoken
// 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);
},

17
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;

18
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;

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

3
src/store/task/state.js

@ -16,8 +16,7 @@ const state = {
bottomEnd: false, // 时间轴向下查任务到底了
permanents: [], // 永久日常任务
dailyTasks: [], // 日常任务
// 定期任务
tasks: [],
tasks: [], // 所有的定期任务
showSkeleton: false, // 定期任务骨架屏
};

180
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}`, []);
}
},
};

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

Loading…
Cancel
Save