request.js 969 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const BASE_URL = 'http://127.0.0.1:28080/api/';
  2. import { msg } from './util'
  3. export const request = (options) => {
  4. return new Promise((resolve, reject) => {
  5. // 发起请求
  6. uni.request({
  7. url: BASE_URL + options.url,
  8. method: options.method || 'GET',
  9. data: options.data || {},
  10. header: {
  11. 'Authorization': '' // TODO 芋艿:带 token
  12. }
  13. }).then(res => {
  14. debugger
  15. res = res[1];
  16. const statusCode = res.statusCode;
  17. if (statusCode !== 200) {
  18. msg('请求失败,请重试');
  19. return;
  20. }
  21. const code = res.data.code;
  22. const message = res.data.msg;
  23. // Token 过期,引导重新登陆
  24. if (code === 401) {
  25. msg('登录信息已过期,请重新登录');
  26. store.commit('logout');
  27. reject('无效的登录信息');
  28. return;
  29. }
  30. // 其它失败情况
  31. if (code > 0) {
  32. msg(message);
  33. reject(message);
  34. return;
  35. }
  36. resolve(res.data.data);
  37. }).catch((err) => {
  38. reject(err);
  39. })
  40. })
  41. }