request.js 3.4 KB

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