request.js 5.9 KB

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