request.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // import {
  2. // checkMaOpenIdForRequest
  3. // } from '../utils/maOpenIdInterceptor.js';
  4. // uni-app请求封装
  5. export default class Request {
  6. http(router, data = {}, method, contentType) {
  7. let that = this;
  8. // let path2 = 'https://userapp.klbycp.com'; //百域承品
  9. let path = 'https://userapp.klbycp.com'; //百域承品
  10. //let path = 'http://e9c6a3a9.natappfree.cc'; //百域承品 todo yhq
  11. // let path = 'http://f6b69df3.natappfree.cc'; //百域承品
  12. // let path2 = 'https://userapp.zkhj6.com';
  13. // let path = 'https://userapp.zkhj6.com';
  14. let token = uni.getStorageSync('AppToken');
  15. //let live= uni.getStorageSync('AppToken')
  16. //uni.setStorageSync('liveToken',live)
  17. // // #ifdef H5
  18. // path = 'https://userapp.klbycp.com'; //h5接口
  19. // // #endif
  20. // if (router.indexOf("/live/liveData/like") != -1) {
  21. // path = 'https://userapp.klbycp.com';
  22. // }
  23. // if (router.indexOf("/common") != -1) {
  24. // path = path2;
  25. // }
  26. // if (router.indexOf("/app/order/getMyMergedOrderList") != -1 || router.indexOf("/app/order/deleteOrder") != -
  27. // 1) {
  28. // path = path2
  29. // }
  30. // if (router.indexOf("/app/order/getMergedAfterSalesList") != -1) {
  31. // path = path2
  32. // }
  33. // 看课模块
  34. if (router.indexOf("/app/course/getRealLink") != -1) {
  35. path = 'https://userapp.klbycp.com';
  36. }
  37. if(router.indexOf("/appLive") !== -1) {
  38. router = router.replace('/appLive','')
  39. // path ='https://userapp.klbycp.com';
  40. // router = router.replace('/appLive','')
  41. // path ='http://z2fae9e9.natappfree.cc'
  42. }
  43. //看课点播
  44. if(router.indexOf("/course_uniapp") != -1 ) {
  45. // path ='https://userapp.klbycp.com';
  46. router = router.replace('/course_uniapp','')
  47. }
  48. if(router.indexOf("/course_auto") != -1 ) {
  49. router = router.replace('/course_auto','')
  50. }
  51. // 腕表模块
  52. // if (router.indexOf("/watch-api") != -1) {
  53. // router = router.replace('/watch-api', '')
  54. // // path = 'http://42.194.245.189:8114'
  55. // }
  56. // // doctorAi模块
  57. // if (router.indexOf("/doctorAi") != -1) {
  58. // router = router.replace('/doctorAi', '')
  59. // // path = 'http://doctor.ai.cdwjyyh.com'
  60. // }
  61. // path = 'http://192.168.110.18:8113'
  62. // 手动发课
  63. // path = 'http://192.168.110.225:8113'
  64. if(router.indexOf("/companyapp") != -1 ) {
  65. // path = 'http://192.168.110.225:8007'
  66. // path ='https://userapp.zkhj6.com';
  67. // router = router.replace('/companyapp','')
  68. token = uni.getStorageSync('ManageToken');
  69. }
  70. // path = 'http://192.168.110.18:8113'
  71. // path = 'https://70538sz006bj.vicp.fun'
  72. // path = 'https://userapp.jnmyunl.com'
  73. const pages = getCurrentPages();
  74. let curPath = ''
  75. if (pages) {
  76. const curPage = pages[pages.length - 1];
  77. curPath = curPage && curPage.route || '';
  78. }
  79. // 商城
  80. // let urlRouter = ['/app/product/getProductDetails', '/app/product/getProducts',
  81. // '/app/product/getProductCate', '/app/product/getProductCateByPid', '/app/product/getCarts',
  82. // '/app/product/getGoodsProducts', '/app/product/addCart', '/app/product/getCartCount',
  83. // '/app/address/getAddressList', '/app/storeOrder/confirm', '/app/common/getWeixinOrderTemps',
  84. // '/app/storeOrder/computed', '/app/storeOrder/create', '/app/storeOrder/getStoreOrderById',
  85. // '/app/coupon/getMyCouponList', '/app/user/getProductFoots', '/app/companyUser/getUserInfo',
  86. // '/app/companyOrder/getSalesProducts', '/app/coupon/getCompanyCouponIssueList',
  87. // '/app/storeOrder/getCompanyStoreOrderList', '/app/storeOrder/getMyStoreOrderById',
  88. // '/app/coupon/getMyEnableCouponList', '/app/fsInterestAi/getRecommendRoleList',
  89. // '/app/common/getStoreConfig', '/app/product/cartNum'
  90. // ]
  91. // const pay = ['/app/storeOrder/editPayType', '/app/storeOrder/pay',
  92. // '/app/storeAfterSales/getStoreAfterSalesList'
  93. // ]
  94. // urlRouter = [...urlRouter, ...pay]
  95. // if (urlRouter.includes(router) || curPath.indexOf("pages_shopping") != -1 || curPath.indexOf(
  96. // "pages_user") != -1 || curPath.indexOf("pages_company") != -1) {
  97. // // path = 'http://192.168.110.18:8113/store'
  98. // path = 'https://userapp.zkhj6.com/store'
  99. // }
  100. uni.setStorageSync('requestPath', path);
  101. // uni.showLoading({
  102. // title: '加载中'
  103. // });
  104. // if (!checkMaOpenIdForRequest(router)) {
  105. // return Promise.reject({
  106. // code: -2,
  107. // msg: '请先绑定小程序'
  108. // });
  109. // }
  110. return new Promise((resolve, reject) => {
  111. //let liveToken = uni.getStorageSync('liveToken');
  112. ///console.log('indexlogin)))))))))))',uni.getStorageSync('liveToken'))
  113. let CompanyUserToken = uni.getStorageSync('CompanyUserToken');
  114. var httpContentType = 'application/x-www-form-urlencoded';
  115. if (contentType != undefined) {
  116. httpContentType = contentType;
  117. }
  118. var routers = router;
  119. // 直播模块使用liveToken
  120. //let useToken = token;
  121. // 检查是否为直播模块路径
  122. // const isLiveApi = router.indexOf("/live") !== -1;
  123. // if(isLiveApi) {
  124. // useToken = liveToken
  125. // }
  126. //console.log("---qxj request url",`${path}${router}`+" params:"+JSON.stringify(data)+" \n AppToken:"+token);
  127. // 请求
  128. uni.request({
  129. header: {
  130. // 'Content-Type': 'application/x-www-form-urlencoded',
  131. 'Content-Type': httpContentType,
  132. 'AppToken': token,
  133. 'CompanyUserToken': CompanyUserToken, //业务员token
  134. 'SourceType': 'APP'
  135. },
  136. url: `${path}${router}`,
  137. data: data,
  138. method: method,
  139. success: (res) => {
  140. //收到开发者服务器成功返回的回调函数
  141. if (res.data.code == 401) { //没有权限直接退出到登录界面
  142. let pages = getCurrentPages();
  143. let url = pages[pages.length - 1]; //当前页页面实例
  144. //如果登录界面已打开,自动关闭
  145. if (url != undefined && url.route == "pages/auth/login") {
  146. resolve(res.data)
  147. return;
  148. }
  149. }
  150. if (res.data.code == 403) {
  151. // return uni.redirectTo({
  152. // url:'/pages_company/auth/login'
  153. // })
  154. }
  155. if (res.token) {
  156. uni.setStorageSync('AppToken', res.token)
  157. }
  158. resolve(res.data)
  159. },
  160. fail: (res) => {
  161. //接口调用失败的回调函数
  162. },
  163. complete: (res) => {
  164. // console.log("complete=======",path,router,res.data)
  165. //接口调用结束的回调函数(调用成功、失败都会执行)
  166. if (res.data && res.data.code == 401) {
  167. uni.$emit('loginOut');
  168. return false
  169. }
  170. uni.hideLoading();
  171. }
  172. });
  173. })
  174. }
  175. }