request.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // uni-app请求封装
  2. // import {TOKEN_WEXIN} from '@/utils/common.js'
  3. import {TOKEN_KEYAuto} from '@/utils/courseTool.js'
  4. import store from '@/store/index.js'
  5. export default class Request {
  6. http(router, data = {}, method,contentType) {
  7. let that = this;
  8. let type = 0;
  9. // let path = 'https://user.test.ylrztop.com/api'//云联融智
  10. // let path = 'https://userappkyt.ylrzcloud.com'//宽益堂
  11. // let path = 'https://userapp.schstyl.cn'//鸿森堂
  12. // let path = 'https://userapp.myks8.com'//恒春来
  13. // let path = 'https://userapp.nmghysmytdyf.com'//蒙一堂
  14. // let path = 'https://usercourse.beliyostore.com'//倍力优
  15. // let path = 'https://app.bainian1000y.cn/prod-api'//百年康成
  16. // let path = 'http://a9e258d6.natappfree.cc'//百年康成本地
  17. // let path = 'https://userapp.zkhj6.com'//中康
  18. // let path = 'http://192.168.10.158:8007'//中康
  19. // let path = 'https://userapp.myks8.com' //恒春来
  20. // let path = 'https://userapp.jiuzhouzaixian.com'//九州
  21. // let path = 'https://userapp.bjyjbao.com'//医健宝
  22. // let path = 'https://userapp.yytcdta.com'//晚景如春
  23. // let path = 'https://userapp.yixingjian.top'//宏医堂
  24. let path = 'https://userapp.cqsft.vip'//四福堂
  25. // let path = 'https://userapp.ljhehualu.com'//济南联志健康
  26. // let path = 'https://yjb.mynatapp.cc'//本地
  27. // let path = 'https://hatuserapp.ylrzfs.com'//恒安图
  28. // let path = 'https://ddgyuserapp.slxinxi.top'//叮当国医
  29. // let path = 'https://userapp.cykbja.cn/prod-api'//纯正堂
  30. // let path = 'https://userapp.moonxiang.com'//存在文化
  31. // let path = 'https://userapp.jilinmaoq.cn'//顺亿景
  32. //let path = 'https://userapp.bjyjbao.com'//医健宝
  33. // let path = 'https://userapp.bainian1000y.cn'//百年康成
  34. // let path = 'https://userapp.jilinmaoq.cn'//致医
  35. // let path = 'https://userapp.cfrytjkzx.cn'//润元堂
  36. uni.setStorageSync('requestPath',path)
  37. // uni.showLoading({
  38. // title: '加载中'
  39. // });
  40. let token = uni.getStorageSync('AppToken');
  41. if(router.indexOf("/companyapp")!=-1){
  42. type = 1;
  43. // router =router.replace('/companyapp','')
  44. // path = 'https://companyapp.ljhehualu.com'//济南联志
  45. token = uni.getStorageSync('ManageToken');
  46. }
  47. if(router.indexOf("/course_uniapp")!=-1){
  48. type = 2;
  49. router =router.replace('/course_uniapp','')
  50. token =uni.getStorageSync('TOKEN_WEXIN')
  51. }
  52. // 企微看课
  53. if(router.indexOf("/course_auto")!=-1){
  54. type = 3;
  55. router =router.replace('/course_auto','')
  56. // path = 'http://a9e258d6.natappfree.cc'//百年康成本地
  57. token =uni.getStorageSync(TOKEN_KEYAuto)
  58. }
  59. return new Promise((resolve, reject) => {
  60. // let token = uni.getStorageSync('AppToken');
  61. let usertoken = uni.getStorageSync("UserToken")
  62. var httpContentType='application/x-www-form-urlencoded';
  63. if(contentType!=undefined){
  64. //application/json;charset=UTF-8
  65. httpContentType=contentType;
  66. }
  67. var routers=router;
  68. // 请求
  69. uni.request({
  70. header: {
  71. // 'Content-Type': 'application/x-www-form-urlencoded',
  72. 'Content-Type': httpContentType,
  73. 'AppToken': token,
  74. 'UserToken':usertoken
  75. },
  76. url: `${path}${router}`,
  77. data: data,
  78. method: method,
  79. success: (res) => {
  80. if(type !==0&&(res.data.code == 401 || res.data.code == 4001||res.data.code == 4004)) {
  81. store.commit('setCoureLogin', 2);
  82. uni.removeStorageSync("userinfos")
  83. // uni.removeStorageSync('userInfo');
  84. uni.removeStorageSync('TOKEN_WEXIN');
  85. if(type==2) {
  86. uni.removeStorageSync(TOKEN_KEYAuto)
  87. }
  88. if(type==1) {
  89. uni.removeStorageSync('ManageToken');
  90. uni.navigateTo({
  91. url:'/pages_manage/login'
  92. })
  93. }
  94. resolve({ code: 401, data: null });
  95. return
  96. }
  97. //收到开发者服务器成功返回的回调函数
  98. if(res.data.code==401){//没有权限直接退出到登录界面
  99. let pages = getCurrentPages();
  100. let currentPage = pages[ pages.length - 1]; //当前页页面实例
  101. // 条件编译判断平台,设置不同登录页路径
  102. let loginPage = '';
  103. loginPage = '/pages/enterprise/enterprise';
  104. //如果登录界面已打开,自动关闭
  105. if (currentPage.route === loginPage.replace(/^\//, '')) {
  106. resolve(res.data);
  107. return;
  108. }
  109. uni.reLaunch({
  110. url:loginPage,
  111. success: () => {
  112. uni.hideLoading();
  113. },
  114. fail: () => {
  115. uni.hideLoading();
  116. }
  117. })
  118. return;
  119. }
  120. if (res.token&&type==0) {
  121. uni.setStorageSync(TOKEN_KEY,res.data.token)
  122. }
  123. resolve(res.data)
  124. },
  125. fail:(res) =>{
  126. //接口调用失败的回调函数
  127. },
  128. complete:(res) =>{
  129. //接口调用结束的回调函数(调用成功、失败都会执行)
  130. if (res.data&& res.data.code == 401) {
  131. return false
  132. }
  133. uni.hideLoading();
  134. }
  135. })
  136. })
  137. }
  138. }