request.js 4.6 KB

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