@@ -167,22 +167,22 @@ public function init($token): bool
167167 }
168168
169169 /**
170- * 会员注册
170+ * 会员注册,可使用关键词参数方式调用:$auth->register('u18888888888', email: '[email protected] ') 171171 * @param string $username
172172 * @param string $password
173173 * @param string $mobile
174174 * @param string $email
175- * @param int $group
176- * @param array $extend
175+ * @param int $group 会员分组 ID 号
176+ * @param array $extend 扩展数据,如 ['status' => 'disable']
177177 * @return bool
178178 */
179- public function register (string $ username , string $ password , string $ mobile = '' , string $ email = '' , int $ group = 1 , array $ extend = []): bool
179+ public function register (string $ username , string $ password = '' , string $ mobile = '' , string $ email = '' , int $ group = 1 , array $ extend = []): bool
180180 {
181181 $ validate = Validate::rule ([
182- 'mobile ' => 'mobile |unique:user ' ,
183- 'email ' => 'email |unique:user ' ,
184- 'username ' => 'regex:^[a-zA-Z][a-zA-Z0-9_]{2,15}$|unique:user ' ,
185- 'password ' => 'regex:^(?!.*[&<>" \'\n\r]).{6,32}$ ' ,
182+ 'email| ' . __ ( ' Email ' ) => 'email |unique:user ' ,
183+ 'mobile| ' . __ ( ' Mobile ' ) => 'mobile |unique:user ' ,
184+ 'username| ' . __ ( ' Username ' ) => 'require| regex:^[a-zA-Z][a-zA-Z0-9_]{2,15}$|unique:user ' ,
185+ 'password| ' . __ ( ' Password ' ) => 'regex:^(?!.*[&<>" \'\n\r]).{6,32}$ ' ,
186186 ]);
187187 $ params = [
188188 'username ' => $ username ,
@@ -191,26 +191,39 @@ public function register(string $username, string $password, string $mobile = ''
191191 'email ' => $ email ,
192192 ];
193193 if (!$ validate ->check ($ params )) {
194- $ this ->setError (' Registration parameter error ' );
194+ $ this ->setError ($ validate -> getError () );
195195 return false ;
196196 }
197197
198+ // 按需生成随机密码
199+ if (!$ password ) {
200+ $ password = Random::build ();
201+ }
202+
203+ // 用户昵称
204+ $ nickname = preg_replace_callback ('/1[3-9]\d{9}/ ' , function ($ matches ) {
205+ // 对 username 中出现的所有手机号进行脱敏处理
206+ $ mobile = $ matches [0 ];
207+ return substr ($ mobile , 0 , 3 ) . '**** ' . substr ($ mobile , 7 );
208+ }, $ username );
209+
198210 $ ip = request ()->ip ();
199211 $ time = time ();
200212 $ salt = Random::build ('alnum ' , 16 );
201213 $ data = [
202214 'password ' => encrypt_password ($ password , $ salt ),
203215 'group_id ' => $ group ,
204- 'nickname ' => preg_match ( " /^1[3-9]\d{9}$/ " , $ username ) ? substr_replace ( $ username , ' **** ' , 3 , 4 ) : $ username ,
216+ 'nickname ' => $ nickname ,
205217 'join_ip ' => $ ip ,
206218 'join_time ' => $ time ,
207219 'last_login_ip ' => $ ip ,
208220 'last_login_time ' => $ time ,
209221 'salt ' => $ salt ,
210- 'status ' => 'enable ' ,
222+ 'status ' => 'enable ' , // 状态:enable=启用,disable=禁用,使用 string 存储可以自定义其他状态
211223 ];
212224 $ data = array_merge ($ params , $ data );
213225 $ data = array_merge ($ data , $ extend );
226+
214227 Db::startTrans ();
215228 try {
216229 $ this ->model = User::create ($ data );
0 commit comments