SchoolPhysicalExamination/application/KitchenScale/controller/admin/Login.php

119 lines
4.7 KiB
PHP

<?php
namespace app\KitchenScale\controller\admin;
use think\Db;
class Login extends Base{
protected $login_hours_out = 24;
// 加 bcadd(,,20)
// 减 bcsub(,,20)
// 乘 bcmul(,,20)
// 除 bcdiv(,,20)
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 登录请求api
public function login($data = ['account'=>123,'password'=>456]){
// try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('account', $data) || !array_key_exists('password', $data)){
return $this->msg(10001);
}
$return_data = $this->login_action($data);
// 成功
$this->record_api_log($data, null, $return_data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["flie"] = $e->getFile();
// $logContent["line"] = $e->getLine();
// $logContent['all_content'] = "异常信息:\n";
// $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
// $logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
// $logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
// $logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
// $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
// $this->record_api_log($data, $logContent, null);
// return $this->msg(99999);
// }
}
// 检测登录信息是否超时
public function check_login($data = ['token'=>'123']){
// try {
// 你的业务逻辑
if(count(input('post.')) > 0){
$data = input('post.');
}
if(!array_key_exists('token', $data)){
return $this->msg(10001);
}
$return_data = $this->check_login_action($data);
// 成功
$this->record_api_log($data, null, $return_data);
return $return_data;
// } catch (\Exception $e) {
// // 捕获异常
// $logContent["flie"] = $e->getFile();
// $logContent["line"] = $e->getLine();
// $logContent['all_content'] = "异常信息:\n";
// $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n";
// $logContent['all_content'] .= "代码: " . $e->getCode() . "\n";
// $logContent['all_content'] .= "文件: " . $e->getFile() . "\n";
// $logContent['all_content'] .= "行号: " . $e->getLine() . "\n";
// $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n";
// $this->record_api_log($data, $logContent, null);
// return $this->msg(99999);
// }
}
#######################################################################action#######################################################################
public function login_action($data){
// dump($data);
$user = Db::table('admin_user_account_number')->where(["account_num"=>$data['account'],'password'=>$data['password']])->find();
// dump($user);
// die;
if($user){
return $this->msg(['token'=>$user['token']]);
}else{
return $this->msg(10004);
}
}
public function check_login_action(){
$user = Db::table('admin_user_account_number')->where(["token"=>$data['token']])->field('create_time')->find();
if (!$user) {
return $this->msg(10001); // 如果用户不存在,直接返回 false
}
// 假设 $user['create_time'] 是一个日期时间字符串
$createTime = new DateTime($user['create_time']);
$currentTime = new DateTime();
// 计算时间差
$interval = $currentTime->diff($createTime);
// 判断时间差是否超过指定小时数
$totalHours = $interval->days * 24 + $interval->h;
if ($totalHours > $this->login_hours_out) {
return $this->msg(10001);
} else {
return $this->msg([]);
}
}
}