Files
Toolbox/miniprogram/mp_time_assistant/api.js
T
yangxiangyuan db7648eb4d feat(mp, web, calendar): 新增微信收藏夹与日历批量生成功能
- 新增微信小程序版个人收藏夹全套功能,包含聚合首页、收藏列表、编辑、详情页面与配套后端API
- 新增web版时光助手门户与个人收藏夹工具,支持与小程序共享后端数据
- 优化日历提醒工具:替换本地bootstrap资源为公共CDN版本,新增批量生成循环提醒的前端界面与后端逻辑,新增数据库batch_id字段,更新鉴权配置文件
- 补充项目配置、站点地图、调试诊断脚本等配套文件
- 完善各模块的页面配置与样式文件
2026-09-02 22:20:37 +08:00

146 lines
4.1 KiB
JavaScript

const app = () => getApp()
const BASE_URL = function () {
try {
const a = getApp()
if (a && a.globalData && a.globalData.apiBase) return a.globalData.apiBase
} catch (e) {}
return 'https://traesite.umersoft.com:8975'
}
const request = (options) => {
return new Promise((resolve, reject) => {
const { url, method = 'GET', data = {}, header = {}, timeout = 30000 } = options
const fullUrl = url.indexOf('http') === 0 ? url : BASE_URL() + url
wx.request({
url: fullUrl,
method,
data,
header: Object.assign({
'content-type': 'application/json',
}, header),
timeout,
success: (res) => {
const body = res.data || {}
if (res.statusCode >= 200 && res.statusCode < 300 && body && body.ok) {
resolve(body)
} else {
const msg = body && body.error ? body.error : `请求失败 (HTTP ${res.statusCode})`
reject(new Error(msg))
}
},
fail: (err) => {
reject(new Error(err && err.errMsg ? err.errMsg : '网络错误'))
},
})
})
}
const uploadFile = (options) => {
return new Promise((resolve, reject) => {
const { url, filePath, name = 'file', formData = {}, timeout = 120000 } = options
const fullUrl = url.indexOf('http') === 0 ? url : BASE_URL() + url
wx.uploadFile({
url: fullUrl,
filePath,
name,
formData,
timeout,
success: (res) => {
try {
const body = typeof res.data === 'string' ? JSON.parse(res.data) : (res.data || {})
if (res.statusCode >= 200 && res.statusCode < 300 && body && body.ok) {
resolve(body)
} else {
const msg = body && body.error ? body.error : `上传失败 (HTTP ${res.statusCode})`
reject(new Error(msg))
}
} catch (e) {
reject(new Error('上传响应解析失败'))
}
},
fail: (err) => {
reject(new Error(err && err.errMsg ? err.errMsg : '上传网络错误'))
},
})
})
}
const core = {
getMeta: () => request({ url: '/api/mp_time_assistant/meta', method: 'GET' }),
getState: () => request({ url: '/api/mp_time_assistant/state', method: 'GET' }),
setActiveKit: (kitId) => request({
url: '/api/mp_time_assistant/state/active-kit',
method: 'PUT',
data: { kitId },
}),
}
const personalCollect = {
list: (keyword = '') => request({
url: '/api/mp_time_assistant/kits/personal_collect/items',
method: 'GET',
data: { keyword: keyword || '' },
}),
get: (id) => request({
url: `/api/mp_time_assistant/kits/personal_collect/items/${encodeURIComponent(id)}`,
method: 'GET',
}),
create: (payload) => request({
url: '/api/mp_time_assistant/kits/personal_collect/items',
method: 'POST',
data: payload,
}),
update: (payload) => request({
url: '/api/mp_time_assistant/kits/personal_collect/items',
method: 'PUT',
data: payload,
}),
remove: (id) => request({
url: '/api/mp_time_assistant/kits/personal_collect/items',
method: 'DELETE',
data: { id },
}),
upload: (filePath, groupId = '') => uploadFile({
url: '/api/mp_time_assistant/kits/personal_collect/upload',
filePath,
name: 'file',
formData: groupId ? { groupId } : {},
}),
}
const chooseChatAttachments = (count = 9) => {
return new Promise((resolve, reject) => {
try {
if (!wx.chooseMessageFile) {
reject(new Error('当前微信版本不支持从聊天选择文件,请升级微信'))
return
}
wx.chooseMessageFile({
count: Math.max(1, Math.min(9, Number(count) || 9)),
type: 'all',
success: (res) => resolve(res.tempFiles || []),
fail: (err) => {
const msg = err && err.errMsg ? String(err.errMsg) : '用户取消或授权失败'
if (msg.indexOf('cancel') !== -1 || msg.indexOf('取消') !== -1) {
resolve([])
} else {
reject(new Error(msg))
}
},
})
} catch (e) {
reject(e)
}
})
}
module.exports = {
BASE_URL,
request,
uploadFile,
core,
personalCollect,
chooseChatAttachments,
}