request.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (function (global) {
  2. var RequestAxios={
  3. baseGet:function(url,successCallback,errorCallback,isMsg){
  4. axios.get(url).then(function(res){
  5. if(res.status == 200 && res.data.code == 200){
  6. successCallback && successCallback(res.data);
  7. }else{
  8. var err = res.data.msg || '请求失败!';
  9. errorCallback && errorCallback(err);
  10. isMsg || layer.open({content: err,skin: 'msg',time: 2});
  11. }
  12. }).catch(function(err){
  13. errorCallback && errorCallback(err);
  14. layer.open({content: err,skin: 'msg',time: 2});
  15. });
  16. },
  17. basePost:function(url,data,successCallback,errorCallback,isMsg){
  18. axios.post(url,data).then(function(res){
  19. if(res.status == 200 && res.data.code == 200){
  20. successCallback && successCallback(res.data);
  21. }else{
  22. var err = res.data.msg || '请求失败!';
  23. errorCallback && errorCallback(err);
  24. isMsg || layer.open({content: err,skin: 'msg',time: 2});
  25. }
  26. }).catch(function(err){
  27. errorCallback && errorCallback(err);
  28. layer.open({content: err,skin: 'msg',time: 2});
  29. });
  30. },
  31. Url :function (opt) {
  32. var m = opt.m || 'wap', c = opt.c || '', a = opt.a || 'index', q = opt.q || '',
  33. p = opt.p || {}, params = '',gets='';
  34. params = Object.keys(p).map(function (key) {
  35. return key + '/' + p[key];
  36. }).join('/');
  37. gets = Object.keys(q).map(function (key) {
  38. return key+'='+ q[key];
  39. }).join('&');
  40. return '/' + m + '/' + c + '/' + a + (params == '' ? '' : '/' + params) + (gets == '' ? '' : '?' + gets);
  41. }
  42. };
  43. global.RequestAxios=RequestAxios
  44. window.Url = RequestAxios.Url;
  45. return RequestAxios;
  46. }(this))