request.js 5.8 KB

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