This commit is contained in:
2026-04-16 18:55:11 +08:00
parent 88c080889e
commit 5146e98479
17 changed files with 3171 additions and 674 deletions
+42
View File
@@ -0,0 +1,42 @@
/**
* 认证相关 API
* 对标 PC 前端 src/api/auth.js
*/
import { request } from './request.js'
export const authApi = {
/** 登录 */
login(username, password, remember = false) {
return request.post('/users/login', { username, password, remember })
},
/** 注册 */
register(username, email, password) {
return request.post('/users/register', { username, useremail: email, userpass: password })
},
/** 通过 cookie 获取用户信息 */
getUserInfo() {
return request.post('/users/getinfo', {})
},
/** 修改密码 */
changePassword(oldPass, newPass) {
return request.post('/users/changePassword', { oldpass: oldPass, newpass: newPass })
},
/** 修改邮箱 */
changeEmail(newEmail) {
return request.post('/users/changeEmail', { newemail: newEmail })
},
/** 修改用户信息 */
updateInfo(data) {
return request.post('/users/updateInfo', data)
},
/** 更新头像(文件上传) */
updateAvatar(filePath) {
return request.upload('/users/updateAvatar', filePath)
},
}
+42
View File
@@ -0,0 +1,42 @@
/**
* 采购订单相关 API
* 对标 PC 前端 src/api/purchase.js
*/
import { request } from './request.js'
export const purchaseApi = {
/** 获取订单列表(分页+搜索+状态过滤) */
getOrders(params = {}) {
return request.post('/purchase/getorders', params)
},
/** 获取单个订单详情 */
getOrder(id) {
return request.post('/purchase/getorder', { id })
},
/** 获取各状态订单数量统计 */
getOrderCount() {
return request.post('/purchase/getordercount', {})
},
/** 新增订单 */
addOrder(data) {
return request.post('/purchase/addorder', data)
},
/** 更新订单 */
updateOrder(data) {
return request.post('/purchase/updateorder', data)
},
/** 更新订单状态 */
updateStatus(data) {
return request.post('/purchase/updatestatus', data)
},
/** 删除订单 */
deleteOrder(id) {
return request.post('/purchase/deleteorder', { id })
},
}
+97
View File
@@ -0,0 +1,97 @@
/**
* 统一网络请求封装
* 对标 PC 前端 src/api/index.js 的设计
* - 自动注入 cookie
* - 统一解析 err_code / return 字段
* - Cookie 过期自动清理并跳转登录
*/
import { userStore } from '../store/user.js'
const API_BASE = '/api'
/**
* 底层 POST 请求
* @param {string} path - 接口路径,如 /users/login
* @param {object} data - 业务数据(会被包在 data 字段下)
* @returns {Promise<{errCode, data, raw}>}
*/
function post(path, data = {}) {
const body = { data }
// 自动注入 cookie
const cookieValue = userStore.getCookieValue()
if (cookieValue) {
body.userCookieValue = cookieValue
}
return new Promise((resolve, reject) => {
uni.request({
url: API_BASE + path,
method: 'POST',
header: { 'Content-Type': 'application/json' },
data: body,
timeout: 15000,
success(res) {
const raw = res.data
const errCode = raw?.err_code ?? -1
// Cookie 过期(err_code === -44),自动登出并跳转登录
if (errCode === -44) {
userStore.logout()
uni.reLaunch({ url: '/pages/signin' })
reject(new Error('Cookie expired'))
return
}
resolve({
errCode,
data: raw?.return ?? null,
raw,
})
},
fail(err) {
uni.showToast({ title: '网络连接失败', icon: 'none' })
reject(err)
},
})
})
}
/**
* 上传文件(FormData
* @param {string} path - 接口路径
* @param {string} filePath - 本地文件路径(uni.chooseImage 返回的 tempFilePaths[0]
* @param {string} name - 文件字段名,默认 'file'
*/
function upload(path, filePath, name = 'file') {
const cookieValue = userStore.getCookieValue()
return new Promise((resolve, reject) => {
uni.uploadFile({
url: API_BASE + path,
filePath,
name,
formData: cookieValue ? { cookie: cookieValue } : {},
timeout: 30000,
success(res) {
try {
const raw = JSON.parse(res.data)
resolve({
errCode: raw?.err_code ?? -1,
data: raw?.return ?? null,
raw,
})
} catch {
reject(new Error('JSON parse error'))
}
},
fail(err) {
uni.showToast({ title: '上传失败', icon: 'none' })
reject(err)
},
})
})
}
export const request = { post, upload }
+27
View File
@@ -0,0 +1,27 @@
/**
* 日程相关 API
* 对标 PC 前端 src/api/schedule.js
*/
import { request } from './request.js'
export const scheduleApi = {
/** 获取日程列表 */
getEvents(params = {}) {
return request.post('/schedule/getevents', params)
},
/** 新增日程 */
addEvent(data) {
return request.post('/schedule/addevent', data)
},
/** 编辑日程 */
editEvent(data) {
return request.post('/schedule/editevent', data)
},
/** 删除日程 */
deleEvent(data) {
return request.post('/schedule/deleevent', data)
},
}