From 92a08c5e6d0c2f6c258fe3f3571c313f07915b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E5=BC=BA?= <357099073@qq.com> Date: Mon, 26 May 2025 17:01:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95=E5=92=8C?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E7=95=8C=E9=9D=A2=E5=8F=8A=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/index/controller/IndexController.php | 85 +++++++- app/index/controller/UserController.php | 237 ++++++++++++++++++++++ app/index/view/user/login.php | 241 +++++++++++++++++++++++ app/index/view/user/register.php | 193 ++++++++++++++++++ composer.json | 3 +- composer.lock | 89 ++++++++- 6 files changed, 842 insertions(+), 6 deletions(-) create mode 100644 app/index/controller/UserController.php create mode 100644 app/index/view/user/login.php create mode 100644 app/index/view/user/register.php diff --git a/app/index/controller/IndexController.php b/app/index/controller/IndexController.php index 31f70bf..993fd9c 100644 --- a/app/index/controller/IndexController.php +++ b/app/index/controller/IndexController.php @@ -13,6 +13,7 @@ use app\index\model\Resources\ResourcesCategory; use app\index\model\Articles\ArticlesCategory; use app\index\model\Resources\Resources; use app\index\model\Articles\Articles; +use PHPMailer\PHPMailer\PHPMailer; class IndexController extends BaseController @@ -27,7 +28,7 @@ class IndexController extends BaseController ->order('sort DESC, id DESC') ->select() ->toArray(); - + View::assign('bannerList', $bannerList); return View::fetch(); } @@ -190,7 +191,7 @@ class IndexController extends BaseController } // 按上传时间排序 - usort($allResources, function($a, $b) { + usort($allResources, function ($a, $b) { return $b['create_time'] - $a['create_time']; }); @@ -256,7 +257,7 @@ class IndexController extends BaseController } // 按上传时间排序 - usort($allPrograms, function($a, $b) { + usort($allPrograms, function ($a, $b) { return $b['create_time'] - $a['create_time']; }); @@ -325,7 +326,7 @@ class IndexController extends BaseController } // 按上传时间排序 - usort($allPrograms, function($a, $b) { + usort($allPrograms, function ($a, $b) { return strtotime($b['create_time']) - strtotime($a['create_time']); }); @@ -341,4 +342,80 @@ class IndexController extends BaseController ] ]); } + + function sendEmail($email, $to, $content, $title) + { + + // 邮件内容 + + //实例化PHPMailer核心类 + $mail = new PHPMailer(); + + //是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式 + $mail->SMTPDebug = 1; + + //使用smtp鉴权方式发送邮件 + $mail->isSMTP(); + + //smtp需要鉴权 这个必须是true + $mail->SMTPAuth = true; + + //链接qq域名邮箱的服务器地址 + $mail->Host = 'smtp.qq.com';//如果是163邮箱 就把qq换成163 + + //设置使用ssl加密方式登录鉴权 + $mail->SMTPSecure = 'ssl'; + + //设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587 + $mail->Port = 465; + + //设置smtp的helo消息头 这个可有可无 内容任意 + // $mail->Helo = 'Hello smtp.qq.com Server'; + + //设置发件人的主机域 可有可无 默认为localhost 内容任意,建议使用你的域名 + $mail->Hostname = $email; + + //设置发送的邮件的编码 可选GB2312 我喜欢utf-8 据说utf8在某些客户端收信下会乱码 + $mail->CharSet = 'UTF-8'; + + //设置发件人姓名(昵称) 任意内容,显示在收件人邮件的发件人邮箱地址前的发件人姓名 + $mail->FromName = '学创网络'; + + //smtp登录的账号 这里填入字符串格式的qq号即可 + $mail->Username = $email; + + //smtp登录的密码 使用qq邮箱生成的授权码(qq邮箱生成授权码参考网址:http://www.huochewu.com/show_1609305/) + $mail->Password = ''; + + //设置发件人邮箱地址 这里填入上述提到的“发件人邮箱” + $mail->From = $email; + + //邮件正文是否为html编码 注意此处是一个方法 不再是属性 true或false + $mail->isHTML(true); + + //设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大 + $mail->addAddress($to, '666'); + + //添加多个收件人 则多次调用方法即可 + // $mail->addAddress('xxx@163.com',''); + + //添加该邮件的主题 + $mail->Subject = $title; + + //添加邮件正文 上方将isHTML设置成了true,则可以是完整的html字符串 如:使用file_get_contents函数读取本地的html文件 + $mail->Body = $content; + + //为该邮件添加附件 该方法也有两个参数 第一个参数为附件存放的目录(相对目录、或绝对目录均可) 第二参数为在邮件附件中该附件的名称 + // $mail->addAttachment('./d.jpg','mm.jpg'); + //同样该方法可以多次调用 上传多个附件 + // $mail->addAttachment('./Jlib-1.1.0.js','Jlib.js'); + $status = $mail->send(); + //简单的判断与提示信息 + if ($status) { + return '发送成功'; + } else { + return '发送失败'; + } + + } } diff --git a/app/index/controller/UserController.php b/app/index/controller/UserController.php new file mode 100644 index 0000000..c183164 --- /dev/null +++ b/app/index/controller/UserController.php @@ -0,0 +1,237 @@ +request->isPost()) { + $data = $this->request->post(); + $type = $data['type'] ?? 'account'; // 注册类型:account-账号密码,phone-手机号,wechat-微信 + + try { + switch ($type) { + case 'account': + // 账号密码注册 + $validate = validate([ + 'username' => 'require|min:3|max:20|unique:user', + 'password' => 'require|min:6|max:20', + 'confirm_password' => 'require|confirm:password' + ], [ + 'username.require' => '用户名不能为空', + 'username.min' => '用户名长度不能小于3个字符', + 'username.max' => '用户名长度不能超过20个字符', + 'username.unique' => '用户名已存在', + 'password.require' => '密码不能为空', + 'password.min' => '密码长度不能小于6个字符', + 'password.max' => '密码长度不能超过20个字符', + 'confirm_password.require' => '确认密码不能为空', + 'confirm_password.confirm' => '两次输入的密码不一致' + ]); + + if (!$validate->check($data)) { + return json(['code' => 0, 'msg' => $validate->getError()]); + } + + $user = new UserModel; + $user->username = $data['username']; + $user->password = password_hash($data['password'], PASSWORD_DEFAULT); + $user->create_time = time(); + $user->save(); + + return json(['code' => 1, 'msg' => '注册成功']); + + case 'phone': + // 手机号注册 + $validate = validate([ + 'phone' => 'require|mobile|unique:user', + 'code' => 'require|number|length:6' + ], [ + 'phone.require' => '手机号不能为空', + 'phone.mobile' => '手机号格式不正确', + 'phone.unique' => '该手机号已注册', + 'code.require' => '验证码不能为空', + 'code.number' => '验证码必须为数字', + 'code.length' => '验证码长度必须为6位' + ]); + + if (!$validate->check($data)) { + return json(['code' => 0, 'msg' => $validate->getError()]); + } + + // 验证短信验证码 + $smsCode = cache('sms_code_' . $data['phone']); + if (!$smsCode || $smsCode != $data['code']) { + return json(['code' => 0, 'msg' => '验证码错误或已过期']); + } + + $user = new UserModel; + $user->phone = $data['phone']; + $user->create_time = time(); + $user->save(); + + // 清除验证码缓存 + cache('sms_code_' . $data['phone'], null); + + return json(['code' => 1, 'msg' => '注册成功']); + + case 'wechat': + // 微信注册 + $validate = validate([ + 'openid' => 'require|unique:user' + ], [ + 'openid.require' => '微信授权失败', + 'openid.unique' => '该微信账号已注册' + ]); + + if (!$validate->check($data)) { + return json(['code' => 0, 'msg' => $validate->getError()]); + } + + $user = new UserModel; + $user->openid = $data['openid']; + $user->nickname = $data['nickname'] ?? ''; + $user->avatar = $data['avatar'] ?? ''; + $user->create_time = time(); + $user->save(); + + return json(['code' => 1, 'msg' => '注册成功']); + + default: + return json(['code' => 0, 'msg' => '注册类型错误']); + } + } catch (\Exception $e) { + return json(['code' => 0, 'msg' => '注册失败:' . $e->getMessage()]); + } + } + + return $this->fetch(); + } + + /** + * 退出登录 + * + * @return \think\Response + */ + public function logout() + { + // 增加日志记录,记录用户退出登录操作 + Log::record('用户退出登录', 'info'); + + // 销毁当前会话中的所有数据 + session(null); + + // 清除缓存中的用户信息 + Cache::tag('user_cache')->clear(); + return redirect('login'); + } + + // 发送短信验证码 + public function sendSmsCode() + { + if ($this->request->isPost()) { + $phone = $this->request->post('phone'); + + // 验证手机号 + $validate = validate([ + 'phone' => 'require|mobile|unique:user' + ], [ + 'phone.require' => '手机号不能为空', + 'phone.mobile' => '手机号格式不正确', + 'phone.unique' => '该手机号已注册' + ]); + + if (!$validate->check(['phone' => $phone])) { + return json(['code' => 0, 'msg' => $validate->getError()]); + } + + // 生成6位随机验证码 + $code = mt_rand(100000, 999999); + + // 这里应该调用短信服务商API发送验证码 + // 示例代码,实际使用时需要替换为真实的短信发送逻辑 + try { + // TODO: 调用短信服务商API发送验证码 + // $result = sendSms($phone, $code); + + // 将验证码保存到缓存,有效期5分钟 + cache('sms_code_' . $phone, $code, 300); + + return json(['code' => 1, 'msg' => '验证码发送成功']); + } catch (\Exception $e) { + return json(['code' => 0, 'msg' => '验证码发送失败:' . $e->getMessage()]); + } + } + + return json(['code' => 0, 'msg' => '非法请求']); + } + + // 微信授权回调 + public function wechatCallback() + { + $code = $this->request->get('code'); + if (!$code) { + return json(['code' => 0, 'msg' => '微信授权失败']); + } + + try { + // 这里应该调用微信API获取用户信息 + // 示例代码,实际使用时需要替换为真实的微信API调用逻辑 + // $wechatUser = getWechatUserInfo($code); + + // 模拟获取到的微信用户信息 + $wechatUser = [ + 'openid' => 'test_openid_' . time(), + 'nickname' => '微信用户', + 'avatar' => '' + ]; + + // 检查用户是否已注册 + $user = UserModel::where('openid', $wechatUser['openid'])->find(); + if ($user) { + // 已注册,直接登录 + session('user_id', $user->id); + return json(['code' => 1, 'msg' => '登录成功']); + } + + // 未注册,返回注册所需信息 + return json([ + 'code' => 2, + 'msg' => '需要注册', + 'data' => $wechatUser + ]); + } catch (\Exception $e) { + return json(['code' => 0, 'msg' => '微信授权失败:' . $e->getMessage()]); + } + } +} diff --git a/app/index/view/user/login.php b/app/index/view/user/login.php new file mode 100644 index 0000000..f5553d9 --- /dev/null +++ b/app/index/view/user/login.php @@ -0,0 +1,241 @@ + + + + + + + + + 用户登录 + + + + +
+
+

欢迎登录

+

请选择登录方式

+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ + + + \ No newline at end of file diff --git a/app/index/view/user/register.php b/app/index/view/user/register.php new file mode 100644 index 0000000..6da29d4 --- /dev/null +++ b/app/index/view/user/register.php @@ -0,0 +1,193 @@ + + + + + + + + + 用户注册 + + + + +
+
+

用户注册

+

创建您的账号

+
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+ +
+
+ + + + diff --git a/composer.json b/composer.json index 6d5105f..4123611 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "topthink/think-multi-app": "^1.1", "topthink/think-view": "^1.0", "topthink/think-captcha": "^3.0", - "phpoffice/phpspreadsheet": "^1.25" + "phpoffice/phpspreadsheet": "^1.25", + "phpmailer/phpmailer": "^6.9" }, "require-dev": { "symfony/var-dumper": "^4.2", diff --git a/composer.lock b/composer.lock index af769e9..c2ae46f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7c23a0e75a1b3f811f5e1afc64aff308", + "content-hash": "e7aa95c6a8e70b3ab7dc4893252beb4d", "packages": [ { "name": "composer/pcre", @@ -649,6 +649,93 @@ ], "time": "2022-08-04T09:53:51+00:00" }, + { + "name": "phpmailer/phpmailer", + "version": "v6.9.3", + "source": { + "type": "git", + "url": "https://github.com/PHPMailer/PHPMailer.git", + "reference": "2f5c94fe7493efc213f643c23b1b1c249d40f47e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/2f5c94fe7493efc213f643c23b1b1c249d40f47e", + "reference": "2f5c94fe7493efc213f643c23b1b1c249d40f47e", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "ext-ctype": "*", + "ext-filter": "*", + "ext-hash": "*", + "php": ">=5.5.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", + "doctrine/annotations": "^1.2.6 || ^1.13.3", + "php-parallel-lint/php-console-highlighter": "^1.0.0", + "php-parallel-lint/php-parallel-lint": "^1.3.2", + "phpcompatibility/php-compatibility": "^9.3.5", + "roave/security-advisories": "dev-latest", + "squizlabs/php_codesniffer": "^3.7.2", + "yoast/phpunit-polyfills": "^1.0.4" + }, + "suggest": { + "decomplexity/SendOauth2": "Adapter for using XOAUTH2 authentication", + "ext-mbstring": "Needed to send email in multibyte encoding charset or decode encoded addresses", + "ext-openssl": "Needed for secure SMTP sending and DKIM signing", + "greew/oauth2-azure-provider": "Needed for Microsoft Azure XOAUTH2 authentication", + "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication", + "league/oauth2-google": "Needed for Google XOAUTH2 authentication", + "psr/log": "For optional PSR-3 debug logging", + "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)", + "thenetworg/oauth2-azure": "Needed for Microsoft XOAUTH2 authentication" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPMailer\\PHPMailer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1-only" + ], + "authors": [ + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + }, + { + "name": "Jim Jagielski", + "email": "jimjag@gmail.com" + }, + { + "name": "Andy Prevost", + "email": "codeworxtech@users.sourceforge.net" + }, + { + "name": "Brent R. Matzelle" + } + ], + "description": "PHPMailer is a full-featured email creation and transfer class for PHP", + "support": { + "issues": "https://github.com/PHPMailer/PHPMailer/issues", + "source": "https://github.com/PHPMailer/PHPMailer/tree/v6.9.3" + }, + "funding": [ + { + "url": "https://github.com/Synchro", + "type": "github" + } + ], + "time": "2024-11-24T18:04:13+00:00" + }, { "name": "phpoffice/phpspreadsheet", "version": "1.29.10",