request.js 6.1 KB

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