request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. console.log('请求接口',Url + '/api/' + url);
  53. uni.request({
  54. url: Url + '/api/' + url,
  55. method: method || 'GET',
  56. header: header,
  57. data: data || {},
  58. success: (res) => {
  59. if (noVerify)
  60. reslove(res.data, res);
  61. else if (res.data.status == 200)
  62. reslove(res.data, res);
  63. else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
  64. // #ifndef MP
  65. toLogin();
  66. // #endif
  67. // #ifdef MP
  68. toLoginMp();
  69. // #endif
  70. reject(res.data);
  71. } else if (res.data.status == 410010) {
  72. uni.showModal({
  73. title: '提示',
  74. content: res.data.msg,
  75. showCancel: false,
  76. confirmText: '我知道了'
  77. });
  78. } else if(res.data.status == 410020){
  79. // reject(res.data.msg);
  80. uni.showModal({
  81. title: '提示',
  82. content: res.data.msg,
  83. showCancel: false,
  84. confirmText: '我知道了'
  85. });
  86. uni.setStorageSync('authIng', true)
  87. } else if(res.data.status == 403){
  88. reject(res.data);
  89. } else
  90. reject(res.data.msg || '系统错误');
  91. },
  92. fail: (msg) => {
  93. let data = {
  94. mag: '请求失败',
  95. status: 1 //1没网
  96. }
  97. // #ifdef APP-PLUS
  98. reject(data);
  99. // #endif
  100. // #ifndef APP-PLUS
  101. reject('请求失败');
  102. // #endif
  103. }
  104. })
  105. });
  106. }
  107. const request = {};
  108. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  109. request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
  110. });
  111. export default request;