338 lines
14 KiB
PHP
338 lines
14 KiB
PHP
<?php
|
||
|
||
namespace app\app\controller;
|
||
|
||
use think\Controller;
|
||
use think\Db;
|
||
use think\Cache;
|
||
use think\Log;
|
||
use \think\Validate;
|
||
use PHPMailer\PHPMailer\PHPMailer;
|
||
|
||
class Login extends Base{
|
||
protected $code_time = 60;
|
||
protected $default_head_pic = 'http://tc.pcxbc.com/tsf/head_pic.jpg';
|
||
|
||
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
|
||
// 注册
|
||
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);
|
||
}
|
||
$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;
|
||
$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);
|
||
}
|
||
}
|
||
// 重置密码
|
||
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('app_account_number')->where([$t_y=>$data['data']])->field('id,token')->find();
|
||
if(!$find_data){
|
||
return $this->msg(10003);
|
||
}
|
||
$result = Db::table('app_account_number')->where([$t_y=>$data['data']])->update(['password'=>$data['password']]);
|
||
if($result){
|
||
cache($find_data['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'=>'746119','type'=>'login','validate_type'=>'code']){
|
||
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'];
|
||
// 检测校验途径
|
||
if($data['validate_type'] == 'code'){
|
||
$code_name = $data['data'];
|
||
if($this->check_code($code_name,$data['validate_data']) === true){
|
||
$result = Db::table('app_account_number')->where($verify_result)->field('id,token')->find();
|
||
if($result){
|
||
cache($result['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('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);
|
||
}
|
||
}
|
||
}else{
|
||
return $this->msg(10003,'登录失败,验证码错误或失效');
|
||
}
|
||
}else if($data['validate_type'] == 'password'){
|
||
// $verify_result['password'] = $data['validate_data'];
|
||
$result = Db::table('app_account_number')->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'], 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([]);
|
||
}
|
||
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
|
||
|
||
|
||
// 发送验证码 手机/邮箱
|
||
/* 接口说明(发邮件)
|
||
* $data(手机或者邮箱信息) 字符串
|
||
* $type(验证类型,是注册用,还是其他用途) 字符串 默认register(注册)(register、login、reset_password)
|
||
* $road(是手机还是邮箱还是其他) 字符串 默认tel或email
|
||
*/
|
||
public function send_phone_email_code($data = ['data'=>'18530934717','type'=>'login']){
|
||
// $data = input('post.');
|
||
if(count(input('post.')) > 0){
|
||
$data = input('post.');
|
||
}
|
||
if(!array_key_exists('data', $data) || !array_key_exists('type', $data)){
|
||
return $this->msg(10001);
|
||
}
|
||
$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]);
|
||
$road = 'email';
|
||
}
|
||
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 true;
|
||
}else{
|
||
return $this->msg(10002);
|
||
// return false;
|
||
}
|
||
}
|
||
|
||
################################内部调用################################
|
||
/* 接口说明(发邮件)
|
||
* $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(10003,$mail->ErrorInfo);
|
||
// return $mail->ErrorInfo;
|
||
} else {
|
||
return $this->msg([]);
|
||
// 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 "未收到响应";
|
||
}
|
||
}
|
||
|
||
public function check_code($data = 18530934717 , $code = 123456){
|
||
// 默认验证码正确
|
||
// return true;
|
||
// dump($data);
|
||
// dump(cache($data));
|
||
// die;
|
||
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;
|
||
}
|
||
|
||
|
||
|
||
} |