request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. <<<<<<< HEAD
  15. =======
  16. // let path = 'http://j26df4a4.natappfree.cc/store'; //百域承品 夏伟
  17. // let path2 = 'http://j26df4a4.natappfree.cc'; //百域承品 夏伟
  18. // let path = 'http://d499966b.natappfree.cc/store'; //本地
  19. // let path2 = 'http://d499966b.natappfree.cc'; //本地
  20. >>>>>>> db97a32e991ee7a9b5a59978a4bee9ad519591ce
  21. //let path = 'http://e895a54d.natappfree.cc/store'; //百域承品 夏伟
  22. //let path2 = 'http://e895a54d.natappfree.cc'; //百域承品 夏伟
  23. //let path = 'http://d499966b.natappfree.cc/store'; //本地
  24. //let path2 = 'http://d499966b.natappfree.cc'; //本地
  25. let type = 0
  26. uni.setStorageSync('requestPath', path2)
  27. // uni.showLoading({
  28. // title: '加载中'
  29. // });
  30. if (router.indexOf("/live/liveData/like") != -1) {
  31. path = 'https://im.fhhx.runtzh.com';
  32. }
  33. if (router.indexOf("/app/order/getMyMergedOrderList") != -1 || router.indexOf("/app/order/deleteOrder") != -
  34. 1) {
  35. path = 'https://userapp.klbycp.com';
  36. }
  37. if (router.indexOf("/app/order/getMergedAfterSalesList") != -1) {
  38. path = 'https://userapp.klbycp.com';
  39. }
  40. if (router.indexOf("/live") !== -1 || router.indexOf("/liveAPP") !== -1) {
  41. // path = 'https://userapp.klbycp.com';
  42. path = path2
  43. router = router.replace('/liveAPP', '')
  44. type = 4
  45. token = uni.getStorageSync('AppToken');
  46. }
  47. if (router.indexOf("/patient/getPatientList") !== -1) {
  48. // path = 'https://userapp.klbycp.com';
  49. path = path2
  50. }
  51. if (router.indexOf("/getGotoWxAppLiveLink") !== -1) {
  52. // path = 'https://userapp.klbycp.com/companyapp';
  53. path = `${path2}/companyapp`;
  54. }
  55. let token = uni.getStorageSync('AppToken');
  56. if (router.indexOf("/companyapp") != -1) {
  57. // router =router.replace('/companyapp','')
  58. // path = 'https://userapp.klbycp.com' //百域承品
  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. }