更新
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 全局属性工具函数
|
||||
* 用于在任何地方访问 main.js 中定义的全局属性
|
||||
*/
|
||||
|
||||
import { getCurrentInstance } from 'vue'
|
||||
|
||||
/**
|
||||
* 获取全局属性对象
|
||||
* @returns {Object} 全局属性对象
|
||||
*/
|
||||
export const getGlobalProperties = () => {
|
||||
try {
|
||||
const instance = getCurrentInstance()
|
||||
if (instance) {
|
||||
return instance.appContext.config.globalProperties
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('无法获取 getCurrentInstance:', e)
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 API 基础 URL
|
||||
* @returns {string} API 基础 URL
|
||||
*/
|
||||
export const getBaseUrl = () => {
|
||||
const globalProps = getGlobalProperties()
|
||||
return globalProps.$url || 'http://api.zzzhengyangtang.cn'
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 apiUrl 方法
|
||||
* @returns {Function} apiUrl 方法
|
||||
*/
|
||||
export const getApiUrl = () => {
|
||||
const globalProps = getGlobalProperties()
|
||||
return globalProps.apiUrl
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用全局属性的 Composition API Hook
|
||||
* @returns {Object} 包含全局属性的对象
|
||||
*/
|
||||
export const useGlobal = () => {
|
||||
const globalProps = getGlobalProperties()
|
||||
|
||||
return {
|
||||
// API 相关
|
||||
baseUrl: globalProps.$url || 'http://api.zzzhengyangtang.cn',
|
||||
apiUrl: globalProps.apiUrl,
|
||||
|
||||
// 应用相关(可选)
|
||||
appName: globalProps.$appName,
|
||||
version: globalProps.$version,
|
||||
|
||||
// 其他全局属性
|
||||
...globalProps
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整的 API URL
|
||||
* @param {string} path - API 路径
|
||||
* @returns {string} 完整的 API URL
|
||||
*/
|
||||
export const getFullUrl = (path) => {
|
||||
const baseUrl = getBaseUrl()
|
||||
return `${baseUrl}${path}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局属性(谨慎使用)
|
||||
* @param {string} key - 属性名
|
||||
* @param {any} value - 属性值
|
||||
*/
|
||||
export const setGlobalProperty = (key, value) => {
|
||||
try {
|
||||
const instance = getCurrentInstance()
|
||||
if (instance) {
|
||||
instance.appContext.config.globalProperties[key] = value
|
||||
return true
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('无法设置全局属性:', e)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取特定的全局属性
|
||||
* @param {string} key - 属性名
|
||||
* @param {any} defaultValue - 默认值
|
||||
* @returns {any} 属性值
|
||||
*/
|
||||
export const getGlobalProperty = (key, defaultValue = null) => {
|
||||
const globalProps = getGlobalProperties()
|
||||
return globalProps[key] !== undefined ? globalProps[key] : defaultValue
|
||||
}
|
||||
|
||||
export default {
|
||||
getGlobalProperties,
|
||||
getBaseUrl,
|
||||
getApiUrl,
|
||||
useGlobal,
|
||||
getFullUrl,
|
||||
setGlobalProperty,
|
||||
getGlobalProperty
|
||||
}
|
||||
Reference in New Issue
Block a user