request.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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("users");
  21. let token = storage.getJson("token");
  22. if(users != null && token != null){
  23. options.header = { "Authorization" : token };
  24. }
  25. this.console(options);
  26. return new Promise((resolve, reject) =>{
  27. uni.request(options).then(data=>{
  28. var [error, res] = data;
  29. if(error != null){
  30. reject(error);
  31. }else{
  32. if(res.data.code == -3){
  33. storage.remove("users")
  34. storage.remove("token")
  35. storage.remove("mobile")
  36. store.commit("DELETEUSERS")
  37. store.commit("DELETEMOBILE")
  38. uni.reLaunch({
  39. url: '/pages/public/login'
  40. })
  41. }else{
  42. resolve(res.data);
  43. }
  44. }
  45. });
  46. });
  47. },
  48. get(url="",data={}){
  49. return this.send({
  50. url: url,
  51. data: data
  52. });
  53. },
  54. post(url="",data={}){
  55. return this.send({
  56. url: url,
  57. data: data,
  58. method: "POST",
  59. });
  60. }
  61. };