request.js 3.1 KB

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