request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // uni-app请求封装
  2. import {
  3. TOKEN_KEYAuto
  4. } from '@/utils/courseTool.js'
  5. import store from '@/store/index.js'
  6. export default class Request {
  7. http(router, data = {}, method, contentType) {
  8. let that = this;
  9. //let path = 'http://localhost:7014';
  10. // let path = 'https://test.userapp.store.cdwjyyh.com';
  11. // let path = 'https://user.test.ylrztop.com/api';
  12. let path = 'https://userapp.klbycp.com/store'; //百域承品
  13. let path2 = 'https://userapp.klbycp.com'; //百域承品
  14. // let path = 'http://j9d5863c.natappfree.cc/store';//百域承品
  15. // let path2 = 'http://j9d5863c.natappfree.cc';//百域承品
  16. let type = 0
  17. uni.setStorageSync('requestPath', path2)
  18. // uni.showLoading({
  19. // title: '加载中'
  20. // });
  21. if (router.indexOf("/live/liveData/like") != -1) {
  22. path = 'https://im.fhhx.runtzh.com';
  23. }
  24. if (router.indexOf("/live") !== -1||router.indexOf("/liveAPP") !== -1) {
  25. path = 'https://userapp.klbycp.com';
  26. console.log("在live里面>>>>.",`${path}${router}`)
  27. router = router.replace('/liveAPP', '')
  28. type=4
  29. token = uni.getStorageSync('AppToken');
  30. }
  31. if (router.indexOf("/patient/getPatientList") !== -1) {
  32. path = 'https://userapp.klbycp.com';
  33. // path = 'http://j9d5863c.natappfree.cc';
  34. }
  35. if (router.indexOf("/getGotoWxAppLiveLink") !== -1) {
  36. path = 'https://userapp.klbycp.com/companyapp';
  37. }
  38. let token = uni.getStorageSync('AppToken');
  39. if (router.indexOf("/companyapp") != -1) {
  40. // router =router.replace('/companyapp','')
  41. path = 'https://userapp.klbycp.com' //百域承品
  42. type = 1
  43. token = uni.getStorageSync('ManageToken');
  44. }
  45. // 企微看课
  46. if (router.indexOf("/course_auto") != -1) {
  47. type = 2
  48. router = router.replace('/course_auto', '')
  49. // path = 'http://w43fa358.natappfree.cc'
  50. path = 'https://userapp.klbycp.com' //百域承品
  51. token = uni.getStorageSync(TOKEN_KEYAuto)
  52. console.log("TOKEN_KEYAuto===", TOKEN_KEYAuto, token)
  53. }
  54. if (router.indexOf("/course_uniapp") != -1) {
  55. type = 3
  56. router = router.replace('/course_uniapp', '')
  57. //张玉朋
  58. // let path = 'http://192.168.10.158:8113'//
  59. path = 'https://userapp.klbycp.com' //百域承品
  60. token = uni.getStorageSync('TOKEN_WEXIN')
  61. }
  62. return new Promise((resolve, reject) => {
  63. // token = uni.getStorageSync('AppToken');
  64. var httpContentType = 'application/x-www-form-urlencoded';
  65. if (contentType != undefined) {
  66. //application/json;charset=UTF-8
  67. httpContentType = contentType;
  68. }
  69. var routers = router;
  70. // 请求
  71. uni.request({
  72. header: {
  73. // 'Content-Type': 'application/x-www-form-urlencoded',
  74. 'Content-Type': httpContentType,
  75. 'AppToken': token
  76. },
  77. url: `${path}${router}`,
  78. data: data,
  79. method: method,
  80. success: (res) => {
  81. if (type !== 0 && (res.data.code == 401 || res.data.code == 4001 || res.data
  82. .code == 4004)) {
  83. store.commit('setCoureLogin', 2);
  84. uni.removeStorageSync("userinfos")
  85. // uni.removeStorageSync('userInfo');
  86. uni.removeStorageSync('TOKEN_WEXIN');
  87. if (type == 2) {
  88. uni.removeStorageSync(TOKEN_KEYAuto)
  89. }
  90. if (type == 1) {
  91. uni.removeStorageSync('ManageToken');
  92. uni.navigateTo({
  93. url: '/pages_manage/login'
  94. })
  95. }
  96. resolve({
  97. code: 401,
  98. data: null
  99. });
  100. return
  101. }
  102. //收到开发者服务器成功返回的回调函数
  103. if (res.data.code == 401) { //没有权限直接退出到登录界面
  104. let pages = getCurrentPages();
  105. pages.forEach(function(element) {
  106. if (element != undefined && element.route ==
  107. "pages/auth/login") {
  108. resolve(res.data)
  109. return;
  110. }
  111. });
  112. // let url = pages[ pages.length - 1]; //当前页页面实例
  113. // //如果登录界面已打开,自动关闭
  114. // if(url!=undefined&&url.route=="pages/auth/numberlogin"){
  115. // resolve(res.data)
  116. // return;
  117. // }
  118. uni.navigateTo({
  119. url: '/pages/auth/login',
  120. success: () => {
  121. uni.hideLoading();
  122. },
  123. fail: () => {
  124. uni.hideLoading();
  125. }
  126. })
  127. return;
  128. }
  129. if (res.data.token && type == 0) {
  130. uni.setStorageSync('AppToken', res.data.token)
  131. }
  132. resolve(res.data)
  133. },
  134. fail: (res) => {
  135. //接口调用失败的回调函数
  136. },
  137. complete: (res) => {
  138. //接口调用结束的回调函数(调用成功、失败都会执行)
  139. if (res.data.code == 401) {
  140. return false
  141. }
  142. uni.hideLoading();
  143. }
  144. })
  145. })
  146. }
  147. }