102 lines
2.5 KiB
PHP
102 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\arw\adjfut\src\Validate;
|
|
use app\BaseController;
|
|
use app\common\logic\Login\CustomerLogin as LogicCustomerLogin;
|
|
use app\common\model\Token as ModelToken;
|
|
use app\common\model\Customer\Customer as ModelCustomer;
|
|
use app\Request;
|
|
use think\captcha\facade\Captcha;
|
|
use think\middleware\SessionInit;
|
|
use think\Response;
|
|
use think\facade\Db;
|
|
|
|
class Login extends BaseController
|
|
{
|
|
/**
|
|
* 初始化
|
|
*
|
|
* @date 2022-03-15
|
|
* @example
|
|
* @author admin
|
|
* @since 1.0.0
|
|
*/
|
|
protected function initialize(): void
|
|
{
|
|
$this->middleware[SessionInit::class] = [
|
|
'only' => ['accountLogin', 'getCaptcha']
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 客户账号登录/注册接口
|
|
*
|
|
* @param Request $request
|
|
* @return array
|
|
* @date 2023-07-07
|
|
* @author xjh
|
|
* @since 1.0.0
|
|
*/
|
|
public function accountLoginRegister(Request $request): array
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
$params = Validate::param([
|
|
'customer_email|客户邮箱' => 'require|email',
|
|
'customer_password|客户密码' => 'require|alphaNum|min:8',
|
|
// 'captcha|验证码' => $request->isProd() ? 'require|captcha' : false
|
|
]);
|
|
$customer = ModelCustomer::where('customer_email', $params['customer_email'])->find();
|
|
$token = null;
|
|
//登录
|
|
if ($customer) {
|
|
$token = LogicCustomerLogin::accountLogin($params);
|
|
}
|
|
//注册
|
|
else {
|
|
$token = LogicCustomerLogin::accountRegistration($params);
|
|
}
|
|
Db::commit();
|
|
return [
|
|
'code' => 0,
|
|
'data' => [
|
|
'token' => $token->token_content,
|
|
'exp_time' => $token->token_exp_time,
|
|
],
|
|
'msg' => $customer ? '登录成功!' : '注册成功!'
|
|
];
|
|
} catch (\Throwable $th) {
|
|
Db::rollback();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 客户登出接口
|
|
*
|
|
* @param Request $request
|
|
* @return array
|
|
* @date 2023-06-25
|
|
* @author xjh
|
|
* @since 1.0.0
|
|
*/
|
|
public function logout(Request $request): array
|
|
{
|
|
ModelToken::logout();
|
|
return msg('登出成功');
|
|
}
|
|
|
|
/**
|
|
* 生成验证码
|
|
*
|
|
* @date 2023-06-25
|
|
* @author xjh
|
|
* @since 1.0.0
|
|
*/
|
|
public function getCaptcha()
|
|
{
|
|
return Captcha::create();
|
|
}
|
|
} |