request.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // uni-app请求封装
  2. export default class Request {
  3. http(router, data = {}, method,contentType,timeout=60000) {
  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://a7ad6869.natappfree.cc';//h5接口
  11. //path = 'http://192.168.110.247:8113'
  12. //path ='https://h5api.his.cdwjyyh.com';//h5接口
  13. // path ='http://z2fae9e9.natappfree.cc';//直播余红奇本地
  14. // path ='https://api.fhhx.runtzh.com';//直播余红奇线上
  15. // #endif
  16. // 看课模块
  17. if(router.indexOf("/app/course/getRealLink") != -1 ) {
  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. // path ='http://z2fae9e9.natappfree.cc'
  26. }
  27. //直播静默登录配置
  28. // if (router.indexOf("/live") !== -1 || router.indexOf("/liveAPP") !== -1) {
  29. // if(router.indexOf("/liveAPP") !== -1){
  30. // router = router.replace('/liveAPP', '')
  31. // }
  32. // token = uni.getStorageSync('LiveToken');
  33. // }
  34. // 腕表模块
  35. if(router.indexOf("/watch-api") != -1 ) {
  36. router = router.replace('/watch-api','')
  37. // path = 'http://192.168.10.155:8114'
  38. // path = 'http://192.168.10.135:8114'
  39. //path = 'http://192.168.110.61: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. let CompanyUserToken = uni.getStorageSync('CompanyUserToken');
  55. var httpContentType='application/x-www-form-urlencoded';
  56. if(contentType!=undefined){
  57. httpContentType=contentType;
  58. }
  59. var routers=router;
  60. //console.log("---qxj request url:"+`${path}${router}`+" params:"+JSON.stringify(data));
  61. // 直播模块使用liveToken
  62. let useToken = token;
  63. // 检查是否为直播模块路径
  64. const isLiveApi = router.indexOf("/appLive") !== -1 || router.indexOf("/liveAPP") !== -1;
  65. if(isLiveApi) {
  66. // 检查liveToken是否存在
  67. if (!liveToken || liveToken === '') {
  68. // 跳转到手机号码一键登录页面
  69. uni.navigateTo({
  70. url: '/pages/auth/login?quickLogin=true'
  71. });
  72. // 拒绝请求
  73. reject({errMsg: 'No liveToken, redirect to login'});
  74. return;
  75. }
  76. useToken = liveToken;
  77. if(router.indexOf("/appLive") !== -1){
  78. router = router.replace('/appLive', '')
  79. }
  80. if(router.indexOf("/liveAPP") !== -1){
  81. router = router.replace('/liveAPP', '')
  82. }
  83. }
  84. // 请求
  85. uni.request({
  86. header: {
  87. // 'Content-Type': 'application/x-www-form-urlencoded',
  88. 'Content-Type': httpContentType,
  89. 'AppToken': useToken,
  90. 'CompanyUserToken':CompanyUserToken //业务员token
  91. },
  92. url: `${path}${router}`,
  93. data: data,
  94. method: method,
  95. timeout: timeout,
  96. success: (res) => {
  97. //收到开发者服务器成功返回的回调函数
  98. if(res.data.code==401){ //没有权限直接退出到登录界面
  99. let pages = getCurrentPages();
  100. let url = pages[ pages.length - 1]; //当前页页面实例
  101. //如果登录界面已打开,自动关闭
  102. if(url!=undefined&&url.route=="pages/auth/login"){
  103. resolve(res.data)
  104. return;
  105. }
  106. }
  107. if(res.token) {
  108. uni.setStorageSync('AppToken',res.token)
  109. }
  110. resolve(res.data)
  111. },
  112. fail:(res) =>{
  113. //接口调用失败的回调函数
  114. reject(res);
  115. },
  116. complete:(res) =>{
  117. // console.log("complete==",`${path}${router}`,res)
  118. if (res.data&&res.data.code == 401) {
  119. uni.$emit('loginOut');
  120. return false
  121. }
  122. uni.hideLoading();
  123. }
  124. });
  125. })
  126. }
  127. }