request.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // uni-app请求封装
  2. import {TOKEN_WEXIN} from '@/utils/common.js'
  3. export default class Request {
  4. http(router, data = {}, method,contentType) {
  5. let that = this;
  6. //曹丽芹
  7. // let path = 'http://192.168.10.131:7015'
  8. //涂小龙
  9. // let path = 'http://192.168.10.125:7015'
  10. //刘明欣
  11. // let path = 'http://192.168.10.150:8080'
  12. //张玉朋
  13. // let path = 'http://192.168.10.157:8113'
  14. //庄旭
  15. // let path = 'http://192.168.10.158:8007'//蜂巢快药
  16. let path = 'https://userapp.fbylive.com'//福本源
  17. // let path = 'https://userapp.zkhj6.com'//中康
  18. // let path = 'https://user.test.ylrztop.com/api'//云融融智
  19. // let path = 'https://userapp.ashyisheng.com'//蜂巢快药
  20. // let path = 'https://userapp.whhm.ylrzcloud.com/prod-api'//惠名大药房
  21. // let path = 'https://userapp.xdtongshuntang.top'//同顺堂
  22. // let path = 'https://userapp.liangmiaoedu.com'//良苗
  23. // let path = 'https://usercourse.beliyostore.com'//倍力优
  24. // let path = 'https://app.bainian1000y.cn/prod-api'//百年康城
  25. // let path = 'https://userapp.drk.ylrzcloud.com/prod-api'//青岛市德瑞康中医医院
  26. // let path = 'https://userapp.moonxiang.com/prod-api'//北京存在文化
  27. // let path = 'https://userapp.fbylive.com'//福本源测试
  28. uni.setStorageSync('requestPath',path)
  29. // uni.showLoading({
  30. // title: '加载中'
  31. // });
  32. let token = uni.getStorageSync('AppToken');
  33. // if(router.indexOf("/companyapp")!=-1){
  34. // router =router.replace('/companyapp','/companyappnew')
  35. // }
  36. if(router.indexOf("/course_uniapp")!=-1){
  37. router =router.replace('/course_uniapp','')
  38. // path = 'https://userapp.fbylive.com'//福本源
  39. // path = 'https://userapp.zkhj6.com'//中康
  40. // path = 'https://user.test.ylrztop.com/api'//云融融智
  41. // path = 'https://userapp.ashyisheng.com'//蜂巢快药
  42. // path = 'https://userapp.liangmiaoedu.com'//良苗
  43. // path = 'https://usercourse.beliyostore.com'//倍力优
  44. // path = 'https://userapp.bainian1000y.cn/prod-api'//百年康城
  45. // path = 'https://userapp.whhm.ylrzcloud.com/prod-api'//惠名大药房
  46. // path = 'https://userapp.cqsft.vip'//四福堂
  47. // path = 'https://userapp.drk.ylrzcloud.com/prod-api'//青岛市德瑞康中医医院
  48. // path = 'http://192.168.10.131:7014'
  49. // path = 'https://userapp.fbylive.com/new'//福本源测试
  50. //#ifdef MP-WEIXIN
  51. token =uni.getStorageSync('TOKEN_WEXIN')
  52. //#endif
  53. // #ifdef H5
  54. token =uni.getStorageSync('TOKEN_KEY')
  55. // #endif
  56. }
  57. if(router.indexOf("/QWcourse")!=-1){
  58. router =router.replace('/QWcourse','')
  59. token = uni.getStorageSync('TOKEN_KEYQW');
  60. }
  61. return new Promise((resolve, reject) => {
  62. // let token = uni.getStorageSync('AppToken');
  63. let usertoken = uni.getStorageSync("UserToken")
  64. var httpContentType='application/x-www-form-urlencoded';
  65. if(contentType!=undefined){
  66. //application/json;charset=UTF-8
  67. httpContentType=contentType;
  68. }
  69. var routers=router;
  70. // 请求
  71. uni.request({
  72. header: {
  73. // 'Content-Type': 'application/x-www-form-urlencoded',
  74. 'Content-Type': httpContentType,
  75. 'AppToken': token,
  76. 'UserToken':usertoken
  77. },
  78. url: `${path}${router}`,
  79. data: data,
  80. method: method,
  81. success: (res) => {
  82. //收到开发者服务器成功返回的回调函数
  83. if(res.data.code==401){//没有权限直接退出到登录界面
  84. let pages = getCurrentPages();
  85. let currentPage = pages[ pages.length - 1]; //当前页页面实例
  86. // 条件编译判断平台,设置不同登录页路径
  87. let loginPage = '';
  88. // #ifdef H5
  89. loginPage = '/pages/auth/login';
  90. // #endif
  91. // #ifdef MP-WEIXIN
  92. loginPage = '/pages/enterprise/enterprise';
  93. // #endif
  94. //如果登录界面已打开,自动关闭
  95. if (currentPage.route === loginPage.replace(/^\//, '')) {
  96. resolve(res.data);
  97. return;
  98. }
  99. uni.reLaunch({
  100. url:loginPage,
  101. success: () => {
  102. uni.hideLoading();
  103. },
  104. fail: () => {
  105. uni.hideLoading();
  106. }
  107. })
  108. return;
  109. }
  110. if (res.token) {
  111. uni.setStorageSync(TOKEN_KEY,res.data.token)
  112. }
  113. resolve(res.data)
  114. },
  115. fail:(res) =>{
  116. //接口调用失败的回调函数
  117. },
  118. complete:(res) =>{
  119. //接口调用结束的回调函数(调用成功、失败都会执行)
  120. console.log('1111',res)
  121. if (res.data&& res.data.code == 401) {
  122. return false
  123. }
  124. uni.hideLoading();
  125. }
  126. })
  127. })
  128. }
  129. }