spa/frontend/technician-miniprogram/utils/api.js

64 lines
1.6 KiB
JavaScript

// 技师端API配置
const BASE_URL = 'http://localhost:8888'
class Api {
constructor() {
this.baseURL = BASE_URL
}
request(options) {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token')
uni.request({
url: this.baseURL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: {
'Content-Type': 'application/json',
'Authorization': token ? `Bearer ${token}` : '',
'User-Type': 'technician', // 标识技师端
...options.header
},
success: (res) => {
if (res.statusCode === 200) {
if (res.data.code === 0) {
resolve(res.data)
} else {
reject(new Error(res.data.msg || '请求失败'))
}
} else if (res.statusCode === 401) {
uni.removeStorageSync('token')
uni.reLaunch({
url: '/pages/login/login'
})
reject(new Error('登录已过期'))
} else {
reject(new Error('网络错误'))
}
},
fail: (err) => {
reject(new Error('网络连接失败'))
}
})
})
}
get(url, data) {
return this.request({ url, method: 'GET', data })
}
post(url, data) {
return this.request({ url, method: 'POST', data })
}
put(url, data) {
return this.request({ url, method: 'PUT', data })
}
delete(url, data) {
return this.request({ url, method: 'DELETE', data })
}
}
module.exports = new Api()