/** * @description 锁信息数据持久化 */ import { defineStore } from 'pinia' import { timeFormat } from 'uview-plus' import { getLockListRequest } from '@/api/lock' import { getPsaawordListRequest } from '@/api/keyboardPwd' import { getKeyListRequest } from '@/api/key' import { setStorage } from '@/utils/storage' export const useLockStore = defineStore('lock', { state() { return { // 锁列表 lockList: [], // 锁总数 lockTotal: 0, // 锁列表搜索数据 lockSearch: { pageNo: 1, pageSize: 50, searchStr: '' }, // 密码列表 passwordList: [], // 密码总数 passwordTotal: 0, // 当前密码详情 currentPasswordInfo: {}, // 密码列表搜索数据 passwordSearch: { pageNo: 1, pageSize: 50, searchStr: '' }, // 电子钥匙总数 keyTotal: 0, // 电子钥匙列表 keyList: [], // 当前电子钥匙详情 currentKeyInfo: {}, // 电子钥匙列表搜索数据 keySearch: { pageNo: 1, pageSize: 50, searchStr: '', endDate: '0', startDate: '0', keyStatus: [110401, 110402, 110412, 110405, 110403], keyRight: 0 } } }, actions: { // 更新锁列表 updateLockList(list) { this.lockList = list }, // 清空列表 clearList(type) { this[type + 'List'] = [] }, // 获取电量icon getPowerIcon(power) { if (power >= 80) { return 'https://oss-lock.xhjcn.ltd/mp/icon_power_5.png' } if (power >= 60) { return 'https://oss-lock.xhjcn.ltd/mp/icon_power_4.png' } if (power >= 40) { return 'https://oss-lock.xhjcn.ltd/mp/icon_power_3.png' } if (power >= 20) { return 'https://oss-lock.xhjcn.ltd/mp/icon_power_2.png' } return 'https://oss-lock.xhjcn.ltd/mp/icon_power_1.png' }, // 将星期转换为中文字符串 convertWeekDaysToChineseString(weekDays) { const dayMap = { 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '日' } const chineseWeekDays = weekDays.map(day => dayMap[day]) return chineseWeekDays.join(',') }, updateLockSearch(search) { this.lockSearch = search }, updatePasswordSearch(search) { this.passwordSearch = search }, updateKeySearch(search) { this.keySearch = search }, updateCurrentKeyInfo(info) { this.currentKeyInfo = info }, updateCurrentPasswordInfo(info) { this.currentPasswordInfo = info }, getRole(userType, keyRight) { if (userType === 110301) { return '超级管理员' } if (keyRight === 1) { return '授权管理员' } return '普通用户' }, getKeyStatus(keyStatus) { if (keyStatus === 110401) { return '' } if (keyStatus === 110403) { return '未生效' } if (keyStatus === 110402) { return '待接收' } if (keyStatus === 110412) { return '已过期' } if (keyStatus === 110405) { return '已冻结' } return '' }, getPasswordStatus(passwordStatus) { if (passwordStatus === 2) { return '已过期' } if (passwordStatus === 3) { return '未生效' } return '' }, getTimeLimit(keyType) { if (keyType === 1) { return '永久' } if (keyType === 2) { return '限时' } if (keyType === 3) { return '单次' } return '循环' }, async getLockList(params) { const { code, data, message } = await getLockListRequest(params) if (code === 0) { this.lockTotal = data.total for (let i = 0; i < data.groupList.length; i++) { for (let j = 0; j < data.groupList[i].lockList.length; j++) { if ( (data.groupList[i].lockList[j].keyType === 2 || data.groupList[i].lockList[j].keyType === 4) && data.groupList[i].lockList[j].keyStatus === 110401 ) { const now = new Date().getTime() const diffInMilliseconds = data.groupList[i].lockList[j].endDate - now const millisecondsPerDay = 24 * 60 * 60 * 1000 const diffInDays = Math.floor(diffInMilliseconds / millisecondsPerDay) if (diffInDays > 0 && diffInDays <= 15) { data.groupList[i].lockList[j].days = diffInDays } } } } if (params.pageNo === 1) { this.lockList = data.groupList } else { this.lockList = this.lockList.concat(data.groupList) } setStorage('lockList', this.lockList) return { code } } return { code, message } }, async getPasswordList(params) { const { code, data, message } = await getPsaawordListRequest(params) if (code === 0) { this.passwordTotal = data.total for (let i = 0; i < data.list.length; i++) { if (data.list[i].keyboardPwdType === 1) { data.list[i].timeText = `${timeFormat(new Date(data.list[i].startDate), 'yyyy-mm-dd h:M')} 单次` } else if (data.list[i].keyboardPwdType === 2) { data.list[i].timeText = `${timeFormat(new Date(data.list[i].created_at), 'yyyy-mm-dd h:M')} 永久` } else if (data.list[i].keyboardPwdType === 3) { data.list[i].timeText = `${data.list[i].validTimeStr} 限时` } else if (data.list[i].keyboardPwdType === 4) { data.list[i].timeText = `${timeFormat(new Date(data.list[i].startDate), 'yyyy-mm-dd h:M')} 清空码` } else { let text = '' if (data.list[i].keyboardPwdType === 5) { text = '周末' } else if (data.list[i].keyboardPwdType === 6) { text = '每日' } else if (data.list[i].keyboardPwdType === 7) { text = '工作日' } else if (data.list[i].keyboardPwdType === 8) { text = '周一' } else if (data.list[i].keyboardPwdType === 9) { text = '周二' } else if (data.list[i].keyboardPwdType === 10) { text = '周三' } else if (data.list[i].keyboardPwdType === 11) { text = '周四' } else if (data.list[i].keyboardPwdType === 12) { text = '周五' } else if (data.list[i].keyboardPwdType === 13) { text = '周六' } else if (data.list[i].keyboardPwdType === 14) { text = '周日' } data.list[i].timeText = `${text} ${data.list[i].hoursStart}:00-${data.list[i].hoursEnd}:00 循环` } if (data.list[i].isCustom === 1) { data.list[i].timeText += ' 自定义' } } if (params.pageNo === 1) { this.passwordList = data.list } else { this.passwordList = this.passwordList.concat(data.list) } return { code } } return { code, message } }, async getKeyList(params) { const { code, data, message } = await getKeyListRequest(params) if (code === 0) { this.keyTotal = data.total for (let i = 0; i < data.list.length; i++) { if (data.list[i].keyType === 1) { data.list[i].timeText = `${timeFormat(new Date(data.list[i].sendDate), 'yyyy-mm-dd h:M')} 永久` } else if (data.list[i].keyType === 2) { data.list[i].timeText = `${timeFormat(new Date(data.list[i].startDate), 'yyyy-mm-dd h:M')} - ${timeFormat(new Date(data.list[i].endDate), 'yyyy-mm-dd h:M')} 限时` } else if (data.list[i].keyType === 3) { data.list[i].timeText = `${timeFormat(new Date(data.list[i].sendDate), 'yyyy-mm-dd h:M')} 单次` } else { data.list[i].timeText = `循环` } } if (params.pageNo === 1) { this.keyList = data.list } else { this.keyList = this.keyList.concat(data.list) } return { code } } return { code, message } } } })