Files

78 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2025-08-11 10:22:05 +08:00
<?php
namespace app\anchor\controller;
use think\Controller;
use think\captcha\Captcha;
use think\DB;
class Apip extends Controller
{
public function initialize()
{
header('Access-Control-Allow-Origin: *');
add_operation(3, 0); //用户行为日志
}
public function login()
{
$captcha = input('captcha');
$username = input('username');
$password = input('password');
if (empty($captcha)) {
return ajaxReturn(201, '验证码不能为空');
}
// if (!captcha_check($captcha)) {
// // 验证失败
// return ajaxReturn(201, '验证码错误');
// }
if (empty($username)) {
return ajaxReturn(201, '用户名不能为空');
}
if (empty($password)) {
return ajaxReturn(201, '密码不能为空');
}
$map = [];
$map[] = ['user_name', '=', $username];
$map[] = ['password', '=', md5($password)];
$info = db::name('user')->where($map)->find();
if (empty($info)) {
return ajaxReturn(201, '用户名不存在');
} else {
if (md5($password) != $info['password']) {
return ajaxReturn(201, '密码错误');
}
if ($info['is_anchor'] != 2) {
return ajaxReturn(201, '普通用户无法登录');
}
$login_token = generateRandom(32);
$login_token = $info['login_token']; //防止用户APP掉线 此处不更新登录token
$data = [];
$data['uid'] = $info['uid'];
$data['login_token'] = $login_token;
$data['update_time'] = time();
$reslut = db::name('user')->update($data);
if (!$reslut) {
return ajaxReturn(201, '登录失败', '');
} else {
return ajaxReturn(200, '登录成功', $login_token);
}
}
}
public function verify()
{
$config = [
'codeSet' => '0123456789',
// 验证码字体大小
'fontSize' => 30,
// 验证码位数
'length' => 4,
// 关闭验证码杂点
'useNoise' => false,
];
$captcha = new Captcha($config);
return $captcha->entry();
}
}