| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // api.js
- const API_BASE_URL = 'https://insts.somnisix.net';
- const API_TOKEN = '268903bf57bdcc2d2433dd31f6d42b9a';
- // 封装 wx.request 为 Promise 形式
- function request(url, method = 'GET', data = {}, headers = {}) {
- return new Promise((resolve, reject) => {
- wx.request({
- url,
- method,
- data,
- header: headers,
- success(res) {
- if (res.statusCode >= 200 && res.statusCode < 300) {
- resolve(res.data);
- } else {
- reject(new Error(`HTTP error! status: ${res.statusCode}`));
- }
- },
- fail(err) {
- reject(new Error('Network error'));
- },
- });
- });
- }
- const apiClient = {
- get: async (endpoint, { params = {}, headers = {} } = {}) => {
- let url = `${API_BASE_URL}/${endpoint}`;
- // 手动拼接 query 参数
- const queryString = Object.keys(params).length > 0
- ? '?' + Object.entries(params)
- .map(([key, value]) =>
- `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
- )
- .join('&')
- : '';
- url += queryString;
- return await request(url, 'GET', {}, headers);
- },
- post: async (endpoint, { body = {}, headers = {} } = {}) => {
- const url = `${API_BASE_URL}/${endpoint}`;
- return await request(url, 'POST', body, headers);
- },
- // 特定接口封装
- bind: async ({ params = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.get('data/api.data/bind', { params, headers });
- },
- isManager: async ({ params = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.get('data/api.data/isManager', { params, headers });
- },
- getRooms: async ({ params = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.get('data/api.data/rooms', { params, headers });
- },
- startCheck: async ({ params = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.get('data/api.data/startCheck', { params, headers });
- },
- nucEquips: async ({ params = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.get('data/api.data/nucEquips', { params, headers });
- },
- inspect: async ({ body = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.post('data/api.data/inspect', { body, headers });
- },
- confirm: async ({ body = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.post('data/api.data/confirm', { body, headers });
- },
- painter: async ({ params = {}, headers = {} } = {}) => {
- if (!headers.token) {
- headers['api-token'] = API_TOKEN;
- }
- return await apiClient.get('data/api.painting/paint', { params, headers });
- },
- };
- export default apiClient;
|