You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

304 lines
9.5 KiB

<template>
<view :style="{ height: height }" class="flex flex-col overflow-hidden u-font-14">
<!-- 标题栏 -->
<Title />
<view class="container flex flex-col flex-1 overflow-hidden bg-gray-100" style="margin: auto">
<!-- 角色栏 -->
<Roles />
<!-- 日常任务面板 -->
<Globals />
<!-- 定期任务面板 -->
<TimeLine @getTasks="getTasks" class="flex-1 overflow-hidden" ref="child" />
</view>
</view>
</template>
<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
import mixin from '@/mixins/timeline';
export default {
mixins: [mixin],
data() {
return { height: '' };
},
computed: {
...mapState('user', ['user', 'token']),
...mapState('role', ['visibleRoles', 'roleId']),
...mapState('task', ['timeNode', 'timeUnit', 'tasks']),
...mapGetters('task', ['timeGranularity']),
},
onLoad(options) {
this.init(options);
},
watch: {
/**
* 当时间基准点发生变化时
* 重新根据时间和角色查询普通日常任务
* 永久日常任务不发生改变
*/
async timeNode(val) {
console.log('时间基准点发生变化: ', val);
if (val && this.roleId) {
// 根据时间和角色查找日常任务
await this.initTasks();
}
},
/**
* 当角色发生变化时
* 重新查询永久日常任务和普通日常任务
* 注意: 切换角色后 重新设置了时间基准点 时间基准点一定会变
* 所以监听时间基准点获取 可变日常任务即可 这里不用获取 避免重复获取
*/
roleId(val) {
if (val) {
console.log('角色发生变化: ', val);
this.setTimeNode(Date.now());
// 根据角色查找永久的日常任务
this.getPermanent(val);
}
},
},
mounted() {
this.height = window.screen.height + 'px';
},
onUnload() {
this.clearEndFlag();
this.clearTasks();
},
methods: {
...mapActions('user', ['getToken']),
...mapActions('project', ['getProjectById']),
...mapActions('role', ['getRoles']),
...mapActions('task', ['getRegulars', 'getPermanent', 'getGlobal']),
...mapMutations('user', ['setToken']),
...mapMutations('project', ['setProject', 'setProjectName']),
...mapMutations('role', ['setInvisibleRoles', 'setVisibleRoles', 'setRoleId']),
...mapMutations('task', [
'setPermanents',
'setUpTasks',
'setDownTasks',
'setDailyTasks',
'setTimeNode',
'clearTasks',
'clearEndFlag',
'setShowSkeleton',
'setTopEnd',
'setBottomEnd',
]),
/**
* 初始化
* @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);
}
}
// 参数里有项目名称 就设置标题里的项目名称
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);
}
} catch (error) {
console.error('project init function:', error);
}
},
// 初始化 时间轴
async initTasks() {
// 清空日常任务的数据
this.setPermanents([]);
this.setDailyTasks([]);
// 清空定期任务数据
this.clearTasks();
// 到顶的标志复位
// 到底的标志复位
this.clearEndFlag();
// 根据时间和角色查找日常任务
this.getGlobalData();
// 向上加载空数据
this.setPrevTasks();
// 向下加载空数据
this.setNextTasks();
await this.getInitTasks();
setTimeout(() => {
this.$refs.child.setScrollPosition();
}, 2000);
},
// 切换了 颗粒度 || 角色时候 获取初始定期任务
async getInitTasks() {
// 根据时间基准点和角色查找定期任务
await this.getTasks({ queryType: 0 });
await 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();
}
},
// 设置 初始显示角色信息
setInitialRoleId(visibleList) {
const index = visibleList.findIndex(item => item.mine === '1');
const currentRole = index > 0 ? visibleList[index] : visibleList[0];
const storageRoleId = this.$t.storage.getStorageSync('roleId');
const currentRoleId = storageRoleId ? storageRoleId : currentRole ? currentRole.id : '';
this.setRoleId(currentRoleId);
// 清空storage
this.$t.storage.setStorageSync('roleId', '');
},
/**
* 根据时间基准点和角色查找定期任务
* @param {object} query
* @param {string} query.roleId 角色id
* @param {string} query.timeNode 时间基准点 默认当前
* @param {string} query.timeUnit 时间颗粒度 默认天
* @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);
} 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 };
this.getGlobal(param);
},
// 设置时间轴向上的空数据
setPrevTasks() {
this.setTopEnd(true);
let sTime = '';
if (!this.tasks || !this.tasks.length) {
sTime = +new Date().getTime();
} else {
sTime = +this.tasks[0].planStart - 0;
}
const initData = this.setTime(sTime, true);
this.setUpTasks(initData);
},
// 设置时间轴向下的空数据
setNextTasks() {
this.setBottomEnd(true);
let sTime = '';
if (!this.tasks || !this.tasks.length) {
sTime = +new Date().getTime();
} else {
sTime = +this.tasks[this.tasks.length - 1].planStart - 0;
}
const initData = this.setTime(sTime, false);
this.setDownTasks(initData);
},
// 筛选相同的时间替换数据
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) {
newTasks.splice(i, 0, item);
}
}
i++;
}
}
}
}
this.clearTasks();
type === 0 ? this.setUpTasks(newTasks) : this.setDownTasks(newTasks);
},
},
};
</script>