Files
evus.acvisa.com/app/home/controller/Login.php

114 lines
3.6 KiB
PHP

<?php
namespace app\home\controller;
use api\Httpcurl;
use think\exception\ValidateException;
use think\facade\Log;
use think\facade\Session;
use think\facade\View;
use think\Model;
class Login extends Base
{
public function index()
{
return View::fetch();
}
public function send_sms()
{
$param = $this->request->post();
try {
$this->validate($param, ['mobile' => 'require'], ['mobile.require' => '手机号不能为空']);
} catch (ValidateException $e) {
return $this->error($e->getError());
}
// $code = 1234;
// $result['code'] = 1;
$code = mt_rand(1001, 9999);
//进行发送验证 走接口
$smsUrl = (string) config('app.yzm_url', '');
if (!filter_var($smsUrl, FILTER_VALIDATE_URL) || strtolower((string) parse_url($smsUrl, PHP_URL_SCHEME)) !== 'https') {
return $this->error('短信服务尚未配置,请联系客服');
}
$res = Httpcurl::request($smsUrl, 'post', [
'mobile' => $param['mobile'],
'code' => $code,
], [], 15);
$result = json_decode((string) ($res[0] ?? ''), true);
if (!empty($res[3]) || (int) ($result['code'] ?? 0) !== 1) {
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 dologin()
{
$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('验证码超时,请重新发送');
}
//查询evus账号
$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('账号不存在');
}
//查询esta账号
$res = Httpcurl::request($user_url, 'post', [
'model' => ESTA_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/order'));
}
public function logout()
{
Session::delete('user');
return redirect(url('home/index/index')->build());
}
}