request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2024 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 { checkLogin } from '../libs/login';
  16. import store from '../store';
  17. import pako from '../plugin/pako/pako.es5.min.js'
  18. function toLogin(){
  19. store.commit("LOGOUT");
  20. uni.showToast({
  21. title: '请登录',
  22. icon: 'none',
  23. duration: 1000
  24. });
  25. }
  26. function decompress(str) {
  27. return pako.inflateRaw(base64ToUint8Array(str), {
  28. to: 'string'
  29. });
  30. }
  31. function base64ToUint8Array(base64String) {
  32. let padding = '='.repeat((4 - base64String.length % 4) % 4);
  33. let base64 = (base64String + padding)
  34. .replace(/\-/g, '+')
  35. .replace(/_/g, '/');
  36. let rawData = atob(base64);
  37. let outputArray = new Uint8Array(rawData.length);
  38. for (var i = 0; i < rawData.length; ++i) {
  39. outputArray[i] = rawData.charCodeAt(i);
  40. }
  41. return outputArray;
  42. }
  43. function atob(input) {
  44. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  45. let str = input.replace(/=+$/, '');
  46. let output = '';
  47. if (str.length % 4 === 1) {
  48. throw new Error('InvalidLengthError');
  49. }
  50. for (let i = 0, len = str.length; i < len; i += 4) {
  51. const a = chars.indexOf(str.charAt(i));
  52. const b = chars.indexOf(str.charAt(i + 1));
  53. const c = chars.indexOf(str.charAt(i + 2));
  54. const d = chars.indexOf(str.charAt(i + 3));
  55. const sum = (a << 18) | (b << 12) | (c << 6) | d;
  56. output += String.fromCharCode((sum >> 16) & 0xFF, (sum >> 8) & 0xFF, sum & 0xFF);
  57. }
  58. return output;
  59. }
  60. /**
  61. * 发送请求
  62. */
  63. function baseRequest(url, method, data, {
  64. noAuth = false,
  65. noVerify = false
  66. }) {
  67. let Url = HTTP_REQUEST_URL,
  68. header = HEADER;
  69. if (!noAuth) {
  70. //登录过期自动登录
  71. if (!store.state.app.token && !checkLogin()) {
  72. toLogin();
  73. return Promise.reject({
  74. msg: '未登录'
  75. });
  76. }
  77. }
  78. if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
  79. if(store.state.app.uuid)header['uuid'] = store.state.app.uuid
  80. return new Promise((reslove, reject) => {
  81. uni.request({
  82. url: Url + '/api/' + url,
  83. method: method || 'GET',
  84. header: header,
  85. data: data || {},
  86. success: (res) => {
  87. if (res.data && res.data.encode) {
  88. try
  89. {
  90. res.data = JSON.parse(decompress(res.data.data));
  91. }catch(e){
  92. res.data = decompress(decodeURI(res.data.data));
  93. }
  94. }
  95. if (noVerify)
  96. reslove(res.data, res);
  97. else if (res.data.status == 200)
  98. reslove(res.data, res);
  99. else if ([410000, 410001, 410002, 40000].indexOf(res.data.status) !== -1) {
  100. toLogin();
  101. reject(res.data);
  102. } else if (res.data.status == 501) {
  103. uni.reLaunch({
  104. url: '/pages/error/index'
  105. })
  106. reject(res.data);
  107. } else
  108. reject(res.data.message || '系统错误');
  109. },
  110. fail: (message) => {
  111. reject('请求失败');
  112. }
  113. })
  114. });
  115. }
  116. const request = {};
  117. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  118. request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
  119. });
  120. export default request;