request.js 5.1 KB

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