SchoolPhysicalExamination/application/app/controller/Login.php

320 lines
12 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 app\bj\controller\Common;
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-03-06 16:58:11 +08:00
################################################################个人资料卡################################################################
################################################################个人资料卡################################################################
################################################################个人资料卡################################################################
// 注册
2024-05-20 13:56:50 +08:00
public function register_action($data = ['data'=>15588885555,'password'=>'ceshi','code'=>'123456']){
2024-03-15 18:15:17 +08:00
// dump('123');
2024-05-20 13:56:50 +08:00
// phpinfo();
// die;
2024-05-16 10:33:57 +08:00
if(count(input()) > 0){
$data = input();
}
2024-03-15 18:15:17 +08:00
$verify_result = $this->verify_parameters($data,'register');
if(!is_array($verify_result)){
return $this->msg(10001,$verify_result);
}
2024-05-20 13:56:50 +08:00
// dump($verify_result);
// die;
2024-03-15 18:15:17 +08:00
// 记录
$result = Db::table('app_account_number')->insert($verify_result);
2024-03-06 16:58:11 +08:00
if($result){
return $this->msg(0,'success');
}else{
2024-03-15 18:15:17 +08:00
return $this->msg(10003,'注册失败');
2024-03-06 16:58:11 +08:00
}
}
// 登录
2024-05-16 10:33:57 +08:00
public function login_action($data = ['data'=>'18530934717','validate_data'=>'734626','type'=>'login','validate_type'=>'code']){
2024-04-03 18:15:07 +08:00
2024-05-16 10:33:57 +08:00
if(count(input()) > 0){
$data = input();
2024-03-15 18:15:17 +08:00
}
2024-05-16 10:33:57 +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-16 10:33:57 +08:00
return $this->msg(10001,'参数缺失');
}
// 检测是否为手机
if (preg_match('/^\d{11}$/', $data['data'])) {
$verify_result['tel'] = $data['data'];
$road = 'tel';
}else{
$verify_result['email'] = $data['data'];
$road = 'email';
}
// 检测校验途径
if($data['validate_type'] == 'code'){
2024-05-20 13:56:50 +08:00
$code_name = $data['data'];
2024-05-16 10:33:57 +08:00
// dump(cache($code_name));
// die;
if(cache($code_name) == $data['validate_data']){
2024-05-20 13:56:50 +08:00
$result = Db::table('app_account_number')->where(['tel'=>$verify_result['tel']])->find();
if($result){
return $this->msg(0,'success',$result['id']);
}else{
return $this->msg(10002,'登录失败,无效账号');
}
2024-05-16 10:33:57 +08:00
}else{
return $this->msg(10001,'登录失败,验证码错误或失效');
}
}else if($data['validate_type'] == 'password'){
$verify_result['password'] = $data['validate_data'];
2024-05-20 13:56:50 +08:00
$result = Db::table('app_account_number')->where($verify_result)->find();
2024-05-16 10:33:57 +08:00
if($result){
2024-05-20 13:56:50 +08:00
return $this->msg(0,'success',$result['id']);
2024-05-16 10:33:57 +08:00
}else{
return $this->msg(10001,'登录失败,账号/密码错误');
}
2024-03-06 16:58:11 +08:00
}else{
2024-05-16 10:33:57 +08:00
return $this->msg(10002,'校验参数错误');
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-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-05-16 10:33:57 +08:00
public function send_phone_email_code($data = ['data'=>'18530934717','type'=>'login']){
// $data = input();
if(count(input()) > 0){
$data = input();
}
if(!array_key_exists('data', $data) || !array_key_exists('type', $data)){
2024-03-15 18:15:17 +08:00
return $this->msg(10001,'数据&场景&通道必须');
}
$num = $this->rand_int();
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-03-29 18:34:01 +08:00
$result = $this->send_email_code($data['data'],['title'=>'体测APP验证码','from_user_name'=>'体测APP','content'=>$num]);
2024-05-16 10:33:57 +08:00
$road = 'email';
2024-03-15 18:15:17 +08:00
}
if(is_array($result) && $result['code'] == 0){
2024-05-20 13:56:50 +08:00
cache($data['data'], $num, 60);
2024-03-29 18:34:01 +08:00
// dump($data['data']."_".$data['road']."_".$data['type']);
2024-05-16 10:33:57 +08:00
return $this->msg(0,'success',$num);
// return true;
2024-03-15 18:15:17 +08:00
}else{
return $this->msg(10001,'error');
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-03-15 18:15:17 +08:00
2024-05-16 10:33:57 +08:00
public function check_code($data = []){
}
################################内部调用################################
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']; //发件人姓名
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格式内容
$mail->Body = $content['content']; //邮件主体内容
//发送
if (!$mail->Send()) {
return $this->msg(10001,$mail->ErrorInfo);
// return $mail->ErrorInfo;
} else {
return $this->msg(0,'success');
// 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
}
################################################################other################################################################
################################################################other################################################################
################################################################other################################################################
2024-03-15 18:15:17 +08:00
public function test(){
dump($this->send_tel_code('18530934717',$this->rand_int()));
// dump(['code']);
}
public function rand_int(){
return mt_rand(100000,999999);
}
public function verify_parameters($data,$type){
// 设置验证
$rule = [
2024-05-20 13:56:50 +08:00
'data' => 'require',
2024-03-15 18:15:17 +08:00
'password' => 'require',
2024-05-20 13:56:50 +08:00
'code' => 'require|number',
2024-03-15 18:15:17 +08:00
];
$msg = [
'password.require' => '密码必须',
2024-05-20 13:56:50 +08:00
'data.require' => '手机或邮箱必须',
2024-03-15 18:15:17 +08:00
'code.number' => '验证码必须是数字',
2024-05-20 13:56:50 +08:00
'code.number' => '验证码必须',
2024-03-15 18:15:17 +08:00
];
$validate = new Validate($rule,$msg);
$result = $validate->check($data);
if(!$result){
return $validate->getError();
}
// dump('3211');
2024-03-15 18:15:17 +08:00
// 判断参数
2024-05-20 13:56:50 +08:00
if(preg_match('/^\d{11}$/', $data['data'])){
$parameter['tel'] = $data['data'];
2024-03-15 18:15:17 +08:00
$montage_data = 'tel';
}else{
2024-05-20 13:56:50 +08:00
$parameter['email'] = $data['data'];
$montage_data = 'email';
2024-03-15 18:15:17 +08:00
}
2024-05-20 13:56:50 +08:00
$parameter['password'] = $data['password'];
2024-03-15 18:15:17 +08:00
2024-05-20 13:56:50 +08:00
// dump($montage_data);
// dump( $parameter);
2024-03-15 18:15:17 +08:00
// 检验是否注册过
if($montage_data == 'tel'){
2024-05-20 13:56:50 +08:00
$inspect_repeat = Db::table('app_account_number')->where(['tel'=>$parameter['tel']])->count();
// $inspect_repeat = Db::query("select count(*) from app_account_number where tel='".$parameter['tel']."'and password='".$parameter['password']);
2024-03-15 18:15:17 +08:00
}else{
2024-05-20 13:56:50 +08:00
$inspect_repeat = Db::table('app_account_number')->where(['email'=>$parameter['email']])->count();
2024-03-15 18:15:17 +08:00
}
2024-05-20 13:56:50 +08:00
// dump( $inspect_repeat);
// die;
// dump($inspect_repeat);
// dump('3212');
2024-05-20 13:56:50 +08:00
if($inspect_repeat>0){
2024-03-15 18:15:17 +08:00
return '注册失败,账号已存在';
}
if($type == 'register'){
if(array_key_exists('code', $data)){
2024-03-29 18:34:01 +08:00
// dump($parameter[$montage_data]."_".$montage_data."_register".'='.cache($parameter[$montage_data]."_".$montage_data."_register"));
2024-05-20 13:56:50 +08:00
if(cache($parameter[$montage_data]) == false){
2024-03-15 18:15:17 +08:00
return '验证码过期';
}else{
2024-05-20 13:56:50 +08:00
if($data['code'] != cache($parameter[$montage_data])){
2024-03-15 18:15:17 +08:00
return '验证码错误';
}
}
}else{
return '验证码必须';
}
$parameter['create_time'] = date('Y-m-d H:i:s');
2024-03-29 18:34:01 +08:00
$parameter['token'] = md5($parameter['tel'].$this->create_random_string(12).time());
2024-03-15 18:15:17 +08:00
}
return $parameter;
}
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;
}
public function demo_db(){
dump('开始测试');
$this->ceshiyong();
2024-03-06 16:58:11 +08:00
}
}