测试
This commit is contained in:
48
app/Http/Controllers/AuthController.php
Normal file
48
app/Http/Controllers/AuthController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\GatewayWorker\UserService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
//
|
||||
private $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new UserService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function login(): JsonResponse
|
||||
{
|
||||
$username = request()->input('username');
|
||||
$password = request()->input('password');
|
||||
|
||||
return jok(
|
||||
$this->service->login($username, $password),
|
||||
'登录成功'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function register(): JsonResponse
|
||||
{
|
||||
$username = request()->input('username');
|
||||
$password = request()->input('password');
|
||||
$nickName = request()->input('nickname');
|
||||
|
||||
return jok(
|
||||
$this->service->register($username, $password, $nickName),
|
||||
'注册成功'
|
||||
);
|
||||
}
|
||||
}
|
||||
28
app/Http/Controllers/ImController.php
Normal file
28
app/Http/Controllers/ImController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\GatewayWorker\ImService;
|
||||
use App\Services\GatewayWorker\UserService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ImController extends Controller
|
||||
{
|
||||
//
|
||||
private $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = ImService::getInstance();
|
||||
}
|
||||
public function bind()
|
||||
{
|
||||
$clientId = request()->input('client_id');
|
||||
|
||||
try {
|
||||
return jok($this->service->bindClientIdByUserId($clientId));
|
||||
} catch (\Exception $e) {
|
||||
return jerr($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
10
app/Http/Controllers/UserController.php
Normal file
10
app/Http/Controllers/UserController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
14
app/Models/UserModel.php
Normal file
14
app/Models/UserModel.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class UserModel extends Model
|
||||
{
|
||||
//
|
||||
protected $table = 'nl_user';
|
||||
protected $primaryKey = 'id';
|
||||
public $timestamps = false;
|
||||
protected $guarded = [];
|
||||
}
|
||||
25
app/Services/Base/BaseService.php
Normal file
25
app/Services/Base/BaseService.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Base;
|
||||
|
||||
class BaseService
|
||||
{
|
||||
protected $userId;
|
||||
protected static $instance;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$token = request()->header('token');
|
||||
$this->userId = $token;
|
||||
}
|
||||
|
||||
// 单例
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (!self::$instance instanceof self) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,19 @@
|
||||
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
class ImService
|
||||
use App\Services\Base\BaseService;
|
||||
|
||||
class ImService extends BaseService
|
||||
{
|
||||
|
||||
public function bindClientIdByUserId($clientId)
|
||||
{
|
||||
if (empty($clientId)) {
|
||||
throw new \Exception('clientId不能为空');
|
||||
}
|
||||
|
||||
GatewayClientService::getInstance()->init()->bindUser($clientId, $this->userId);
|
||||
|
||||
return ['绑定成功'];
|
||||
}
|
||||
}
|
||||
|
||||
40
app/Services/GatewayWorker/UserService.php
Normal file
40
app/Services/GatewayWorker/UserService.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\GatewayWorker;
|
||||
|
||||
use App\Models\UserModel;
|
||||
|
||||
class UserService
|
||||
{
|
||||
public function login($username, $password)
|
||||
{
|
||||
$userModel = UserModel::where('username', $username)->first();
|
||||
if ($userModel) {
|
||||
if ($userModel->password == md5($password)) {
|
||||
return [
|
||||
'token' => $userModel->id,
|
||||
'user_info' => $userModel
|
||||
];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function register($username, $password, $nickName)
|
||||
{
|
||||
$userModel = UserModel::create([
|
||||
'username' => $username,
|
||||
'password' => md5($password),
|
||||
'nick_name' => $nickName,
|
||||
'created_at' => time()
|
||||
]);
|
||||
|
||||
return [
|
||||
'token' => $userModel->id,
|
||||
'user_info' => $userModel
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user