http.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import store from './store'
  2. const baseUrl = "http://192.168.1.101:8000/api";
  3. const baseImageUrl = "http://192.168.1.101:8000/";
  4. function initPramas() {
  5. arguments[0].url = baseUrl + arguments[0].url;
  6. // console.log(typeof arguments[0].fail)
  7. if (typeof arguments[0].fail === "undefined") {
  8. arguments[0].fail = (res) => {
  9. console.log(res)
  10. uni.showToast({
  11. title: res.data.msg,
  12. icon: "none"
  13. })
  14. console.log(res.data.code)
  15. }
  16. }
  17. const success = arguments[0].success
  18. arguments[0].success = (res) => {
  19. console.log(res)
  20. if (res.data.code === 0) {
  21. uni.showToast({
  22. title: res.data.msg,
  23. icon: "none"
  24. })
  25. arguments[0].fail(res)
  26. }
  27. if (res.data.code === 1) {
  28. success(res)
  29. }
  30. if (res.data.code === 401) {
  31. uni.navigateTo({
  32. url: "/pages/user/login"
  33. })
  34. }
  35. }
  36. if (typeof arguments[0].header === "undefined") {
  37. arguments[0].header = {}
  38. }
  39. if (store.state.user.token) {
  40. arguments[0].header["token"] = store.state.user.token;
  41. }
  42. return arguments[0];
  43. }
  44. export default {
  45. baseImageUrl: baseImageUrl,
  46. get: function() {
  47. let obj = initPramas(arguments[0]);
  48. obj.method = "GET";
  49. uni.request(obj);
  50. },
  51. post: function() {
  52. let obj = initPramas(arguments[0]);
  53. obj.method = "POST";
  54. obj.header["Content-type"] = "application/x-www-form-urlencoded"
  55. console.log(obj);
  56. uni.request(obj);
  57. }
  58. }