Browse Source

call service api to send sms code done

master
filesite 7 months ago
parent
commit
d4ddbb4a22
  1. 5
      conf/app.php
  2. 5
      plugins/Common.php
  3. 88
      themes/tajian/controller/FrontapiController.php
  4. 2
      themes/tajian/views/site/register.php
  5. 45
      www/js/tajian.js

5
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' => '你的密钥',
); );
//自定义配置支持 //自定义配置支持

5
plugins/Common.php

@ -33,7 +33,12 @@ Class Common {
try { try {
$rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $rootDir = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
$userDir = "{$rootDir}{$cellphone}";
mkdir("{$userDir}/data/", 0755, true); //分享视频目录 mkdir("{$userDir}/data/", 0755, true); //分享视频目录
if (!is_dir("{$userDir}/data/")) {
throw new Exception("创建用户数据目录失败:{$userDir}/data/");
}
mkdir("{$userDir}/tags/", 0700, true); //分类目录 mkdir("{$userDir}/tags/", 0700, true); //分类目录
copy("{$rootDir}README.md", "{$userDir}/README.md"); copy("{$rootDir}README.md", "{$userDir}/README.md");
copy("{$rootDir}README_title.txt", "{$userDir}/README_title.txt"); copy("{$rootDir}README_title.txt", "{$userDir}/README_title.txt");

88
themes/tajian/controller/FrontapiController.php

@ -419,6 +419,37 @@ eof;
return $isLimited; 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() { public function actionSendsmscode() {
$ip = $this->getUserIp(); $ip = $this->getUserIp();
@ -431,8 +462,42 @@ eof;
throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...'); 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 位,或者加客服微信索取"; $err = "邀请码不存在,请填写邀请你的朋友的手机号码末 6 位,或者加客服微信索取";
} }
//TODO: 验证短信验证码是否正确 //验证短信验证码是否正确
$mySmsCode = $this->getMySmsCode();
if (empty($mySmsCode) || $mySmsCode != $sms_code) {
$err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确";
}
if (empty($err)) { //如果数据检查通过,尝试注册新用户 if (empty($err)) { //如果数据检查通过,尝试注册新用户
$userDataDir = Common::getUserDataDir($cellphone); $userDataDir = Common::getUserDataDir($cellphone);
@ -529,7 +598,11 @@ eof;
$err = "{$cellphone}还没注册哦,先去注册吧"; $err = "{$cellphone}还没注册哦,先去注册吧";
} }
//TODO: 验证短信验证码是否正确 //验证短信验证码是否正确
$mySmsCode = $this->getMySmsCode();
if (empty($mySmsCode) || $mySmsCode != $sms_code) {
$err = "{$sms_code} 验证码已过期或错误,请检查是否输入正确";
}
if (empty($err)) { //如果数据检查通过,尝试登录 if (empty($err)) { //如果数据检查通过,尝试登录
$newUser = Common::saveUserIntoSession($cellphone); $newUser = Common::saveUserIntoSession($cellphone);
@ -547,4 +620,11 @@ eof;
return $this->renderJson(compact('code', 'msg', 'err', 'shareUrl')); return $this->renderJson(compact('code', 'msg', 'err', 'shareUrl'));
} }
//TODO: 分类管理
//TODO: 昵称设置
//TODO: 视频管理
} }

2
themes/tajian/views/site/register.php

@ -15,7 +15,7 @@
<div class="mb-3 twocol"> <div class="mb-3 twocol">
<label for="text_input_sm" class="form-label">验证码</label> <label for="text_input_sm" class="form-label">验证码</label>
<input id="text_input_sm" name="smscode" placeholder="输入收到的短信验证码"> <input id="text_input_sm" name="smscode" placeholder="输入收到的短信验证码">
<button disabled="disabled" type="button">发送验证码</button> <button disabled="disabled" type="button" class="bt_sms_JS">发送验证码</button>
</div> </div>
<div class="avform_bt vercenter"> <div class="avform_bt vercenter">
<button class="jsbtn" aria-label="注册" type="button"> <button class="jsbtn" aria-label="注册" type="button">

45
www/js/tajian.js

@ -7,6 +7,7 @@ var taJian = {
domain: 'https://tajian.tv', domain: 'https://tajian.tv',
apis: { apis: {
addVideos: '/frontapi/addfav', //添加视频 addVideos: '/frontapi/addfav', //添加视频
sendSmsCode: '/frontapi/sendsmscode', //发送短信验证码
register: '/frontapi/createuser', //注册 register: '/frontapi/createuser', //注册
login: '/frontapi/loginuser' //登入 login: '/frontapi/loginuser' //登入
} }
@ -203,8 +204,52 @@ if ($('.bt_kf_JS').get(0)) {
} }
} }
var smsCodeBtnHandler = function(e) {
var cellphone = $('input[name=username]').val();
if (/^1[3-9][0-9]{9}$/.test(cellphone)) {
$('.bt_sms_JS').removeAttr('disabled');
}
};
//验证码发送
if ($('.bt_sms_JS').get(0)) {
var timer_restore_smsBtn = null, timer_num = 60;
var autoRestoreSmsBtn = function() {
if (timer_restore_smsBtn) {clearTimeout(timer_restore_smsBtn);}
if (timer_num <= 0) {
$('.bt_sms_JS').removeAttr('disabled').text('发送验证码');
timer_num = 60;
return false;
}
timer_num --;
$('.bt_sms_JS').prop('disabled', true).text('已发送(' + timer_num + ')');
timer_restore_smsBtn = setTimeout(autoRestoreSmsBtn, 1000);
};
$('.bt_sms_JS').click(function(e) {
autoRestoreSmsBtn();
//调用api发送验证码
var cellphone = $('input[name=username]').val();
var datas = {
'phoneNum': cellphone
};
publicAjax(taJian.apis.sendSmsCode, 'POST', datas, function (data) {
if (data.code == 0 && data.err) {
alert(data.err);
}
}, function (jqXHR, textStatus, errorThrown) {
alert('网络请求失败,请重试。');
});
});
}
// 注册 // 注册
if ($('#register_form').get(0)) { if ($('#register_form').get(0)) {
$('#register_form input[name=username]').blur(smsCodeBtnHandler);
$('#register_form .jsbtn').click(function(e) { $('#register_form .jsbtn').click(function(e) {
e.preventDefault(); e.preventDefault();

Loading…
Cancel
Save