petstore-frontend/src/api/index.js

122 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const API_ORIGIN = (import.meta.env.VITE_API_ORIGIN || 'http://localhost:8080').replace(/\/$/, '')
const BASE_URL = `${API_ORIGIN}/api`
// uniapp request 封装
const request = (options) => new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: options.header || {},
success: (res) => resolve(res.data),
fail: (err) => reject(err)
})
})
const get = (url, params = {}) => {
const qs = Object.entries(params).filter(([, v]) => v != null).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&')
return request({ url: `${url}${qs ? '?' + qs : ''}`, method: 'GET' })
}
const post = (url, body = {}) =>
request({ url, method: 'POST', data: body, header: { 'Content-Type': 'application/json' } })
const put = (url, body = {}) =>
request({ url, method: 'PUT', data: body, header: { 'Content-Type': 'application/json' } })
const del = (url) =>
request({ url, method: 'DELETE' })
// 导出 BASE_URL 供页面使用
export { API_ORIGIN, BASE_URL }
// 图片完整URL避免重复拼接
export const imgUrl = (path) => {
if (!path) return ''
if (path.startsWith('http')) return path
return `${API_ORIGIN}${path}`
}
// 发送验证码
export const sendSms = (phone) => post('/sms/send', { phone })
// 登录
export const login = (phone, code) => post('/user/login', { phone, code })
/** 微信小程序getPhoneNumber 返回的 detail.code */
export const wxPhoneLogin = (phoneCode) => post('/user/wx-phone-login', { phoneCode })
// 注册老板
export const registerBoss = (data) => post('/user/register-boss', data)
// 注册员工
export const registerStaff = (data) => post('/user/register-staff', data)
// 预约列表
export const getAppointmentList = (userId, storeId) => get('/appointment/list', { userId, storeId })
// 预约详情
export const getAppointmentDetail = (id) => get('/appointment/detail', { id })
// 创建预约
export const createAppointment = (data) => post('/appointment/create', data)
// 开始服务
export const startAppointment = (appointmentId, staffUserId) =>
post('/appointment/start', { appointmentId, staffUserId })
// 取消预约
export const cancelAppointment = (id) => put(`/appointment/status?id=${id}&status=cancel`)
// 提交报告
export const createReport = (data) => post('/report/create', data)
// 获取报告通过token
export const getReportByToken = (token) => get('/report/get', { token })
// 报告列表
export const getReportList = (params) => get('/report/list', params)
// 服务类型列表
export const getServiceTypeList = (storeId) => get('/service-type/list', { storeId })
// 创建服务类型
export const createServiceType = (storeId, name) => post('/service-type/create', { storeId, name })
// 删除服务类型
export const deleteServiceType = (id) => del(`/service-type/delete?id=${id}`)
// 员工列表
export const getStaffList = (storeId) => get('/user/staff-list', { storeId })
// 添加员工
export const createStaff = (data) => post('/user/create-staff', data)
// 删除员工
export const deleteStaff = (staffId) => del(`/user/staff?staffId=${staffId}`)
// 更新用户信息
export const updateUser = (data) => put('/user/update', data)
// 更新店铺
export const updateStore = (data) => put('/store/update', data)
// 上传图片uniapp 版:使用 uni.uploadFile
export const uploadImage = (filePath) =>
new Promise((resolve, reject) => {
uni.uploadFile({
url: `${BASE_URL}/upload/image`,
filePath: filePath,
name: 'file',
success: (res) => {
try {
const data = JSON.parse(res.data)
resolve(data)
} catch (e) {
reject(e)
}
},
fail: (err) => reject(err)
})
})