request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import Vue from 'vue'
  2. import uni_request from './uni_request.js'
  3. import {
  4. wecatLogin
  5. } from '../api/index.js';
  6. const request = uni_request({
  7. timeout: 123456,
  8. baseURL: 'https://api.szy.jiuweiyun.cn/api', //线上
  9. // baseURL: 'http://192.168.0.6:8024/api', //本地
  10. // baseURL: 'https://apidemo.szy.jiuweiyun.cn/api', //本地
  11. header: {
  12. 'content-type': 'application/json'
  13. }
  14. })
  15. // 请求前拦截器
  16. request.interceptors.request.use(config => {
  17. uni.showLoading({
  18. title: '加载中',
  19. mask: true
  20. })
  21. config.header.Authorization = 'Bearer ' + uni.getStorageSync("token")
  22. return config
  23. })
  24. function doRequest(response, url) {
  25. return new Promise((resolve, reject) => {
  26. uni.login({
  27. success({
  28. code
  29. }) {
  30. wx.getLocation({
  31. success({
  32. latitude,
  33. longitude
  34. }) {
  35. wecatLogin({
  36. openid: '',
  37. avatar: uni.getStorageSync('userInfo').avatar,
  38. nickname: uni.getStorageSync('userInfo').nickname,
  39. code: code,
  40. longitude,
  41. latitude
  42. })
  43. .then(res => {
  44. if (res.code == 200) {
  45. uni.setStorageSync('token', res.data.token);
  46. request.get(url).then(res => {
  47. if (res.code == 200) {
  48. if (url == '/user/get_info') {
  49. uni.setStorageSync('userInfo', res.data);
  50. }
  51. resolve(res);
  52. }
  53. })
  54. }
  55. if (res.code == 300) {
  56. uni.switchTab({
  57. url: '../index/index?isPhone=' + true
  58. })
  59. }
  60. })
  61. .catch(e => {})
  62. },
  63. fail(e) {
  64. uni.showModal({
  65. content: '不授权将无法获取到您的位置信息',
  66. showCancel: false
  67. });
  68. }
  69. });
  70. }
  71. });
  72. })
  73. }
  74. // 请求后拦截器
  75. request.interceptors.response.use(async (response, ...args) => { // 拦截器
  76. uni.hideLoading()
  77. if (response.data.code === 50000 && uni.getStorageSync('userInfo')) { //token过期
  78. response = await doRequest(response, args[1]);
  79. return response
  80. } else if (response.data.code === 50000 && !uni.getStorageSync('userInfo')) {
  81. uni.removeStorageSync('token')
  82. uni.switchTab({
  83. url: '/pages/index/index'
  84. })
  85. } else if (response.data.code === 200 || response.data.code === 300) {
  86. return response
  87. } else {
  88. // uni.showModal({
  89. // content: response.data.message || '请求失败',
  90. // showCancel: false
  91. // })
  92. return response
  93. }
  94. });
  95. request.onerror = (...args) => { // 请求失败统一处理方法
  96. console.log(args)
  97. uni.hideLoading()
  98. uni.showModal({
  99. title: '请求失败',
  100. content: '网络出错',
  101. // content: JSON.stringify(args),
  102. showCancel: false
  103. })
  104. return Promise.reject(args)
  105. }
  106. export default request