request.js 5.1 KB

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