request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 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. import i18n from './lang.js';
  21. /**
  22. * 发送请求
  23. */
  24. function baseRequest(url, method, data, {
  25. noAuth = false,
  26. noVerify = false
  27. }) {
  28. let Url = HTTP_REQUEST_URL,
  29. header = HEADER;
  30. if (!noAuth) {
  31. //登录过期自动登录
  32. if (!store.state.app.token && !checkLogin()) {
  33. toLogin();
  34. return Promise.reject({
  35. msg: i18n.t(`未登录`)
  36. });
  37. }
  38. }
  39. if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
  40. return new Promise((reslove, reject) => {
  41. if (uni.getStorageSync('locale')) {
  42. header['Cb-lang'] = uni.getStorageSync('locale')
  43. }
  44. uni.request({
  45. url: Url + '/api/' + url,
  46. method: method || 'GET',
  47. header: header,
  48. data: data || {},
  49. success: (res) => {
  50. if (noVerify)
  51. reslove(res.data, res);
  52. else if (res.data.status == 200)
  53. reslove(res.data, res);
  54. else if ([110002, 110003, 110004].indexOf(res.data.status) !== -1) {
  55. toLogin();
  56. reject(res.data);
  57. } else if (res.data.status == 100103) {
  58. uni.showModal({
  59. title: i18n.t(`提示`),
  60. content: res.data.msg,
  61. showCancel: false,
  62. confirmText: i18n.t(`我知道了`)
  63. });
  64. }else if(res.data.code == "980001"){
  65. uni.showModal({
  66. title: i18n.t(`提示`),
  67. content: res.data.msg,
  68. confirmText: i18n.t(`立即认证`),
  69. success(re) {
  70. if(re.confirm){
  71. uni.navigateTo({
  72. url: '/pages/users/real_name/index'
  73. });
  74. }
  75. }
  76. });
  77. } else{
  78. // console.log(res,'错误数据');
  79. reject(res.data.msg || i18n.t(`系统错误`));
  80. }
  81. },
  82. fail: (msg) => {
  83. console.log(msg,'请求错误');
  84. let data = {
  85. mag: i18n.t(`请求失败`),
  86. status: 1 //1没网
  87. }
  88. // #ifdef APP-PLUS
  89. reject(data);
  90. // #endif
  91. // #ifndef APP-PLUS
  92. reject(i18n.t(`请求失败`));
  93. // #endif
  94. }
  95. })
  96. });
  97. }
  98. const request = {};
  99. ['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  100. request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
  101. });
  102. export default request;