SchoolPhysicalExamination/application/app/controller/Login.php

447 lines
22 KiB
PHP
Raw Normal View History

2024-03-06 16:58:11 +08:00
<?php
namespace app\app\controller;
use think\Controller;
use think\Db;
2024-03-15 18:15:17 +08:00
use think\Cache;
2024-03-06 16:58:11 +08:00
use think\Log;
use \think\Validate;
2024-03-15 18:15:17 +08:00
use PHPMailer\PHPMailer\PHPMailer;
2024-03-06 16:58:11 +08:00
2024-05-16 10:33:57 +08:00
class Login extends Base{
2024-06-07 16:49:19 +08:00
protected $code_time = 60;
2024-07-26 18:34:47 +08:00
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
2024-06-05 18:10:32 +08:00
2024-05-28 18:43:48 +08:00
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
2024-03-06 16:58:11 +08:00
// 注册
2024-05-20 18:49:27 +08:00
public function register_action($data = ['data'=>18530934717,'password'=>'ceshi','code'=>'746119']){
2024-03-15 18:15:17 +08:00
2024-05-22 21:37:39 +08:00
if(count(input('post.')) > 0){
$data = input('post.');
2024-05-16 10:33:57 +08:00
}
2024-06-05 18:10:32 +08:00
if(!array_key_exists('data', $data) || !array_key_exists('password', $data) || !array_key_exists('code', $data)){
return $this->msg(10001);
}
if(!$data['data'] || !$data['password'] || !$data['code']){
return $this->msg(10006);
2024-03-15 18:15:17 +08:00
}
2024-06-05 18:10:32 +08:00
$set_data = [];
$montage_data = $this->is_tel_email($data['data']);
if($montage_data == false){
return $this->msg(10005);
}
$inspect_repeat = Db::table('app_account_number')->where([$montage_data=>$data['data']])->count();
if($inspect_repeat >= 0){
return $this->msg(10002,'注册失败,账号已存在');
}
$code_result = $this->check_code($data['data'],$data['code']);
if($code_result !== true){
return $this->msg(10002,$code_result);
}
// 验证完之后
if($montage_data == 'tel'){
$set_data['tel'] = $data['data'];
}else{
$set_data['email'] = $data['data'];
}
$set_data['password'] = $data['password'];
$set_data['head_pic'] = $this->default_head_pic;
2024-07-26 18:34:47 +08:00
$set_data['nickname'] = '用户'.time();
2024-06-05 18:10:32 +08:00
$set_data['create_time'] = date('Y-m-d H:i:s');
$set_data['token'] = md5($data['data'].$this->create_random_string(12).time());
$result = Db::table('app_account_number')->insertGetId($set_data);
2024-03-06 16:58:11 +08:00
if($result){
2024-06-05 18:10:32 +08:00
cache($set_data['token'], time());
return $this->msg(['token'=>$set_data['token'],'aan_id'=>$result]);
2024-03-06 16:58:11 +08:00
}else{
2024-05-22 18:36:27 +08:00
return $this->msg(10002);
2024-03-06 16:58:11 +08:00
}
}
2024-05-20 18:49:27 +08:00
// 重置密码
public function reset_password($data = ['data'=>'18530934717','password'=>'ceshi1','c_password'=>'ceshi1','code'=>'491661']){
2024-05-22 21:37:39 +08:00
if(count(input('post.')) > 0){
$data = input('post.');
2024-05-20 18:49:27 +08:00
}
if(!array_key_exists('data', $data) || !array_key_exists('password', $data) || !array_key_exists('c_password', $data) || !array_key_exists('code', $data)){
2024-05-22 18:36:27 +08:00
return $this->msg(10001);
2024-05-20 18:49:27 +08:00
}
if($data['password'] != $data['c_password']){
2024-05-22 18:36:27 +08:00
return $this->msg(10003,'两次密码不一致');
2024-05-20 18:49:27 +08:00
}
2024-06-05 18:10:32 +08:00
if($data['password'] == ''){
return $this->msg(10003,'密码不能为空');
}
2024-05-20 18:49:27 +08:00
$code_result = $this->check_code($data['data'],$data['code']);
if($code_result !== true){
return $this->msg(10003,$code_result);
}
$t_y = $this->is_tel_email($data['data']);
if($t_y === false){
2024-05-22 18:36:27 +08:00
return $this->msg(10003,'账号格式错误');
2024-05-20 18:49:27 +08:00
}
$find_data = Db::table('app_account_number')->where([$t_y=>$data['data']])->field('id,token')->find();
if(!$find_data){
2024-05-22 18:36:27 +08:00
return $this->msg(10003);
2024-05-20 18:49:27 +08:00
}
$result = Db::table('app_account_number')->where([$t_y=>$data['data']])->update(['password'=>$data['password']]);
if($result){
cache($find_data['token'], time());
2024-05-22 18:36:27 +08:00
return $this->msg(['token'=>$find_data['token'],'aan_id'=>$find_data['id']]);
2024-05-20 18:49:27 +08:00
}else{
2024-05-22 18:36:27 +08:00
return $this->msg(10002);
2024-05-20 18:49:27 +08:00
}
}
2024-03-06 16:58:11 +08:00
// 登录
2024-05-20 18:49:27 +08:00
public function login_action($data = ['data'=>'18530934717','validate_data'=>'746119','type'=>'login','validate_type'=>'code']){
2024-05-22 21:37:39 +08:00
if(count(input('post.')) > 0){
$data = input('post.');
2024-03-15 18:15:17 +08:00
}
2024-05-20 13:56:50 +08:00
if(!array_key_exists('data', $data) || !array_key_exists('validate_data', $data) || !array_key_exists('validate_type', $data)){
2024-05-22 18:36:27 +08:00
return $this->msg(10001);
2024-05-16 10:33:57 +08:00
}
// 检测是否为手机
2024-06-05 18:10:32 +08:00
$montage_data = $this->is_tel_email($data['data']);
if($montage_data == false){
return $this->msg(10005);
2024-05-16 10:33:57 +08:00
}
2024-06-05 18:10:32 +08:00
$verify_result[$montage_data] = $data['data'];
2024-05-16 10:33:57 +08:00
// 检测校验途径
if($data['validate_type'] == 'code'){
2024-05-20 13:56:50 +08:00
$code_name = $data['data'];
2024-05-20 18:49:27 +08:00
if($this->check_code($code_name,$data['validate_data']) === true){
$result = Db::table('app_account_number')->where($verify_result)->field('id,token')->find();
2024-05-20 13:56:50 +08:00
if($result){
2024-05-20 18:49:27 +08:00
cache($result['token'], time());
2024-05-22 18:36:27 +08:00
return $this->msg(['token'=>$result['token'],'aan_id'=>$result['id']]);
2024-05-20 13:56:50 +08:00
}else{
2024-06-05 18:10:32 +08:00
$set_data['password'] = '';
$set_data[$montage_data] = $data['data'];
$set_data['head_pic'] = $this->default_head_pic;
$set_data['nickname'] = '用户'.$data['data'];
$set_data['create_time'] = date('Y-m-d H:i:s');
$set_data['token'] = md5($data['data'].$this->create_random_string(12).time());
$result = Db::table('app_account_number')->insertGetId($set_data);
if($result){
cache($set_data['token'], time());
return $this->msg(['token'=>$set_data['token'],'aan_id'=>$result]);
}else{
return $this->msg(10002);
}
2024-05-20 13:56:50 +08:00
}
2024-05-16 10:33:57 +08:00
}else{
2024-05-22 18:36:27 +08:00
return $this->msg(10003,'登录失败,验证码错误或失效');
2024-05-16 10:33:57 +08:00
}
}else if($data['validate_type'] == 'password'){
2024-06-05 18:10:32 +08:00
// $verify_result['password'] = $data['validate_data'];
$result = Db::table('app_account_number')->where($verify_result)->field('id,token,password')->find();
2024-05-16 10:33:57 +08:00
if($result){
2024-06-05 18:10:32 +08:00
if($result['password'] == ''){
return $this->msg(10003,'该账户未设密码,请用验证码登录');
}
if($data['validate_data'] != $result['password']){
return $this->msg(10003,'账号密码错误');
}else{
cache($result['token'], time());
return $this->msg(['token'=>$result['token'],'aan_id'=>$result['id']]);
}
2024-05-16 10:33:57 +08:00
}else{
2024-06-05 18:10:32 +08:00
return $this->msg(10003,'账号未注册,请先注册');
2024-05-16 10:33:57 +08:00
}
2024-03-06 16:58:11 +08:00
}else{
2024-05-22 18:36:27 +08:00
return $this->msg(10003,'校验参数错误');
2024-03-06 16:58:11 +08:00
}
2024-05-16 10:33:57 +08:00
2024-03-06 16:58:11 +08:00
}
2024-05-28 18:43:48 +08:00
// 退出登录操作
public function user_quit_account($data=['token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001);
}
if($this->token_time_validate($data['token']) === false){
return $this->msg(20001);
}
cache($data['token'], NULL);
return $this->msg([]);
}
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
2024-03-06 16:58:11 +08:00
2024-05-16 10:33:57 +08:00
// 发送验证码 手机/邮箱
2024-03-15 18:15:17 +08:00
/* 接口说明(发邮件)
* $data手机或者邮箱信息 字符串
2024-05-16 10:33:57 +08:00
* $type验证类型是注册用还是其他用途 字符串 默认register注册register、login、reset_password
2024-03-15 18:15:17 +08:00
* $road是手机还是邮箱还是其他 字符串 默认tel或email
*/
2024-07-26 18:34:47 +08:00
public function send_phone_email_code($data = ['data'=>'tsf3920322@126.com','type'=>'login']){
2024-05-22 21:37:39 +08:00
// $data = input('post.');
if(count(input('post.')) > 0){
$data = input('post.');
2024-05-16 10:33:57 +08:00
}
2024-07-26 18:34:47 +08:00
if(!array_key_exists('data', $data)){
2024-05-22 18:36:27 +08:00
return $this->msg(10001);
2024-03-15 18:15:17 +08:00
}
2024-08-03 17:56:26 +08:00
if(cache($data['data'])){
return $this->msg(10002,'60秒仅可发送一次验证码');
}
2024-06-05 18:10:32 +08:00
$num = mt_rand(100000,999999);
2024-05-16 10:33:57 +08:00
if (preg_match('/^\d{11}$/', $data['data'])) {
2024-03-29 18:34:01 +08:00
$result = $this->send_tel_code($data['data'],$num);
2024-05-16 10:33:57 +08:00
$road = 'tel';
}else{
2024-07-26 18:34:47 +08:00
$result = $this->send_email_code([$data['data']],['title'=>'体测APP验证码','from_user_name'=>'体测APP','content'=>$num]);
// dump($result);
2024-05-16 10:33:57 +08:00
$road = 'email';
2024-03-15 18:15:17 +08:00
}
2024-07-26 18:34:47 +08:00
// dump($result);
// die;
2024-03-15 18:15:17 +08:00
if(is_array($result) && $result['code'] == 0){
2024-05-20 18:49:27 +08:00
cache($data['data'], $num, $this->code_time);
2024-03-29 18:34:01 +08:00
// dump($data['data']."_".$data['road']."_".$data['type']);
2024-07-26 18:34:47 +08:00
// return $this->msg(['code'=>$num]);
return $this->msg([]);
2024-05-16 10:33:57 +08:00
// return true;
2024-03-15 18:15:17 +08:00
}else{
2024-05-22 18:36:27 +08:00
return $this->msg(10002);
2024-05-16 10:33:57 +08:00
// return false;
2024-03-15 18:15:17 +08:00
}
2024-03-06 16:58:11 +08:00
}
2024-05-16 10:33:57 +08:00
################################内部调用################################
2024-03-15 18:15:17 +08:00
/* 接口说明(发邮件)
* $address收件人的邮箱地址 数组 格式: ['460834639@qq.com','460834639@qq.com'.......]
* $content邮件的主题数据信息 数组 格式:['title'=>'123','from_user_name'=>'123','content'=>'123']
* $annex附件路径信息 字符串
*/
public function send_email_code($address,$content,$annex=''){
// $ad = '460834639@qq.com';
$ad1 = '295155911@qq.com';
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
$mail->Host = "smtp.126.com"; //SMTP服务器 163邮箱例子
$mail->Port = 465; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->SMTPSecure = 'ssl';
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = "tsf3920322@126.com"; //你的邮箱
$mail->Password = "HLWXNRPUCTHJFIIX"; //你的密码(邮箱后台的授权密码)
$mail->From = "tsf3920322@126.com"; //发件人地址(也就是你的邮箱)
// $mail->Subject = "微盟测试邮件"; //邮件标题
$mail->Subject = $content['title']; //邮件标题
// $mail->FromName = "微盟体测中心"; //发件人姓名
$mail->FromName = $content['from_user_name']; //发件人姓名
2024-07-26 18:34:47 +08:00
2024-03-15 18:15:17 +08:00
for ($i=0; $i < count($address); $i++) {
$mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称)
}
2024-07-26 18:34:47 +08:00
2024-03-15 18:15:17 +08:00
if($annex != ''){
// $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg';
$mail->AddAttachment($annex,''); // 添加附件,并指定名称
}
$mail->IsHTML(true); //支持html格式内容
2024-07-26 18:34:47 +08:00
$neirong = '<div style="margin: 0; padding: 0;">
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="background: #f3f3f3; min-width: 350px; font-size: 1px; line-height: normal;">
<tbody><tr>
<td align="center" valign="top">
<table cellpadding="0" cellspacing="0" border="0" width="750" class="table750" style="width: 100%; max-width: 750px; min-width: 350px; background: #f3f3f3;">
<tbody><tr>
<td class="mob_pad" width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
<td align="center" valign="top" style="background: #ffffff;">
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f3f3f3;">
<tbody><tr>
<td align="right" valign="top">
<div class="top_pad" style="height: 25px; line-height: 25px; font-size: 23px;">&nbsp;</div>
</td>
</tr>
</tbody></table>
<table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
<tbody><tr>
<td align="left" valign="top">
<div style="height: 39px; line-height: 39px; font-size: 37px;">&nbsp;</div>
<font class="mob_title1" face="\'Source Sans Pro\', sans-serif" color="#1a1a1a" style="font-size: 52px; line-height: 55px; font-weight: 300; letter-spacing: -1.5px;">
<span class="mob_title1" style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #fb966e; font-size: 48px; line-height: 55px; font-weight: 700; letter-spacing: -1.5px;">QingCe!</span>
</font>
<div style="height: 73px; line-height: 73px; font-size: 71px;">&nbsp;</div>
</td>
</tr>
</tbody></table>
<table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
<tbody><tr>
<td align="left" valign="top">
<div style="height: 33px; line-height: 33px; font-size: 31px;">&nbsp;</div>
<font face="\'Nunito\', sans-serif" color="#585858" style="font-size: 24px; line-height: 32px;">
<span style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #585858; font-size: 24px; line-height: 32px;">感谢您选择青测产品!</span>
</font>
<div style="height: 33px; line-height: 33px; font-size: 31px;">&nbsp;</div>
<font face="\'Nunito\', sans-serif" color="#585858" style="font-size: 24px; line-height: 32px;">
<span style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #585858; font-size: 24px; line-height: 32px;">以下6位数字是邮箱验证码请在需要的位置填写以通过验证</span>
</font>
<div style="height: 18px; line-height: 33px; font-size: 31px;">&nbsp;</div>
<font face="\'Nunito\', sans-serif" color="#585858" style="font-size: 24px; line-height: 32px;">
<span style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #aaaaaa; font-size: 16px; line-height: 32px;">(如果您从未请求发送邮箱验证码,请忽略此邮件)</span>
</font>
<div style="height: 33px; line-height: 33px; font-size: 31px;">&nbsp;</div>
<table class="mob_btn" cellpadding="0" cellspacing="0" border="0" style="background: #fb966e; border-radius: 4px;">
<tbody><tr>
<td align="center" valign="top">
<span style="display: block; border: 1px solid #fb966e; border-radius: 0px; padding: 6px 12px; font-family: \'Nunito\', Arial, Verdana, Tahoma, Geneva, sans-serif; color: #ffffff; font-size: 20px; line-height: 30px; text-decoration: none; white-space: nowrap; font-weight: 600;">
<font face="\'Nunito\', sans-serif" color="#ffffff" style="font-size: 20px; line-height: 30px; text-decoration: none; white-space: nowrap; font-weight: 600;">
<span style="font-family: \'Nunito\', Arial, Verdana, Tahoma, Geneva, sans-serif; color: #ffffff; font-size: 20px; line-height: 30px; text-decoration: none; white-space: nowrap; font-weight: 600;">'.$content['content'].'</span>
</font>
</span>
</td>
</tr>
</tbody></table>
<div style="height: 75px; line-height: 75px; font-size: 73px;">&nbsp;</div>
</td>
</tr>
</tbody></table>
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f3f3f3;">
<tbody><tr>
<td align="center" valign="top">
<div style="height: 34px; line-height: 34px; font-size: 32px;">&nbsp;</div>
<table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
<tbody><tr>
<td align="center" valign="top">
<div style="height:12px; line-height: 34px; font-size: 32px;">&nbsp;</div>
<font face="\'Nunito\', sans-serif" color="#868686" style="font-size: 17px; line-height: 20px;">
<span style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #868686; font-size: 17px; line-height: 20px;">© Zhengzhou Pinchuan Technology Co., Ltd. </span>
</font>
<div style="height: 3px; line-height: 3px; font-size: 1px;">&nbsp;</div>
<font face="\'Nunito\', sans-serif" color="#1a1a1a" style="font-size: 17px; line-height: 20px;">
<span style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #1a1a1a; font-size: 17px; line-height: 20px;"><a target="_blank" style="font-family: \'Nunito\', Arial, Tahoma, Geneva, sans-serif; color: #1a1a1a; font-size: 17px; line-height: 20px; text-decoration: none;" href="https://paoluz.link/"></a></span>
</font>
<div style="height: 35px; line-height: 35px; font-size: 33px;">&nbsp;</div>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</td>
<td class="mob_pad" width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</div>';
2024-03-15 18:15:17 +08:00
2024-07-26 18:34:47 +08:00
$mail->Body = $neirong; //邮件主体内容
// dump($address);
// die;
2024-03-15 18:15:17 +08:00
//发送
if (!$mail->Send()) {
2024-07-26 18:34:47 +08:00
return ['code' => 10003,'msg'=>$mail->ErrorInfo];
2024-03-15 18:15:17 +08:00
// return $mail->ErrorInfo;
} else {
2024-07-26 18:34:47 +08:00
return ['code' => 0];
2024-03-15 18:15:17 +08:00
// return 'success';
}
}
public function send_tel_code($tel,$code){
// 初始化cURL会话
$ch = curl_init();
$headers = [
'Accept: application/json',
'Content-Type: application/json',
];
// 设置头部信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 设置请求的URL
$url = "http://sms.ybhdmob.com/Message/Send?token=ybhdmob";
curl_setopt($ch, CURLOPT_URL, $url);
// 设置为POST请求
curl_setopt($ch, CURLOPT_POST, 1);
// 设置POST数据
$postData = array(
'phone' => $tel,
'content' => '【xxxx】您好欢迎使用xxx,,您的手机验证码是:'.$code.',验证码一分钟内有效,若非本人操作,请忽略!'
);
$postData = json_encode($postData);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
// 设置返回结果不直接输出,而是返回到变量中
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 发送请求并获取响应
$response = curl_exec($ch);
// 检查是否有错误发生
if (curl_errno($ch)) {
$error_message = curl_error($ch);
return "请求错误: " . $error_message;
}
// 关闭cURL会话
curl_close($ch);
// 处理响应
if ($response) {
return json_decode($response,true);
} else {
echo "未收到响应";
}
2024-03-06 16:58:11 +08:00
}
2024-05-20 18:49:27 +08:00
public function check_code($data = 18530934717 , $code = 123456){
2024-06-05 18:10:32 +08:00
// 默认验证码正确
2024-06-14 17:58:50 +08:00
return true;
2024-05-20 18:49:27 +08:00
// dump($data);
// dump(cache($data));
// die;
if(cache($data) == false){
return '验证码过期';
}else{
if($code != cache($data)){
return '验证码错误';
}
}
return true;
}
2024-03-06 16:58:11 +08:00
################################################################other################################################################
################################################################other################################################################
################################################################other################################################################
2024-03-15 18:15:17 +08:00
2024-03-29 18:34:01 +08:00
public function create_random_string($length = 12)
{
//创建随机字符
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
2024-06-05 18:10:32 +08:00
2024-03-06 16:58:11 +08:00
}