request.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // uni-app请求封装
  2. import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
  3. export default class Request {
  4. http(router, data = {}, method,contentType) {
  5. let that = this;
  6. //let path = 'http://localhost:7014';
  7. // let path = 'https://test.userapp.store.cdwjyyh.com';
  8. // let path = 'https://user.test.ylrztop.com/api';
  9. // let path = 'https://userapp.zkhj6.com/store'//中康
  10. // let path = 'https://userapp.cqsft.vip/store'//四福堂
  11. let path = 'https://userapp.ysya.top/store'//益寿缘
  12. // let path = 'https://userapp.ysya.top/ceshi/store'//益寿缘测试
  13. // let path = 'http://w43fa358.natappfree.cc/store'//本地接口
  14. // let path = 'https://userapp.moonxiang.com/prod-api/store'//北京存在文化商城
  15. // let path = 'https://user.test.ylrztop.com/prod-api/store'//云联融智优选
  16. let type = 0
  17. uni.setStorageSync('requestPath',path)
  18. // uni.showLoading({
  19. // title: '加载中'
  20. // });
  21. let token = uni.getStorageSync('AppToken');
  22. if(router.indexOf("/companyapp")!=-1){
  23. // router =router.replace('/companyapp','')
  24. // path = 'https://userapp.zkhj6.com'//中康
  25. // path = 'https://userapp.cqsft.vip'//四福堂
  26. path = 'https://userapp.ysya.top'//益寿缘
  27. // path = 'https://user.test.ylrztop.com/prod-api'//云联融智优选
  28. // path = 'http://w43fa358.natappfree.cc'//本地接口
  29. type = 1
  30. token = uni.getStorageSync('ManageToken');
  31. }
  32. // 企微看课
  33. if(router.indexOf("/course_auto")!=-1){
  34. type = 2
  35. router =router.replace('/course_auto','')
  36. // path = 'https://userapp.gjh2024.com/apis'//金康键
  37. // path = 'https://userapp.moonxiang.com/prod-api'//青岛市德瑞康
  38. // path = 'http://w43fa358.natappfree.cc'
  39. // path = 'https://userapp.zkhj6.com'//中康
  40. // path = 'https://userapp.cqsft.vip'//四福堂
  41. // path = 'https://userapp.ysya.top'//益寿缘
  42. path = 'https://h5.ysya.top'//益寿缘
  43. // path = 'https://user.test.ylrztop.com/prod-api'//云联融智优选
  44. token =uni.getStorageSync(TOKEN_KEYAuto)
  45. console.log("TOKEN_KEYAuto===",TOKEN_KEYAuto,token)
  46. }
  47. if(router.indexOf("/course_uniapp")!=-1){
  48. type = 3
  49. router =router.replace('/course_uniapp','')
  50. //张玉朋
  51. // let path = 'http://192.168.10.158:7014'//
  52. // path = 'https://userapp.zkhj6.com'//中康
  53. // path = 'https://user.test.ylrztop.com/api'//云融融智
  54. // path = 'https://userapp.ashyisheng.com'//蜂巢快药(爱上嘉园)
  55. // path = 'https://userapp.liangmiaoedu.com'//良苗
  56. // path = 'https://usercourse.beliyostore.com'//倍力优
  57. // path = 'https://userapp.bainian1000y.cn/prod-api'//百年康城
  58. // path = 'https://userapp.whhm.ylrzcloud.com/prod-api'//惠名大药房
  59. // path = 'https://userapp.cqsft.vip'//四福堂
  60. // path = 'https://userapp.drkzyy.cn/prod-api'//青岛市德瑞康
  61. // path = 'http://w43fa358.natappfree.cc'
  62. // path = 'https://userapp.zkhj6.com'//中康
  63. // path = 'https://userapp.cqsft.vip'//四福堂
  64. path = 'https://userapp.ysya.top'//益寿缘
  65. // path = 'https://user.test.ylrztop.com/prod-api'//云联融智优选
  66. token =uni.getStorageSync('TOKEN_WEXIN')
  67. }
  68. return new Promise((resolve, reject) => {
  69. // token = uni.getStorageSync('AppToken');
  70. var httpContentType='application/x-www-form-urlencoded';
  71. if(contentType!=undefined){
  72. //application/json;charset=UTF-8
  73. httpContentType=contentType;
  74. }
  75. var routers=router;
  76. // 请求
  77. uni.request({
  78. header: {
  79. // 'Content-Type': 'application/x-www-form-urlencoded',
  80. 'Content-Type': httpContentType,
  81. 'AppToken': token
  82. },
  83. url: `${path}${router}`,
  84. data: data,
  85. method: method,
  86. success: (res) => {
  87. //收到开发者服务器成功返回的回调函数
  88. if(res.data.code==401){//没有权限直接退出到登录界面
  89. let pages = getCurrentPages();
  90. pages.forEach(function(element) {
  91. if(element!=undefined&&element.route=="pages/auth/login"){
  92. resolve(res.data)
  93. return;
  94. }
  95. });
  96. // let url = pages[ pages.length - 1]; //当前页页面实例
  97. // //如果登录界面已打开,自动关闭
  98. // if(url!=undefined&&url.route=="pages/auth/login"){
  99. // resolve(res.data)
  100. // return;
  101. // }
  102. uni.navigateTo({
  103. url:'/pages/auth/login',
  104. success: () => {
  105. uni.hideLoading();
  106. },
  107. fail: () => {
  108. uni.hideLoading();
  109. }
  110. })
  111. return;
  112. }
  113. if (res.data.token&&type==0) {
  114. uni.setStorageSync('AppToken',res.data.token)
  115. }
  116. resolve(res.data)
  117. },
  118. fail:(res) =>{
  119. //接口调用失败的回调函数
  120. },
  121. complete:(res) =>{
  122. //接口调用结束的回调函数(调用成功、失败都会执行)
  123. if (res.data.code == 401) {
  124. return false
  125. }
  126. uni.hideLoading();
  127. }
  128. })
  129. })
  130. }
  131. }