request.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // uni-app请求封装
  2. export default class Request {
  3. http(router, data = {}, method,contentType) {
  4. let that = this;
  5. // let path = 'http://localhost:8004';
  6. // let path = 'http://n9jxvn.natappfree.cc';
  7. let path = 'https://bd.runtzh.com/prod-api';
  8. uni.setStorageSync('requestPath',path)
  9. // uni.showLoading({
  10. // title: '載入中'
  11. // });
  12. return new Promise((resolve, reject) => {
  13. let token = uni.getStorageSync('AppToken');
  14. var httpContentType='application/x-www-form-urlencoded';
  15. if(contentType!=undefined){
  16. //application/json;charset=UTF-8
  17. httpContentType=contentType;
  18. }
  19. var routers=router;
  20. // 请求
  21. uni.request({
  22. header: {
  23. // 'Content-Type': 'application/x-www-form-urlencoded',
  24. 'Content-Type': httpContentType,
  25. },
  26. url: `${path}${router}`,
  27. data: data,
  28. method: method,
  29. success: (res) => {
  30. //收到开发者服务器成功返回的回调函数
  31. resolve(res.data)
  32. },
  33. fail:(res) =>{
  34. //接口调用失败的回调函数
  35. },
  36. complete:(res) =>{
  37. //接口调用结束的回调函数(调用成功、失败都会执行)
  38. if (res.data.code == 401) {
  39. return false
  40. }
  41. uni.hideLoading();
  42. }
  43. })
  44. })
  45. }
  46. }