封装request.js
request.js放到项目的utils文件夹下:
csharp
import global from "@/global.js"
export default (param) =>{
let url = global.apiUrl + param.url;
let method = param.method;
let data = param.data;
//将param数据赋值给data
let params = param.params;
if (params) data = params;
//头
let header = {}
header["content-type"] = "application/json";
header["Authorization"] = 'Bearer ' + global.getApiToken();//token是登录若依的token
//返回Promist
return new Promise((resolve, reject) => {
uni.request({
url: url,
method: method,
data: data,
header: header,
success: res => {
resolve(res)
},
fail: err => {
reject(err)
}
})
})
}
调用
直接把若依生成的api文件复制到项目的api文件夹就可以