request.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import config from "@/config";
  2. import storage from "./storage";
  3. export default {
  4. console(options){
  5. if(config.debug){
  6. console.log("====================【request start】===========================");
  7. console.log("header: " + JSON.stringify(options.header));
  8. console.log("method: " + options.method + " URL: " + options.url);
  9. console.log(options.data);
  10. console.log("====================【request end】===========================");
  11. }
  12. },
  13. domain(){
  14. return config.uni_app_web_api_url.replace("api","");
  15. },
  16. send(options={}){
  17. options.url = config.uni_app_web_api_url + '' + options.url;
  18. options.method = options.method || "GET";
  19. let users = storage.getJson("users");
  20. if(users != null){
  21. options.header = { "Auth-Token" : 'Bearer ' + users.token };
  22. }
  23. this.console(options);
  24. return new Promise((resolve, reject) =>{
  25. uni.request(options).then(data=>{
  26. var [error, res] = data;
  27. this.console(res);
  28. if(error != null){
  29. reject(error);
  30. }else{
  31. if(res.data.status == '-1001'){
  32. uni.hideLoading();
  33. uni.navigateTo({
  34. url: '/pages/public/login'
  35. });
  36. }else{
  37. resolve(res.data);
  38. }
  39. }
  40. });
  41. });
  42. },
  43. get(url="",data={}){
  44. return this.send({
  45. url: url,
  46. data: data
  47. });
  48. },
  49. post(url="",data={}){
  50. return this.send({
  51. url: url,
  52. data: data,
  53. method: "POST"
  54. });
  55. }
  56. };