request.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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://userapi.scrm.ylrzcloud.com'//云联融智
  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. uni.setStorageSync('requestPath',path)
  36. // uni.showLoading({
  37. // title: '加载中'
  38. // });
  39. let token = uni.getStorageSync('AppToken');
  40. if(router.indexOf("/companyapp")!=-1){
  41. type = 1;
  42. // router =router.replace('/companyapp','')
  43. // path = 'https://companyapp.ljhehualu.com'//济南联志
  44. token = uni.getStorageSync('ManageToken');
  45. }
  46. if(router.indexOf("/course_uniapp")!=-1){
  47. type = 2;
  48. router =router.replace('/course_uniapp','')
  49. token =uni.getStorageSync('TOKEN_WEXIN')
  50. }
  51. // 企微看课
  52. if(router.indexOf("/course_auto")!=-1){
  53. type = 3;
  54. router =router.replace('/course_auto','')
  55. // path = 'http://a9e258d6.natappfree.cc'//百年康成本地
  56. token =uni.getStorageSync(TOKEN_KEYAuto)
  57. }
  58. return new Promise((resolve, reject) => {
  59. // let token = uni.getStorageSync('AppToken');
  60. let usertoken = uni.getStorageSync("UserToken")
  61. var httpContentType='application/x-www-form-urlencoded';
  62. if(contentType!=undefined){
  63. //application/json;charset=UTF-8
  64. httpContentType=contentType;
  65. }
  66. var routers=router;
  67. // 请求
  68. uni.request({
  69. header: {
  70. // 'Content-Type': 'application/x-www-form-urlencoded',
  71. 'Content-Type': httpContentType,
  72. 'AppToken': token,
  73. 'UserToken':usertoken
  74. },
  75. url: `${path}${router}`,
  76. data: data,
  77. method: method,
  78. success: (res) => {
  79. if(type !==0&&(res.data.code == 401 || res.data.code == 4001||res.data.code == 4004)) {
  80. store.commit('setCoureLogin', 2);
  81. uni.removeStorageSync("userinfos")
  82. // uni.removeStorageSync('userInfo');
  83. uni.removeStorageSync('TOKEN_WEXIN');
  84. if(type==2) {
  85. uni.removeStorageSync(TOKEN_KEYAuto)
  86. }
  87. if(type==1) {
  88. uni.removeStorageSync('ManageToken');
  89. uni.navigateTo({
  90. url:'/pages_manage/login'
  91. })
  92. }
  93. resolve({ code: 401, data: null });
  94. return
  95. }
  96. //收到开发者服务器成功返回的回调函数
  97. if(res.data.code==401){//没有权限直接退出到登录界面
  98. let pages = getCurrentPages();
  99. let currentPage = pages[ pages.length - 1]; //当前页页面实例
  100. // 条件编译判断平台,设置不同登录页路径
  101. let loginPage = '';
  102. loginPage = '/pages/enterprise/enterprise';
  103. //如果登录界面已打开,自动关闭
  104. if (currentPage.route === loginPage.replace(/^\//, '')) {
  105. resolve(res.data);
  106. return;
  107. }
  108. uni.reLaunch({
  109. url:loginPage,
  110. success: () => {
  111. uni.hideLoading();
  112. },
  113. fail: () => {
  114. uni.hideLoading();
  115. }
  116. })
  117. return;
  118. }
  119. if (res.token&&type==0) {
  120. uni.setStorageSync(TOKEN_KEY,res.data.token)
  121. }
  122. resolve(res.data)
  123. },
  124. fail:(res) =>{
  125. //接口调用失败的回调函数
  126. },
  127. complete:(res) =>{
  128. //接口调用结束的回调函数(调用成功、失败都会执行)
  129. if (res.data&& res.data.code == 401) {
  130. return false
  131. }
  132. uni.hideLoading();
  133. }
  134. })
  135. })
  136. }
  137. }