Files
hztha.hkpgsow.cn/app/home/controller/Login.php
gaofeng 0b165153c6 提交
2026-05-13 10:44:29 +08:00

93 lines
2.8 KiB
PHP

<?php
namespace app\home\controller;
use api\Httpcurl;
use think\exception\ValidateException;
use think\facade\Log;
use think\facade\Session;
class Login extends Base
{
public function login_code()
{
$param = $this->request->post();
try {
$this->validate($param, ['mobile' => 'require'], ['mobile.require' => '手机号不能为空']);
} catch (ValidateException $e) {
return $this->error($e->getError());
}
$code = mt_rand(1001, 9999);
//进行发送验证 走接口
$res = Httpcurl::request(config('app.YZM_URL'), 'post', [
'mobile' => $param['mobile'],
'code' => $code,
]);
$result = json_decode($res[0], true);
Log::info(json_encode($result));
if (!$result['code']) {
return $this->error('发送验证码失败');
}
//进行记录
if ($result['code'] == 1) {
//把内容存入session
Session::set($param['mobile'] . '_code', $code);
Session::set($param['mobile'] . '_' . $code, time());
return $this->success('发送成功');
} else {
return $this->error('发送失败');
}
}
// 登录
public function login()
{
$param = $this->request->post();
try {
$this->validate($param, ['phone' => 'require', 'vercode' => 'require'], ['phone.require' => '手机号不能为空', 'vercode.require' => '验证码不能为空']);
} catch (ValidateException $e) {
return $this->error($e->getError());
}
$mobile = $param['phone'];
$code = $param['vercode'];
$res = 0;
$sms = Session::get($mobile . '_code');
if (empty($sms)) {
return $this->error('登录失败');
}
if ($sms != $code) {
return $this->error('验证码不正确');
}
$time = time();
$code_time = Session::get($mobile . '_' . $code);
//判断是否已经超时5分钟
if (($time - $code_time) > 300) {
Session::delete($mobile . '_code');
Session::delete($mobile . '_' . $code);
return $this->error('验证码超时,请重新发送');
}
//查询账号
$user_url = config('app.user_url');
$res = Httpcurl::request($user_url, 'post', [
'model' => MODEL,
'mobile' => $mobile,
]);
$result = json_decode($res[0], true);
Log::info(json_encode($result));
if (!$result['code']) {
return $this->error('账号不存在');
}
Session::set('user', $result['data']);
return $this->success('登录成功', url('user/index'));
}
public function logout()
{
Session::delete('user');
return redirect('/home/index');
}
}