request.js 6.0 KB

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