request.js 4.7 KB

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