first commit
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
import request from './request'
|
||||
import mock from './mock'
|
||||
|
||||
const useMock = import.meta.env.VITE_API_MOCK === 'true'
|
||||
|
||||
export const customerApi = {
|
||||
list(params) {
|
||||
return useMock ? mock.customers.list(params) : request.get('/api/customers', { params })
|
||||
},
|
||||
getById(id) {
|
||||
return useMock ? mock.customers.getById(id) : request.get(`/api/customers/${id}`)
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.customers.create(data) : request.post('/api/customers', data)
|
||||
},
|
||||
update(id, data) {
|
||||
return useMock ? mock.customers.update(id, data) : request.put(`/api/customers/${id}`, data)
|
||||
},
|
||||
remove(id) {
|
||||
return useMock ? mock.customers.remove(id) : request.delete(`/api/customers/${id}`)
|
||||
}
|
||||
}
|
||||
|
||||
export const orderApi = {
|
||||
list(params) {
|
||||
return useMock ? mock.orders.list(params) : request.get('/api/orders', { params })
|
||||
},
|
||||
getById(id) {
|
||||
return useMock ? mock.orders.getById(id) : request.get(`/api/orders/${id}`)
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.orders.create(data) : request.post('/api/orders', data)
|
||||
},
|
||||
update(id, data) {
|
||||
return useMock ? mock.orders.update(id, data) : request.put(`/api/orders/${id}`, data)
|
||||
},
|
||||
remove(id) {
|
||||
return useMock ? mock.orders.remove(id) : request.delete(`/api/orders/${id}`)
|
||||
}
|
||||
}
|
||||
|
||||
export const productApi = {
|
||||
list() {
|
||||
return useMock ? mock.products.list() : request.get('/api/products')
|
||||
}
|
||||
}
|
||||
|
||||
export const appointmentApi = {
|
||||
list() {
|
||||
return useMock ? mock.appointments.list() : request.get('/api/appointments')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.appointments.create(data) : request.post('/api/appointments', data)
|
||||
}
|
||||
}
|
||||
|
||||
export const measurementApi = {
|
||||
list() {
|
||||
return useMock ? mock.measurementTasks.list() : request.get('/api/measurement-tasks')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.measurementTasks.create(data) : request.post('/api/measurement-tasks', data)
|
||||
},
|
||||
update(id, data) {
|
||||
return useMock ? mock.measurementTasks.update(id, data) : request.put(`/api/measurement-tasks/${id}`, data)
|
||||
}
|
||||
}
|
||||
|
||||
export const productionApi = {
|
||||
list() {
|
||||
return useMock ? mock.productionTasks.list() : request.get('/api/production-tasks')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.productionTasks.create(data) : request.post('/api/production-tasks', data)
|
||||
}
|
||||
}
|
||||
|
||||
export const installationApi = {
|
||||
list() {
|
||||
return useMock ? mock.installations.list() : request.get('/api/installations')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.installations.create(data) : request.post('/api/installations', data)
|
||||
}
|
||||
}
|
||||
|
||||
export const quoteApi = {
|
||||
list() {
|
||||
return useMock ? mock.quotes.list() : request.get('/api/quotes')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.quotes.create(data) : request.post('/api/quotes', data)
|
||||
}
|
||||
}
|
||||
|
||||
export const purchaseApi = {
|
||||
list() {
|
||||
return useMock ? mock.purchaseOrders.list() : request.get('/api/purchase-orders')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.purchaseOrders.create(data) : request.post('/api/purchase-orders', data)
|
||||
}
|
||||
}
|
||||
|
||||
export const afterSaleApi = {
|
||||
list() {
|
||||
return useMock ? mock.afterSales.list() : request.get('/api/after-sales')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.afterSales.create(data) : request.post('/api/after-sales', data)
|
||||
}
|
||||
}
|
||||
|
||||
export const channelApi = {
|
||||
list() {
|
||||
return useMock ? mock.channels.list() : request.get('/api/channels')
|
||||
},
|
||||
create(data) {
|
||||
return useMock ? mock.channels.create(data) : request.post('/api/channels', data)
|
||||
},
|
||||
update(id, data) {
|
||||
return useMock ? mock.channels.update(id, data) : request.put(`/api/channels/${id}`, data)
|
||||
},
|
||||
remove(id) {
|
||||
return useMock ? mock.channels.remove(id) : request.delete(`/api/channels/${id}`)
|
||||
}
|
||||
}
|
||||
|
||||
export const surveyApi = {
|
||||
list() {
|
||||
return useMock ? mock.surveys.list() : request.get('/api/surveys')
|
||||
}
|
||||
}
|
||||
|
||||
export const staffApi = {
|
||||
measurers() {
|
||||
return useMock ? mock.staff.measurers() : request.get('/api/staff/measurers')
|
||||
},
|
||||
installers() {
|
||||
return useMock ? mock.staff.installers() : request.get('/api/staff/installers')
|
||||
}
|
||||
}
|
||||
|
||||
export const authApi = {
|
||||
login(data) {
|
||||
return useMock
|
||||
? Promise.resolve({ token: 'mock-jwt-token', user: { name: data.username, role: data.username === 'admin' ? 'admin' : 'user' } })
|
||||
: request.post('/api/auth/login', data)
|
||||
}
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
import AppData from '@/utils/dataService'
|
||||
|
||||
const delay = (ms = 200) => new Promise(r => setTimeout(r, ms))
|
||||
|
||||
const mock = {
|
||||
customers: {
|
||||
list(params = {}) {
|
||||
return delay().then(() => {
|
||||
let list = AppData.getCustomers()
|
||||
if (params.keyword) {
|
||||
const kw = params.keyword.toLowerCase()
|
||||
list = list.filter(c => c.name.includes(kw) || c.phone.includes(kw) || (c.address && c.address.includes(kw)))
|
||||
}
|
||||
if (params.status) {
|
||||
list = list.filter(c => c.status === params.status)
|
||||
}
|
||||
if (params.source) {
|
||||
list = list.filter(c => c.source === params.source)
|
||||
}
|
||||
const page = params.page || 1
|
||||
const size = params.size || 10
|
||||
const total = list.length
|
||||
const start = (page - 1) * size
|
||||
return { total, page, size, list: list.slice(start, start + size) }
|
||||
})
|
||||
},
|
||||
getById(id) {
|
||||
return delay().then(() => AppData.getCustomerById(id) || null)
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const customers = AppData.getCustomers()
|
||||
const maxId = customers.length > 0 ? Math.max(...customers.map(c => parseInt(c.id.replace('C', '')))) : 0
|
||||
const record = { ...data, id: 'C' + String(maxId + 1).padStart(3, '0'), createTime: new Date().toLocaleString('zh-CN') }
|
||||
customers.unshift(record)
|
||||
AppData.saveCustomers(customers)
|
||||
return record
|
||||
})
|
||||
},
|
||||
update(id, data) {
|
||||
return delay().then(() => {
|
||||
const customers = AppData.getCustomers()
|
||||
const idx = customers.findIndex(c => c.id === id)
|
||||
if (idx === -1) throw new Error('客户不存在')
|
||||
customers[idx] = { ...customers[idx], ...data }
|
||||
AppData.saveCustomers(customers)
|
||||
return customers[idx]
|
||||
})
|
||||
},
|
||||
remove(id) {
|
||||
return delay().then(() => {
|
||||
const customers = AppData.getCustomers()
|
||||
AppData.saveCustomers(customers.filter(c => c.id !== id))
|
||||
return true
|
||||
})
|
||||
}
|
||||
},
|
||||
orders: {
|
||||
list(params = {}) {
|
||||
return delay().then(() => {
|
||||
let list = AppData.getOrder()
|
||||
if (params.keyword) {
|
||||
const kw = params.keyword.toLowerCase()
|
||||
list = list.filter(o => o.id.toLowerCase().includes(kw) || o.customer.toLowerCase().includes(kw))
|
||||
}
|
||||
if (params.status) list = list.filter(o => o.status === params.status)
|
||||
const page = params.page || 1
|
||||
const size = params.size || 10
|
||||
const total = list.length
|
||||
const start = (page - 1) * size
|
||||
return { total, page, size, list: list.slice(start, start + size) }
|
||||
})
|
||||
},
|
||||
getById(id) {
|
||||
return delay().then(() => AppData.getOrderById(id) || null)
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const orders = AppData.getOrder()
|
||||
const record = { ...data, id: 'O' + Date.now(), createTime: new Date().toLocaleString('zh-CN') }
|
||||
orders.unshift(record)
|
||||
AppData.saveOrders(orders)
|
||||
return record
|
||||
})
|
||||
},
|
||||
update(id, data) {
|
||||
return delay().then(() => {
|
||||
const orders = AppData.getOrder()
|
||||
const idx = orders.findIndex(o => o.id === id)
|
||||
if (idx === -1) throw new Error('订单不存在')
|
||||
orders[idx] = { ...orders[idx], ...data }
|
||||
AppData.saveOrders(orders)
|
||||
return orders[idx]
|
||||
})
|
||||
},
|
||||
remove(id) {
|
||||
return delay().then(() => {
|
||||
const orders = AppData.getOrder()
|
||||
AppData.saveOrders(orders.filter(o => o.id !== id))
|
||||
return true
|
||||
})
|
||||
}
|
||||
},
|
||||
products: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getProducts())
|
||||
}
|
||||
},
|
||||
appointments: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getAppointments())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getAppointments()
|
||||
list.push({ ...data, id: 'A' + Date.now() })
|
||||
AppData.saveAppointments(list)
|
||||
return data
|
||||
})
|
||||
}
|
||||
},
|
||||
measurementTasks: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getMeasurementTasks())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getMeasurementTasks()
|
||||
list.push({ ...data, id: 'M' + (list.length + 1).toString().padStart(3, '0') })
|
||||
AppData.saveMeasurementTasks(list)
|
||||
return data
|
||||
})
|
||||
},
|
||||
update(id, data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getMeasurementTasks()
|
||||
const idx = list.findIndex(t => t.id === id)
|
||||
if (idx === -1) throw new Error('任务不存在')
|
||||
list[idx] = { ...list[idx], ...data }
|
||||
AppData.saveMeasurementTasks(list)
|
||||
return list[idx]
|
||||
})
|
||||
}
|
||||
},
|
||||
productionTasks: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getProductionTask())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getProductionTask()
|
||||
list.push({ ...data, id: 'PT' + (list.length + 1).toString().padStart(3, '0') })
|
||||
AppData.saveProductionTasks(list)
|
||||
return data
|
||||
})
|
||||
}
|
||||
},
|
||||
installations: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getInstallation())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getInstallation()
|
||||
list.push({ ...data, id: 'IN' + (list.length + 1).toString().padStart(3, '0') })
|
||||
AppData.saveInstallations(list)
|
||||
return data
|
||||
})
|
||||
}
|
||||
},
|
||||
quotes: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getQuotes())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getQuotes()
|
||||
list.push({ ...data, id: 'Q' + (list.length + 1).toString().padStart(3, '0') })
|
||||
AppData.saveQuotes(list)
|
||||
return data
|
||||
})
|
||||
}
|
||||
},
|
||||
purchaseOrders: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getPurchaseOrders())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getPurchaseOrders()
|
||||
list.push({ ...data, id: 'PO' + (list.length + 1).toString().padStart(3, '0') })
|
||||
AppData.savePurchaseOrders(list)
|
||||
return data
|
||||
})
|
||||
}
|
||||
},
|
||||
afterSales: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getAfterSales())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => {
|
||||
const list = AppData.getAfterSales()
|
||||
list.push({ ...data, id: 'AS' + (list.length + 1).toString().padStart(3, '0') })
|
||||
AppData.saveAfterSales(list)
|
||||
return data
|
||||
})
|
||||
}
|
||||
},
|
||||
channels: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getChannels())
|
||||
},
|
||||
create(data) {
|
||||
return delay().then(() => AppData.addChannel(data))
|
||||
},
|
||||
update(id, data) {
|
||||
return delay().then(() => AppData.updateChannel(id, data))
|
||||
},
|
||||
remove(id) {
|
||||
return delay().then(() => AppData.deleteChannel(id))
|
||||
}
|
||||
},
|
||||
surveys: {
|
||||
list() {
|
||||
return delay().then(() => AppData.getSurveys())
|
||||
}
|
||||
},
|
||||
staff: {
|
||||
measurers() {
|
||||
return delay().then(() => AppData.getSurveyors())
|
||||
},
|
||||
installers() {
|
||||
return delay().then(() => AppData.getInstallers())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default mock
|
||||
@@ -0,0 +1,57 @@
|
||||
import axios from 'axios'
|
||||
import { useUserStore } from '@/stores/userStore'
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL,
|
||||
timeout: 30000,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
})
|
||||
|
||||
request.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('accessToken')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
|
||||
request.interceptors.response.use(
|
||||
(res) => {
|
||||
const body = res.data
|
||||
if (body && typeof body === 'object' && 'code' in body) {
|
||||
if (body.code === 200) {
|
||||
return body.data
|
||||
}
|
||||
if (body.code === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
window.location.hash = '#/login'
|
||||
return Promise.reject(new Error(body.msg || '认证已过期'))
|
||||
}
|
||||
return Promise.reject(new Error(body.msg || '请求失败'))
|
||||
}
|
||||
return body
|
||||
},
|
||||
(error) => {
|
||||
if (error.response) {
|
||||
const { status, data } = error.response
|
||||
if (status === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
window.location.hash = '#/login'
|
||||
return Promise.reject(new Error('认证已过期,请重新登录'))
|
||||
}
|
||||
const msg = data?.msg || data?.message || `服务器错误 (${status})`
|
||||
return Promise.reject(new Error(msg))
|
||||
}
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
return Promise.reject(new Error('请求超时,请重试'))
|
||||
}
|
||||
return Promise.reject(new Error('网络异常,请检查网络连接'))
|
||||
}
|
||||
)
|
||||
|
||||
export default request
|
||||
Reference in New Issue
Block a user