You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB

3 months ago
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2025 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. HEADERPARAMS
  15. } from '@/config/app';
  16. import {
  17. toLogin,
  18. checkLogin
  19. } from '../libs/login';
  20. import store from '../store';
  21. /**
  22. * 发送请求
  23. */
  24. function baseRequest(url, method, data, {
  25. noAuth = false,
  26. noVerify = false
  27. }, params,prefix) {
  28. let Url = HTTP_REQUEST_URL,header = HEADER
  29. if (params != undefined) {
  30. header = HEADERPARAMS;
  31. }
  32. if (!noAuth) {
  33. //登录过期自动登录
  34. if (!store.state.app.token && !checkLogin()) {
  35. toLogin();
  36. return Promise.reject({
  37. msg: '未登录'
  38. });
  39. }
  40. }
  41. if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
  42. return new Promise((reslove, reject) => {
  43. uni.request({
  44. url: Url + `${prefix?'/api/public/':'/api/front/'}` + url,
  45. method: method || 'GET',
  46. header: header,
  47. data: data || {},
  48. success: (res) => {
  49. if (noVerify)
  50. reslove(res.data, res);
  51. else if (res.data.code == 200)
  52. reslove(res.data, res);
  53. else if ([410000, 410001, 410002, 401,402].indexOf(res.data.code) !== -1) {
  54. toLogin();
  55. reject(res.data);
  56. }else if (res.data.code == 500){
  57. reject(res.data.message || '系统异常');
  58. }else if (res.data.code == 400){
  59. reject(res.data.message || '参数校验失败');
  60. }else if (res.data.code == 404){
  61. reject(res.data.message || '没有找到相关数据');
  62. }else if (res.data.code == 403){
  63. reject(res.data.message || '没有相关权限');
  64. } else
  65. reject(res.data.message || '系统错误');
  66. },
  67. fail: (msg) => {
  68. reject('请求失败');
  69. }
  70. })
  71. });
  72. }
  73. const request = {};
  74. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  75. request[method] = (api, data, opt, params,prefix) => baseRequest(api, method, data, opt || {}, params,prefix)
  76. });
  77. export default request;