request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import {
  11. HTTP_REQUEST_URL,
  12. HEADER,
  13. TOKENNAME
  14. } from '@/config/app';
  15. import {
  16. toLogin,
  17. checkLogin
  18. } from '../libs/login';
  19. import store from '../store';
  20. function toLoginMp(){
  21. uni.showToast({
  22. title: '请登录',
  23. icon: 'none',
  24. duration: 1000
  25. });
  26. }
  27. /**
  28. * 发送请求
  29. */
  30. function baseRequest(url, method, data, {
  31. noAuth = false,
  32. noVerify = false
  33. }) {
  34. let Url = HTTP_REQUEST_URL,
  35. header = HEADER;
  36. if (!noAuth) {
  37. //登录过期自动登录
  38. if (!store.state.app.token && !checkLogin()) {
  39. // #ifndef MP
  40. toLogin();
  41. // #endif
  42. // #ifdef MP
  43. toLoginMp();
  44. // #endif
  45. return Promise.reject({
  46. msg: '未登录'
  47. });
  48. }
  49. }
  50. if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
  51. return new Promise((reslove, reject) => {
  52. uni.request({
  53. url: Url + '/api/' + url,
  54. method: method || 'GET',
  55. header: header,
  56. data: data || {},
  57. success: (res) => {
  58. if (noVerify)
  59. reslove(res.data, res);
  60. else if (res.data.status == 200)
  61. reslove(res.data, res);
  62. else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
  63. // #ifndef MP
  64. toLogin();
  65. // #endif
  66. // #ifdef MP
  67. toLoginMp();
  68. // #endif
  69. reject(res.data);
  70. } else if (res.data.status == 410010) {
  71. uni.showModal({
  72. title: '提示',
  73. content: res.data.msg,
  74. showCancel: false,
  75. confirmText: '我知道了'
  76. });
  77. } else if(res.data.status == 403){
  78. reject(res.data);
  79. } else
  80. reject(res.data.msg || '系统错误');
  81. },
  82. fail: (msg) => {
  83. let data = {
  84. mag: '请求失败',
  85. status: 1 //1没网
  86. }
  87. // #ifdef APP-PLUS
  88. reject(data);
  89. // #endif
  90. // #ifndef APP-PLUS
  91. reject('请求失败');
  92. // #endif
  93. }
  94. })
  95. });
  96. }
  97. const request = {};
  98. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  99. request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
  100. });
  101. export default request;