request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // uni-app请求封装
  2. export default class Request {
  3. http(router, data = {}, method,contentType,timeout=60000, hideLoading=true) {
  4. let that = this;
  5. // let path ='https://api.cdwjyyh.com';//app正式环境
  6. let path ='https://userapp.hshsyy.com';//河山正式环境
  7. //let path = 'http://192.168.110.247:8113'
  8. // #ifdef H5
  9. //path = 'http://192.168.110.245:8113'
  10. path ='https://h5api.his.cdwjyyh.com';//h5接口
  11. //path = 'http://192.168.110.247:8113'
  12. // path ='http://z2fae9e9.natappfree.cc';//直播余红奇本地
  13. // path ='https://api.fhhx.runtzh.com';//直播余红奇线上
  14. // #endif
  15. // 看课模块
  16. if(router.indexOf("/course_auto") != -1 ) {
  17. router = router.replace('/course_auto', '')
  18. path ='https://h5api.his.cdwjyyh.com';
  19. }
  20. // 直播模块
  21. if(router.indexOf("/appLive") !== -1) {
  22. // router = router.replace('/appLive','')
  23. path ='https://api.fhhx.runtzh.com';
  24. router = router.replace('/appLive','')
  25. }
  26. // 直播用户
  27. if(router.indexOf("/liveUser") !== -1) {
  28. // router = router.replace('/appLive','')
  29. path ='https://api.fhhx.runtzh.com';
  30. }
  31. //直播静默登录配置
  32. if(router.indexOf("/liveAPP") !== -1 ){
  33. path ='https://api.fhhx.runtzh.com';
  34. router = router.replace('/liveAPP', '')
  35. }
  36. // 腕表模块
  37. if(router.indexOf("/watch-api") != -1 ) {
  38. router = router.replace('/watch-api','')
  39. // path = 'http://192.168.10.155:8114'
  40. // path = 'http://192.168.10.135:8114'
  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. uni.setStorageSync('requestPath',path);
  49. // uni.showLoading({
  50. // title: '加载中'
  51. // });
  52. return new Promise((resolve, reject) => {
  53. let token = uni.getStorageSync('AppToken');
  54. let liveToken = uni.getStorageSync('liveToken');
  55. //console.log('indexlogin)))))))))))',uni.getStorageSync('liveToken'))
  56. //console.log('userData)))))))))))',uni.getStorageSync('userData'))
  57. let CompanyUserToken = uni.getStorageSync('CompanyUserToken');
  58. var httpContentType='application/x-www-form-urlencoded';
  59. if(contentType!=undefined){
  60. httpContentType=contentType;
  61. }
  62. var routers=router;
  63. // 直播模块使用liveToken
  64. let useToken = token;
  65. // 检查是否为直播模块路径
  66. //console.log('router',router)
  67. const isLiveApi = router.indexOf("/live") !== -1 || router.indexOf("/liveUser/app/user/getUserInfo") !== -1 || router.indexOf("/app/address/getAddressByDefault") !== -1;
  68. if(isLiveApi) {
  69. useToken = liveToken
  70. if(router.indexOf("/liveUser") !== -1){
  71. router = router.replace('/liveUser','')
  72. }
  73. }else{
  74. useToken = token
  75. }
  76. // 请求
  77. uni.request({
  78. header: {
  79. // 'Content-Type': 'application/x-www-form-urlencoded',
  80. 'Content-Type': httpContentType,
  81. 'AppToken': useToken,
  82. 'liveToken':liveToken,
  83. 'CompanyUserToken':CompanyUserToken //业务员token
  84. },
  85. url: `${path}${router}`,
  86. data: data,
  87. method: method,
  88. timeout: timeout,
  89. success: (res) => {
  90. //收到开发者服务器成功返回的回调函数
  91. if(res.data.code==401){ //没有权限直接退出到登录界面
  92. let pages = getCurrentPages();
  93. let url = pages[ pages.length - 1]; //当前页页面实例
  94. //如果登录界面已打开,自动关闭
  95. if(url!=undefined&&url.route=="pages/auth/login"){
  96. resolve(res.data)
  97. return;
  98. }
  99. }
  100. // if(res.token) {
  101. // uni.setStorageSync('AppToken',res.token)
  102. // }
  103. resolve(res.data)
  104. },
  105. fail:(res) =>{
  106. //接口调用失败的回调函数
  107. reject(res);
  108. },
  109. complete:(res) =>{
  110. //console.log("complete==",`${path}${router}`,res)
  111. if (res.data&&res.data.code == 401) {
  112. uni.$emit('loginOut');
  113. return false
  114. }
  115. if(hideLoading){
  116. uni.hideLoading();
  117. }
  118. }
  119. });
  120. })
  121. }
  122. }