request.js 4.8 KB

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