request.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import config from "@/config";
  2. import storage from "./storage";
  3. import store from "../store/index"
  4. export default {
  5. console(options) {
  6. // if(config.debug){
  7. // console.log("====================【request start】===========================");
  8. // console.log("header: " + JSON.stringify(options.header));
  9. // console.log("method: " + options.method + " URL: " + options.url);
  10. // console.log(options.data);
  11. // console.log("====================【request end】===========================");
  12. // }
  13. },
  14. domain() {
  15. return config.uni_app_web_api_url.replace("api", "");
  16. },
  17. send(options = {}) {
  18. options.url = config.uni_app_web_api_url + '' + options.url;
  19. options.method = options.method || "GET";
  20. let users = storage.getJson("userId");
  21. let token = storage.getJson("token");
  22. if (users != null && token != null) {
  23. options.header = {
  24. "Authorization": token
  25. };
  26. }
  27. this.console(options);
  28. return new Promise((resolve, reject) => {
  29. // uni.showLoading({
  30. // title: '加载中...',
  31. // mask: true
  32. // });
  33. uni.request(options).then(data => {
  34. var [error, res] = data;
  35. if (error != null) {
  36. reject(error);
  37. uni.showToast({
  38. title: '请求接口失败!',
  39. icon: 'none',
  40. })
  41. } else {
  42. if (res.data.code == -3) {
  43. storage.remove("users")
  44. storage.remove("token")
  45. store.commit("DELETEUSERS")
  46. uni.reLaunch({
  47. url: '/pages/public/login'
  48. })
  49. } else {
  50. resolve(res.data);
  51. }
  52. }
  53. // uni.hideLoading();
  54. });
  55. });
  56. },
  57. get(url = "", data = {}) {
  58. return this.send({
  59. url: url,
  60. data: data
  61. });
  62. },
  63. post(url = "", data = {}) {
  64. return this.send({
  65. url: url,
  66. data: data,
  67. method: "POST",
  68. });
  69. }
  70. };