diff --git a/class/Normal.php b/class/Normal.php index 90f210e..016aa8d 100644 --- a/class/Normal.php +++ b/class/Normal.php @@ -68,6 +68,10 @@ private static function OutputMessage(int $gType, int $bCode) if ($bCode == 0) return 'SqlInsertFail'; else if ($bCode == 1) return 400; else return "数据表内容插入失败"; + else if ($gType == 301) + if ($bCode == 0) return 'SqlSelectFail'; + else if ($bCode == 1) return 400; + else return "数据表内容查询失败"; else if ($gType == 310) if ($bCode == 0) return 'TokenTooShort'; else if ($bCode == 1) return 502; diff --git a/class/Sql.php b/class/Sql.php index 91a7ea8..0da4221 100644 --- a/class/Sql.php +++ b/class/Sql.php @@ -27,65 +27,73 @@ public static function MySqlConn() } /** - * MySQL查找库 - * @param string $Mysql_Query + * MySQL查找库 | + * [Tips] 在PHP中,Mysql查询语句一次只允许查询一次数据,不可多个代码进行连续查询 + * @param string $Mysql_Query 输入Mysql查询语句 * @return string[] 查找到结果返回结果 */ public static function SELECT(string $Mysql_Query): array { $CC_i = 0; - $Result = null; $Array_OutPut = []; if (preg_match('/^SELECT/', $Mysql_Query)) { $Result = mysqli_query(self::MySqlConn(), $Mysql_Query); + echo mysqli_error(self::MySqlConn()); for (; $Result_Object = mysqli_fetch_object($Result); $CC_i++) { $Array_OutPut['output'] = 'Success'; $Array_OutPut['data'][$CC_i] = $Result_Object; } if ($CC_i == 0) $Array_OutPut['output'] = 'EmptyResult'; + else mysqli_free_result($Result); } else $Array_OutPut['output'] = 'TypeError'; - mysqli_free_result($Result); + mysqli_close(self::MySqlConn()); return $Array_OutPut; } /** * MySQL插入库 - * @param string $Mysql_Query + * @param string $Mysql_InsertQuery * @return bool */ - public static function INSERT(string $Mysql_Query): bool + public static function INSERT(string $Mysql_InsertQuery): bool { - if (preg_match('/^INSERT/', $Mysql_Query)) - return mysqli_query(self::MySqlConn(), $Mysql_Query); - else + if (preg_match('/^INSERT/', $Mysql_InsertQuery)) + return mysqli_query(self::MySqlConn(), $Mysql_InsertQuery); + else { + mysqli_close(self::MySqlConn()); return false; + } } /** * MySQL更新库 - * @param string $Mysql_Query + * @param string $Mysql_UpdateQuery * @return bool */ - public static function UPDATE(string $Mysql_Query): bool + public static function UPDATE(string $Mysql_UpdateQuery): bool { - if (preg_match('/^UPDATE/', $Mysql_Query)) - return mysqli_query(self::MySqlConn(), $Mysql_Query); - else + if (preg_match('/^UPDATE/', $Mysql_UpdateQuery)) + return mysqli_query(self::MySqlConn(), $Mysql_UpdateQuery); + else { + mysqli_close(self::MySqlConn()); return false; + } } /** * MySQL删除库 - * @param string $Mysql_Query + * @param string $Mysql_DeleteQuery * @return bool */ - public static function DELETE(string $Mysql_Query): bool + public static function DELETE(string $Mysql_DeleteQuery): bool { - if (preg_match('/^DELETE/', $Mysql_Query)) - return mysqli_query(self::MySqlConn(), $Mysql_Query); - else + if (preg_match('/^DELETE/', $Mysql_DeleteQuery)) + return mysqli_query(self::MySqlConn(), $Mysql_DeleteQuery); + else { + mysqli_close(self::MySqlConn()); return false; + } } } \ No newline at end of file diff --git a/mastermind/OutputMessage.md b/mastermind/OutputMessage.md index 6d7c2fb..d7aeb74 100644 --- a/mastermind/OutputMessage.md +++ b/mastermind/OutputMessage.md @@ -6,6 +6,7 @@ # OutputMessage 标准对照表 | 200 | Success | 200 | 操作成功 | | 201 | SuccessButEmail | 200 | 操作成功但邮件发送失败 | | 300 | SqlInsertFail | 400 | 数据表内容插入失败 | +| 301 | SqlSelectFail | 400 | 数据表内容查询失败 | | 310 | TokenTooShort | 502 | Token长度过短 | | 311 | TokenTooLong | 502 | Token长度过长 | | 400 | usernameFormat | 405 | 用户名格式不符合 (格式允许0-9,A-Z,a-z及_) | diff --git a/public/api/auth/register/index.php b/public/api/auth/register/index.php index 9b354d2..4c0968c 100644 --- a/public/api/auth/register/index.php +++ b/public/api/auth/register/index.php @@ -41,19 +41,22 @@ // 创建用户 if (Sql::INSERT("INSERT INTO `index`.xf_user (`username`,`email`,`password`,`reg_time`,`reg_ip`) VALUES ('{$PostData['username']}','{$PostData['email']}','{$PostData['password']}','" . time() . "','" . $_SERVER['REMOTE_ADDR'] . "')")) { // 生成激活码 - $Data_Captcha = Key::Captcha(100); + $Data_Captcha = Key::Captcha(50); $Data_NowTime = time(); // 查找是否需要重新生成激活码 - $AResult_UserEmailVerify = Sql::SELECT("SELECT * FROM `index`.xf_email_verify WHERE `uid`={$PostData['username']} AND `time` >= $Data_NowTime-{$Array_ConfigData['Mail']['ExpDate']}"); - if ($AResult_UserEmailVerify['output'] == "EmptyResult") { - // 创建激活码 - if (Sql::INSERT("INSERT INTO `index`.xf_email_verify (`uid`, `code`, `time`) VALUES ('{$PostData['username']}','$Data_Captcha','$Data_NowTime')")) { - // 邮件发送 - if ($ClassMailer->PostMail($PostData['email'], 1, $Data_Captcha)) - Normal::Output(200); - else Normal::Output(201); - } else Normal::Output(300); - } else Normal::Output(500); + $AResult_UserData = Sql::SELECT("SELECT * FROM `index`.xf_user WHERE `username`='{$PostData['username']}' OR `email`='{$PostData['email']}'"); + if ($AResult_UserData['output'] == "Success") { + $AResult_UserEmailVerify = Sql::SELECT("SELECT * FROM `index`.xf_email_verify WHERE `uid`='{$AResult_UserData['data'][0]->uid}' AND `time` >= " . ($Data_NowTime - $Array_ConfigData['Mail']['ExpDate'])); + if ($AResult_UserEmailVerify['output'] == "EmptyResult") { + // 创建激活码 + if (Sql::INSERT("INSERT INTO `index`.xf_email_verify (`uid`, `code`, `time`) VALUES ('{$AResult_UserData['data'][0]->uid}','$Data_Captcha','$Data_NowTime')")) { + // 邮件发送 + if ($ClassMailer->PostMail($PostData['email'], 1, $Data_Captcha)) + Normal::Output(200); + else Normal::Output(201); + } else Normal::Output(300); + } else Normal::Output(500); + } else Normal::Output(301); } else Normal::Output(300); } else Normal::Output(600); } else Normal::Output(401); @@ -86,7 +89,9 @@ ], ]; } + echo json_encode($Json_Data, JSON_UNESCAPED_UNICODE); } } else { Normal::Output(100); -} \ No newline at end of file +} +End: \ No newline at end of file