request.js 3.9 KB

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