登录注册系统

代码:

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
import io
from urllib.parse import unquote

import qrcode
from flask import Flask, Response, request

app = Flask(__name__)
app.config["DEBUG"] = True
max_ticket = 0 # 目前最大ticket(登录请求ID)
tickets = {} # ticket(登录请求ID)状态信息
tickets_scan = {} # ticket(登录请求ID)扫描状态信息

max_uid = 0 # 目前最大uid
uinfo = {} # 用户信息
uname = [] # 用户名称


def apply_if(condition, decorators):
def decorator(func):
# 先应用所有有条件的装饰器
result = func
if condition:
for dec in decorators:
result = dec(result)
return result

return decorator


def is_localhost():
host = request.host.lower()

# 黑名单 - 绝对的本地环境
blacklist = [
"127.0.0.1:5000",
"localhost:5000",
"0.0.0.0:5000",
]

if host in blacklist:
return True

# 白名单 - 绝对的生产环境
whitelist = [
"pythonanywhere.com",
"herokuapp.com",
"vercel.app",
"netlify.app",
"render.com",
]

# 检查白名单
for production_domain in whitelist:
if host.endswith(production_domain):
return False

# 多条件判断 - 本地环境特征
local_patterns = [
host.startswith("127."), # 127.x.x.x
host.startswith("localhost"), # localhost
host.startswith("192.168."), # 局域网
host.startswith("10."), # 内网
host.startswith("172."), # 内网
":" in host
and not any(
host.endswith(domain) for domain in whitelist
), # 有端口且不在白名单
host.endswith(".local"), # .local 域名
host.endswith(".test"), # .test 域名
host.endswith(".localhost"), # .localhost 域名
]

return any(local_patterns)


def get_host():
return f"http{"" if is_localhost() else "s"}://{request.host}"


def error_page(code: int, msg: str):
return {"status": str(code), "msg": msg, "data": {}}, code


# -----主页-----
@app.route("/", methods=["GET"])
def homepage():
return f"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Login Demo - 扫码登录测试平台</title>
<style>
* {{
margin: 0;
padding: 0;
box-sizing: border-box;
}}

body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
background: linear-gradient(135deg, #2c3e50 0%, #3498db 50%, #1abc9c 100%);
min-height: 100vh;
}}

.container {{
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}}

.header {{
text-align: center;
margin-bottom: 3rem;
color: white;
}}

.header h1 {{
font-size: 3rem;
margin-bottom: 1rem;
text-shadow: 0 2px 10px rgba(0,0,0,0.3);
}}

.header p {{
font-size: 1.2rem;
opacity: 0.9;
}}

.card-grid {{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
gap: 2rem;
margin-bottom: 3rem;
}}

.card {{
background: white;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
border: 1px solid #e3f2fd;
}}

.card:hover {{
transform: translateY(-3px);
box-shadow: 0 12px 35px rgba(0,0,0,0.15);
}}

.card h2 {{
color: #2c3e50;
margin-bottom: 1rem;
font-size: 1.5rem;
border-left: 4px solid #3498db;
padding-left: 1rem;
}}

.card h3 {{
color: #2980b9;
margin: 1.5rem 0 0.5rem 0;
font-size: 1.1rem;
}}

.feature-list {{
list-style: none;
margin: 1rem 0;
}}

.feature-list li {{
padding: 0.5rem 0;
border-bottom: 1px solid #ecf0f1;
color: #2c3e50;
}}

.feature-list li:before {{
content: "🔹 ";
color: #3498db;
margin-right: 0.5rem;
}}

.btn {{
display: inline-block;
background: linear-gradient(135deg, #3498db, #2980b9);
color: white;
padding: 12px 30px;
border-radius: 8px;
text-decoration: none;
font-weight: 600;
margin: 0.5rem;
transition: all 0.3s ease;
border: none;
cursor: pointer;
font-size: 0.95rem;
}}

.btn:hover {{
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4);
background: linear-gradient(135deg, #2980b9, #3498db);
}}

.btn-outline {{
background: transparent;
border: 2px solid white;
color: white;
}}

.btn-outline:hover {{
background: white;
color: #3498db;
}}

.btn-large {{
padding: 15px 40px;
font-size: 1.1rem;
}}

.btn-success {{
background: linear-gradient(135deg, #27ae60, #2ecc71);
}}

.btn-success:hover {{
background: linear-gradient(135deg, #2ecc71, #27ae60);
box-shadow: 0 5px 15px rgba(46, 204, 113, 0.4);
}}

.code-block {{
background: #f8fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 1.2rem;
margin: 1rem 0;
overflow-x: auto;
font-family: 'SF Mono', 'Monaco', 'Menlo', monospace;
font-size: 0.9rem;
line-height: 1.4;
color: #2d3748;
}}

.endpoint {{
background: #edf2f7;
padding: 0.3rem 0.6rem;
border-radius: 6px;
font-family: 'SF Mono', monospace;
margin: 0.2rem 0;
display: inline-block;
color: #2c5282;
border: 1px solid #cbd5e0;
font-size: 0.85rem;
}}

.footer {{
text-align: center;
color: white;
margin-top: 3rem;
opacity: 0.9;
}}

.alert {{
background: #d1ecf1;
border: 1px solid #bee5eb;
border-radius: 8px;
padding: 1.2rem;
margin: 1.5rem 0;
color: #0c5460;
border-left: 4px solid #17a2b8;
}}

.badge {{
display: inline-block;
background: #e74c3c;
color: white;
padding: 0.3rem 0.8rem;
border-radius: 20px;
font-size: 0.8rem;
margin-left: 0.5rem;
font-weight: bold;
}}

.tech-stack {{
display: flex;
justify-content: center;
gap: 1rem;
margin: 1.5rem 0;
flex-wrap: wrap;
}}

.tech-item {{
background: rgba(255,255,255,0.1);
padding: 0.5rem 1rem;
border-radius: 20px;
color: white;
font-size: 0.9rem;
backdrop-filter: blur(10px);
}}

@media (max-width: 768px) {{
.header h1 {{
font-size: 2.2rem;
}}

.card-grid {{
grid-template-columns: 1fr;
}}

.container {{
padding: 1rem;
}}
}}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🔐 QR Login Demo</h1>
<p>专业的扫码登录测试平台 - 为开发者提供完整的测试环境</p>

<div class="tech-stack">
<div class="tech-item">Python Flask</div>
<div class="tech-item">RESTful API</div>
<div class="tech-item">移动端友好</div>
<div class="tech-item">零成本部署</div>
</div>

<div style="margin-top: 2rem; display:flex; justify-content:center; gap:12px; flex-wrap:wrap;">
<a href="./reg/" class="btn btn-large btn-success">🚀 注册</a>
<a href="./user/info/" class="btn btn-large btn-outline">🔎 查询用户</a>
<a href="#api-docs" class="btn btn-large btn-outline">📖 API 文档</a>
</div>
</div>

<div class="alert">
<strong>💡 重要提示:</strong> 这是一个完全开放的公共测试服务,所有数据在服务重启后自动重置,请放心进行各种测试!
</div>

<div class="card-grid">
<div class="card">
<h2>🎯 核心特性</h2>
<ul class="feature-list">
<li>完整的扫码登录业务流程</li>
<li>符合 RESTful 设计的 API 接口</li>
<li>移动端优化的响应式界面</li>
<li>零配置,开箱即用</li>
<li>自动数据清理(重启重置)</li>
<li>100% 免费使用 <span class="badge">FREE</span></li>
</ul>
</div>

<div class="card">
<h2>⚡ 快速开始</h2>
<ol style="margin-left: 1.5rem; margin-top: 1rem; color: #2c3e50;">
<li style="margin-bottom: 0.8rem;">访问 <a href="./reg/" style="color: #3498db; text-decoration: none; font-weight: 500;">注册页面</a> 创建测试账号</li>
<li style="margin-bottom: 0.8rem;">调用 <span class="endpoint">GET /api/login/qr/</span> 获取二维码</li>
<li style="margin-bottom: 0.8rem;">使用手机扫码并选择登录账号</li>
<li>轮询 <span class="endpoint">GET /api/login/status/&lt;ticket&gt;/</span> 获取登录状态</li>
</ol>

<div style="margin-top: 1.5rem; text-align: center;">
<a href="./reg/" class="btn">📝 注册测试账号</a>
<a href="./api/login/qr/" class="btn">🔐 获取登录二维码</a>
</div>
</div>
</div>

<div class="card" id="api-docs">
<h2>📚 API 文档</h2>

<div class="endpoint" id="base-url"></div>
<p style="margin-bottom: 1rem;">基础 URL</p>

<h3>🔐 登录认证</h3>
<div class="endpoint">GET /api/login/qr/</div>
<p style="margin-bottom: 1rem;">获取登录二维码和 ticket</p>

<div class="endpoint">GET /api/login/status/&lt;ticket&gt;/</div>
<p style="margin-bottom: 1rem;">检查登录状态</p>

<h3>👤 用户管理</h3>
<div class="endpoint">POST /api/reg/&lt;username&gt;/</div>
<p style="margin-bottom: 1rem;">注册新用户</p>

<div class="endpoint">GET /api/user/info/&lt;userid&gt;/</div>
<p style="margin-bottom: 1rem;">获取用户信息</p>

<div class="endpoint">POST /api/user/editdesc/&lt;userid&gt;/&lt;new_desc&gt;/</div>
<p style="margin-bottom: 1rem;">修改用户描述</p>

<div class="endpoint">GET /api/reg/havename/&lt;name&gt;/</div>
<p style="margin-bottom: 1rem;">昵称是否存在</p>

<div class="endpoint">POST/DELETE /api/user/del/&lt;userid&gt;/</div>
<p style="margin-bottom: 1rem;">删除用户</p>

<h3>🌐 页面端点</h3>
<div class="endpoint">GET /reg/</div>
<p style="margin-bottom: 1rem;">用户注册页面</p>

<div class="endpoint">GET /login/&lt;ticket&gt;/</div>
<p style="margin-bottom: 1rem;">扫码登录确认页面</p>

<div class="endpoint">GET /user/editdesc/</div>
<p style="margin-bottom: 1rem;">编辑用户简介页面</p>

<div class="endpoint">GET /user/editdesc/ok/&lt;userid&gt;/&lt;newdata&gt;/</div>
<p style="margin-bottom: 1rem;">编辑用户简介完成页面</p>

<div class="endpoint">GET /user/del/</div>
<p style="margin-bottom: 1rem;">删除用户页面</p>

<div class="endpoint">GET /user/del/ok/&lt;userid&gt;/</div>
<p style="margin-bottom: 1rem;">删除用户完成页面</p>

<div class="endpoint">GET /user/info/</div>
<p style="margin-bottom: 1rem;">查询用户信息页面</p>

<div class="endpoint">GET /user/info/&lt;userid&gt;/</div>
<p style="margin-bottom: 1rem;">查询用户信息结果页面</p>

<h3>📦其他</h3>
<div class="endpoint">GET /createqr/&lt;data&gt;/</div>
<p style="margin-bottom: 1rem;">生成二维码</p>
</div>

<div class="footer">
<p>💙 由 Flask + 热爱驱动 | 🔄 服务重启自动重置 | 🆓 100% 免费服务</p>
</div>
</div>

<script>
if (!window.location.href.endsWith("/")) {{
window.location.href = "./lt/"
}}
document.getElementById('base-url').textContent = window.location.href;
</script>
</body>
</html>
"""


# -----登录QR码页面-----
@app.route("/login/<int:ticket>", methods=["GET"])
def login(ticket: int):
if str(ticket) not in tickets.keys():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(ticket:{ticket})</title>
</head>
<body>
<p>Error: ticket 不存在<p>
<h5>ticket:{ticket}</h5>
</body>
"""

if tickets[str(ticket)] != "Created":
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(ticket:{ticket})</title>
</head>
<body>
<p>Error: ticket 已被扫描!({tickets[str(ticket)].split("+")[0]})<p>
<h5>ticket:{ticket}</h5>
</body>
"""

tickets[str(ticket)] = "Scanned"
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login(ticket:{ticket})</title>
</head>
<body>
<input id="loginuid" placeholder="输入要登录账号的ID">
<button type="button" id="ok">确认</button>
<button type="button" id=cancel>取消</button>
<h5>ticket:{ticket}</h5>
<script>
document.getElementById('ok').addEventListener('click', function() {{
const luid = document.getElementById('loginuid').value;
const list = {list(uinfo.keys())};
if (list.includes(luid)) {{
window.location.href = `./ok/{ticket}/${{luid}}`;
}} else {{
alert(`${{luid}}账号不存在呢~`);
}}
}})
document.getElementById('cancel').addEventListener('click', function() {{
window.location.href = `./cancel/{ticket}`;
}})
</script>
</body>
"""


@app.route("/login/ok/<int:ticket>/<int:loginuserid>", methods=["GET"])
def login_ok(ticket: int, loginuserid: int):
if str(ticket) not in tickets.keys():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(ticket:{ticket})</title>
</head>
<body>
<p>Error: ticket 不存在<p>
<h5>ticket:{ticket}</h5>
</body>
"""

if tickets[str(ticket)] != "Created":
if not (tickets[str(ticket)] == "Scanned" and not tickets_scan[str(ticket)]):
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(ticket:{ticket})</title>
</head>
<body>
<p>Error: ticket 已被扫描!({tickets[str(ticket)].split("+")[0]})<p>
<h5>ticket:{ticket}</h5>
</body>
"""

tickets[str(ticket)] = f"Verifed+{loginuserid}"
tickets_scan[str(ticket)] = True
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OK(ticket:{ticket})</title>
</head>
<body>
<p>已确认(账号:{uinfo[str(loginuserid)]['name']})<p>
<h5>ticket:{ticket}</h5>
</body>
"""


@app.route("/login/cancel/<int:ticket>", methods=["GET"])
def login_cancel(ticket: int):
if str(ticket) not in tickets.keys():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(ticket:{ticket})</title>
</head>
<body>
<p>Error: ticket 不存在<p>
<h5>ticket:{ticket}</h5>
</body>
"""

if tickets[str(ticket)] != "Created":
if not (tickets[str(ticket)] == "Scanned" and not tickets_scan[str(ticket)]):
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(ticket:{ticket})</title>
</head>
<body>
<p>Error: ticket 已被扫描!({tickets[str(ticket)].split("+")[0]})<p>
<h5>ticket:{ticket}</h5>
</body>
"""

tickets[str(ticket)] = "Canceled"
tickets_scan[str(ticket)] = True
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cancel(ticket:{ticket})</title>
</head>
<body>
<p>已取消<p>
<h5>ticket:{ticket}</h5>
</body>
"""


# -----登录API-----
@app.route("/api/login/qr/", methods=["GET"])
def login_qr():
global max_ticket
max_ticket += 1
tickets[str(max_ticket)] = "Created"
tickets_scan[str(max_ticket)] = False
return {"status": "200", "msg": "OK", "data": {"ticket": max_ticket}}


@app.route("/api/login/status/<int:ticket>", methods=["GET"])
def login_status(ticket: int):
if str(ticket) not in tickets.keys():
return error_page(404, "ticket 不存在")
return {
"status": "200",
"msg": "OK",
"data": {
"ticket": ticket,
"status": tickets[str(ticket)].split("+")[0],
"loginuid": (
tickets[str(ticket)].split("+")[1]
if "+" in tickets[str(ticket)]
else ""
),
},
}


# -----注册页面-----
@app.route("/reg/", methods=["GET"])
def reg():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reg(userid:{max_uid+1})</title>
</head>
<body>
<p>输入用户名</p><input id="reg_name" placeholder="输入用户名~"><br>
<button type="button"id="regBtn">注册</button>
<h5>userid:{max_uid+1}</h5>
<script>
function isallchar(str, char) {{
const allSame = new Set(str).size === 1;
return allSame && str[0] === char;
}}

document.getElementById('regBtn').addEventListener('click', function() {{
const uname = document.getElementById('reg_name').value;
const list = {uname};
if (list.includes(uname)) {{
alert(`${{uname}}已被注册!换一个吧~`);
}} else {{
if (uname == "" || isallchar(uname, " ")) {{
alert("用户名不能为空哦~")
}}
else {{
window.location.href = `./ok/${{uname}}`;
}}
}}
}})
</script>
</body>
"""


@app.route("/reg/ok/<string:name>", methods=["GET"])
def reg_ok(name: str):
if name in uname:
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(用户名已存在)</title>
</head>
<body>
<p>Error: 用户名已存在<p>
</body>
"""

if "<script>" in name or "</script>" in name:
return """
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(XSS防护)</title>
</head>
<body>
<p>Error: 用户名包含禁止的脚本标签<p>
</body>
"""

global max_uid
max_uid += 1
uinfo[str(max_uid)] = {"name": name, "desc": ""}
uname.append(name)
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OK(userid:{max_uid})</title>
</head>
<body>
<h2>你的账号已成功注册!(NAME={name})</h2>
<h5>userid:{max_uid}</h5>
</body>
"""


# ------注册API-----
@app.route("/api/reg/havename/<string:name>", methods=["GET"])
def havename(name: str):
return {
"status": "200",
"msg": "OK",
"data": {"name": name, "ishave": name in uname},
}


@app.route("/api/reg/<string:name>", defaults={"desc": ""}, methods=["POST"])
@app.route("/api/reg/<string:name>/<string:desc>", methods=["POST"])
def reg_api(name: str, desc: str):
if name in uname:
return error_page(409, "用户名已存在")
if name == "" or name.count(" ") == len(name):
return error_page(422, "用户名不能为空")
if ("<script>" in name or "</script>" in name) or (
"<script>" in desc or "</script>" in desc
):
return error_page(422, "用户名或简介包含禁止的脚本标签")
global max_uid
max_uid += 1
uinfo[str(max_uid)] = {"name": name, "desc": desc}
uname.append(name)
return {
"status": "200",
"msg": "OK",
"data": {"name": name, "desc": desc, "uid": max_uid},
}


# -----用户信息获取-----
@app.route("/api/user/info/<int:userid>", methods=["GET"])
def userinfo(userid: int):
if str(userid) not in uinfo.keys():
return error_page(404, "用户不存在")
return {"status": "200", "msg": "OK", "data": uinfo[str(userid)]}


# -----用户信息查询页面-----
@app.route("/user/info/", methods=["GET"])
def user_info_page():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>查询用户信息</title>
</head>
<body>
<p>输入要查询的用户ID</p><input id="info_uid" placeholder="输入用户ID~"><br>
<button type="button" id="infoBtn">查询</button>
<script>
document.getElementById('infoBtn').addEventListener('click', function() {{
const uid = document.getElementById('info_uid').value;
const list = {list(uinfo.keys())};
if (list.includes(uid)) {{
window.location.href = `./s/${{uid}}`;
}} else {{
alert(`${{uid}}账号不存在呢~`);
}}
}})
</script>
</body>
"""


@app.route("/user/info/s/<int:userid>", methods=["GET"])
def user_info_show(userid: int):
if str(userid) not in uinfo.keys():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(info:{userid})</title>
</head>
<body>
<p>Error: 用户不存在<p>
<h5>userid:{userid}</h5>
</body>
"""

data = uinfo[str(userid)]
name = data.get("name", "")
desc = data.get("desc", "")
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户信息(UID:{userid})</title>
</head>
<body>
<h3>用户名: {name}</h3>
<p>简介: {desc}</p>
</body>
"""


# -----用户简介编辑页面-----
@app.route("/user/editdesc/", methods=["GET"])
def userinfo_edit_page():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑用户简介</title>
</head>
<body>
<p>输入用户ID</p><input id="edit_uid" placeholder="输入用户ID~"><br>
<p>输入新简介</p><input id="edit_desc" placeholder="输入新简介~"><br>
<button type="button" id="editBtn">提交</button>
<script>
console.log("loaded");
document.getElementById('editBtn').addEventListener('click', function() {{
console.log("clicked");
const uid = document.getElementById('edit_uid').value;
const newdesc = document.getElementById('edit_desc').value;
const list = {list(uinfo.keys())};
if (list.includes(uid)) {{
window.location.href = `./ok/${{uid}}/${{newdesc}}`;
}} else {{
alert(`${{uid}}账号不存在呢~`);
}}
}})
</script>
</body>
"""


@app.route("/user/editdesc/ok/<int:userid>/<string:newdata>", methods=["GET"])
def userinfo_edit_page_ok(userid: int, newdata: str):
if str(userid) not in uinfo.keys():
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(编辑简介用户UID:{userid})</title>
</head>
<body>
<p>Error: 用户不存在<p>
<h5>userid:{userid}</h5>
</body>
"""

if "<script>" in newdata or "</script>" in newdata:
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(edit:{userid})</title>
</head>
<body>
<p>Error: 提交的简介包含禁止脚本标签</p>
<h5>userid:{userid}</h5>
</body>
"""

uinfo[str(userid)]["desc"] = newdata
return f"""
<head>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑用户简介完成(UID:{userid})</title>
</head>
<body>
<p>用户ID:{userid}的简介已更新为:{newdata}</p>
</body>
"""


# -----删除用户页面-----
@app.route("/user/del/", methods=["GET"])
def user_del_page():
return f"""
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>删除用户</title>
</head>
<body>
<p>输入要删除的用户ID</p><input id="del_uid" placeholder="输入用户ID~"><br>
<button type="button" id="delBtn">删除</button>
<script>
document.getElementById('delBtn').addEventListener('click', function() {{
const uid = document.getElementById('del_uid').value;
const list = {list(uinfo.keys())};
if (list.includes(uid)) {{
if (confirm(`确认要删除用户ID:${{uid}} 吗?`)) {{
window.location.href = `./ok/${{uid}}`;
}} else {{
alert('已取消删除');
}}
}} else {{
alert(`${{uid}}账号不存在呢~`);
}}
}})
</script>
</body>
"""


@app.route("/user/del/ok/<int:userid>", methods=["GET"])
def user_del_ok(userid: int):
if str(userid) not in uinfo.keys():
return f"""
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error(删除用户UID:{userid})</title>
</head>
<body>
<p>Error: 用户不存在<p>
<h5>userid:{userid}</h5>
</body>
"""

del uname[uname.index(uinfo[str(userid)]["name"])]
del uinfo[str(userid)]

return f"""
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>删除完成(UID:{userid})</title>
</head>
<body>
<p>用户ID:{userid} 已成功删除。</p>
</body>
"""


# -----用户简介编辑API-----
@app.route("/api/user/editdesc/<int:userid>/<string:newdata>", methods=["POST", "PUT"])
def userinfo_edit(userid: int, newdata: str):
if str(userid) not in uinfo.keys():
return error_page(404, "用户不存在")
# XSS 防护
if "<script>" in newdata or "</script>" in newdata:
return error_page(422, "提交的简介包含禁止脚本标签")
uinfo[str(userid)]["desc"] = newdata
return {
"status": "200",
"msg": "OK",
"data": {"uid": userid, "name": uinfo[str(userid)]["name"], "desc": newdata},
}


# -----删除用户API-----
@app.route("/api/user/del/<int:userid>", methods=["POST", "DELETE"])
def deluser_api(userid: int):
if str(userid) not in uinfo.keys():
return error_page(404, "用户不存在")
del uname[uname.index(uinfo[str(userid)]["name"])]
del uinfo[str(userid)]
return {"status": "200", "msg": "OK"}


# -----创建QR-----
@app.route("/createqr/<path:data>", methods=["GET"])
def create_qr(data: str):
qr_url = unquote(data)

qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(qr_url)
qr.make(fit=True)

qc = qr.make_image(fill_color="black", back_color="white")

img_buffer = io.BytesIO()
qc.save(img_buffer, format="PNG")
img_buffer.seek(0)

return Response(img_buffer.getvalue(), mimetype="image/png")


if __name__ == "__main__":
app.run("127.0.0.1", 5000, True)

API:

🔐 QR Login Demo - 扫码登录测试平台


登录注册系统
https://qiufengcute.github.io/Blog/posts/登录注册/
作者
Qiufeng
发布于
2026年3月2日
许可协议