//config
const BASE_URL = process.env.NODE_ENV == 'development'?'':''
const API_BASE_URL = 'https://xxx/Public/uploads/'
const HTTPURL = 'xxx'
const config = {
base_url: BASE_URL,
aip_url: API_BASE_URL,
httpUrl: HTTPURL,
appId: 'xxx',
appKey: 'xxx'
}
export default config
//http.js
import config from '../config.js'
class HTTP {
constructor() {
this.baseUrl = config.base_url
this.apiUrlHttp = config.aip_url
this.httpUrl = config.httpUrl
}
request({
url,
data = {},
configs = {},
method = 'GET'
}) {
return new Promise((resolve, reject) => {
this._request(url, resolve, reject, data, method, configs)
})
}
_request(url, resolve, reject, data = {}, method = 'GET', configs) {
uni.request({
url: `${this.baseUrl}${url}`,
method: method,
data: data,
header: {
'content-type': 'application/json',
'appId': config.appId,
'token': uni.getStorageSync('AuthTokens') ? uni.getStorageSync('AuthTokens') : null,
...configs,
},
...configs,
success: (res) => {
if (res.data) {
const _success = parseInt(res.data.type) > 0;
if (_success) {
resolve(res.data)
} else {
reject(res)
const _message = res.data.text;
this._show_error(_message)
}
} else {
resolve(res.data)
}
},
fail: (err) => {
reject()
this._show_error(err.message)
}
})
}
_show_error(_message) {
uni.showToast({
title: `${_message}`,
icon: 'none',
duration: 2000
})
}
}
export {
HTTP
}
//api
import {
HTTP
} from '../utils/http.js'
class AppModel extends HTTP {
login(params) {
return this.request({
url: `Login/WxLogin`,
method: 'POST',
data: params
})
}
....
}
const api = new AppModel()
export {
api
}