api.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // API工具类
  2. // 开发环境:使用 localhost
  3. // 生产环境:需要配置为 HTTPS 域名
  4. const BASE_URL = 'http://localhost:8080/api';
  5. // 开发环境提示
  6. if (BASE_URL.includes('localhost')) {
  7. console.warn('当前使用 localhost,请在微信开发者工具中关闭域名校验:详情 -> 本地设置 -> 不校验合法域名');
  8. }
  9. // 请求封装
  10. function request(url, method = 'GET', data = {}, needAuth = true) {
  11. return new Promise((resolve, reject) => {
  12. // 获取token
  13. const token = wx.getStorageSync('token') || '';
  14. // 构建请求头
  15. const header = {
  16. 'Content-Type': 'application/json'
  17. };
  18. if (needAuth && token) {
  19. header['Authorization'] = `Bearer ${token}`;
  20. }
  21. wx.request({
  22. url: BASE_URL + url,
  23. method: method,
  24. data: data,
  25. header: header,
  26. success: (res) => {
  27. console.log('API请求成功:', {
  28. url: BASE_URL + url,
  29. statusCode: res.statusCode,
  30. data: res.data
  31. });
  32. if (res.statusCode === 200) {
  33. if (res.data.code === 200) {
  34. resolve(res.data.data);
  35. } else {
  36. console.error('API返回错误:', {
  37. code: res.data.code,
  38. message: res.data.message || res.data.msg
  39. });
  40. // token过期或未授权
  41. if (res.data.code === 401 || res.statusCode === 401) {
  42. wx.removeStorageSync('token');
  43. wx.removeStorageSync('userInfo');
  44. wx.reLaunch({
  45. url: '/pages/login/login'
  46. });
  47. }
  48. // 403禁止访问(如管理员尝试登录小程序)
  49. if (res.data.code === 403 || res.statusCode === 403) {
  50. // 不跳转,只显示错误信息
  51. }
  52. reject(res.data.message || res.data.msg || '请求失败');
  53. }
  54. } else if (res.statusCode === 400) {
  55. // 400错误通常是参数问题
  56. console.error('请求参数错误:', res.data);
  57. reject(res.data.message || res.data.msg || '请求参数错误');
  58. } else if (res.statusCode === 403) {
  59. // 403禁止访问
  60. console.error('访问被禁止:', res.data);
  61. reject(res.data.message || res.data.msg || '访问被禁止');
  62. } else {
  63. console.error('HTTP错误:', res.statusCode, res.data);
  64. reject('网络错误: ' + res.statusCode + (res.data ? (' - ' + (res.data.message || JSON.stringify(res.data))) : ''));
  65. }
  66. },
  67. fail: (err) => {
  68. console.error('API请求失败:', {
  69. url: BASE_URL + url,
  70. error: err
  71. });
  72. reject(err.errMsg || '网络请求失败');
  73. }
  74. });
  75. });
  76. }
  77. // GET请求
  78. function get(url, data = {}, needAuth = true) {
  79. return request(url, 'GET', data, needAuth);
  80. }
  81. // POST请求
  82. function post(url, data = {}, needAuth = true, params = {}) {
  83. // 如果有查询参数,拼接到URL
  84. if (Object.keys(params).length > 0) {
  85. const queryString = Object.keys(params)
  86. .map(key => `${key}=${encodeURIComponent(params[key])}`)
  87. .join('&');
  88. url = url + (url.includes('?') ? '&' : '?') + queryString;
  89. }
  90. return request(url, 'POST', data, needAuth);
  91. }
  92. // PUT请求
  93. function put(url, data = {}, needAuth = true) {
  94. return request(url, 'PUT', data, needAuth);
  95. }
  96. // DELETE请求
  97. function del(url, data = {}, needAuth = true) {
  98. // DELETE请求的参数通常放在URL中
  99. if (Object.keys(data).length > 0) {
  100. const queryString = Object.keys(data)
  101. .map(key => `${key}=${encodeURIComponent(data[key])}`)
  102. .join('&');
  103. url = url + (url.includes('?') ? '&' : '?') + queryString;
  104. }
  105. return request(url, 'DELETE', {}, needAuth);
  106. }
  107. module.exports = {
  108. request,
  109. get,
  110. post,
  111. put,
  112. del,
  113. BASE_URL
  114. };