request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // uni-app请求封装
  2. export default class Request {
  3. http(router, data = {}, method, contentType, url) {
  4. let that = this;
  5. let path = 'https://api.fhhx.runtzh.com';
  6. // let path = 'http://192.168.10.122:7014';
  7. // let path = 'http://192.168.10.126:7014';
  8. // let path = 'https://live.test.ylrztop.com/live-api'; // 余红奇
  9. // let path = 'http://192.168.10.166:7014'; // 余红奇
  10. let token = "";
  11. let type = 0
  12. if (url != null) {
  13. type = 1
  14. let projectCode = uni.getStorageSync('projectCode')
  15. path = uni.getStorageSync('addressUrl_' + projectCode)
  16. token = uni.getStorageSync('AppTokenmini_MYCourse')
  17. } else {
  18. type = 0
  19. uni.setStorageSync('requestPath', path)
  20. token = uni.getStorageSync('AppToken');
  21. }
  22. if (router.indexOf("/course_uniapp") != -1) {
  23. type = 2;
  24. router = router.replace('/course_uniapp', '')
  25. let projectCode = uni.getStorageSync('projectCode')
  26. path = uni.getStorageSync('addressUrl_' + projectCode)
  27. token = uni.getStorageSync('TOKEN_WEXIN')
  28. }
  29. // uni.showLoading({
  30. // title: '加载中'
  31. // });
  32. return new Promise((resolve, reject) => {
  33. var httpContentType = 'application/x-www-form-urlencoded';
  34. if (!path) {
  35. uni.showToast({
  36. title: '链接有误',
  37. icon: 'none'
  38. });
  39. return
  40. reject('链接有误')
  41. }
  42. if (contentType != undefined) {
  43. //application/json;charset=UTF-8
  44. httpContentType = contentType;
  45. }
  46. var routers = router;
  47. // 请求
  48. uni.request({
  49. header: {
  50. // 'Content-Type': 'application/x-www-form-urlencoded',
  51. 'Content-Type': httpContentType,
  52. 'AppToken': token
  53. },
  54. url: `${path}${router}`,
  55. data: data,
  56. method: method,
  57. success: (res) => {
  58. //收到开发者服务器成功返回的回调函数
  59. if (res.data.code == 401) { //没有权限直接退出到登录界面
  60. let pages = getCurrentPages();
  61. pages.forEach(function(element) {
  62. if (element != undefined && element.route ==
  63. "pages/auth/login") {
  64. resolve(res.data)
  65. return;
  66. }
  67. });
  68. // let url = pages[ pages.length - 1]; //当前页页面实例
  69. // //如果登录界面已打开,自动关闭
  70. // if(url!=undefined&&url.route=="pages/auth/login"){
  71. // resolve(res.data)
  72. // return;
  73. // }
  74. uni.navigateTo({
  75. url: '/pages/auth/login',
  76. success: () => {
  77. uni.hideLoading();
  78. },
  79. fail: () => {
  80. uni.hideLoading();
  81. }
  82. })
  83. return;
  84. }
  85. if (res.data.token && type == 0) {
  86. uni.setStorageSync('AppToken', res.data.token)
  87. }
  88. resolve(res.data)
  89. },
  90. fail: (res) => {
  91. //接口调用失败的回调函数
  92. },
  93. complete: (res) => {
  94. //接口调用结束的回调函数(调用成功、失败都会执行)
  95. if (res.data.code == 401) {
  96. return false
  97. }
  98. uni.hideLoading();
  99. }
  100. })
  101. })
  102. }
  103. }