request.js 1.4 KB

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