Browse Source

user register and login basic logic ready

master
filesite 7 months ago
parent
commit
808d11ce6e
  1. 1
      conf/app.php
  2. 1
      conf/custom_config_tajian.json
  3. 54
      plugins/Common.php
  4. 44
      themes/tajian/controller/FrontapiController.php
  5. 2
      themes/tajian/views/site/login.php
  6. 2
      themes/tajian/views/site/register.php
  7. 163
      www/js/tajian.js

1
conf/app.php

@ -58,6 +58,7 @@ $configs = array( @@ -58,6 +58,7 @@ $configs = array(
*/
//皮肤TaJian相关
'default_friends_code' => '888888', //默认的注册邀请码
'tajian' => array(
'data_dir' => 'data/', //数据目录
'tag_dir' => 'tags/', //tag分类目录

1
conf/custom_config_tajian.json

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
{
"theme": "tajian",
"content_directory": "tajian/",
"default_friends_code": "888888",
"multipleUserUriParse": true,
"defaultUserId": "",
"heroUnionEnable": true,

54
plugins/Common.php

@ -13,7 +13,7 @@ Class Common { @@ -13,7 +13,7 @@ Class Common {
}
//用户注册成功后,保存他的手机号码 6 位尾号作为邀请码
protected function saveFriendsCode($cellphone) {
public static function saveFriendsCode($cellphone) {
$logTime = date('Y-m-d H:i:s');
$logDir = __DIR__ . '/../runtime/friendscode/';
$logFilename = substr($cellphone, -6) . '.log';
@ -24,11 +24,61 @@ Class Common { @@ -24,11 +24,61 @@ Class Common {
}
}
protected function existFriendsCode($code) {
//检查朋友的邀请码是否存在
public static function existFriendsCode($code) {
if (self::isFriendsCode($code) == false) {return false;}
if (!empty(FSC::$app['config']['default_friends_code']) && $code == FSC::$app['config']['default_friends_code']) {
return true;
}
$logDir = __DIR__ . '/../runtime/friendscode/';
$logFilename = "{$logDir}{$code}.log";
return file_exists($logFilename);
}
//用户注册或登录成功时保存用户信息到session
//login_time, username, friends_code
public static function saveUserIntoSession($cellphone, $friends_code = '') {
if(session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$login_time = time();
$username = $cellphone;
if (empty($friends_code) && !empty($_COOKIE['friends_code'])) {
$friends_code = $_COOKIE['friends_code'];
}
$_SESSION['login_time'] = $login_time;
$_SESSION['username'] = $username;
$_SESSION['friends_code'] = $friends_code;
//cookie保存 1 年
if (!empty($friends_code)) {
setcookie('friends_code', $friends_code, $login_time + 86400*365, '/');
}
return compact('login_time', 'username', 'friends_code');
}
//从session里获取用户数据
public static function getUserFromSession() {
if(session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$login_time = !empty($_SESSION['login_time']) ? $_SESSION['login_time'] : 0;
$username = !empty($_SESSION['username']) ? $_SESSION['username'] : '';
$friends_code = !empty($_SESSION['friends_code']) ? $_SESSION['friends_code'] : '';
//尝试从cookie中获取
if (empty($friends_code) && !empty($_COOKIE['friends_code'])) {
$friends_code = $_COOKIE['friends_code'];
}
return compact('login_time', 'username', 'friends_code');
}
}

44
themes/tajian/controller/FrontapiController.php

@ -394,7 +394,9 @@ eof; @@ -394,7 +394,9 @@ eof;
$isLimited = false;
try {
if(session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
$current_time = microtime(true)*1000;
@ -446,10 +448,9 @@ eof; @@ -446,10 +448,9 @@ eof;
}
//返回给视图的变量
$code = 1;
$code = 0;
$msg = '';
$err = '';
$newUser = array();
//用户提交的数据检查
$postParams = $this->post();
@ -470,9 +471,17 @@ eof; @@ -470,9 +471,17 @@ eof;
//TODO: 验证短信验证码是否正确
if (empty($err)) { //如果数据检查通过,尝试注册新用户
$newUser = Common::saveUserIntoSession($cellphone, $friends_code);
if (!empty($newUser)) {
Common::saveFriendsCode($cellphone);
}
$msg = "注册完成,开始收藏你喜欢的视频吧";
$code = 1;
}
}
return $this->renderJson(compact('code', 'msg', 'err', 'data'));
return $this->renderJson(compact('code', 'msg', 'err'));
}
//用户登录
@ -487,8 +496,33 @@ eof; @@ -487,8 +496,33 @@ eof;
throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...');
}
echo "Building...";
exit;
//返回给视图的变量
$code = 0;
$msg = '';
$err = '';
//用户提交的数据检查
$postParams = $this->post();
if (!empty($postParams)) {
$cellphone = $this->post('username', '');
$sms_code = $this->post('smscode', '');
if (empty($cellphone) || empty($sms_code)) {
$err = "请填写注册邀请码、手机号码和短信验证码哦";
}else if (Common::isCellphoneNumber($cellphone) == false) {
$err = "手机号码格式错误,请填写正确的手机号码";
}
//TODO: 验证短信验证码是否正确
if (empty($err)) { //如果数据检查通过,尝试登录
$newUser = Common::saveUserIntoSession($cellphone);
$msg = "登录成功,开始收藏你喜欢的视频吧";
$code = 1;
}
}
return $this->renderJson(compact('code', 'msg', 'err'));
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
<main class="g_main_lay g_main_border">
<form class="add_video_form g_form_style mt65" id="add_video_form" action="" method="get">
<form class="add_video_form g_form_style mt65" id="login_form" action="" method="POST">
<div class="mb-3 pt20">
<label for="text_input_username" class="form-label">手机号码</label>
<input id="text_input_username" name="username" placeholder="请填写手机号码">

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
<main class="g_main_lay g_main_border">
<form class="add_video_form g_form_style mt65" id="add_video_form" action="" method="get">
<form class="add_video_form g_form_style mt65" id="register_form" action="" method="POST">
<div class="mb-3 pt20 twocol">
<label for="text_input_code" class="form-label">邀请码</label>
<input id="text_input_code" name="friendscode" placeholder="邀请者手机号末 6 位">

163
www/js/tajian.js

@ -7,11 +7,36 @@ var taJian = { @@ -7,11 +7,36 @@ var taJian = {
domain: 'https://tajian.tv',
apis: {
addVideos: '/frontapi/addfav', //添加视频
register: '/frontapi/register', //注册
login: '/frontapi/login' //登入
register: '/frontapi/createuser', //注册
login: '/frontapi/loginuser' //登入
}
};
var publicAjax = function(apiUrl, method, datas, callback, fail) {
let self = this;
let Options = {
url: apiUrl,
method: method,
data: datas,
dataType: 'json'
};
if (self.debug) {
Options.crossDomain = true;
Options.xhrFields = {
withCredentials: true
};
}
$.ajax(Options).done(function(data) {
callback(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
fail(jqXHR, textStatus, errorThrown);
});
};
//多用户支持
if (typeof(current_user_id) != 'undefined' && current_user_id) {
for (var apiKey in taJian.apis) {
@ -79,7 +104,6 @@ if ($('.search_mob_JS').get(0)) { @@ -79,7 +104,6 @@ if ($('.search_mob_JS').get(0)) {
// 搜索相关
$('#search_form .jsbtn').click(function (e) {
e.preventDefault();
if (!$('#search_form .input_sf_JS').val()) {
@ -114,7 +138,6 @@ if ($('#add_video_form').get(0)) { @@ -114,7 +138,6 @@ if ($('#add_video_form').get(0)) {
}
let apiUrl = taJian.debug ? taJian.domain + taJian.apis.addVideos : taJian.apis.addVideos;
// console.log(apiUrl);
publicAjax(apiUrl, 'POST', datas, function (data) {
bt.prop('disabled', false);
btText.text('提交');
@ -128,9 +151,8 @@ if ($('#add_video_form').get(0)) { @@ -128,9 +151,8 @@ if ($('#add_video_form').get(0)) {
}, function (jqXHR, textStatus, errorThrown) {
bt.prop('disabled', false);
btText.text('提交');
btLoading.removeClass('elementNone');
console.log(jqXHR);
btLoading.addClass('elementNone');
alert('网络请求失败,请重试。');
});
});
}
@ -161,32 +183,7 @@ if ($('.g_form_js').get(0)) { @@ -161,32 +183,7 @@ if ($('.g_form_js').get(0)) {
});
}
function publicAjax(apiUrl, method, datas, callback, fail) {
let self = this;
let Options = {
url: apiUrl,
method: method,
data: datas,
dataType: 'json'
};
if (self.debug) {
Options.crossDomain = true;
Options.xhrFields = {
withCredentials: true
};
}
$.ajax(Options).done(function (data) {
callback(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
fail(jqXHR, textStatus, errorThrown);
});
};
// 注册
// 注册/登录等页面展示客服微信二维码切换按钮
if ($('.bt_kf_JS').get(0)) {
var kf_hide_text = $('.bt_kf_JS').attr('data-hide'),
kf_text = $('.bt_kf_JS').text();
@ -200,11 +197,109 @@ if ($('.bt_kf_JS').get(0)) { @@ -200,11 +197,109 @@ if ($('.bt_kf_JS').get(0)) {
}
});
var win_width = $(window).width();
if (win_width > 768 && $('.tajian_index').get(0)) {
$('.bt_kf_JS').click().addClass('hide');
}
}
// 注册
if ($('#register_form').get(0)) {
$('#register_form .jsbtn').click(function(e) {
e.preventDefault();
var friends_code = $('input[name=friendscode]').val(),
cellphone = $('input[name=username]').val(),
smscode = $('input[name=smscode]').val();
if (!friends_code || !cellphone || !smscode) {
alert('请填写完整再注册!');
return false;
}else if (!friends_code) {
alert('请填写邀请码:朋友的手机尾号 6 位(或加客服微信索要)!');
return false;
}else if (!cellphone) {
alert('请填写手机号码!');
return false;
}else if (!smscode) {
alert('请填写你手机收到的短信验证码!');
return false;
}
var bt = $(this), btLoading = bt.children('.bt_class_JS'), btText = bt.children('.bt_text_JS');
btLoading.removeClass('elementNone');
bt.prop('disabled', true);
btText.text('提交中...');
var datas = {
'friendscode': friends_code,
'username': cellphone,
'smscode': smscode
};
publicAjax(taJian.apis.register, 'POST', datas, function (data) {
btLoading.addClass('elementNone');
if (data.code == 1) {
btText.text('完成');
alert(data.msg);
} else {
btText.text('注册');
bt.prop('disabled', false);
alert(data.err);
}
}, function (jqXHR, textStatus, errorThrown) {
bt.prop('disabled', false);
btText.text('注册');
btLoading.addClass('elementNone');
alert('网络请求失败,请重试。');
});
});
}
// 登录
if ($('#login_form').get(0)) {
$('#login_form .jsbtn').click(function(e) {
e.preventDefault();
var cellphone = $('input[name=username]').val(),
smscode = $('input[name=smscode]').val();
if (!cellphone || !smscode) {
alert('请填写完整再登录!');
return false;
}else if (!cellphone) {
alert('请填写手机号码!');
return false;
}else if (!smscode) {
alert('请填写你手机收到的短信验证码!');
return false;
}
var bt = $(this), btLoading = bt.children('.bt_class_JS'), btText = bt.children('.bt_text_JS');
btLoading.removeClass('elementNone');
bt.prop('disabled', true);
btText.text('提交中...');
var datas = {
'username': cellphone,
'smscode': smscode
};
publicAjax(taJian.apis.login, 'POST', datas, function (data) {
btLoading.addClass('elementNone');
if (data.code == 1) {
btText.text('完成');
alert(data.msg);
} else {
btText.text('登录');
bt.prop('disabled', false);
alert(data.err);
}
}, function (jqXHR, textStatus, errorThrown) {
bt.prop('disabled', false);
btText.text('登录');
btLoading.addClass('elementNone');
alert('网络请求失败,请重试。');
});
});
}
})();

Loading…
Cancel
Save