27 lines
681 B
JavaScript
27 lines
681 B
JavaScript
import { API_BASE_URL, SHOP_ID } from './config.js'
|
|
|
|
function buildUrl(path) {
|
|
if (!path) return API_BASE_URL
|
|
if (path.startsWith('http')) return path
|
|
return API_BASE_URL + (path.startsWith('/') ? path : '/' + path)
|
|
}
|
|
|
|
export function get(path, params = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: buildUrl(path),
|
|
method: 'GET',
|
|
data: params,
|
|
header: { 'X-Shop-Id': SHOP_ID },
|
|
success: (res) => {
|
|
const { statusCode, data } = res
|
|
if (statusCode >= 200 && statusCode < 300) return resolve(data)
|
|
reject(new Error('HTTP ' + statusCode))
|
|
},
|
|
fail: (err) => reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
|