request.js 2.8 KB

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