http.js 1.5 KB

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