http.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const ui = require('./ui');
  2. const BASE_URL = 'https://www.dghdwy.cn/monitor/api/lt/we'
  3. // const BASE_URL = 'http://119.29.97.109:8080/monitor'
  4. // const BASE_URL = 'http://192.168.3.51:8089/monitor/api/lt/we'
  5. function request(obj) {
  6. return new Promise(function(resolve, reject) {
  7. if(obj.showLoading){
  8. ui.showLoading(obj.message? obj.message : '加载中...');
  9. }
  10. var data = {};
  11. if(obj.data) {
  12. data = obj.data;
  13. }
  14. var contentType = 'application/json';
  15. if(obj.contentType){
  16. contentType = obj.contentType;
  17. }
  18. var method = 'post';
  19. if(obj.method){
  20. method = obj.method;
  21. }
  22. wx.request({
  23. url: BASE_URL + obj.url,
  24. data: data,
  25. method: method,
  26. //添加请求头
  27. // header: {
  28. // 'Content-Type': contentType ,
  29. // 'token': wx.getStorageSync('token') //获取保存的token
  30. // },
  31. //请求成功
  32. success: function(res) {
  33. if (res.statusCode == 200) {
  34. resolve(res);
  35. } else if (res.statusCode == 500) {//授权失效
  36. reject("登录已过期");
  37. jumpToLogin();//跳转到登录页
  38. } else {
  39. //请求失败
  40. reject("请求失败:" + res.statusCode)
  41. }
  42. },
  43. fail: function(err) {
  44. reject("服务器连接异常,请检查网络再试");
  45. },
  46. complete: function() {
  47. ui.hideLoading();
  48. }
  49. })
  50. });
  51. }
  52. //跳转到登录页
  53. function jumpToLogin(){
  54. wx.reLaunch({
  55. url: '/pages/index/index',
  56. })
  57. }
  58. module.exports = {
  59. request,
  60. }