py-test-components 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +214 -0
- package/dist/313f7dacf2076822059d.woff +0 -0
- package/dist/4520188144a17fb24a6a.ttf +0 -0
- package/dist/py-component.esm.js +2 -0
- package/dist/py-component.esm.js.LICENSE.txt +35 -0
- package/dist/py-component.js +2 -0
- package/dist/py-component.js.LICENSE.txt +35 -0
- package/package.json +42 -0
- package/src/components/PyTable.vue +280 -0
- package/src/components/PyWeather.vue +387 -0
- package/src/index.js +51 -0
- package/src/react/index.js +156 -0
- package/src/store/index.js +66 -0
- package/src/utils/api.js +101 -0
- package/src/utils/request.js +113 -0
- package/src/vue/index.js +32 -0
package/src/utils/api.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API 请求封装
|
|
3
|
+
*/
|
|
4
|
+
import { get, post } from './request';
|
|
5
|
+
import store from '../store';
|
|
6
|
+
|
|
7
|
+
// 获取基础 URL
|
|
8
|
+
function getBaseUrl() {
|
|
9
|
+
return store.get('baseUrl') || '';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 获取 API Key
|
|
13
|
+
function getApiKey() {
|
|
14
|
+
return store.get('apiKey') || '';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 构建带认证的请求头
|
|
18
|
+
function getAuthHeaders() {
|
|
19
|
+
const apiKey = getApiKey();
|
|
20
|
+
return apiKey ? { 'X-API-Key': apiKey } : {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 天气相关 API
|
|
25
|
+
*/
|
|
26
|
+
export const weatherApi = {
|
|
27
|
+
/**
|
|
28
|
+
* 获取天气信息
|
|
29
|
+
* @param {string} city - 城市名
|
|
30
|
+
* @returns {Promise} 天气数据
|
|
31
|
+
*/
|
|
32
|
+
getWeather(city) {
|
|
33
|
+
return get('/api/weather', { city }, {
|
|
34
|
+
baseURL: getBaseUrl(),
|
|
35
|
+
headers: getAuthHeaders()
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 获取forecast
|
|
41
|
+
* @param {string} city - 城市名
|
|
42
|
+
* @returns {Promise} forecast数据
|
|
43
|
+
*/
|
|
44
|
+
getForecast(city) {
|
|
45
|
+
return get('/api/weather/forecast', { city }, {
|
|
46
|
+
baseURL: getBaseUrl(),
|
|
47
|
+
headers: getAuthHeaders()
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 数据表格相关 API
|
|
54
|
+
*/
|
|
55
|
+
export const tableApi = {
|
|
56
|
+
/**
|
|
57
|
+
* 获取表格数据
|
|
58
|
+
* @param {object} params - 查询参数
|
|
59
|
+
* @returns {Promise} 表格数据
|
|
60
|
+
*/
|
|
61
|
+
getData(params = {}) {
|
|
62
|
+
return get('/api/table/data', params, {
|
|
63
|
+
baseURL: getBaseUrl(),
|
|
64
|
+
headers: getAuthHeaders()
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 提交表格数据
|
|
70
|
+
* @param {object} data - 提交的数据
|
|
71
|
+
* @returns {Promise} 提交结果
|
|
72
|
+
*/
|
|
73
|
+
submitData(data) {
|
|
74
|
+
return post('/api/table/data', data, {
|
|
75
|
+
baseURL: getBaseUrl(),
|
|
76
|
+
headers: getAuthHeaders()
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 用户相关 API
|
|
83
|
+
*/
|
|
84
|
+
export const userApi = {
|
|
85
|
+
/**
|
|
86
|
+
* 获取用户信息
|
|
87
|
+
* @returns {Promise} 用户数据
|
|
88
|
+
*/
|
|
89
|
+
getUserInfo() {
|
|
90
|
+
return get('/api/user/info', {}, {
|
|
91
|
+
baseURL: getBaseUrl(),
|
|
92
|
+
headers: getAuthHeaders()
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export default {
|
|
98
|
+
weather: weatherApi,
|
|
99
|
+
table: tableApi,
|
|
100
|
+
user: userApi
|
|
101
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 封装的 fetch 请求工具
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// 默认配置
|
|
6
|
+
const defaultConfig = {
|
|
7
|
+
baseURL: '',
|
|
8
|
+
timeout: 10000,
|
|
9
|
+
headers: {
|
|
10
|
+
'Content-Type': 'application/json'
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 发起 HTTP 请求
|
|
16
|
+
* @param {string} url - 请求地址
|
|
17
|
+
* @param {object} options - 请求选项
|
|
18
|
+
* @returns {Promise} 响应结果
|
|
19
|
+
*/
|
|
20
|
+
export function request(url, options = {}) {
|
|
21
|
+
const config = {
|
|
22
|
+
...defaultConfig,
|
|
23
|
+
...options,
|
|
24
|
+
headers: {
|
|
25
|
+
...defaultConfig.headers,
|
|
26
|
+
...options.headers
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// 处理 URL
|
|
31
|
+
const fullUrl = config.baseURL ? `${config.baseURL}${url}` : url;
|
|
32
|
+
|
|
33
|
+
// 创建 AbortController 用于超时控制
|
|
34
|
+
const controller = new AbortController();
|
|
35
|
+
const timeoutId = setTimeout(() => controller.abort(), config.timeout);
|
|
36
|
+
|
|
37
|
+
return fetch(fullUrl, {
|
|
38
|
+
...config,
|
|
39
|
+
signal: controller.signal
|
|
40
|
+
})
|
|
41
|
+
.then(response => {
|
|
42
|
+
clearTimeout(timeoutId);
|
|
43
|
+
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 根据返回类型解析数据
|
|
49
|
+
const contentType = response.headers.get('content-type');
|
|
50
|
+
if (contentType && contentType.includes('application/json')) {
|
|
51
|
+
return response.json();
|
|
52
|
+
}
|
|
53
|
+
return response.text();
|
|
54
|
+
})
|
|
55
|
+
.catch(error => {
|
|
56
|
+
clearTimeout(timeoutId);
|
|
57
|
+
|
|
58
|
+
if (error.name === 'AbortError') {
|
|
59
|
+
throw new Error('Request timeout');
|
|
60
|
+
}
|
|
61
|
+
throw error;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* GET 请求
|
|
67
|
+
* @param {string} url - 请求地址
|
|
68
|
+
* @param {object} params - URL 参数
|
|
69
|
+
* @param {object} options - 其他选项
|
|
70
|
+
* @returns {Promise} 响应结果
|
|
71
|
+
*/
|
|
72
|
+
export function get(url, params = {}, options = {}) {
|
|
73
|
+
const queryString = Object.keys(params)
|
|
74
|
+
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
|
75
|
+
.join('&');
|
|
76
|
+
|
|
77
|
+
const fullUrl = queryString ? `${url}?${queryString}` : url;
|
|
78
|
+
|
|
79
|
+
return request(fullUrl, {
|
|
80
|
+
method: 'GET',
|
|
81
|
+
...options
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* POST 请求
|
|
87
|
+
* @param {string} url - 请求地址
|
|
88
|
+
* @param {object} data - 请求数据
|
|
89
|
+
* @param {object} options - 其他选项
|
|
90
|
+
* @returns {Promise} 响应结果
|
|
91
|
+
*/
|
|
92
|
+
export function post(url, data = {}, options = {}) {
|
|
93
|
+
return request(url, {
|
|
94
|
+
method: 'POST',
|
|
95
|
+
body: JSON.stringify(data),
|
|
96
|
+
...options
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 设置默认配置
|
|
102
|
+
* @param {object} config - 配置对象
|
|
103
|
+
*/
|
|
104
|
+
export function setDefaultConfig(config) {
|
|
105
|
+
Object.assign(defaultConfig, config);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default {
|
|
109
|
+
request,
|
|
110
|
+
get,
|
|
111
|
+
post,
|
|
112
|
+
setDefaultConfig
|
|
113
|
+
};
|
package/src/vue/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Vue 入口 - 直接导出 Vue 组件
|
|
2
|
+
|
|
3
|
+
import PyTable from '../components/PyTable.vue';
|
|
4
|
+
import PyWeather from '../components/PyWeather.vue';
|
|
5
|
+
import store from '../store';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 初始化 Store
|
|
9
|
+
* @param {object} config - 配置对象
|
|
10
|
+
*/
|
|
11
|
+
function initStore(config) {
|
|
12
|
+
if (!config || typeof config !== 'object') {
|
|
13
|
+
console.warn('[PyComponent] initStore 需要传入配置对象');
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
Object.keys(config).forEach(key => {
|
|
18
|
+
store.set(key, config[key]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
console.log('[PyComponent] Store 已初始化');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 导出组件和函数
|
|
25
|
+
export { PyTable, PyWeather, initStore };
|
|
26
|
+
|
|
27
|
+
// 默认导出
|
|
28
|
+
export default {
|
|
29
|
+
PyTable,
|
|
30
|
+
PyWeather,
|
|
31
|
+
initStore
|
|
32
|
+
};
|