request.js 5.4 KB

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