project/utils/request.js

82 lines
1.6 KiB
JavaScript

// 不需要携带token
let urlList = ["/user/login", "/user/info"]
const baseUrl = "https://weiapi.youbeiw.top/api";
/**
* GET请求封装
*/
function get(url, data = {}) {
return request(url, 'GET', data)
}
/**
* POST请求封装
*/
function post(url, data = {}) {
return request(url, 'POST', data)
}
//获取code
function login() {
return new Promise((reslove) => {
uni.login({
success(res) {
reslove(res.code);
}
})
})
}
/**
*/
function request(url, method = "GET", data = {}, header = "application/x-www-form-urlencoded") {
var isShowLoading = true;
//这里是判断不需要携带token
if (urlList.indexOf(url) > -1) {
header = {
'Content-Type': header,
}
} else {
uni.showLoading({
title: '努力加载中',
mask: true
})
header = {
'Content-Type': header,
// 'MINIAPP-TOKEN': wx.getStorageSync('token')
}
}
return new Promise(function (resolve, reject) {
uni.request({
url: baseUrl + url,
data: data,
method: method,
header: header,
success: function (res) {
var info = res.data;
if (info.code == 200) {
resolve(info.data);
} else {
reject(info)
}
},
fail: function (err) {
//服务器连接异常
reject(err, "服务器连接异常,请检查网络再试")
single.Disconnect()
},
complete: function (params) {
if (isShowLoading) {
uni.hideLoading();
isShowLoading = false
}
}
})
})
}
module.exports = {
request,
get,
post,
login,
}