SchoolPhysicalExamination/application/testapp/controller/Login.php

485 lines
23 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\testapp\controller;
use think\Db;
use PHPMailer\PHPMailer\PHPMailer;
class Login extends Base{
protected $code_time = 50;
// protected $token_time = 2592000;//30天的秒数
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.png';
protected $login_use_db_name = [
'1'=>'test_app_account_number',
];
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 注册
public function register_action($data = ['data'=>18530934717,'password'=>'ceshi','code'=>'746119']){
// 验证是否前段发送过来的数据
if(count(input('post.')) > 0){
$data = input('post.');
}
// 验证数据项是否完整
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);
}
// 验证是手机还是邮箱
$montage_data = $this->is_tel_email($data['data']);
if($montage_data == false){
return $this->msg(10005);
}
// 查询账号是否已经注册
$inspect_repeat = Db::table($this->login_use_db_name['1'])->where([$montage_data=>$data['data'],'is_del'=>0])->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);
}
// 验证完之后
$set_data = [];
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;
$set_data['nickname'] = '用户'.time();
$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($this->login_use_db_name['1'])->insertGetId($set_data);
if($result){
cache($set_data['token'],date('Y-m-d H:i:s'),$this->token_time);
return $this->msg(['token'=>$set_data['token'],'aan_id'=>$result]);
}else{
return $this->msg(10002);
}
}
// 重置密码
public function reset_password($data = ['data'=>'18530934717','password'=>'ceshi1','c_password'=>'ceshi1','code'=>'491661']){
// 验证是否前段发送过来的数据
if(count(input('post.')) > 0){
$data = input('post.');
}
// 验证数据项是否完整
if(!array_key_exists('data', $data) || !array_key_exists('password', $data) || !array_key_exists('c_password', $data) || !array_key_exists('code', $data)){
return $this->msg(10001);
}
// 验证数据值是否合规
if($data['password'] != $data['c_password']){
return $this->msg(10003,'两次密码不一致');
}
if($data['password'] == ''){
return $this->msg(10003,'密码不能为空');
}
// 检查验证码
$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){
return $this->msg(10003,'账号格式错误');
}
// 检查账号是否存在
$find_data = Db::table($this->login_use_db_name['1'])->where([$t_y=>$data['data'],'is_del'=>0])->field('id,token')->find();
if(!$find_data){
return $this->msg(10003);
}
$result = Db::table($this->login_use_db_name['1'])->where([$t_y=>$data['data']])->update(['password'=>$data['password']]);
if($result){
cache($find_data['token'],date('Y-m-d H:i:s'),$this->token_time);
return $this->msg(['token'=>$find_data['token'],'aan_id'=>$find_data['id']]);
}else{
return $this->msg(10002);
}
}
// 登录
public function login_action($data = ['data'=>'18530934717','validate_data'=>'0932','type'=>'login','validate_type'=>'password']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('data', $data) || !array_key_exists('validate_data', $data) || !array_key_exists('validate_type', $data)){
return $this->msg(10001);
}
// 检测是否为手机
$montage_data = $this->is_tel_email($data['data']);
if($montage_data == false){
return $this->msg(10005);
}
$verify_result[$montage_data] = $data['data'];
$verify_result['is_del'] = 0;
// dump($data);
// die;
// 检测校验途径
if($data['validate_type'] == 'code'){
$code_name = $data['data'];
if($this->check_code($code_name,$data['validate_data']) === true){
$result = Db::table($this->login_use_db_name['1'])->where($verify_result)->field('id,token')->find();
if($result){
cache($result['token'],date('Y-m-d H:i:s'),$this->token_time);
return $this->msg(['token'=>$result['token'],'aan_id'=>$result['id']]);
}else{
$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($this->login_use_db_name['1'])->insertGetId($set_data);
if($result){
cache($set_data['token'],date('Y-m-d H:i:s'),$this->token_time);
return $this->msg(['token'=>$set_data['token'],'aan_id'=>$result],'登录成功');
}else{
return $this->msg(10002);
}
}
}else{
return $this->msg(10003,'登录失败,验证码错误或失效');
}
}else if($data['validate_type'] == 'password'){
// $verify_result['password'] = $data['validate_data'];
$result = Db::table($this->login_use_db_name['1'])->where($verify_result)->field('id,token,password')->find();
if($result){
if($result['password'] == ''){
return $this->msg(10003,'该账户未设密码,请用验证码登录');
}
if($data['validate_data'] != $result['password']){
return $this->msg(10003,'账号密码错误');
}else{
cache($result['token'],date('Y-m-d H:i:s'),$this->token_time);
return $this->msg(['token'=>$result['token'],'aan_id'=>$result['id']],'登录成功');
}
}else{
return $this->msg(10003,'账号未注册,请先注册');
}
}else{
return $this->msg(10003,'校验参数错误');
}
}
// 退出登录操作
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([]);
}
// 删除账号
public function delete_account($data=['token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001);
}
$result = Db::table($this->login_use_db_name['1'])->where(['token'=>$data['token']])->update(['is_del'=>1]);
if($result){
cache($data['token'], NULL);
return $this->msg([]);
}else{
return $this->msg(10002);
}
}
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 发送验证码 手机/邮箱
/* 接口说明(发邮件)
* $data手机或者邮箱信息 字符串
* $type验证类型是注册用还是其他用途 字符串 默认register注册register、login、reset_password
* $road是手机还是邮箱还是其他 字符串 默认tel或email
*/
public function send_phone_email_code($data = ['data'=>'18530934717']){
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('data', $data)){
return $this->msg(10001);
}
if(cache($data['data'])){
return $this->msg(10002,'60秒仅可发送一次验证码');
}
$num = mt_rand(100000,999999);
if (preg_match('/^\d{11}$/', $data['data'])) {
$result = $this->send_tel_code($data['data'],$num);
$road = 'tel';
}else{
$result = $this->send_email_code([$data['data']],['title'=>'体测APP验证码','from_user_name'=>'体测APP','content'=>$num]);
// dump($result);
$road = 'email';
}
// dump($result);
// dump($road);
// die;
if(is_array($result) && $result['code'] == 0){
cache($data['data'], $num, $this->code_time);
// dump($data['data']."_".$data['road']."_".$data['type']);
// return $this->msg(['code'=>$num]);
return $this->msg([]);
// return true;
}else{
return $this->msg(10010,'验证码发送失败');
// return false;
}
}
################################内部调用################################
/* 接口说明(发手机短信)
*/
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' => '您好欢迎使用Reedaw,您的手机验证码是:'.$code.',验证码三分钟内有效,若非本人操作,请忽略!'
'content' => '【Reedaw】您好欢迎使用Reedaw您的验证码是'.$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 "未收到响应";
}
}
/* 接口说明(发邮件)
* $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']; //发件人姓名
for ($i=0; $i < count($address); $i++) {
$mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称)
}
if($annex != ''){
// $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg';
$mail->AddAttachment($annex,''); // 添加附件,并指定名称
}
$mail->IsHTML(true); //支持html格式内容
$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>';
$mail->Body = $neirong; //邮件主体内容
// dump($address);
// die;
//发送
if (!$mail->Send()) {
return ['code' => 10003,'msg'=>$mail->ErrorInfo];
// return $mail->ErrorInfo;
} else {
return ['code' => 0];
// return 'success';
}
}
public function check_code($data = 18530934717 , $code = 123456){
// // 默认验证码正确
if(cache($data) == false){
return '验证码过期';
}else{
if($code != cache($data)){
return '验证码错误';
}
}
return true;
}
################################################################other################################################################
################################################################other################################################################
################################################################other################################################################
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;
}
}