'对接接口部分'
This commit is contained in:
89
utils/request.js
Normal file
89
utils/request.js
Normal file
@@ -0,0 +1,89 @@
|
||||
const ui = require('./ui');
|
||||
const BASE_URL = 'https://app.19year.cn'
|
||||
|
||||
|
||||
/**
|
||||
* 网络请求request
|
||||
* obj.data 请求接口需要传递的数据
|
||||
* obj.showLoading 控制是否显示加载Loading 默认为false不显示
|
||||
* obj.contentType 默认为 application/json
|
||||
* obj.method 请求的方法 默认为GET
|
||||
* obj.url 请求的接口路径
|
||||
* obj.message 加载数据提示语
|
||||
*/
|
||||
function request(obj) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if(obj.showLoading){
|
||||
ui.showLoading(obj.message? obj.message : '加载中...');
|
||||
}
|
||||
var data = {};
|
||||
if(obj.data) {
|
||||
data = obj.data;
|
||||
}
|
||||
var contentType = 'application/json';
|
||||
if(obj.contentType){
|
||||
contentType = obj.contentType;
|
||||
}
|
||||
|
||||
var method = 'GET';
|
||||
if(obj.method){
|
||||
method = obj.method;
|
||||
}
|
||||
|
||||
wx.request({
|
||||
url: BASE_URL + obj.url,
|
||||
data: data,
|
||||
method: method,
|
||||
//添加请求头
|
||||
header: {
|
||||
'Content-Type': contentType ,
|
||||
'token': wx.getStorageSync('token') //获取保存的token
|
||||
},
|
||||
//请求成功
|
||||
success: function(res) {
|
||||
console.log('===============================================================================================')
|
||||
console.log('== 接口地址:' + obj.url);
|
||||
console.log('== 接口参数:' + JSON.stringify(data));
|
||||
console.log('== 请求类型:' + method);
|
||||
console.log("== 接口状态:" + res.statusCode);
|
||||
console.log("== 接口数据:" + JSON.stringify(res.data));
|
||||
console.log('===============================================================================================')
|
||||
if (res.statusCode == 200) {
|
||||
resolve(res);
|
||||
} else if (res.statusCode == 401) {//授权失效
|
||||
reject("登录已过期");
|
||||
jumpToLogin();//跳转到登录页
|
||||
} else {
|
||||
//请求失败
|
||||
reject("请求失败:" + res.statusCode)
|
||||
}
|
||||
},
|
||||
fail: function(err) {
|
||||
//服务器连接异常
|
||||
console.log('===============================================================================================')
|
||||
console.log('== 接口地址:' + url)
|
||||
console.log('== 接口参数:' + JSON.stringify(data))
|
||||
console.log('== 请求类型:' + method)
|
||||
console.log("== 服务器连接异常")
|
||||
console.log('===============================================================================================')
|
||||
reject("服务器连接异常,请检查网络再试");
|
||||
},
|
||||
complete: function() {
|
||||
ui.hideLoading();
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
//跳转到登录页
|
||||
function jumpToLogin(){
|
||||
wx.reLaunch({
|
||||
url: '/pages/login/login',
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
request,
|
||||
}
|
||||
|
||||
26
utils/ui.js
Normal file
26
utils/ui.js
Normal file
@@ -0,0 +1,26 @@
|
||||
export const showToast = function(content,duration) {
|
||||
if(!duration) duration = 2000
|
||||
wx.showToast({
|
||||
title: content,
|
||||
icon: 'none',
|
||||
duration: duration,
|
||||
})
|
||||
}
|
||||
|
||||
var isShowLoading = false
|
||||
export const showLoading = function(title) {
|
||||
if(isShowLoading) return
|
||||
wx.showLoading({
|
||||
title: title?title:'',
|
||||
mask:true,
|
||||
success:()=>{
|
||||
isShowLoading = true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const hideLoading = function() {
|
||||
if(!isShowLoading) return
|
||||
isShowLoading = false
|
||||
wx.hideLoading()
|
||||
}
|
||||
Reference in New Issue
Block a user