request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // uni-app请求封装
  2. export default class Request {
  3. http(router, data = {}, method,contentType) {
  4. let that = this;
  5. //let path = 'http://localhost:7014';
  6. // let path = 'https://test.userapp.store.cdwjyyh.com';
  7. // let path = 'https://user.test.ylrztop.com/api';
  8. // let path = 'https://userapp.zkhj6.com'//中康
  9. let path = 'http://zd7f9ae7.natappfree.cc'//中康
  10. uni.setStorageSync('requestPath',path)
  11. // uni.showLoading({
  12. // title: '加载中'
  13. // });
  14. let token = uni.getStorageSync('AppToken');
  15. if(router.indexOf("/companyapp")!=-1){
  16. // router =router.replace('/companyapp','')
  17. // path = 'http://192.168.10.158:7015'//
  18. token = uni.getStorageSync('ManageToken');
  19. }
  20. if(router.indexOf("/course_uniapp")!=-1){
  21. router =router.replace('/course_uniapp','')
  22. //张玉朋
  23. // let path = 'http://192.168.10.158:7014'//
  24. // path = 'https://userapp.zkhj6.com'//中康
  25. // path = 'https://user.test.ylrztop.com/api'//云融融智
  26. // path = 'https://userapp.ashyisheng.com'//蜂巢快药(爱上嘉园)
  27. // path = 'https://userapp.liangmiaoedu.com'//良苗
  28. // path = 'https://usercourse.beliyostore.com'//倍力优
  29. // path = 'https://userapp.bainian1000y.cn/prod-api'//百年康城
  30. // path = 'https://userapp.whhm.ylrzcloud.com/prod-api'//惠名大药房
  31. // path = 'https://userapp.cqsft.vip'//四福堂
  32. // path = 'https://userapp.drkzyy.cn/prod-api'//青岛市德瑞康
  33. // path = 'http://192.168.10.131:7014'
  34. token =uni.getStorageSync('TOKEN_WEXIN')
  35. }
  36. return new Promise((resolve, reject) => {
  37. // token = uni.getStorageSync('AppToken');
  38. var httpContentType='application/x-www-form-urlencoded';
  39. if(contentType!=undefined){
  40. //application/json;charset=UTF-8
  41. httpContentType=contentType;
  42. }
  43. var routers=router;
  44. // 请求
  45. uni.request({
  46. header: {
  47. // 'Content-Type': 'application/x-www-form-urlencoded',
  48. 'Content-Type': httpContentType,
  49. 'AppToken': token
  50. },
  51. url: `${path}${router}`,
  52. data: data,
  53. method: method,
  54. success: (res) => {
  55. //收到开发者服务器成功返回的回调函数
  56. if(res.data.code==401){//没有权限直接退出到登录界面
  57. let pages = getCurrentPages();
  58. pages.forEach(function(element) {
  59. if(element!=undefined&&element.route=="pages/auth/login"){
  60. resolve(res.data)
  61. return;
  62. }
  63. });
  64. // let url = pages[ pages.length - 1]; //当前页页面实例
  65. // //如果登录界面已打开,自动关闭
  66. // if(url!=undefined&&url.route=="pages/auth/login"){
  67. // resolve(res.data)
  68. // return;
  69. // }
  70. uni.navigateTo({
  71. url:'/pages/auth/login',
  72. success: () => {
  73. uni.hideLoading();
  74. },
  75. fail: () => {
  76. uni.hideLoading();
  77. }
  78. })
  79. return;
  80. }
  81. if (res.data.token) {
  82. uni.setStorageSync('AppToken',res.data.token)
  83. }
  84. resolve(res.data)
  85. },
  86. fail:(res) =>{
  87. //接口调用失败的回调函数
  88. },
  89. complete:(res) =>{
  90. //接口调用结束的回调函数(调用成功、失败都会执行)
  91. if (res.data.code == 401) {
  92. return false
  93. }
  94. uni.hideLoading();
  95. }
  96. })
  97. })
  98. }
  99. }