request.js 5.3 KB

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