request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import store from '@/store/index.js'
  2. // uni-app请求封装
  3. export default class Request {
  4. http(router, data = {}, method, contentType, url) {
  5. let that = this;
  6. let path = 'https://api.fhhx.runtzh.com';
  7. // let path = 'http://x5d7cc68.natappfree.cc';//刘明欣
  8. // let path = 'http://192.168.10.122:7014';
  9. // let path = 'http://192.168.10.126:7014';
  10. // let path = 'https://live.test.ylrztop.com/live-api'; // 余红奇
  11. // let path = 'http://192.168.10.166:7014'; // 余红奇
  12. let token = "";
  13. let type = 0
  14. if (url != null) {
  15. type = 1
  16. let projectCode = uni.getStorageSync('projectCode')
  17. path = uni.getStorageSync('addressUrl_' + projectCode)
  18. token = uni.getStorageSync('AppTokenmini_MYCourse')
  19. } else {
  20. type = 0
  21. uni.setStorageSync('requestPath', path)
  22. token = uni.getStorageSync('AppToken');
  23. }
  24. if (router.indexOf("/course_uniapp") != -1) {
  25. type = 2;
  26. router = router.replace('/course_uniapp', '')
  27. let projectCode = uni.getStorageSync('projectCode')
  28. path = uni.getStorageSync('addressUrl_' + projectCode)
  29. token = uni.getStorageSync('TOKEN_WEXIN')
  30. }
  31. // uni.showLoading({
  32. // title: '加载中'
  33. // });
  34. return new Promise((resolve, reject) => {
  35. var httpContentType = 'application/x-www-form-urlencoded';
  36. if (!path) {
  37. uni.showToast({
  38. title: '链接有误',
  39. icon: 'none'
  40. });
  41. return
  42. reject('链接有误')
  43. }
  44. if (contentType != undefined) {
  45. //application/json;charset=UTF-8
  46. httpContentType = contentType;
  47. }
  48. var routers = router;
  49. // 请求
  50. uni.request({
  51. header: {
  52. // 'Content-Type': 'application/x-www-form-urlencoded',
  53. 'Content-Type': httpContentType,
  54. 'AppToken': token
  55. },
  56. url: `${path}${router}`,
  57. data: data,
  58. method: method,
  59. success: (res) => {
  60. //收到开发者服务器成功返回的回调函数
  61. if(type !==0&&(res.data.code == 401 || res.data.code == 4001||res.data.code == 4004)) {
  62. store.commit('setCoureLogin', 2);
  63. resolve({ code: 401, msg: '处理中', data: null });
  64. return
  65. }
  66. if (res.data.code == 401) { //没有权限直接退出到登录界面
  67. let pages = getCurrentPages();
  68. pages.forEach(function(element) {
  69. if (element != undefined && element.route ==
  70. "pages/auth/login") {
  71. resolve(res.data)
  72. return;
  73. }
  74. });
  75. let url = pages[ pages.length - 1]; //当前页页面实例
  76. //如果登录界面已打开,自动关闭
  77. if(url!=undefined&&url.route=="pages/auth/login"){
  78. resolve(res.data)
  79. return;
  80. }
  81. uni.navigateTo({
  82. url: '/pages/auth/login',
  83. success: () => {
  84. uni.hideLoading();
  85. },
  86. fail: () => {
  87. uni.hideLoading();
  88. }
  89. })
  90. return;
  91. }
  92. if (res.data.token && type == 0) {
  93. uni.setStorageSync('AppToken', res.data.token)
  94. }
  95. resolve(res.data)
  96. },
  97. fail: (res) => {
  98. //接口调用失败的回调函数
  99. },
  100. complete: (res) => {
  101. //接口调用结束的回调函数(调用成功、失败都会执行)
  102. if (res.data.code == 401) {
  103. return false
  104. }
  105. uni.hideLoading();
  106. }
  107. })
  108. })
  109. }
  110. }