forked from lesnolie/movecar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovecar.js
More file actions
1188 lines (1136 loc) · 48.1 KB
/
Copy pathmovecar.js
File metadata and controls
1188 lines (1136 loc) · 48.1 KB
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request, event))
})
// ========== 配置常量 ==========
const CONFIG = {
KV_TTL: 3600,
NOTIFY_EXPIRY: 600,
DELAY_TIMEOUT: 30000,
CACHE_TTL: 60,
APP_NAME: '挪车通知系统',
VERSION: '2.2.0',
LOG_EXPIRY: 86400 * 7
}
// 推送渠道配置
const PUSH_CONFIG = {
DINGTALK_ENABLED: typeof DINGTALK_WEBHOOK !== 'undefined',
WEWORK_ENABLED: typeof WEWORK_WEBHOOK !== 'undefined',
PUSHPLUS_ENABLED: typeof PUSHPLUS_TOKEN !== 'undefined',
MULTI_CHANNEL: true,
RETRY_ATTEMPTS: 2
}
// 状态枚举
const STATUS = {
WAITING: 'waiting',
CONFIRMED: 'confirmed',
ERROR: 'error',
PENDING: 'pending'
}
// 消息模板
const MESSAGES = {
NOTIFY_TITLE: '🚗 挪车请求',
NOTIFY_BODY: '车旁有人等待,请尽快处理',
DEFAULT_MESSAGE: '车旁有人等待',
LOCATION_PROVIDED: '📍 已附带位置信息',
LOCATION_MISSING: '⚠️ 未提供位置信息',
ERROR: '操作失败,请重试',
SUCCESS: '操作成功',
DELAYED_NOTIFY: '⏳ 通知已发送(延迟30秒)'
}
// ========== 工具函数 ==========
function wgs84ToGcj02(lat, lng) {
const a = 6378245.0;
const ee = 0.00669342162296594323;
if (outOfChina(lat, lng)) return { lat, lng };
let dLat = transformLat(lng - 105.0, lat - 35.0);
let dLng = transformLng(lng - 105.0, lat - 35.0);
const radLat = lat / 180.0 * Math.PI;
let magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
const sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math.PI);
dLng = (dLng * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
return { lat: lat + dLat, lng: lng + dLng };
}
function outOfChina(lat, lng) {
return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271;
}
function transformLat(x, y) {
let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0;
return ret;
}
function transformLng(x, y) {
let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0;
return ret;
}
function generateMapUrls(lat, lng) {
try {
const gcj = wgs84ToGcj02(lat, lng);
return {
amapUrl: `https://uri.amap.com/marker?position=${gcj.lng},${gcj.lat}&name=位置&src=MoveCar`,
appleUrl: `https://maps.apple.com/?ll=${gcj.lat},${gcj.lng}&q=位置&t=m`,
baiduUrl: `https://api.map.baidu.com/marker?location=${gcj.lat},${gcj.lng}&title=位置&output=html&coord_type=gcj02&src=MoveCar`,
googleUrl: `https://www.google.com/maps?q=${gcj.lat},${gcj.lng}&z=17`
};
} catch (error) {
console.error('地图URL生成失败:', error);
return null;
}
}
function validateLocation(location) {
if (!location || typeof location !== 'object') return null;
const lat = parseFloat(location.lat);
const lng = parseFloat(location.lng);
if (isNaN(lat) || isNaN(lng)) return null;
if (lat < -90 || lat > 90) return null;
if (lng < -180 || lng > 180) return null;
return { lat, lng };
}
function formatTime(timestamp) {
const date = new Date(timestamp);
return date.toLocaleString('zh-CN', {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit',
hour12: false
});
}
function generateId(length = 8) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
function errorResponse(error, status = 500) {
return new Response(JSON.stringify({
success: false,
error: error.message || '服务器内部错误',
timestamp: Date.now(),
requestId: generateId()
}), {
status,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
});
}
function successResponse(data = {}, status = 200) {
return new Response(JSON.stringify({
success: true,
data,
timestamp: Date.now(),
requestId: generateId()
}), {
status,
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'no-cache' }
});
}
// ========== 推送日志 ==========
async function logPush(channel, notifyId, success, responseData, error, event) {
try {
const logEntry = {
channel,
notifyId,
timestamp: Date.now(),
success,
response: responseData || null,
error: error ? (error.message || String(error)) : null
};
const logKey = `push_log_${channel}_${notifyId}_${Date.now()}`;
await MOVE_CAR_STATUS.put(logKey, JSON.stringify(logEntry), { expirationTtl: CONFIG.LOG_EXPIRY });
} catch (err) {
console.error('写入推送日志失败:', err);
}
}
// ========== 推送渠道实现 ==========
// 钉钉机器人推送
async function sendDingTalkNotification(data, event) {
try {
let webhookUrl = DINGTALK_WEBHOOK;
if (typeof DINGTALK_SECRET !== 'undefined' && DINGTALK_SECRET) {
const timestamp = Date.now();
const stringToSign = `${timestamp}\n${DINGTALK_SECRET}`;
const encoder = new TextEncoder();
const keyData = encoder.encode(DINGTALK_SECRET);
const messageData = encoder.encode(stringToSign);
const key = await crypto.subtle.importKey('raw', keyData, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
const signature = await crypto.subtle.sign('HMAC', key, messageData);
const signatureBase64 = btoa(String.fromCharCode(...new Uint8Array(signature)));
const encodedSignature = encodeURIComponent(signatureBase64);
webhookUrl += `×tamp=${timestamp}&sign=${encodedSignature}`;
}
let text = `## ${data.title}\n\n**留言**: ${data.message}\n\n**位置状态**: ${data.locationStatus}\n\n**时间**: ${formatTime(data.timestamp)}\n\n**通知ID**: ${data.id}\n\n`;
if (data.mapUrls) {
text += `### 地图链接\n- [高德地图](${data.mapUrls.amapUrl})\n- [Apple地图](${data.mapUrls.appleUrl})\n- [百度地图](${data.mapUrls.baiduUrl})\n\n`;
}
text += `[点击确认已收到](${data.confirmUrl})`;
const dingTalkMessage = {
msgtype: "markdown",
markdown: { title: data.title, text: text },
at: { isAtAll: false }
};
const response = await fetch(webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'User-Agent': `MoveCar/${CONFIG.VERSION}` },
body: JSON.stringify(dingTalkMessage)
});
const result = await response.json();
if (result.errcode !== 0) {
throw new Error(`钉钉推送失败: ${result.errmsg} (code: ${result.errcode})`);
}
event.waitUntil(logPush('dingtalk', data.id, true, result, null, event));
return { success: true, response: result, timestamp: Date.now() };
} catch (error) {
event.waitUntil(logPush('dingtalk', data.id, false, null, error, event));
console.error('钉钉推送失败:', error);
throw error;
}
}
// 企业微信机器人推送(模板卡片)
async function sendWeWorkNotification(data, event) {
try {
const webhookUrl = WEWORK_WEBHOOK;
const templateCard = {
msgtype: "template_card",
template_card: {
card_type: "text_notice",
source: {
icon_url: "https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0",
desc: "挪车通知系统",
desc_color: 0
},
main_title: {
title: data.title,
desc: data.message
},
emphasis_content: {
title: "🚗",
desc: "请尽快处理"
},
sub_title_text: `位置状态:${data.locationStatus}`,
horizontal_content_list: [
{ keyname: "留言", value: data.message },
{ keyname: "时间", value: formatTime(data.timestamp) },
{ keyname: "通知ID", value: data.id }
],
jump_list: [
{ type: 1, url: data.confirmUrl, title: "✅ 确认已收到" }
],
card_action: {
type: 1,
url: data.confirmUrl
}
}
};
if (data.mapUrls) {
if (data.mapUrls.amapUrl) {
templateCard.template_card.horizontal_content_list.push({
keyname: "高德地图",
value: "点击导航",
type: 1,
url: data.mapUrls.amapUrl
});
}
if (data.mapUrls.appleUrl) {
templateCard.template_card.horizontal_content_list.push({
keyname: "Apple地图",
value: "点击导航",
type: 1,
url: data.mapUrls.appleUrl
});
}
if (data.mapUrls.baiduUrl) {
templateCard.template_card.horizontal_content_list.push({
keyname: "百度地图",
value: "点击导航",
type: 1,
url: data.mapUrls.baiduUrl
});
}
}
const response = await fetch(webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'User-Agent': `MoveCar/${CONFIG.VERSION}` },
body: JSON.stringify(templateCard)
});
const result = await response.json();
if (result.errcode !== 0) {
throw new Error(`企业微信推送失败: ${result.errmsg} (code: ${result.errcode})`);
}
event.waitUntil(logPush('wework', data.id, true, result, null, event));
return { success: true, response: result, timestamp: Date.now() };
} catch (error) {
event.waitUntil(logPush('wework', data.id, false, null, error, event));
console.error('企业微信推送失败:', error);
throw error;
}
}
// PushPlus 推送(微信)
async function sendPushPlusNotification(data, event) {
try {
const pushplusUrl = 'https://www.pushplus.plus/send';
const token = PUSHPLUS_TOKEN;
let content = `## ${data.title}\n\n`;
content += `**留言**:${data.message}\n\n`;
content += `**位置状态**:${data.locationStatus}\n\n`;
content += `**时间**:${formatTime(data.timestamp)}\n\n`;
content += `**通知ID**:${data.id}\n\n`;
if (data.mapUrls) {
content += `### 地图导航\n`;
content += `[高德地图](${data.mapUrls.amapUrl}) | [Apple地图](${data.mapUrls.appleUrl}) | [百度地图](${data.mapUrls.baiduUrl})\n\n`;
}
content += `[点击确认已收到](${data.confirmUrl})`;
const params = new URLSearchParams();
params.append('token', token);
params.append('title', data.title);
params.append('content', content);
params.append('template', 'markdown');
const response = await fetch(pushplusUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: params
});
const result = await response.json();
if (result.code !== 200) {
throw new Error(`PushPlus推送失败: ${result.msg} (code: ${result.code})`);
}
event.waitUntil(logPush('pushplus', data.id, true, result, null, event));
return { success: true, response: result, timestamp: Date.now() };
} catch (error) {
event.waitUntil(logPush('pushplus', data.id, false, null, error, event));
console.error('PushPlus推送失败:', error);
throw error;
}
}
// 发送通知到所有渠道
async function sendNotifications(data, event) {
const results = [];
const promises = [];
await MOVE_CAR_STATUS.put(`notify_status_${data.id}`, 'sending', { expirationTtl: CONFIG.NOTIFY_EXPIRY });
if (PUSH_CONFIG.DINGTALK_ENABLED && typeof DINGTALK_WEBHOOK !== 'undefined') {
promises.push(
sendDingTalkNotification(data, event).then(result => {
results.push({ channel: 'dingtalk', ...result });
}).catch(error => {
results.push({ channel: 'dingtalk', success: false, error: error.message });
})
);
}
if (PUSH_CONFIG.WEWORK_ENABLED && typeof WEWORK_WEBHOOK !== 'undefined') {
promises.push(
sendWeWorkNotification(data, event).then(result => {
results.push({ channel: 'wework', ...result });
}).catch(error => {
results.push({ channel: 'wework', success: false, error: error.message });
})
);
}
if (PUSH_CONFIG.PUSHPLUS_ENABLED && typeof PUSHPLUS_TOKEN !== 'undefined') {
promises.push(
sendPushPlusNotification(data, event).then(result => {
results.push({ channel: 'pushplus', ...result });
}).catch(error => {
results.push({ channel: 'pushplus', success: false, error: error.message });
})
);
}
if (promises.length > 0) {
await Promise.allSettled(promises);
const success = results.some(r => r.success);
await MOVE_CAR_STATUS.put(`notify_status_${data.id}`, success ? 'sent' : 'failed', { expirationTtl: CONFIG.NOTIFY_EXPIRY });
} else {
throw new Error('未启用任何推送渠道');
}
return results;
}
// ========== API处理函数 ==========
async function handleHealthCheck() {
const kvStatus = typeof MOVE_CAR_STATUS !== 'undefined';
const now = Date.now();
let startTime = await MOVE_CAR_STATUS.get('start_time');
if (!startTime) {
startTime = now;
await MOVE_CAR_STATUS.put('start_time', startTime.toString(), { expirationTtl: 86400 * 30 });
}
return successResponse({
status: 'healthy',
timestamp: now,
uptime: now - parseInt(startTime),
services: {
kv: kvStatus,
dingtalk: PUSH_CONFIG.DINGTALK_ENABLED,
wework: PUSH_CONFIG.WEWORK_ENABLED,
pushplus: PUSH_CONFIG.PUSHPLUS_ENABLED
},
config: { version: CONFIG.VERSION, multiChannel: PUSH_CONFIG.MULTI_CHANNEL }
});
}
async function handlePushChannelsInfo() {
const channels = {
dingtalk: {
enabled: PUSH_CONFIG.DINGTALK_ENABLED,
configured: typeof DINGTALK_WEBHOOK !== 'undefined',
hasSecret: typeof DINGTALK_SECRET !== 'undefined',
name: '钉钉机器人',
description: '钉钉群聊机器人推送',
icon: '🤖'
},
wework: {
enabled: PUSH_CONFIG.WEWORK_ENABLED,
configured: typeof WEWORK_WEBHOOK !== 'undefined',
name: '企业微信机器人',
description: '企业微信群机器人推送',
icon: '💬'
},
pushplus: {
enabled: PUSH_CONFIG.PUSHPLUS_ENABLED,
configured: typeof PUSHPLUS_TOKEN !== 'undefined',
name: 'PushPlus',
description: '微信推送服务(通过PushPlus)',
icon: '📱'
}
};
return successResponse({
channels,
multiChannel: PUSH_CONFIG.MULTI_CHANNEL,
activeChannels: Object.values(channels).filter(c => c.configured).length
});
}
async function handleNotify(request, url, event) {
try {
const body = await request.json();
const message = body.message?.trim() || MESSAGES.DEFAULT_MESSAGE;
const location = validateLocation(body.location);
const delayed = body.delayed || false;
const notifyId = generateId(12);
const confirmUrl = `${url.origin}/owner-confirm?notifyId=${notifyId}`;
const notifyData = {
id: notifyId,
title: MESSAGES.NOTIFY_TITLE,
message: message,
location: location,
confirmUrl: confirmUrl,
origin: url.origin,
timestamp: Date.now(),
status: STATUS.PENDING,
requesterInfo: {
ip: request.headers.get('cf-connecting-ip') || 'unknown',
userAgent: request.headers.get('user-agent') || 'unknown'
}
};
if (location) {
const urls = generateMapUrls(location.lat, location.lng);
if (urls) {
notifyData.mapUrls = urls;
notifyData.locationStatus = MESSAGES.LOCATION_PROVIDED;
await MOVE_CAR_STATUS.put(`requester_location_${notifyId}`, JSON.stringify({
lat: location.lat, lng: location.lng, ...urls, timestamp: Date.now()
}), { expirationTtl: CONFIG.KV_TTL });
} else {
notifyData.locationStatus = MESSAGES.LOCATION_MISSING;
}
} else {
notifyData.locationStatus = MESSAGES.LOCATION_MISSING;
}
await MOVE_CAR_STATUS.put(`notify_record_${notifyId}`, JSON.stringify(notifyData), { expirationTtl: CONFIG.KV_TTL });
await MOVE_CAR_STATUS.put(`notify_status_${notifyId}`, STATUS.WAITING, { expirationTtl: CONFIG.NOTIFY_EXPIRY });
await updateStats('total_requests');
if (delayed) {
setTimeout(async () => {
await sendNotifications(notifyData, event);
}, CONFIG.DELAY_TIMEOUT);
return successResponse({
id: notifyId,
status: 'delayed',
message: MESSAGES.DELAYED_NOTIFY,
estimatedTime: Date.now() + CONFIG.DELAY_TIMEOUT,
hasLocation: !!location
});
} else {
const pushResults = await sendNotifications(notifyData, event);
const success = pushResults.some(r => r.success);
if (success) await updateStats('success_requests');
return successResponse({
id: notifyId,
status: success ? 'sent' : 'failed',
message: success ? MESSAGES.SUCCESS : MESSAGES.ERROR,
hasLocation: !!location,
channels: pushResults,
summary: {
total: pushResults.length,
success: pushResults.filter(r => r.success).length,
failed: pushResults.filter(r => !r.success).length
}
});
}
} catch (error) {
console.error('通知发送失败:', error);
return errorResponse(error);
}
}
async function updateStats(type) {
const key = `stats_${type}`;
let value = await MOVE_CAR_STATUS.get(key) || '0';
value = (parseInt(value) + 1).toString();
await MOVE_CAR_STATUS.put(key, value, { expirationTtl: 86400 * 30 });
await MOVE_CAR_STATUS.put('stats_last_request', Date.now().toString(), { expirationTtl: 86400 * 30 });
}
async function handleGetLocation(request) {
try {
const notifyId = new URL(request.url).searchParams.get('notifyId');
if (!notifyId) return errorResponse(new Error('缺少通知ID参数'), 400);
const data = await MOVE_CAR_STATUS.get(`requester_location_${notifyId}`);
if (data) return successResponse(JSON.parse(data));
return errorResponse(new Error('位置信息不存在或已过期'), 404);
} catch (error) {
return errorResponse(error);
}
}
async function handleOwnerConfirmAction(request) {
try {
const body = await request.json();
const notifyId = body.notifyId || generateId(12);
const ownerLocation = validateLocation(body.location);
let locationData = null;
if (ownerLocation) {
const urls = generateMapUrls(ownerLocation.lat, ownerLocation.lng);
if (urls) {
locationData = { lat: ownerLocation.lat, lng: ownerLocation.lng, ...urls, timestamp: Date.now(), type: 'owner_location' };
await MOVE_CAR_STATUS.put(`owner_location_${notifyId}`, JSON.stringify(locationData), { expirationTtl: CONFIG.KV_TTL });
}
}
await MOVE_CAR_STATUS.put(`notify_status_${notifyId}`, STATUS.CONFIRMED, { expirationTtl: CONFIG.NOTIFY_EXPIRY });
await MOVE_CAR_STATUS.put(`notify_confirmed_${notifyId}`, Date.now().toString(), { expirationTtl: CONFIG.KV_TTL });
return successResponse({ message: '确认成功', hasLocation: !!locationData, notifyId, timestamp: Date.now() });
} catch (error) {
const notifyId = body.notifyId || generateId(12);
await MOVE_CAR_STATUS.put(`notify_status_${notifyId}`, STATUS.CONFIRMED, { expirationTtl: CONFIG.NOTIFY_EXPIRY });
return successResponse({ message: '确认成功(位置信息处理失败)', error: error.message, notifyId });
}
}
async function handleCheckStatus(request) {
try {
const notifyId = new URL(request.url).searchParams.get('notifyId');
if (!notifyId) return errorResponse(new Error('缺少通知ID参数'), 400);
const [status, ownerLocation, confirmedTime] = await Promise.all([
MOVE_CAR_STATUS.get(`notify_status_${notifyId}`),
MOVE_CAR_STATUS.get(`owner_location_${notifyId}`),
MOVE_CAR_STATUS.get(`notify_confirmed_${notifyId}`)
]);
return successResponse({
status: status || STATUS.WAITING,
ownerLocation: ownerLocation ? JSON.parse(ownerLocation) : null,
confirmedTime: confirmedTime ? parseInt(confirmedTime) : null,
confirmedAgo: confirmedTime ? Date.now() - parseInt(confirmedTime) : null,
timestamp: Date.now()
});
} catch (error) {
return errorResponse(error);
}
}
async function testDingTalkPush(event) {
try {
if (typeof DINGTALK_WEBHOOK === 'undefined') return errorResponse(new Error('钉钉机器人未配置'), 400);
const testData = {
id: generateId(12),
title: '🚗 挪车通知测试',
message: '这是一条测试消息,用于验证钉钉机器人配置是否正常。关键词:挪车',
locationStatus: MESSAGES.LOCATION_PROVIDED,
confirmUrl: 'https://car.fireboxwork.workers.dev/owner-confirm?test=true',
origin: 'https://car.fireboxwork.workers.dev',
timestamp: Date.now(),
mapUrls: {
amapUrl: 'https://uri.amap.com/marker?position=116.397428,39.90923&name=测试位置',
appleUrl: 'https://maps.apple.com/?ll=39.90923,116.397428&q=测试位置',
baiduUrl: 'https://api.map.baidu.com/marker?location=39.90923,116.397428&title=测试位置'
}
};
const result = await sendDingTalkNotification(testData, event);
return successResponse({ message: '钉钉测试推送发送成功!请检查钉钉群是否收到消息。', result, timestamp: Date.now() });
} catch (error) {
return errorResponse(error);
}
}
async function testWeWorkPush(event) {
try {
if (typeof WEWORK_WEBHOOK === 'undefined') return errorResponse(new Error('企业微信机器人未配置'), 400);
const testData = {
id: generateId(12),
title: '🚗 挪车通知测试',
message: '这是一条测试消息,用于验证企业微信机器人配置是否正常。',
locationStatus: MESSAGES.LOCATION_PROVIDED,
confirmUrl: 'https://car.fireboxwork.workers.dev/owner-confirm?test=true',
origin: 'https://car.fireboxwork.workers.dev',
timestamp: Date.now(),
mapUrls: {
amapUrl: 'https://uri.amap.com/marker?position=116.397428,39.90923&name=测试位置',
appleUrl: 'https://maps.apple.com/?ll=39.90923,116.397428&q=测试位置',
baiduUrl: 'https://api.map.baidu.com/marker?location=39.90923,116.397428&title=测试位置'
}
};
const result = await sendWeWorkNotification(testData, event);
return successResponse({ message: '企业微信测试推送发送成功!请检查企业微信群是否收到消息。', result, timestamp: Date.now() });
} catch (error) {
return errorResponse(error);
}
}
async function testPushPlusPush(event) {
try {
if (typeof PUSHPLUS_TOKEN === 'undefined') return errorResponse(new Error('PushPlus未配置'), 400);
const testData = {
id: generateId(12),
title: '🚗 挪车通知测试',
message: '这是一条测试消息,用于验证PushPlus配置是否正常。',
locationStatus: MESSAGES.LOCATION_PROVIDED,
confirmUrl: 'https://car.fireboxwork.workers.dev/owner-confirm?test=true',
origin: 'https://car.fireboxwork.workers.dev',
timestamp: Date.now(),
mapUrls: {
amapUrl: 'https://uri.amap.com/marker?position=116.397428,39.90923&name=测试位置',
appleUrl: 'https://maps.apple.com/?ll=39.90923,116.397428&q=测试位置',
baiduUrl: 'https://api.map.baidu.com/marker?location=39.90923,116.397428&title=测试位置'
}
};
const result = await sendPushPlusNotification(testData, event);
return successResponse({ message: 'PushPlus测试推送发送成功!请检查微信是否收到消息。', result, timestamp: Date.now() });
} catch (error) {
return errorResponse(error);
}
}
// ========== 主请求处理器 ==========
async function handleRequest(request, event) {
const url = new URL(request.url);
const path = url.pathname;
try {
if (request.method === 'OPTIONS') {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Max-Age': '86400'
}
});
}
if (path.startsWith('/api/')) {
if (path === '/api/health') return handleHealthCheck();
if (path === '/api/push-channels') return handlePushChannelsInfo();
if (path === '/api/notify' && request.method === 'POST') return await handleNotify(request, url, event);
if (path === '/api/get-location') return await handleGetLocation(request);
if (path === '/api/owner-confirm' && request.method === 'POST') return await handleOwnerConfirmAction(request);
if (path === '/api/check-status') return await handleCheckStatus(request);
if (path === '/api/test-dingtalk') return await testDingTalkPush(event);
if (path === '/api/test-wework') return await testWeWorkPush(event);
if (path === '/api/test-pushplus') return await testPushPlusPush(event);
return new Response('Not Found', { status: 404 });
}
if (path === '/owner-confirm') return await renderOwnerPage(url.origin);
return await renderMainPage(url.origin);
} catch (error) {
console.error('请求处理错误:', error);
return errorResponse(error, 500);
}
}
// ========== 页面渲染函数 ==========
async function renderConfigErrorPage() {
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>配置错误 - ${CONFIG.APP_NAME}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
margin: 0;
}
.error-container {
max-width: 500px;
width: 100%;
background: white;
border-radius: 16px;
padding: 40px;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
text-align: center;
}
.error-icon { font-size: 64px; color: #ef4444; margin-bottom: 20px; }
h1 { color: #1f2937; margin-bottom: 16px; font-size: 24px; }
p { color: #6b7280; margin-bottom: 24px; line-height: 1.6; }
.config-steps { text-align: left; background: #f9fafb; border-radius: 8px; padding: 20px; margin: 24px 0; }
.config-step { margin-bottom: 12px; padding-left: 24px; position: relative; }
.config-step:before { content: '•'; position: absolute; left: 8px; color: #3b82f6; font-weight: bold; }
code { background: #e5e7eb; padding: 2px 6px; border-radius: 4px; font-family: 'Courier New', monospace; font-size: 14px; }
.btn { display: inline-block; background: #3b82f6; color: white; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 500; transition: background 0.2s; }
.btn:hover { background: #2563eb; }
</style>
</head>
<body>
<div class="error-container">
<div class="error-icon">⚠️</div>
<h1>推送渠道未配置</h1>
<p>请至少配置一个推送渠道才能使用${CONFIG.APP_NAME}</p>
<div class="config-steps">
<div class="config-step">在Cloudflare Workers环境变量中添加:</div>
<div class="config-step"><code>DINGTALK_WEBHOOK = "your_webhook_url"</code> (钉钉机器人)</div>
<div class="config-step"><code>WEWORK_WEBHOOK = "your_webhook_url"</code> (企业微信机器人)</div>
<div class="config-step"><code>PUSHPLUS_TOKEN = "your_token"</code> (PushPlus)</div>
</div>
<a href="https://developers.cloudflare.com/workers/platform/environment-variables/" class="btn" target="_blank">查看配置文档</a>
</div>
</body>
</html>`;
return new Response(html, { headers: { 'Content-Type': 'text/html' } });
}
async function renderMainPage(origin) {
const phone = typeof PHONE_NUMBER !== 'undefined' ? PHONE_NUMBER : '';
const hasAtLeastOneChannel = PUSH_CONFIG.DINGTALK_ENABLED || PUSH_CONFIG.WEWORK_ENABLED || PUSH_CONFIG.PUSHPLUS_ENABLED;
if (!hasAtLeastOneChannel) return renderConfigErrorPage();
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${CONFIG.APP_NAME}</title>
<style>
:root {
--primary: #0093E9;
--secondary: #80D0C7;
--success: #10B981;
--warning: #F59E0B;
--danger: #EF4444;
--light: #F9FAFB;
--dark: #1F2937;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
min-height: 100vh;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
}
.container { width: 100%; max-width: 500px; }
.card {
background: white;
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
transition: transform 0.2s, box-shadow 0.2s;
}
.card:hover { transform: translateY(-4px); box-shadow: 0 24px 48px rgba(0, 0, 0, 0.15); }
.header { text-align: center; padding: 30px 20px; }
.icon {
width: 80px; height: 80px;
background: linear-gradient(135deg, var(--primary), var(--secondary));
border-radius: 20px;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 20px;
}
.icon span { font-size: 40px; color: white; }
h1 { color: var(--dark); margin-bottom: 10px; font-size: 28px; }
.subtitle { color: #6B7280; font-size: 16px; }
.input-group { margin-bottom: 20px; }
textarea {
width: 100%; min-height: 120px; padding: 15px;
border: 2px solid #E5E7EB; border-radius: 12px;
font-size: 16px; font-family: inherit; resize: vertical;
transition: border-color 0.3s;
}
textarea:focus { outline: none; border-color: var(--primary); }
.tags { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 15px; }
.tag {
background: #F3F4F6; color: #4B5563; padding: 10px 15px;
border-radius: 20px; font-size: 14px; cursor: pointer;
transition: all 0.2s; border: none;
}
.tag:hover { background: #E5E7EB; transform: translateY(-2px); }
.tag:active { transform: scale(0.98); }
.location-card {
display: flex; align-items: center; padding: 20px;
background: #F9FAFB; border-radius: 12px; margin-bottom: 20px;
cursor: pointer; transition: background 0.2s, transform 0.2s;
}
.location-card:hover { background: #F3F4F6; }
.location-card:active { transform: scale(0.99); }
.location-icon {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--secondary));
border-radius: 12px; display: flex; align-items: center; justify-content: center;
margin-right: 15px;
}
.location-icon span { font-size: 24px; color: white; }
.location-content { flex: 1; }
.location-title { font-weight: 600; color: var(--dark); margin-bottom: 5px; }
.location-status { color: #6B7280; font-size: 14px; }
.btn-primary {
width: 100%; padding: 18px;
background: linear-gradient(135deg, var(--primary), var(--secondary));
color: white; border: none; border-radius: 12px; font-size: 18px; font-weight: 600;
cursor: pointer; transition: transform 0.2s; display: flex;
align-items: center; justify-content: center; gap: 10px;
}
.btn-primary:hover { transform: translateY(-2px); }
.btn-primary:active { transform: scale(0.98); }
.channels-status { display: flex; justify-content: center; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.channel-badge {
padding: 8px 16px; border-radius: 20px; font-size: 12px; font-weight: 600;
display: flex; align-items: center; gap: 5px;
}
.channel-badge.active { background: #D1FAE5; color: #065F46; }
.channel-badge.inactive { background: #F3F4F6; color: #6B7280; }
.channel-dot { width: 8px; height: 8px; border-radius: 50%; }
.active .channel-dot { background: #10B981; }
.inactive .channel-dot { background: #9CA3AF; }
.toast {
position: fixed; top: 20px; left: 50%;
transform: translateX(-50%) translateY(-100px);
background: white; padding: 15px 25px; border-radius: 12px;
box-shadow: 0 10px 30px rgba(0,0,0,0.1); font-weight: 600;
opacity: 0; transition: all 0.3s; z-index: 1000;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (max-width: 480px) {
.card { padding: 20px; }
.header { padding: 20px 15px; }
h1 { font-size: 24px; }
}
</style>
</head>
<body>
<div class="container">
<div class="card header">
<div class="icon"><span>🚗</span></div>
<h1>${CONFIG.APP_NAME}</h1>
<p class="subtitle">一键通知车主挪车</p>
</div>
<div class="card">
<div class="input-group">
<textarea id="messageInput" placeholder="请输入留言给车主...(可选)"></textarea>
<div class="tags">
<button class="tag" onclick="setMessage('您的车挡住出口了')">🚗 挡路</button>
<button class="tag" onclick="setMessage('临时停靠,请尽快挪车')">⏱️ 临停</button>
<button class="tag" onclick="setMessage('电话无法接通')">📞 未接</button>
<button class="tag" onclick="setMessage('麻烦尽快挪车,谢谢')">🙏 加急</button>
</div>
</div>
<div class="location-card" onclick="requestLocation()">
<div class="location-icon"><span id="locationIcon">📍</span></div>
<div class="location-content">
<div class="location-title">我的位置</div>
<div class="location-status" id="locationStatus">点击获取位置信息</div>
</div>
</div>
<button id="notifyBtn" class="btn-primary" onclick="sendNotification()">
<span>🔔</span><span>一键通知车主</span>
</button>
<div class="channels-status" id="channelsStatus"></div>
</div>
</div>
<div id="toast" class="toast"></div>
<script>
let userLocation = null;
let notificationId = null;
window.addEventListener('DOMContentLoaded', () => { checkPushChannels(); });
function setMessage(message) { document.getElementById('messageInput').value = message; }
async function requestLocation() {
const statusEl = document.getElementById('locationStatus');
const iconEl = document.getElementById('locationIcon');
statusEl.textContent = '正在获取位置...';
iconEl.textContent = '⏳';
if (!navigator.geolocation) {
statusEl.textContent = '❌ 浏览器不支持定位';
iconEl.textContent = '📍';
return;
}
try {
const position = await new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject, { enableHighAccuracy: true, timeout: 10000, maximumAge: 0 });
});
userLocation = { lat: position.coords.latitude, lng: position.coords.longitude };
statusEl.textContent = '✅ 位置获取成功';
iconEl.textContent = '📍';
} catch (error) {
let msg = '❌ 位置获取失败';
if (error.code === error.PERMISSION_DENIED) msg = '❌ 位置权限被拒绝';
else if (error.code === error.TIMEOUT) msg = '❌ 定位超时';
statusEl.textContent = msg;
iconEl.textContent = '📍';
}
}
async function checkPushChannels() {
try {
const response = await fetch('/api/push-channels');
const data = await response.json();
updateChannelStatus(data.data.channels);
} catch (error) { console.error('渠道检查失败:', error); }
}
function updateChannelStatus(channels) {
const container = document.getElementById('channelsStatus');
let html = '';
if (channels.dingtalk.enabled) {
html += \`<div class="channel-badge \${channels.dingtalk.configured ? 'active' : 'inactive'}" title="\${channels.dingtalk.description}">
<span class="channel-dot"></span><span>\${channels.dingtalk.icon} \${channels.dingtalk.name}</span>
</div>\`;
}
if (channels.wework.enabled) {
html += \`<div class="channel-badge \${channels.wework.configured ? 'active' : 'inactive'}" title="\${channels.wework.description}">
<span class="channel-dot"></span><span>\${channels.wework.icon} \${channels.wework.name}</span>
</div>\`;
}
if (channels.pushplus && channels.pushplus.enabled) {
html += \`<div class="channel-badge \${channels.pushplus.configured ? 'active' : 'inactive'}" title="\${channels.pushplus.description}">
<span class="channel-dot"></span><span>\${channels.pushplus.icon} \${channels.pushplus.name}</span>
</div>\`;
}
container.innerHTML = html;
}
async function sendNotification() {
const btn = document.getElementById('notifyBtn');
const message = document.getElementById('messageInput').value.trim();
const delayed = !userLocation;
btn.disabled = true;
const originalText = btn.innerHTML;
btn.innerHTML = '<span>⏳</span><span>发送中...</span>';
try {
const response = await fetch('/api/notify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: message || '车旁有人等待,请尽快挪车', location: userLocation, delayed: delayed })
});
const data = await response.json();
if (data.success) {
notificationId = data.data.id;
showToast(delayed ? '⏳ 通知将在30秒后发送' : '✅ 通知发送成功', delayed ? 'warning' : 'success');
showSuccessPage(data.data);
} else {
throw new Error(data.error || '发送失败');
}
} catch (error) {
showToast(\`❌ \${error.message}\`, 'error');
btn.disabled = false;
btn.innerHTML = originalText;
}
}
function showSuccessPage(data) {
const container = document.querySelector('.container');
const channelName = (c) => {
if (c.channel === 'dingtalk') return '钉钉';
if (c.channel === 'wework') return '企业微信';
if (c.channel === 'pushplus') return '微信';
return c.channel;
};
const channelsText = data.channels.filter(c => c.success).map(channelName).join('、');
container.innerHTML = \`
<div class="card header">
<div class="icon" style="background: linear-gradient(135deg, #10B981, #059669);"><span>✅</span></div>
<h1>通知已发送!</h1>
<p class="subtitle">车主已收到通知,正在赶来...</p>
</div>
<div class="card">
<div style="text-align: center; padding: 20px;">
<div style="font-size: 48px; margin-bottom: 20px;">🎉</div>
<h2 style="margin-bottom: 10px;">等待车主回应</h2>
<p style="color: #6B7280; margin-bottom: 30px;">已通过\${channelsText}发送通知</p>