odf_new/odf-uniapp/services/api.js
zpc 88f86efd7a
All checks were successful
continuous-integration/drone/push Build is passing
21
2026-04-06 13:43:39 +08:00

62 lines
1.6 KiB
JavaScript
Raw Permalink 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.

// services/api.js
import store from '@/store'
// export const BASE_URL = 'http://49.233.115.141:11082'
// export const BASE_URL = 'https://ssl.api.suigongxj.top'
// const BASE_URL = 'http://115.190.188.216:2861'
export const BASE_URL = 'https://api.wux.shhmkjgs.cn'
export const APP_VERSION = '1.0.5'
// export const BASE_URL = 'http://127.0.0.1:8888'
const TIMEOUT = 20000
/**
* 统一请求封装
* @param {string} method - GET | POST
* @param {string} url - 接口路径
* @param {object} data - 请求参数
* @param {object} options - 额外选项 { timeout }
* @returns {Promise<{code, msg, data}>}
*/
export function request(method, url, data = {}, options = {}) {
return new Promise((resolve, reject) => {
const header = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${store.token}`,
'Userid': store.userId,
'Username': store.userName
}
uni.request({
url: BASE_URL + url,
method,
data: method === 'GET' ? undefined : data,
// GET 参数通过 data 字段传递UniApp 会自动拼接为 queryString
...(method === 'GET' ? {
data
} : {}),
header,
timeout: options.timeout || TIMEOUT,
success(res) {
const {
code,
msg,
data: resData
} = res.data
resolve({
code,
msg,
data: resData
})
},
fail(err) {
reject({
code: -1,
msg: err.errMsg || '网络异常'
})
}
})
})
}
export const get = (url, params, options) => request('GET', url, params, options)
export const post = (url, data, options) => request('POST', url, data, options)