From d4ddbb4a222b02d4ea4d54a6d9450497c769ae3b Mon Sep 17 00:00:00 2001 From: filesite Date: Sun, 5 May 2024 12:08:57 +0000 Subject: [PATCH] call service api to send sms code done --- conf/app.php | 5 ++ plugins/Common.php | 5 ++ .../tajian/controller/FrontapiController.php | 88 ++++++++++++++++++- themes/tajian/views/site/register.php | 2 +- www/js/tajian.js | 45 ++++++++++ 5 files changed, 140 insertions(+), 5 deletions(-) diff --git a/conf/app.php b/conf/app.php index 4747c26..7b25ff8 100644 --- a/conf/app.php +++ b/conf/app.php @@ -121,6 +121,11 @@ $configs = array( ), ), + //对接第三方API服务service_3rd + //源码下载:https://git.filesite.io/filesite/service-3rd + 'service_3rd_api_domain' => 'https://service.filesite.io', + 'service_3rd_api_key' => '你的密钥', + ); //自定义配置支持 diff --git a/plugins/Common.php b/plugins/Common.php index a06ab80..cdfdec1 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -33,7 +33,12 @@ Class Common { try { $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; + $userDir = "{$rootDir}{$cellphone}"; mkdir("{$userDir}/data/", 0755, true); //分享视频目录 + if (!is_dir("{$userDir}/data/")) { + throw new Exception("创建用户数据目录失败:{$userDir}/data/"); + } + mkdir("{$userDir}/tags/", 0700, true); //分类目录 copy("{$rootDir}README.md", "{$userDir}/README.md"); copy("{$rootDir}README_title.txt", "{$userDir}/README_title.txt"); diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index 6c4053e..0c274a4 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -419,6 +419,37 @@ eof; return $isLimited; } + //生成4随机数,并保存生成时间,10 分钟内有效 + protected function generateRandSmsCode() { + if(session_status() !== PHP_SESSION_ACTIVE) { + session_start(); + } + + $rndCode = rand(1000, 9999); //4位随机数 + + $_SESSION['randSmsCode'] = $rndCode; + $_SESSION['randSmsCode_created'] = time(); + + return $rndCode; + } + + //短信验证码 10 分钟内有效 + protected function getMySmsCode() { + if(session_status() !== PHP_SESSION_ACTIVE) { + session_start(); + } + + $rndCode = !empty($_SESSION['randSmsCode']) ? $_SESSION['randSmsCode'] : 0; + $rndCode_created = !empty($_SESSION['randSmsCode_created']) ? $_SESSION['randSmsCode_created'] : 0; + $current_time = time(); + + if (!empty($rndCode_created) && $current_time - $rndCode_created > 600) { + $rndCode = 0; + } + + return $rndCode; + } + //获取短信验证码 public function actionSendsmscode() { $ip = $this->getUserIp(); @@ -431,8 +462,42 @@ eof; throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...'); } - echo "Building..."; - exit; + //返回给视图的变量 + $code = 0; + $msg = ''; + $err = ''; + + $postParams = $this->post(); + if (!empty($postParams)) { + $cellphone = $this->post('phoneNum', ''); + + if (empty($cellphone) || Common::isCellphoneNumber($cellphone) == false) { + $err = "手机号码格式错误,请填写正确的手机号码"; + }else { + $params = array( + 'phoneNumber' => $cellphone, + 'codeNumber' => $this->generateRandSmsCode(), + 'action' => 'register', + ); + $params['sign'] = $this->sign($params, FSC::$app['config']['service_3rd_api_key']); + + $api = FSC::$app['config']['service_3rd_api_domain'] . '/aliyun/sendverifycode/'; + $timeout = 10; + $pc = false; + $headers = array("Content-Type: application/json"); + //以json格式post数据 + $res = $this->request($api, json_encode($params), $timeout, $pc, $headers); + + if (!empty($res) && $res['status'] == 200) { + $code = 1; + $msg = '短信验证码已成功发送'; + }else { + $err = '短信验证码发送失败,请稍后再试'; + } + } + } + + return $this->renderJson(compact('code', 'msg', 'err')); } //新用户注册 @@ -470,7 +535,11 @@ eof; $err = "邀请码不存在,请填写邀请你的朋友的手机号码末 6 位,或者加客服微信索取"; } - //TODO: 验证短信验证码是否正确 + //验证短信验证码是否正确 + $mySmsCode = $this->getMySmsCode(); + if (empty($mySmsCode) || $mySmsCode != $sms_code) { + $err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确"; + } if (empty($err)) { //如果数据检查通过,尝试注册新用户 $userDataDir = Common::getUserDataDir($cellphone); @@ -529,7 +598,11 @@ eof; $err = "{$cellphone}还没注册哦,先去注册吧"; } - //TODO: 验证短信验证码是否正确 + //验证短信验证码是否正确 + $mySmsCode = $this->getMySmsCode(); + if (empty($mySmsCode) || $mySmsCode != $sms_code) { + $err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确"; + } if (empty($err)) { //如果数据检查通过,尝试登录 $newUser = Common::saveUserIntoSession($cellphone); @@ -547,4 +620,11 @@ eof; return $this->renderJson(compact('code', 'msg', 'err', 'shareUrl')); } + //TODO: 分类管理 + + //TODO: 昵称设置 + + //TODO: 视频管理 + + } diff --git a/themes/tajian/views/site/register.php b/themes/tajian/views/site/register.php index ccba7bc..b41d315 100644 --- a/themes/tajian/views/site/register.php +++ b/themes/tajian/views/site/register.php @@ -15,7 +15,7 @@
- +