一、存储域名和接口的文件config.js 类似如下
const host = "";//域名
export default {
host:`${host}`,
/****首页***/
homepage:`/customer/homepage/index`,//接口名称路径
}
二、使用uni.request方法和Promise进行ajax封装,代码如下:
let Promises = Promise
import config from './config' //这是我存放接口api的文件
const host = config.host; //这是获取域名
Promises.prototype.finally = function(callback) {
let P = this.constructor
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => {
throw reason
}))
}
class Https {
host(){
return config.host
}
request (url, method, data) {
const cachekey = uni.getStorageSync('cachekey') //获取用户登录的cachekey或者token
return new Promises