1- from typing import Any , List
1+ import json
2+ from typing import Any , Dict , List
23
3- from quark import Request , Resource , models
4- from quark .app import actions , searches
4+ from tortoise import Model
5+
6+ from quark import Message , Request , Resource , models , utils
7+ from quark .app import actions
58from quark .component .form import field
9+ from quark .component .form .rule import Rule
10+ from quark .services .auth import AuthService
11+ from quark .services .user import UserService
612
713
814class Account (Resource ):
@@ -20,32 +26,75 @@ async def init(self, request: Request):
2026
2127 return self
2228
29+ # 字段
2330 async def fields (self , request : Request ) -> List [Any ]:
24- """字段定义"""
25- return [
26- field .id ("id" , "ID" ),
27- field .text ("nickname" , "昵称" ),
28- field .text ("username" , "用户名" ),
29- field .password ("password" , "密码" ).only_on_forms (),
30- field .text ("email" , "邮箱" ).set_editable (True ),
31- field .text ("phone" , "手机号" ),
32- ]
3331
34- async def searches (self , request : Request ) -> List [Any ]:
35- """搜索项定义"""
3632 return [
37- searches .Input ("username" , "用户名" ),
33+ field .image ("avatar" , "头像" ),
34+ field .text ("nickname" , "昵称" ).set_rules ([Rule .required ("昵称必须填写" )]),
35+ field .text ("email" , "邮箱" ).set_rules ([Rule .required ("邮箱必须填写" )]),
36+ field .text ("phone" , "手机号" ).set_rules ([Rule .required ("手机号必须填写" )]),
37+ field .radio ("sex" , "性别" )
38+ .set_options (
39+ [
40+ field .radio_option ("男" , 1 ),
41+ field .radio_option ("女" , 2 ),
42+ ]
43+ )
44+ .set_default_value (1 ),
45+ field .password ("password" , "密码" )
46+ .set_rules (
47+ [
48+ Rule .regexp (r"/^.{6,}$/" , "密码不少于六位" ),
49+ Rule .regexp (r"/.*[A-Z].*/" , "至少包含一个大写字母" ),
50+ Rule .regexp (r"/.*[a-z].*/" , "至少包含一个小写字母" ),
51+ Rule .regexp (r"/.*[0-9].*/" , "至少包含一个数字" ),
52+ Rule .regexp (
53+ r"/.*[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-].*/" ,
54+ "至少包含一个特殊字符" ,
55+ ),
56+ ]
57+ )
58+ .set_help (
59+ "密码不少于六位,且至少包含一个大写字母、小写字母、数字和特殊字符"
60+ ),
3861 ]
3962
63+ # 行为
4064 async def actions (self , request : Request ) -> List [Any ]:
41- """行为定义"""
4265 return [
43- actions .CreateLink (self .title ),
44- actions .BatchDelete (),
45- actions .EditLink (),
46- actions .DeleteSpecial (),
4766 actions .FormSubmit (),
4867 actions .FormReset (),
4968 actions .FormBack (),
5069 actions .FormExtraBack (),
5170 ]
71+
72+ # 表单显示前回调
73+ async def before_form_showing (self , request : Request ) -> Any :
74+ admin_info = await AuthService (request ).get_current_admin ()
75+ user = await UserService ().get_info_by_id (admin_info .id )
76+ user_dict = user .__dict__ .copy ()
77+ user_dict .pop ("password" , None ) # 安全地移除密码字段
78+ return user_dict
79+
80+ # 表单提交处理
81+ async def form_handle (
82+ self , request : Request , model : Model , data : Dict [str , Any ]
83+ ) -> Any :
84+ # 头像处理
85+ if data .get ("avatar" ):
86+ data ["avatar" ] = json .dumps (data ["avatar" ], ensure_ascii = False )
87+
88+ # 加密密码
89+ if data .get ("password" ):
90+ data ["password" ] = utils .hash_password (data ["password" ])
91+
92+ try :
93+ # 获取登录管理员信息
94+ admin_info = await AuthService (request ).get_current_admin ()
95+ model = await UserService ().get_info_by_id (admin_info .id )
96+ await model .update_from_dict (data )
97+ except Exception as e :
98+ return Message .error (str (e ))
99+
100+ return Message .success ("操作成功" )
0 commit comments