//网络请求
function request_WANG(url,method,params) {
return new Promise(function(resolve, reject) {
wx.showLoading({
title: '加载中',
})
var data = {};
if(params) {
data = params;
}
// return false
var contentType = 'application/json';
//默认的网络请求为"GET"
if(!method){
method = 'GET'
}
else{
method = method;
}
// console.log('====================================');
console.log(wx.getStorageSync('token'));
wx.request({
url: BASE_URL_WANG + url,
data: data,
method: method,
//添加请求头
header: {
'Content-Type': contentType ,
'Authorization':'bearer '+ wx.getStorageSync('token')
},
//请求成功
success: function(res) {
if (res.statusCode == 200 || res.statusCode == 201 || res.statusCode == 204) {
resolve(res);
}else {
//请求失败
reject("请求失败:" + res.statusCode)
console.log(res);
if(res.data.msg == 'user already exists'){
wx.showToast({
title: '用户已经存在了!',
icon: 'error',
duration: 2000
})
}
else if(res.data.msg == 'phone already exists'){
wx.showToast({
title: '手机号已被注册!',
icon: 'error',
duration: 2000
})
}
else if(res.data.msg == 'invalid user'){
wx.showToast({
title: '无效的用户!',
icon: 'error',
duration: 2000
})
}
else{
wx.showToast({
title: '出错!',
icon: 'error',
duration: 2000
})
}
// resolve(res);
}
},
fail: function(err) {
console.log(err);
reject("服务器连接异常,请检查网络再试");
},
complete: function() {
wx.hideLoading();
}
})
});
}
//================
api.POSTgetCode(paramr)
.then(res => {
console.log('==1');
console.log(res);
console.log(res.data.data);
wx.removeStorageSync('token')
wx.setStorageSync('token', res.data.data)
console.log(wx.getStorageSync('token'));
setTimeout(function(){
api.GETuserNormal()
.then(res => {
console.log('==2');
console.log(res);
resolve('33')
})
.catch(err => {
console.log('==2');
console.log(err);
})
},3000)
resolve('22')
})
.catch(err => {
console.log(err);
})
前端代码没有问题,只有看一下后端的获取方式了