@kbapp/market-partner-sdk 0.0.1
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 +661 -0
- package/dist/core/index.d.ts +79 -0
- package/dist/core/lib/flutter-ds-bridge.d.ts +7 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.es.js +324 -0
- package/dist/index.umd.js +1 -0
- package/dist/lib/app-version-support.d.ts +37 -0
- package/dist/lib/auth-token.d.ts +58 -0
- package/dist/lib/bridge-code.d.ts +14 -0
- package/dist/lib/callback-options.d.ts +10 -0
- package/dist/lib/env.d.ts +2 -0
- package/dist/lib/get-app-base-info.d.ts +49 -0
- package/dist/lib/report-getui.d.ts +31 -0
- package/dist/lib/run-action.d.ts +22 -0
- package/dist/lib/share-image.d.ts +18 -0
- package/dist/lib/share-model.d.ts +171 -0
- package/dist/util/filter-undefined-properties.d.ts +8 -0
- package/dist/util/promise-cache.d.ts +10 -0
- package/examples/index.html +217 -0
- package/package.json +18 -0
- package/server.js +103 -0
- package/tsconfig.json +24 -0
- package/vite.config.ts +24 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { CallbackOptions } from './callback-options';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @description app分享model
|
|
5
|
+
*/
|
|
6
|
+
export declare class AppShareModel {
|
|
7
|
+
/**
|
|
8
|
+
* 标题
|
|
9
|
+
*/
|
|
10
|
+
title: string;
|
|
11
|
+
/**
|
|
12
|
+
* 文字内容
|
|
13
|
+
*/
|
|
14
|
+
content?: string;
|
|
15
|
+
/**
|
|
16
|
+
* 图片
|
|
17
|
+
*/
|
|
18
|
+
imageUrl: string;
|
|
19
|
+
/**
|
|
20
|
+
* 分享链接
|
|
21
|
+
*/
|
|
22
|
+
url: string;
|
|
23
|
+
/**
|
|
24
|
+
* 分享功能是否开启. 默认为 true.
|
|
25
|
+
* 当分享功能关闭时, 其他分享相关字段均无效.
|
|
26
|
+
*/
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* 海报分享标题.
|
|
30
|
+
* 客户端: 海报分享必须. 若不存在则取 {@link #title}, 若也不存在, 则海报分享功能关闭.
|
|
31
|
+
* H5: 非必填. 通过桥接给到客户端时, 建议可以只填充 {@link #title}.
|
|
32
|
+
* 后端: 非必填.
|
|
33
|
+
*/
|
|
34
|
+
posterTitle?: string;
|
|
35
|
+
/**
|
|
36
|
+
* 海报分享内容.
|
|
37
|
+
* 客户端: 非必须. 若不存在则取 {@link #content}, 若也不存在, 则海报只有标题内容.
|
|
38
|
+
* H5: 非必填. 通过桥接给到客户端时, 建议可以只填充 {@link #content}.
|
|
39
|
+
* 后端: 非必填.
|
|
40
|
+
*/
|
|
41
|
+
posterContent?: string;
|
|
42
|
+
/**
|
|
43
|
+
* 海报分享封面图.
|
|
44
|
+
* 客户端: 海报分享非必须. 若不存在则取 {@link #imageUrl}, 若也不存在, 则海报无封面.
|
|
45
|
+
* H5: 非必填. 通过桥接给到客户端时, 建议可以只填充 {@link #imageUrl}.
|
|
46
|
+
* 后端: 非必填.
|
|
47
|
+
*/
|
|
48
|
+
posterUrl?: string;
|
|
49
|
+
/**
|
|
50
|
+
* 海报分享的展示时间.
|
|
51
|
+
* 客户端: 海报分享非必须. 若不存在则海报不展示时间.
|
|
52
|
+
* H5: 非必填. 通过桥接给到客户端时, 建议忽略该字段.
|
|
53
|
+
* 后端: 非必填.
|
|
54
|
+
*/
|
|
55
|
+
posterTime?: string;
|
|
56
|
+
/** 海报分享展示时间格式. */
|
|
57
|
+
posterTimeFormat?: string;
|
|
58
|
+
/**
|
|
59
|
+
* 海报分享功能是否开启. 默认为 false.
|
|
60
|
+
* 当海报分享功能关闭时, 其他海报分享相关字段均无效.
|
|
61
|
+
*/
|
|
62
|
+
posterEnabled?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* 分享小程序设置: 小程序原始ID.
|
|
65
|
+
* 获取方法: 登录小程序管理后台-设置-基本设置-帐号信息.
|
|
66
|
+
*
|
|
67
|
+
* 当此字段非空时, 客户端会显示 '分享至小程序' 功能. 详见微信小程序分享文档:
|
|
68
|
+
* https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html
|
|
69
|
+
*/
|
|
70
|
+
wxMiniUserName?: string;
|
|
71
|
+
/**
|
|
72
|
+
* 分享小程序设置: 小程序页面路径.
|
|
73
|
+
*/
|
|
74
|
+
wxMiniPath?: string;
|
|
75
|
+
/**
|
|
76
|
+
* 分享小程序设置: 额外信息.
|
|
77
|
+
* 通常开发者希望分享出去的小程序被二次打开时可以获取到更多信息, 例如群的标识.
|
|
78
|
+
*/
|
|
79
|
+
wxMiniShareTicket?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* 分享小程序设置: 小程序的类型.
|
|
82
|
+
* 0为正式版, 1为测试版, 2为体验版.
|
|
83
|
+
*/
|
|
84
|
+
wxMiniType?: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* @description 定义app点击转发时候的分享卡片内容
|
|
89
|
+
* @example
|
|
90
|
+
* **示例代码**
|
|
91
|
+
```js
|
|
92
|
+
import { defineAppShareModel } from '@kbapp/market-partner-sdk';
|
|
93
|
+
|
|
94
|
+
// 方式1
|
|
95
|
+
defineAppShareModel({
|
|
96
|
+
title: '分享标题',
|
|
97
|
+
content: '分享内容',
|
|
98
|
+
imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png', // 分享图片
|
|
99
|
+
url: 'http://www.kaiba315.com.cn/' // 分享网页地址
|
|
100
|
+
success() {
|
|
101
|
+
console.log('设置成功')
|
|
102
|
+
},
|
|
103
|
+
fail() {
|
|
104
|
+
console.log('设置失败')
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
// 方式2
|
|
109
|
+
defineAppShareModel({
|
|
110
|
+
onShareApp() {
|
|
111
|
+
// 点击页面右上角的时候触发该回调, 定义转发内容
|
|
112
|
+
return {
|
|
113
|
+
title: '分享标题',
|
|
114
|
+
content: '分享内容',
|
|
115
|
+
imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png', // 分享图片
|
|
116
|
+
url: 'http://www.kaiba315.com.cn/' // 分享网页地址
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
success() {
|
|
120
|
+
console.log('设置成功')
|
|
121
|
+
},
|
|
122
|
+
fail() {
|
|
123
|
+
console.log('设置失败')
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
*/
|
|
128
|
+
export declare function defineAppShareModel(params: (AppShareModel & CallbackOptions<void>) | ({
|
|
129
|
+
onShareApp: () => AppShareModel;
|
|
130
|
+
} & CallbackOptions<void>)): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
*
|
|
133
|
+
* @description 主动唤起 分享面板
|
|
134
|
+
* @version 开吧APP:80801开始支持,低版本需开发者做兼容处理
|
|
135
|
+
* @example
|
|
136
|
+
* **示例代码**
|
|
137
|
+
```js
|
|
138
|
+
import { openAppSharePanel } from '@kbapp/market-partner-sdk';
|
|
139
|
+
|
|
140
|
+
openAppSharePanel({
|
|
141
|
+
title: '开吧分享',
|
|
142
|
+
content: '开吧,开汽车上新生活!',
|
|
143
|
+
url: 'http://www.kaiba315.com.cn/',
|
|
144
|
+
imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png',
|
|
145
|
+
success() {
|
|
146
|
+
console.log('打开分享面板成功')
|
|
147
|
+
},
|
|
148
|
+
fail() {
|
|
149
|
+
console.log('打开分享面板失败')
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
```
|
|
153
|
+
*
|
|
154
|
+
*/
|
|
155
|
+
export declare function openAppSharePanel(params: AppShareModel & CallbackOptions<void>): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
*
|
|
158
|
+
* @description 监听 分享面板打开时触发 (主动和被动)
|
|
159
|
+
* @example
|
|
160
|
+
* **示例代码**
|
|
161
|
+
```js
|
|
162
|
+
import { onAppSharePanelShow } from '@kbapp/market-partner-sdk';
|
|
163
|
+
|
|
164
|
+
const stopHandle = onAppSharePanelShow(() => {
|
|
165
|
+
console.log('分享面板打开')
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
// stopHandle() 停止监听
|
|
169
|
+
```
|
|
170
|
+
*/
|
|
171
|
+
export declare function onAppSharePanelShow(callback: EventListenerOrEventListenerObject): () => void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 过滤对象第一层中值为 undefined 的属性(不处理嵌套对象)
|
|
3
|
+
* @param obj 待过滤的对象(仅支持纯对象,不支持数组/非对象类型)
|
|
4
|
+
* @returns 过滤后的新对象(自动剔除值为 undefined 的属性)
|
|
5
|
+
*/
|
|
6
|
+
export declare function filterUndefinedProperties<T extends Record<string, any>>(obj: T): Pick<T, {
|
|
7
|
+
[K in keyof T]: T[K] extends undefined ? never : K;
|
|
8
|
+
}[keyof T]>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @description 缓存promise结果
|
|
4
|
+
*/
|
|
5
|
+
export declare function decorateCacheAsyncResult<CB extends (...params: any[]) => any>(cb: CB): (...params: Parameters<CB>) => ReturnType<CB>;
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @description 异步节流
|
|
9
|
+
*/
|
|
10
|
+
export declare function decorateThrottleAsync<CALLBACK extends (...params: any[]) => any>(cb: CALLBACK): (...params: Parameters<CALLBACK>) => ReturnType<CALLBACK>;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>测试demo</title>
|
|
7
|
+
<link
|
|
8
|
+
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css"
|
|
9
|
+
rel="stylesheet"
|
|
10
|
+
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
|
|
11
|
+
crossorigin="anonymous"
|
|
12
|
+
/>
|
|
13
|
+
</head>
|
|
14
|
+
<body>
|
|
15
|
+
<div id="app">
|
|
16
|
+
<div class="card m-2" v-for="(item, index) in list" :key="index">
|
|
17
|
+
<h5 class="card-header">
|
|
18
|
+
<div class="d-flex justify-content-between align-items-center">
|
|
19
|
+
<span>{{item.title}}</span>
|
|
20
|
+
|
|
21
|
+
<template v-if="item.done.status.loading">
|
|
22
|
+
<button type="button" class="btn btn-primary" disabled>
|
|
23
|
+
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
|
|
24
|
+
</button>
|
|
25
|
+
</template>
|
|
26
|
+
<template v-else>
|
|
27
|
+
<button type="button" class="btn btn-primary" @click="item.done">{{item.buttonText}}</button>
|
|
28
|
+
</template>
|
|
29
|
+
</div>
|
|
30
|
+
</h5>
|
|
31
|
+
|
|
32
|
+
<div class="card-body">
|
|
33
|
+
<template v-if="item.subtitle">
|
|
34
|
+
<div class="card-title fw-bold">{{item.subtitle}}</div>
|
|
35
|
+
</template>
|
|
36
|
+
|
|
37
|
+
<template v-if="item.desc">
|
|
38
|
+
<div class="card-text">{{item.desc}}</div>
|
|
39
|
+
</template>
|
|
40
|
+
|
|
41
|
+
<template v-if="item.done.status.data">
|
|
42
|
+
<div class="alert alert-success mt-2" role="alert">{{item.done.status.data}}</div>
|
|
43
|
+
</template>
|
|
44
|
+
|
|
45
|
+
<template v-if="item.done.status.error">
|
|
46
|
+
<div class="alert alert-danger mt-2" role="alert">{{item.done.status.error}}</div>
|
|
47
|
+
</template>
|
|
48
|
+
</div>
|
|
49
|
+
</div>
|
|
50
|
+
</div>
|
|
51
|
+
</body>
|
|
52
|
+
<script type="module">
|
|
53
|
+
import { createApp, ref, reactive } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
|
|
54
|
+
import { getAuthToken, runAction, defineAppShareModel, openAppSharePanel, reportGetui, getAppBaseInfo, shareImage } from 'https://unpkg.com/@kbapp/market-partner-sdk@latest/dist/index.es.js'
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 装饰器,用于处理done函数的调用
|
|
58
|
+
* @param {Function} callback - 实际执行的函数
|
|
59
|
+
* @returns {Function} - 装饰后的函数
|
|
60
|
+
*/
|
|
61
|
+
const decorateDone = (callback) => {
|
|
62
|
+
/** 状态 */
|
|
63
|
+
const status = reactive({
|
|
64
|
+
/** 请求中 */
|
|
65
|
+
loading: false,
|
|
66
|
+
|
|
67
|
+
/** 请求完成 */
|
|
68
|
+
data: null,
|
|
69
|
+
|
|
70
|
+
/** 异常值 */
|
|
71
|
+
error: null,
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
async function done() {
|
|
75
|
+
status.data = null
|
|
76
|
+
status.error = null
|
|
77
|
+
status.loading = true
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const data = await Promise.resolve(callback())
|
|
81
|
+
status.data = data
|
|
82
|
+
} catch (error) {
|
|
83
|
+
status.error = error
|
|
84
|
+
throw error
|
|
85
|
+
} finally {
|
|
86
|
+
status.loading = false
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
done['status'] = status
|
|
91
|
+
|
|
92
|
+
return done
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const list = reactive([
|
|
96
|
+
{
|
|
97
|
+
title: 'runAction',
|
|
98
|
+
subtitle: '通用跳转',
|
|
99
|
+
buttonText: '执行',
|
|
100
|
+
desc: `执行结果: 跳转到百度页面`,
|
|
101
|
+
done: decorateDone(async () => {
|
|
102
|
+
await runAction({
|
|
103
|
+
action: 'pageWeb',
|
|
104
|
+
actionParams: {
|
|
105
|
+
url: 'https://www.baidu.com',
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
return '执行成功'
|
|
109
|
+
}),
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
title: 'defineAppShareModel',
|
|
113
|
+
subtitle: '定义分享model',
|
|
114
|
+
buttonText: '执行',
|
|
115
|
+
desc: `执行结果: 设置点击页面右上角分享卡片内容`,
|
|
116
|
+
done: decorateDone(async () => {
|
|
117
|
+
await defineAppShareModel({
|
|
118
|
+
title: '分享标题',
|
|
119
|
+
content: '分享内容',
|
|
120
|
+
imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png',
|
|
121
|
+
url: 'http://www.kaiba315.com.cn/',
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return '设置成功'
|
|
125
|
+
}),
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
title: 'getAuthToken',
|
|
129
|
+
subtitle: '强制登录 mode=force',
|
|
130
|
+
buttonText: '执行',
|
|
131
|
+
desc: `执行结果: 如果未登录则唤起登录, 登录成功后返回token`,
|
|
132
|
+
done: decorateDone(async () => {
|
|
133
|
+
const result = await getAuthToken({
|
|
134
|
+
mode: 'force',
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
return result
|
|
138
|
+
}),
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
title: 'getAuthToken',
|
|
142
|
+
subtitle: '静默登录 mode=silent',
|
|
143
|
+
buttonText: '执行',
|
|
144
|
+
desc: `执行结果: 如果未登录则返回code = 1, 已登录返回token`,
|
|
145
|
+
done: decorateDone(async () => {
|
|
146
|
+
const result = await getAuthToken({
|
|
147
|
+
mode: 'silent',
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
return result
|
|
151
|
+
}),
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
title: 'openAppSharePanel',
|
|
155
|
+
subtitle: '打开分享面板',
|
|
156
|
+
buttonText: '执行',
|
|
157
|
+
desc: `执行结果: 打开分享面板`,
|
|
158
|
+
done: decorateDone(async () => {
|
|
159
|
+
await openAppSharePanel({
|
|
160
|
+
title: '开吧分享',
|
|
161
|
+
content: '开吧,开汽车上新生活!',
|
|
162
|
+
url: 'http://www.kaiba315.com.cn/',
|
|
163
|
+
imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png',
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
return '执行成功'
|
|
167
|
+
}),
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
title: 'reportGetui',
|
|
171
|
+
subtitle: '上报数据埋点到个推',
|
|
172
|
+
buttonText: '执行',
|
|
173
|
+
desc: `执行结果:上报数据埋点到个推`,
|
|
174
|
+
done: decorateDone(async () => {
|
|
175
|
+
await reportGetui({
|
|
176
|
+
eventName: 'text_event_name',
|
|
177
|
+
eventParams: {
|
|
178
|
+
test_param: 'test_value',
|
|
179
|
+
},
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
return '执行成功'
|
|
183
|
+
}),
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
title: 'getAppBaseInfo',
|
|
187
|
+
subtitle: '获取开吧 App 基础信息',
|
|
188
|
+
buttonText: '执行',
|
|
189
|
+
desc: `执行结果: 返回开吧 App 基础信息`,
|
|
190
|
+
done: decorateDone(async () => {
|
|
191
|
+
return getAppBaseInfo()
|
|
192
|
+
}),
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
title: 'shareImage',
|
|
196
|
+
subtitle: '分享图片',
|
|
197
|
+
buttonText: '执行',
|
|
198
|
+
desc: `执行结果 分享图片`,
|
|
199
|
+
done: decorateDone(async () => {
|
|
200
|
+
await shareImage({
|
|
201
|
+
imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png',
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
return '分享成功'
|
|
205
|
+
}),
|
|
206
|
+
},
|
|
207
|
+
])
|
|
208
|
+
|
|
209
|
+
createApp({
|
|
210
|
+
setup() {
|
|
211
|
+
return {
|
|
212
|
+
list,
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
}).mount('#app')
|
|
216
|
+
</script>
|
|
217
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kbapp/market-partner-sdk",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "开吧商城接入jssdk",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "./dist/index.umd.js",
|
|
9
|
+
"module": "./dist/index.es.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"unpkg": "./dist/index.umd.js",
|
|
12
|
+
"browser": "./dist/index.umd.js",
|
|
13
|
+
"author": "along",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "rm -rf ./dist && vite build && npx tsc --emitDeclarationOnly",
|
|
16
|
+
"publish": "npm run build && npm publish"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/server.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// 简单的HTTP服务器,正确处理ES模块的MIME类型
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
const PORT = 8080;
|
|
7
|
+
const BASE_DIR = process.cwd();
|
|
8
|
+
|
|
9
|
+
const server = http.createServer((req, res) => {
|
|
10
|
+
console.log(`请求路径: ${req.url}`);
|
|
11
|
+
// 移除URL中的查询字符串
|
|
12
|
+
const urlPath = req.url.split('?')[0];
|
|
13
|
+
|
|
14
|
+
// 特殊路径重定向逻辑
|
|
15
|
+
let redirectPath = null;
|
|
16
|
+
|
|
17
|
+
// 重定向 /lib/bridge-code 到 /dist/lib/bridge-code
|
|
18
|
+
if (urlPath === '/lib/bridge-code') {
|
|
19
|
+
redirectPath = '/dist/lib/bridge-code';
|
|
20
|
+
console.log(`重定向路径: ${urlPath} -\u003e ${redirectPath}`);
|
|
21
|
+
}
|
|
22
|
+
// 重定向 /dist/lib/flutter-ds-bridge 到 /dist/core/lib/flutter-ds-bridge
|
|
23
|
+
else if (urlPath === '/dist/lib/flutter-ds-bridge') {
|
|
24
|
+
redirectPath = '/dist/core/lib/flutter-ds-bridge';
|
|
25
|
+
console.log(`重定向路径: ${urlPath} -\u003e ${redirectPath}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 确定文件路径
|
|
29
|
+
let filePath = path.join(BASE_DIR, redirectPath || urlPath);
|
|
30
|
+
|
|
31
|
+
// 如果请求没有扩展名,尝试添加.js
|
|
32
|
+
if (!path.extname(urlPath)) {
|
|
33
|
+
console.log(`检测到无扩展名路径,尝试添加.js扩展名`);
|
|
34
|
+
filePath = filePath + '.js';
|
|
35
|
+
} else if (urlPath.endsWith('/')) {
|
|
36
|
+
// 如果是目录,默认返回index.html
|
|
37
|
+
filePath = path.join(filePath, 'index.html');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 确定MIME类型
|
|
41
|
+
const extname = path.extname(filePath);
|
|
42
|
+
let contentType = 'text/html';
|
|
43
|
+
|
|
44
|
+
switch (extname) {
|
|
45
|
+
case '.js':
|
|
46
|
+
contentType = 'application/javascript';
|
|
47
|
+
break;
|
|
48
|
+
case '.mjs':
|
|
49
|
+
contentType = 'application/javascript';
|
|
50
|
+
break;
|
|
51
|
+
case '.css':
|
|
52
|
+
contentType = 'text/css';
|
|
53
|
+
break;
|
|
54
|
+
case '.json':
|
|
55
|
+
contentType = 'application/json';
|
|
56
|
+
break;
|
|
57
|
+
case '.png':
|
|
58
|
+
contentType = 'image/png';
|
|
59
|
+
break;
|
|
60
|
+
case '.jpg':
|
|
61
|
+
case '.jpeg':
|
|
62
|
+
contentType = 'image/jpeg';
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
console.log(`尝试加载文件: ${filePath}`);
|
|
67
|
+
|
|
68
|
+
// 读取并发送文件
|
|
69
|
+
fs.readFile(filePath, (error, content) => {
|
|
70
|
+
if (error) {
|
|
71
|
+
console.error(`文件加载错误: ${error.message}`);
|
|
72
|
+
if (error.code === 'ENOENT') {
|
|
73
|
+
// 文件不存在,尝试备选路径/index.js
|
|
74
|
+
const altFilePath = path.join(filePath.replace('.js', ''), 'index.js');
|
|
75
|
+
console.log(`尝试备选路径: ${altFilePath}`);
|
|
76
|
+
fs.readFile(altFilePath, (altError, altContent) => {
|
|
77
|
+
if (altError) {
|
|
78
|
+
res.writeHead(404);
|
|
79
|
+
res.end(`文件不存在: ${filePath}`);
|
|
80
|
+
} else {
|
|
81
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
82
|
+
res.end(altContent, 'utf-8');
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 服务器错误
|
|
89
|
+
res.writeHead(500);
|
|
90
|
+
res.end('服务器错误: ' + error.code);
|
|
91
|
+
} else {
|
|
92
|
+
// 文件存在,发送内容
|
|
93
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
94
|
+
res.end(content, 'utf-8');
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
server.listen(PORT, () => {
|
|
100
|
+
console.log(`服务器运行在 http://localhost:${PORT}`);
|
|
101
|
+
console.log(`当前工作目录: ${BASE_DIR}`);
|
|
102
|
+
console.log(`访问示例: http://localhost:${PORT}/examples/index.html`);
|
|
103
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"strict": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"baseUrl": "./",
|
|
9
|
+
"rootDir": "./src",
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"declarationDir": "./dist",
|
|
12
|
+
"lib": [
|
|
13
|
+
"esnext",
|
|
14
|
+
"dom"
|
|
15
|
+
],
|
|
16
|
+
"sourceMap": false,
|
|
17
|
+
"moduleResolution": "Node",
|
|
18
|
+
"esModuleInterop": true,
|
|
19
|
+
"resolveJsonModule": true
|
|
20
|
+
},
|
|
21
|
+
"include": [
|
|
22
|
+
"src"
|
|
23
|
+
]
|
|
24
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import react from '@vitejs/plugin-react';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
plugins: [react()],
|
|
6
|
+
build: {
|
|
7
|
+
lib: {
|
|
8
|
+
entry: './src/index.ts',
|
|
9
|
+
name: 'kbMarket',
|
|
10
|
+
fileName: (format) => `index.${format}.js`,
|
|
11
|
+
formats: ['es', 'umd']
|
|
12
|
+
},
|
|
13
|
+
rollupOptions: {
|
|
14
|
+
// 确保外部化处理那些你不想打包进库的依赖
|
|
15
|
+
external: [],
|
|
16
|
+
output: {
|
|
17
|
+
// 为 UMD/iife 构建提供全局变量名称
|
|
18
|
+
globals: {
|
|
19
|
+
// 如果有依赖需要全局变量映射,可以在这里配置
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|