49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import { API_BASE_URL, SHOP_ID, ENABLE_DEFAULT_USER, DEFAULT_USER_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) => {
|
|
const headers = { 'X-Shop-Id': SHOP_ID }
|
|
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) headers['X-User-Id'] = DEFAULT_USER_ID
|
|
uni.request({
|
|
url: buildUrl(path),
|
|
method: 'GET',
|
|
data: params,
|
|
header: headers,
|
|
success: (res) => {
|
|
const { statusCode, data } = res
|
|
if (statusCode >= 200 && statusCode < 300) return resolve(data)
|
|
reject(new Error('HTTP ' + statusCode))
|
|
},
|
|
fail: (err) => reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
export function post(path, body = {}) {
|
|
return new Promise((resolve, reject) => {
|
|
const headers = { 'Content-Type': 'application/json', 'X-Shop-Id': SHOP_ID }
|
|
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) headers['X-User-Id'] = DEFAULT_USER_ID
|
|
uni.request({
|
|
url: buildUrl(path),
|
|
method: 'POST',
|
|
data: body,
|
|
header: headers,
|
|
success: (res) => {
|
|
const { statusCode, data } = res
|
|
if (statusCode >= 200 && statusCode < 300) return resolve(data)
|
|
reject(new Error('HTTP ' + statusCode))
|
|
},
|
|
fail: (err) => reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
|