odf_new/odf-uniapp/services/api.js

45 lines
1.3 KiB
JavaScript
Raw 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'
// const BASE_URL = 'http://49.233.115.141:11082'
// const BASE_URL = 'http://115.190.188.216:2861'
export const BASE_URL = 'https://api.wux.shhmkjgs.cn'
const TIMEOUT = 20000
/**
* 统一请求封装
* @param {string} method - GET | POST
* @param {string} url - 接口路径
* @param {object} data - 请求参数
* @returns {Promise<{code, msg, data}>}
*/
export function request(method, url, data = {}) {
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: 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) => request('GET', url, params)
export const post = (url, data) => request('POST', url, data)