http.js 1.6 KB

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