request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 == 410020){
  78. // reject(res.data.msg);
  79. uni.showModal({
  80. title: '提示',
  81. content: res.data.msg,
  82. showCancel: false,
  83. confirmText: '我知道了'
  84. });
  85. uni.setStorageSync('authIng', true)
  86. } else if(res.data.status == 403){
  87. reject(res.data);
  88. } else
  89. reject(res.data.msg || '系统错误');
  90. },
  91. fail: (msg) => {
  92. let data = {
  93. mag: '请求失败',
  94. status: 1 //1没网
  95. }
  96. // #ifdef APP-PLUS
  97. reject(data);
  98. // #endif
  99. // #ifndef APP-PLUS
  100. reject('请求失败');
  101. // #endif
  102. }
  103. })
  104. });
  105. }
  106. const request = {};
  107. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  108. request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
  109. });
  110. export default request;