import {getUserInfo} from "@/api/modules/account"; import {getSignTime, getSignSearch} from "@/api/modules/sign"; import {ElMessage} from "element-plus"; import {getSeatDutyPersonSearch} from "@/api/modules/seat"; import {getConfigConfigKey, editConfigEditConfigKey} from "@/api/modules/config"; import * as Handle from "@/views/staging/common/handle"; import {YMD, YMDHms} from '@/utils/util' import {getDailyReportSearch} from "@/api/modules/daily"; import {getWeekReportSearch} from "@/api/modules/weekly"; const state = { apiProxy: { ztptApi: 'api-ztpt', // 总体平台基本接口 }, userInfo: {}, userRoleList: [], timestamp: new Date().getTime(), tempTimestamp: new Date().getTime(), weekStart: 6, // 日历起始日期,周六 weeklySubmitDay: 5, // 周报提交日期,周五 dutyInfo: {}, publicConfig: { 'sign.begin.time': '', 'sign.remind.time': '', 'sign.end.time': '', 'log.submit.time': '', 'log.remind.time': '', 'week.submit.date': '', 'week.submit.time': '', 'week.remind.time': '', }, signInfo: null, dailyInfo: null, weeklyInfo: null, projectFirstDate: new Date('2023-11-24 00:00:00'), } const getters = { isZBY: (state) => { return state.userRoleList.some(v => v.permissionValue === 'ZBY') }, isZBGL: (state) => { return state.userRoleList.some(v => v.permissionValue === 'ZBGL') }, userRoleStr: (state) => { return state.userRoleList.map(v => v.permissionValue).join(',') }, isWeeklyDay: (state) => { return new Date(state.timestamp).getDay() === state.weeklySubmitDay }, yearWeeks: (state) => { const arr: any = [] const map = new Map() let index = 0 for (let year = state.projectFirstDate.getFullYear(); year <= new Date(state.timestamp).getFullYear(); year++) { let num = 1 let firstWeeklyDate = new Date(year, 0, 1) while (firstWeeklyDate.getDay() !== state.weeklySubmitDay) { firstWeeklyDate = new Date(firstWeeklyDate.getTime() + Handle.oneDayTime) } let start = new Date(firstWeeklyDate.getTime() - Handle.oneDayTime * 6) while (new Date(start).getTime() < new Date(year + 1, 0, 1).getTime()) { const end = new Date(start).getTime() + Handle.oneDayTime * 6 if (end >= state.projectFirstDate.getTime() && end <= state.timestamp) { const obj = { index: index, date: [new Date(start), new Date(end)], dateStr: [YMD(new Date(start)), YMD(new Date(end))], yearWeekStr: `${year}年第${num}周`, weekStr: `第${num}周`, yearWeekSelectValue: `${year}年第${num}周(${YMD(start)}~${YMD(end)})`, weekSelectValue: `第${num}周(${YMD(start)}~${YMD(end)})`, isToday: (new Date(start).getTime() <= new Date(state.timestamp).getTime()) && (new Date(end + Handle.oneDayTime).getTime() > new Date(state.timestamp).getTime()), year: year, week: num, id: `${year}_${num}` } arr.push(obj) map.set(obj.id, obj) index++ } start = new Date(end + Handle.oneDayTime) num++ } } return { list: arr, map: map } } } const mutations = { SET_USER_INFO(state: any, data: any) { state.userInfo = data state.userRoleList = data.roles }, SET_TIMESTAMP(state: any, data: any) { state.timestamp = new Date(data).getTime() state.tempTimestamp = new Date(data).getTime() setInterval(() => { state.timestamp += 1000 if (new Date(state.tempTimestamp).getDay() !== new Date(state.timestamp).getDay()) { state.tempTimestamp = new Date(state.timestamp).getTime() state.signInfo = null } }, 1000) }, SET_DUTY_INFO(state: any, data: any) { state.dutyInfo = data }, SET_PUBLIC_CONFIG(state: any, {data, key}) { state.publicConfig[key] = data }, SET_SIGN_INFO(state: any, data) { state.signInfo = data }, SET_DAILY_INFO(state: any, data) { state.dailyInfo = data }, SET_WEEKLY_INFO(state: any, data) { state.weeklyInfo = data }, } const actions = { LOAD_USER_INFO({ commit }: any, refresh: boolean = false) { return new Promise((resolve, reject) => { if (refresh || !state.userInfo?.id) { getUserInfo().then((res) => { if (res.code === 200) { commit('SET_USER_INFO', res.data) resolve(res.data) } else { reject(res.message) } }) } else { resolve(state.userInfo) } }) }, LOAD_TIMESTAMP({ commit }: any) { getSignTime().then((res) => { if (res.code === 200) { commit('SET_TIMESTAMP', new Date(res.data)) } else { ElMessage.error(res.message) } }) }, LOAD_DUTY_INFO({ commit }: any) { getSeatDutyPersonSearch().then((res) => { if (res.code === 200) { if (res.data) { commit('SET_DUTY_INFO', res.data) } else { ElMessage.error('缺少值班人员') } } else { ElMessage.error(res.message) } }) }, LOAD_PUBLIC_CONFIG({ commit }: any, {key, value}) { const initKey = (k, resolve) => { getConfigConfigKey(k).then((res) => { if (res.code === 200) { commit('SET_PUBLIC_CONFIG', {key: k, data: res.message}) resolve(res.message) } else { ElMessage.error(res.message) } }) } return new Promise((resolve, reject) => { if (value) { editConfigEditConfigKey({ configKey: key, configValue: value }).then(res => { if (res.code === 200) { ElMessage.success(res.message) initKey(key, resolve) } else { ElMessage.error(res.message) } }) } else { initKey(key, resolve) } }) }, LOAD_SIGN_INFO({ commit }: any) { return new Promise((resolve, reject) => { getSignSearch().then((res) => { if (res.code === 200) { commit('SET_SIGN_INFO', res.data) resolve(res.message) } else { ElMessage.error(res.message) } }) }) }, LOAD_DAILY_INFO({ commit }: any) { return new Promise((resolve, reject) => { getDailyReportSearch().then((res) => { if (res.code === 200) { commit('SET_DAILY_INFO', res.data) resolve(res.message) } else { ElMessage.error(res.message) } }) }) }, LOAD_WEEKLY_INFO({ commit }: any) { return new Promise((resolve, reject) => { getWeekReportSearch().then((res) => { if (res.code === 200) { commit('SET_WEEKLY_INFO', res.data) resolve(res.message) } else { ElMessage.error(res.message) } }) }) }, } export default { namespaced: true, state, getters, mutations, actions }