Files
yunzerwebsiteallinone/uniapp/api/oaSchedule.js
T
2026-11-16 17:22:50 +08:00

142 lines
3.7 KiB
JavaScript

import { request } from '@/api/request.js'
import { getBaseURL } from '@/api/config.js'
import { getToken } from '@/utils/auth.js'
export const OA_SCHEDULE_PRIORITY_OPTIONS = [
{ value: 0, label: '普通' },
{ value: 1, label: '重要' },
{ value: 2, label: '紧急' }
]
export function normalizeOaSchedule(item) {
const date = String(item.schedule_date || '').slice(0, 10)
const startTime = item.start_time || ''
let timestamp = 0
if (date) {
const [y, m, d] = date.split('-').map(Number)
const [hh = 0, mm = 0] = startTime ? startTime.split(':').map(Number) : [0, 0]
timestamp = new Date(y, m - 1, d, hh, mm).getTime()
}
return {
...item,
schedule_date: date,
completed: item.status === 1,
allDay: item.all_day === 1,
timestamp
}
}
export async function fetchOaSchedules(params = {}) {
const data = await request({
url: '/api/oa/schedule/list',
data: {
page: params.page || 1,
pageSize: params.pageSize || 500,
keyword: params.keyword || '',
status: params.status || '',
priority: params.priority || '',
start_date: params.startDate || '',
end_date: params.endDate || '',
category: params.category || 'all'
}
})
const list = (data.list || []).map(normalizeOaSchedule)
return { list, total: data.total || 0, counts: data.counts || {} }
}
export async function getOaSchedule(id) {
const data = await request({ url: `/api/oa/schedule/detail/${id}` })
if (!data || !data.item) return null
return { ...normalizeOaSchedule(data.item), images: data.images || [] }
}
export async function createOaSchedule(payload) {
return request({
url: '/api/oa/schedule/create',
method: 'POST',
data: buildOaSchedulePayload(payload)
})
}
export async function updateOaSchedule(id, payload) {
return request({
url: `/api/oa/schedule/update/${id}`,
method: 'POST',
data: buildOaSchedulePayload(payload)
})
}
export async function deleteOaSchedule(id) {
await request({ url: `/api/oa/schedule/delete/${id}`, method: 'DELETE' })
return true
}
export async function toggleOaScheduleComplete(id) {
const res = await request({ url: `/api/oa/schedule/finish/${id}`, method: 'POST' })
return { status: res?.status ?? 0, completed: res?.status === 1 }
}
export function uploadOaScheduleImage(filePath) {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: `${getBaseURL().replace(/\/$/, '')}/api/oa/schedule/upload`,
filePath,
name: 'file',
header: { Authorization: `Bearer ${getToken()}` },
success(res) {
let body = {}
try {
body = JSON.parse(res.data || '{}')
} catch (e) {
}
if (res.statusCode >= 200 && res.statusCode < 300 && body.code === 200 && body.data) {
resolve(body.data)
} else {
const msg = body.msg || `上传失败(${res.statusCode})`
uni.showToast({ title: msg, icon: 'none' })
reject(new Error(msg))
}
},
fail(err) {
uni.showToast({ title: '网络异常,上传失败', icon: 'none' })
reject(err)
}
})
})
}
export async function setOaScheduleImages(id, images) {
const res = await request({
url: `/api/oa/schedule/images/${id}`,
method: 'POST',
data: {
images: (images || []).map(img => ({
id: Number(img.id) || 0,
url: img.url,
name: img.name || ''
}))
}
})
return res?.images || []
}
function buildOaSchedulePayload(p) {
return {
title: p.title,
content: p.content || '',
schedule_date: p.scheduleDate,
start_time: p.allDay ? '' : p.startTime || '',
end_time: p.allDay ? '' : p.endTime || '',
all_day: p.allDay ? 1 : 0,
priority: p.priority || 0,
color: p.color || '#409EFF',
reminder_enabled: false
}
}
export function getOaPriorityLabel(priority) {
return (
OA_SCHEDULE_PRIORITY_OPTIONS.find(item => item.value === Number(priority))?.label || '普通'
)
}