request.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. this.console(res.data.code);
  30. if(error != null){
  31. reject(error);
  32. }else{
  33. if(res.data.code == -3){
  34. storage.remove("users")
  35. storage.remove("token")
  36. storage.remove("mobile")
  37. store.commit("DELETEUSERS")
  38. store.commit("DELETEMOBILE")
  39. uni.reLaunch({
  40. url: '/pages/public/login'
  41. })
  42. }else{
  43. resolve(res.data);
  44. }
  45. }
  46. });
  47. });
  48. },
  49. get(url="",data={}){
  50. return this.send({
  51. url: url,
  52. data: data
  53. });
  54. },
  55. post(url="",data={}){
  56. return this.send({
  57. url: url,
  58. data: data,
  59. method: "POST",
  60. });
  61. }
  62. };