request.js 4.0 KB

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