api.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // api.js
  2. const API_BASE_URL = 'https://insts.somnisix.net';
  3. const API_TOKEN = '268903bf57bdcc2d2433dd31f6d42b9a';
  4. // 封装 wx.request 为 Promise 形式
  5. function request(url, method = 'GET', data = {}, headers = {}) {
  6. return new Promise((resolve, reject) => {
  7. wx.request({
  8. url,
  9. method,
  10. data,
  11. header: headers,
  12. success(res) {
  13. if (res.statusCode >= 200 && res.statusCode < 300) {
  14. resolve(res.data);
  15. } else {
  16. reject(new Error(`HTTP error! status: ${res.statusCode}`));
  17. }
  18. },
  19. fail(err) {
  20. reject(new Error('Network error'));
  21. },
  22. });
  23. });
  24. }
  25. const apiClient = {
  26. get: async (endpoint, { params = {}, headers = {} } = {}) => {
  27. let url = `${API_BASE_URL}/${endpoint}`;
  28. // 手动拼接 query 参数
  29. const queryString = Object.keys(params).length > 0
  30. ? '?' + Object.entries(params)
  31. .map(([key, value]) =>
  32. `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
  33. )
  34. .join('&')
  35. : '';
  36. url += queryString;
  37. return await request(url, 'GET', {}, headers);
  38. },
  39. post: async (endpoint, { body = {}, headers = {} } = {}) => {
  40. const url = `${API_BASE_URL}/${endpoint}`;
  41. return await request(url, 'POST', body, headers);
  42. },
  43. // 特定接口封装
  44. bind: async ({ params = {}, headers = {} } = {}) => {
  45. if (!headers.token) {
  46. headers['api-token'] = API_TOKEN;
  47. }
  48. return await apiClient.get('data/api.data/bind', { params, headers });
  49. },
  50. isManager: async ({ params = {}, headers = {} } = {}) => {
  51. if (!headers.token) {
  52. headers['api-token'] = API_TOKEN;
  53. }
  54. return await apiClient.get('data/api.data/isManager', { params, headers });
  55. },
  56. getRooms: async ({ params = {}, headers = {} } = {}) => {
  57. if (!headers.token) {
  58. headers['api-token'] = API_TOKEN;
  59. }
  60. return await apiClient.get('data/api.data/rooms', { params, headers });
  61. },
  62. startCheck: async ({ params = {}, headers = {} } = {}) => {
  63. if (!headers.token) {
  64. headers['api-token'] = API_TOKEN;
  65. }
  66. return await apiClient.get('data/api.data/startCheck', { params, headers });
  67. },
  68. nucEquips: async ({ params = {}, headers = {} } = {}) => {
  69. if (!headers.token) {
  70. headers['api-token'] = API_TOKEN;
  71. }
  72. return await apiClient.get('data/api.data/nucEquips', { params, headers });
  73. },
  74. inspect: async ({ body = {}, headers = {} } = {}) => {
  75. if (!headers.token) {
  76. headers['api-token'] = API_TOKEN;
  77. }
  78. return await apiClient.post('data/api.data/inspect', { body, headers });
  79. },
  80. confirm: async ({ body = {}, headers = {} } = {}) => {
  81. if (!headers.token) {
  82. headers['api-token'] = API_TOKEN;
  83. }
  84. return await apiClient.post('data/api.data/confirm', { body, headers });
  85. },
  86. painter: async ({ params = {}, headers = {} } = {}) => {
  87. if (!headers.token) {
  88. headers['api-token'] = API_TOKEN;
  89. }
  90. return await apiClient.get('data/api.painting/paint', { params, headers });
  91. },
  92. };
  93. export default apiClient;