Files
zyt/TUICallKit-Vue3/utils/api.js
T
2026-03-16 15:08:28 +08:00

87 lines
2.2 KiB
JavaScript

/**
* API 请求封装
* 使用 main.js 中定义的全局 apiUrl 方法
*/
/**
* 发送HTTP请求
* @param {string} url - 请求URL
* @param {string} method - 请求方法 (GET/POST)
* @param {object} data - 请求数据
* @param {boolean} loading - 是否显示加载中
* @returns {Promise}
*/
export const request = (url, method = 'POST', data = {}, loading = true) => {
// 获取全局 apiUrl 方法
const apiUrl = uni.$u?.apiUrl || (typeof apiUrl !== 'undefined' ? apiUrl : null)
const { proxy } = getCurrentInstance()
const instance = getCurrentInstance();
// 2. 从实例中获取全局 $url
const $url = instance.appContext.config.globalProperties.$url;
if (!apiUrl) {
// 降级方案:直接使用 uni.request
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token') || ''
const baseUrl = $url
uni.request({
url: `${baseUrl}${url}`,
method,
data,
header: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': token
},
success: (res) => {
resolve(res.data)
},
fail: (err) => {
reject(err)
}
})
})
}
// 使用全局 apiUrl 方法
return apiUrl({
url,
method,
data,
header: {
'content-type': 'application/x-www-form-urlencoded'
}
}, loading)
}
/**
* GET 请求
*/
export const get = (url, data = {}, loading = false) => {
return request(url, 'GET', data, loading)
}
/**
* POST 请求
*/
export const post = (url, data = {}, loading = true) => {
return request(url, 'POST', data, loading)
}
/**
* 医生相关API
*/
export const doctorApi = {
// 获取医生列表
getDoctorList: (params) => get('/api/doctor/lists', params, false),
// 获取医生详情
getDoctorDetail: (id) => get('/api/doctor/detail', { id }, false),
// 获取医生排班
getDoctorRoster: (doctorId, date) => get('/api/doctor/roster', { doctor_id: doctorId, date }, false),
// 获取医生评价列表
getDoctorReviews: (doctorId, pageNo = 1, pageSize = 10) =>
get('/api/doctor/reviews', { doctor_id: doctorId, page_no: pageNo, page_size: pageSize }, false)
}