request.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. toLogin();
  40. return Promise.reject({
  41. msg: '未登录'
  42. });
  43. }
  44. }
  45. if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
  46. return new Promise((reslove, reject) => {
  47. uni.request({
  48. url: Url + '/api/' + url,
  49. method: method || 'GET',
  50. header: header,
  51. data: data || {},
  52. success: (res) => {
  53. if (noVerify)
  54. reslove(res.data, res);
  55. else if (res.data.status == 200)
  56. reslove(res.data, res);
  57. else if ([410000, 410001, 410002].indexOf(res.data.status) !== -1) {
  58. toLogin();
  59. reject(res.data);
  60. } else if (res.data.status == 410010) {
  61. uni.showModal({
  62. title: '提示',
  63. content: res.data.msg,
  64. showCancel: false,
  65. confirmText: '我知道了'
  66. });
  67. } else if(res.data.status == 410020){
  68. // reject(res.data.msg);
  69. uni.showModal({
  70. title: '提示',
  71. content: res.data.msg,
  72. showCancel: false,
  73. confirmText: '我知道了'
  74. });
  75. uni.setStorageSync('authIng', true)
  76. } else if(res.data.status == 403){
  77. reject(res.data);
  78. } else
  79. reject(res.data.msg || '系统错误');
  80. },
  81. fail: (msg) => {
  82. let data = {
  83. mag: '请求失败',
  84. status: 1 //1没网
  85. }
  86. // #ifdef APP-PLUS
  87. reject(data);
  88. // #endif
  89. // #ifndef APP-PLUS
  90. reject('请求失败');
  91. // #endif
  92. }
  93. })
  94. });
  95. }
  96. const request = {};
  97. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  98. request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
  99. });
  100. export default request;