index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import http from './interface'
  2. export const $http = (url, method, data, json, isAuth, isBuffer) => {
  3. let authorization = uni.getStorageSync('authorization') || 'Basic cmVucmVuaW86cmVucmVuaW8=';
  4. let access_token = uni.getStorageSync('access_token') || '';
  5. //设置请求前拦截器
  6. http.interceptor.request = (config) => {
  7. uni.showLoading({
  8. title: '加载中...'
  9. })
  10. config.header = {
  11. 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
  12. 'access_token': access_token
  13. }
  14. if (isAuth) config.header.authorization = authorization;
  15. if (isBuffer) config.responseType = 'arrayBuffer';
  16. }
  17. //设置请求结束后拦截器
  18. http.interceptor.response = async (response) => {
  19. //判断返回状态 执行相应操作
  20. uni.hideLoading()
  21. if ((response.data && response.data.code && response.data.code === 401) ||
  22. (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
  23. response.data.msg.indexOf('重新登录') > -1))) {
  24. return uni.showModal({
  25. title: '温馨提示',
  26. content: '当前登录已失效,是否重新登录?',
  27. success: (res) => {
  28. if (res.confirm) {
  29. uni.clearStorageSync();
  30. // #ifdef APP-PLUS
  31. uni.reLaunch({
  32. url: '/pages/login/appIndex'
  33. })
  34. // #endif
  35. // #ifdef MP-WEIXIN
  36. uni.reLaunch({
  37. url: '/pages/login/index'
  38. })
  39. // #endif
  40. }
  41. }
  42. })
  43. }
  44. // 请根据后端规定的状态码判定
  45. if (response.data.code === 300) { //token失效
  46. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  47. } else {
  48. if (response.data.code == 10021 && response.data.msg) {
  49. uni.showToast({
  50. title: response.data.msg,
  51. icon: 'none',
  52. duration: 1500
  53. })
  54. }
  55. }
  56. return response;
  57. }
  58. return http.request({
  59. method: method,
  60. url: url,
  61. dataType: 'json',
  62. data,
  63. })
  64. }
  65. async function login() {
  66. return new Promise(resolve => {
  67. uni.login({
  68. provider: 'weixin',
  69. success(loginRes) {
  70. resolve(loginRes.code)
  71. },
  72. fail() {}
  73. });
  74. })
  75. }
  76. function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
  77. return $http(url, 'POST', data, json, isAuth, isBuffer)
  78. }
  79. function get(url, data, json = true, isAuth = true, isBuffer = false) {
  80. return $http(url, 'GET', data, json, isAuth, isBuffer)
  81. }
  82. function post(url, data, json = true, isAuth = true, isBuffer = false) {
  83. return $http(url, 'POST', data, json, isAuth, isBuffer)
  84. }
  85. function put(url, data, json = true, isAuth = true, isBuffer = false) {
  86. return $http(url, 'PUT', data, json, isAuth, isBuffer)
  87. }
  88. function del(url, data, json = true, isAuth = true, isBuffer = false) {
  89. return $http(url, 'DELETE', data, json, isAuth, isBuffer)
  90. }
  91. export default {
  92. postJson,
  93. get,
  94. post,
  95. put,
  96. del
  97. }