request.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 = 'http://192.168.110.225:8113/store'//中康本地
  12. // let path = 'https://userapp.cqsft.vip/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://user.test.ylrztop.com/prod-api'//云联融智优选
  27. // path = 'http://w43fa358.natappfree.cc'//本地接口
  28. type = 1
  29. token = uni.getStorageSync('ManageToken');
  30. }
  31. // 企微看课
  32. if(router.indexOf("/course_auto")!=-1){
  33. type = 2
  34. router =router.replace('/course_auto','')
  35. // path = 'https://userapp.gjh2024.com/apis'//金康键
  36. // path = 'https://userapp.moonxiang.com/prod-api'//青岛市德瑞康
  37. // path = 'http://w43fa358.natappfree.cc'
  38. path = 'https://userapp.zkhj6.com'//中康
  39. // path = 'https://userapp.cqsft.vip'//四福堂
  40. // path = 'https://user.test.ylrztop.com/prod-api'//云联融智优选
  41. token =uni.getStorageSync(TOKEN_KEYAuto)
  42. console.log("TOKEN_KEYAuto===",TOKEN_KEYAuto,token)
  43. }
  44. if(router.indexOf("/course_uniapp")!=-1){
  45. type = 3
  46. router =router.replace('/course_uniapp','')
  47. //张玉朋
  48. path = 'https://userapp.zkhj6.com'//中康
  49. // path = 'https://userapp.cqsft.vip'//四福堂
  50. // path = 'https://user.test.ylrztop.com/prod-api'//云联融智优选
  51. token =uni.getStorageSync('TOKEN_WEXIN')
  52. }
  53. return new Promise((resolve, reject) => {
  54. // token = uni.getStorageSync('AppToken');
  55. var httpContentType='application/x-www-form-urlencoded';
  56. if(contentType!=undefined){
  57. //application/json;charset=UTF-8
  58. httpContentType=contentType;
  59. }
  60. var routers=router;
  61. // 请求
  62. uni.request({
  63. header: {
  64. // 'Content-Type': 'application/x-www-form-urlencoded',
  65. 'Content-Type': httpContentType,
  66. 'AppToken': token
  67. },
  68. url: `${path}${router}`,
  69. data: data,
  70. method: method,
  71. success: (res) => {
  72. if(type !==0&&(res.data.code == 401 || res.data.code == 4001||res.data.code == 4004)) {
  73. store.commit('setCoureLogin', 2);
  74. uni.removeStorageSync("userinfos")
  75. // uni.removeStorageSync('userInfo');
  76. uni.removeStorageSync('TOKEN_WEXIN');
  77. if(type==2) {
  78. uni.removeStorageSync(TOKEN_KEYAuto)
  79. }
  80. if(type==1) {
  81. uni.removeStorageSync('ManageToken');
  82. uni.navigateTo({
  83. url:'/pages_manage/login'
  84. })
  85. }
  86. resolve({ code: 401, data: null });
  87. return
  88. }
  89. //收到开发者服务器成功返回的回调函数
  90. if(res.data.code==401){//没有权限直接退出到登录界面
  91. let pages = getCurrentPages();
  92. pages.forEach(function(element) {
  93. if(element!=undefined&&element.route=="pages/auth/login"){
  94. resolve(res.data)
  95. return;
  96. }
  97. });
  98. // let url = pages[ pages.length - 1]; //当前页页面实例
  99. // //如果登录界面已打开,自动关闭
  100. // if(url!=undefined&&url.route=="pages/auth/login"){
  101. // resolve(res.data)
  102. // return;
  103. // }
  104. uni.navigateTo({
  105. url:'/pages/auth/login',
  106. success: () => {
  107. uni.hideLoading();
  108. },
  109. fail: () => {
  110. uni.hideLoading();
  111. }
  112. })
  113. return;
  114. }
  115. if (res.data.token&&type==0) {
  116. uni.setStorageSync('AppToken',res.data.token)
  117. }
  118. resolve(res.data)
  119. },
  120. fail:(res) =>{
  121. //接口调用失败的回调函数
  122. },
  123. complete:(res) =>{
  124. //接口调用结束的回调函数(调用成功、失败都会执行)
  125. if (res.data.code == 401) {
  126. return false
  127. }
  128. uni.hideLoading();
  129. }
  130. })
  131. })
  132. }
  133. }