@syfei49/mini-dynamic-renderer 1.0.0 → 1.0.2
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 +84 -102
- package/components/ly-ai-assistant/ly-ai-assistant.vue +523 -0
- package/index.js +16 -14
- package/package.json +27 -18
- package/src/adapters/request.js +106 -0
- package/src/chat/parser.js +55 -0
- package/src/chat/service.js +296 -0
- package/src/config.js +51 -0
- package/src/event.js +43 -0
- package/src/handler.js +43 -49
- package/src/init.js +38 -57
- package/src/interceptor.js +105 -104
package/src/handler.js
CHANGED
|
@@ -1,49 +1,43 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 从响应数据中解析渲染配置(预留,供后续版本使用)
|
|
3
|
-
* @param {*} data - 接口返回的 data 字段
|
|
4
|
-
* @returns {Object|null}
|
|
5
|
-
*/
|
|
6
|
-
function parseRenderConfig(data) {
|
|
7
|
-
if (!data || typeof data !== 'object') {
|
|
8
|
-
return null;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (data.renderConfig && typeof data.renderConfig === 'object') {
|
|
12
|
-
return data.renderConfig;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (data.type) {
|
|
16
|
-
return data;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @param {Object} res - wx.request success 回调的响应对象
|
|
25
|
-
* @param {Object} requestOptions - 原始 wx.request 入参
|
|
26
|
-
* @param {Object} config - SDK 配置
|
|
27
|
-
*/
|
|
28
|
-
function handleResponse(res, requestOptions, config) {
|
|
29
|
-
var renderConfig = parseRenderConfig(res.data);
|
|
30
|
-
|
|
31
|
-
if (config.debug) {
|
|
32
|
-
console.log('[mini-dynamic-renderer] intercepted:', requestOptions.url, res.data);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
module.exports = {
|
|
47
|
-
parseRenderConfig: parseRenderConfig,
|
|
48
|
-
handleResponse: handleResponse
|
|
49
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 从响应数据中解析渲染配置(预留,供后续版本使用)
|
|
3
|
+
* @param {*} data - 接口返回的 data 字段
|
|
4
|
+
* @returns {Object|null}
|
|
5
|
+
*/
|
|
6
|
+
function parseRenderConfig(data) {
|
|
7
|
+
if (!data || typeof data !== 'object') {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (data.renderConfig && typeof data.renderConfig === 'object') {
|
|
12
|
+
return data.renderConfig;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (data.type) {
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 拦截响应处理(预留,默认不再弹窗)
|
|
24
|
+
* @param {Object} res - wx.request success 回调的响应对象
|
|
25
|
+
* @param {Object} requestOptions - 原始 wx.request 入参
|
|
26
|
+
* @param {Object} config - SDK 配置
|
|
27
|
+
*/
|
|
28
|
+
function handleResponse(res, requestOptions, config) {
|
|
29
|
+
var renderConfig = parseRenderConfig(res.data);
|
|
30
|
+
|
|
31
|
+
if (config.debug) {
|
|
32
|
+
console.log('[mini-dynamic-renderer] intercepted:', requestOptions.url, res.data);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (renderConfig && config.debug) {
|
|
36
|
+
console.log('[mini-dynamic-renderer] renderConfig:', renderConfig);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
parseRenderConfig: parseRenderConfig,
|
|
42
|
+
handleResponse: handleResponse
|
|
43
|
+
};
|
package/src/init.js
CHANGED
|
@@ -1,57 +1,38 @@
|
|
|
1
|
-
var interceptor = require('./interceptor');
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
interceptor.installInterceptor(currentConfig);
|
|
40
|
-
initialized = true;
|
|
41
|
-
|
|
42
|
-
if (currentConfig.debug) {
|
|
43
|
-
console.log('[mini-dynamic-renderer] initialized', currentConfig);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return currentConfig;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
module.exports = {
|
|
50
|
-
init: init,
|
|
51
|
-
getConfig: function () {
|
|
52
|
-
return currentConfig;
|
|
53
|
-
},
|
|
54
|
-
isInitialized: function () {
|
|
55
|
-
return initialized;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
1
|
+
var interceptor = require('./interceptor');
|
|
2
|
+
var configModule = require('./config');
|
|
3
|
+
var chatService = require('./chat/service');
|
|
4
|
+
|
|
5
|
+
var initialized = false;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 初始化 SDK
|
|
9
|
+
* @param {Object} [userConfig]
|
|
10
|
+
* @returns {Object} 当前配置
|
|
11
|
+
*/
|
|
12
|
+
function init(userConfig) {
|
|
13
|
+
if (initialized) {
|
|
14
|
+
return configModule.getConfig();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var config = configModule.setConfig(userConfig);
|
|
18
|
+
initialized = true;
|
|
19
|
+
|
|
20
|
+
if (config.enableInterceptor) {
|
|
21
|
+
interceptor.installInterceptor(config);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (config.debug) {
|
|
25
|
+
console.log('[mini-dynamic-renderer] initialized', config);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return config;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isInitialized() {
|
|
32
|
+
return initialized;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
init: init,
|
|
37
|
+
isInitialized: isInitialized
|
|
38
|
+
};
|
package/src/interceptor.js
CHANGED
|
@@ -1,104 +1,105 @@
|
|
|
1
|
-
var handler = require('./handler');
|
|
2
|
-
|
|
3
|
-
var originalRequest = null;
|
|
4
|
-
var installed = false;
|
|
5
|
-
|
|
6
|
-
var DEFAULT_CONFIG = {
|
|
7
|
-
urlPatterns: ['*'],
|
|
8
|
-
debug: false
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*
|
|
14
|
-
* @param {
|
|
15
|
-
* @
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
*
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
1
|
+
var handler = require('./handler');
|
|
2
|
+
|
|
3
|
+
var originalRequest = null;
|
|
4
|
+
var installed = false;
|
|
5
|
+
|
|
6
|
+
var DEFAULT_CONFIG = {
|
|
7
|
+
urlPatterns: ['*'],
|
|
8
|
+
debug: false,
|
|
9
|
+
enableInterceptor: false
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 判断 URL 是否匹配配置的模式
|
|
14
|
+
* @param {string} url
|
|
15
|
+
* @param {Array<string|RegExp>} patterns
|
|
16
|
+
* @returns {boolean}
|
|
17
|
+
*/
|
|
18
|
+
function matchUrl(url, patterns) {
|
|
19
|
+
if (!patterns || patterns.length === 0) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
for (var i = 0; i < patterns.length; i++) {
|
|
24
|
+
var pattern = patterns[i];
|
|
25
|
+
|
|
26
|
+
if (pattern === '*') {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (pattern instanceof RegExp) {
|
|
31
|
+
if (pattern.test(url)) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (typeof pattern === 'string' && url.indexOf(pattern) !== -1) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 安装 wx.request 拦截器
|
|
47
|
+
* @param {Object} config
|
|
48
|
+
*/
|
|
49
|
+
function installInterceptor(config) {
|
|
50
|
+
if (installed) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (typeof wx === 'undefined' || typeof wx.request !== 'function') {
|
|
55
|
+
console.warn('[mini-dynamic-renderer] wx.request is not available, skip interceptor install');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
originalRequest = wx.request;
|
|
60
|
+
installed = true;
|
|
61
|
+
|
|
62
|
+
wx.request = function (options) {
|
|
63
|
+
var userSuccess = options.success;
|
|
64
|
+
var mergedConfig = config || DEFAULT_CONFIG;
|
|
65
|
+
|
|
66
|
+
options.success = function (res) {
|
|
67
|
+
if (res && res.statusCode === 200 && matchUrl(options.url || '', mergedConfig.urlPatterns)) {
|
|
68
|
+
try {
|
|
69
|
+
handler.handleResponse(res, options, mergedConfig);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error('[mini-dynamic-renderer] handleResponse error:', err);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof userSuccess === 'function') {
|
|
76
|
+
userSuccess(res);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return originalRequest.call(wx, options);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 卸载拦截器,恢复原始 wx.request
|
|
86
|
+
*/
|
|
87
|
+
function uninstallInterceptor() {
|
|
88
|
+
if (!installed || !originalRequest) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
wx.request = originalRequest;
|
|
93
|
+
originalRequest = null;
|
|
94
|
+
installed = false;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = {
|
|
98
|
+
DEFAULT_CONFIG: DEFAULT_CONFIG,
|
|
99
|
+
matchUrl: matchUrl,
|
|
100
|
+
installInterceptor: installInterceptor,
|
|
101
|
+
uninstallInterceptor: uninstallInterceptor,
|
|
102
|
+
isInstalled: function () {
|
|
103
|
+
return installed;
|
|
104
|
+
}
|
|
105
|
+
};
|