request.js 5.0 KB

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