interceptos.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {
  2. Encrypt,
  3. UrlPath,
  4. Message
  5. } from "./../commonuni"
  6. // 请求拦截
  7. const fetch = (action, opt) => {
  8. let params = opt.params
  9. opt.url = UrlPath.getBaseUrl() + action
  10. opt.method = opt.method || "GET";
  11. var header = {}
  12. let token = uni.getStorageSync("token")
  13. if (token != null && token != "") {
  14. header = {
  15. Authorization: 'Bearer ' + token
  16. }
  17. }
  18. if(opt.isFile){
  19. header['Content-Type'] = 'application/json; application/octet-stream'
  20. opt.responseType = 'arraybuffer'
  21. delete opt.isFile
  22. }
  23. if (!opt.url.indexOf("app-api/wxLogin") == 0 || !url.indexOf("app-api/common") == 0) {
  24. opt.attribute = {
  25. "login_user_type": 2
  26. }
  27. }
  28. opt.header = {
  29. ...opt.header,
  30. ...header,
  31. }
  32. // if (opt.method == "POST") {
  33. // if (opt.data) {
  34. // opt.data = {
  35. // data: Encrypt.AESEncode(JSON.stringify(opt.data))
  36. // }
  37. // }
  38. // }
  39. return new Promise((resolve, reject) => {
  40. let options = {}
  41. Object.keys(opt).map(key => {
  42. if (key !== "params") {
  43. return options[key] = opt[key]
  44. }
  45. })
  46. uni.request(options)
  47. .then(res => interceptorsRes(res, resolve, reject))
  48. .catch(err => interceptorsErr(err, reject))
  49. })
  50. }
  51. // 请求拦截
  52. const fetchfile = (action, opt) => {
  53. let params = opt.params
  54. opt.url = UrlPath.getBaseUrl() + action
  55. opt.method = "POST";
  56. var header = {}
  57. let token = uni.getStorageSync("token")
  58. if (token != null && token != "") {
  59. header = {
  60. Authorization: 'Bearer ' + token
  61. }
  62. }
  63. opt.header = {
  64. ...opt.header,
  65. ...header,
  66. }
  67. opt.responseType = 'application/json'
  68. return new Promise((resolve, reject) => {
  69. uni.uploadFile(opt)
  70. .then(res => interceptorsRes(res, resolve, reject))
  71. .catch(err => interceptorsErr(err, reject))
  72. })
  73. }
  74. // 响应拦截
  75. const interceptorsRes = (resp, resolve, reject) => {
  76. const {
  77. code
  78. } = resp.data
  79. if (resp.statusCode == 401 || code == 401) {
  80. Message.info("登录已过期,请重新登录!")
  81. uni.navigateTo({
  82. url: '../../pages/index/index'
  83. });
  84. resolve(null);
  85. return;
  86. }
  87. if (resp.statusCode == '201'){
  88. resolve(resp.data);
  89. return;
  90. }
  91. if (typeof(resp.data) == 'string'){
  92. resp.data = JSON.parse(resp.data)
  93. }
  94. if (resp.data.code != 0){
  95. Message.info(resp.data.msg);
  96. reject(null)
  97. return;
  98. }
  99. if (typeof(resp.data) == "string") {
  100. // Encrypt.AESDecode()
  101. resp.data = JSON.parse(JSON.parse(resp.data), null, null);
  102. } else {
  103. // Encrypt.AESDecode()
  104. resp.data = resp.data.data;
  105. }
  106. resolve(resp.data)
  107. }
  108. // 异常处理
  109. const interceptorsErr = (err, reject) => {
  110. Message.error("网络异常", err)
  111. reject(err)
  112. }
  113. export {
  114. fetch,
  115. fetchfile
  116. }