requestOld.js 2.9 KB

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