You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
7.5 KiB
PHP
215 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Request\EditAdmin;
|
|
use App\Http\Request\UpdateAdminInfo;
|
|
use App\Models\Admin;
|
|
use App\Models\AdminAccount;
|
|
use App\Models\AdminToken;
|
|
use App\Models\Config;
|
|
use Illuminate\Http\Request;
|
|
use Yo;
|
|
use Login;
|
|
use Illuminate\Support\Str;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
public function create(EditAdmin $request)
|
|
{
|
|
Login::admin([5]);
|
|
$account = $request->post('account');
|
|
$admin_account = AdminAccount::where('account', $account)->where('type', 1)->where('del', 2)->first();
|
|
if ($admin_account) Yo::error_echo(100023);
|
|
$admin = new Admin();
|
|
$admin->nickname = $request->post('nickname');
|
|
$admin->avatar = $request->post('avatar');
|
|
$admin->admin_auth_group = $request->post('admin_auth_group');
|
|
$admin->initial_password = $request->post('initial_password');
|
|
$admin->status = $request->post('status');
|
|
$admin->save();
|
|
$admin_account = new AdminAccount();
|
|
$admin_account->admin = $admin->id;
|
|
$admin_account->account = $account;
|
|
$admin_account->secret = bcrypt($request->post('password'));
|
|
$admin_account->type = 1;
|
|
$admin_account->save();
|
|
$admin_info = Admin::select(['id', 'nickname', 'avatar', 'status', 'admin_auth_group', 'initial_password'])
|
|
->selectRaw("IFNULL((select account from admin_accounts where admin_accounts.admin = admins.id and type = 1),'') as account")
|
|
->selectRaw("IFNULL((select name from admin_auth_groups where admin_auth_groups.id = admins.admin_auth_group),'') as admin_auth_group_name")
|
|
->where('id', $admin->id)->first();
|
|
return Yo::echo([
|
|
'info' => $admin_info
|
|
]);
|
|
}
|
|
|
|
public function update(EditAdmin $request)
|
|
{
|
|
Login::admin([5]);
|
|
$id = $request->post('id');
|
|
$account = $request->post('account');
|
|
$admin_account = AdminAccount::where('admin', '!=', $id)->where('account', $account)->where('type', 1)->where('del', 2)->first();
|
|
if ($admin_account) Yo::error_echo(100023);
|
|
$admin = Admin::where('id', $id)->where('del', 2)->first();
|
|
if (!$admin) Yo::error_echo(100001, ['管理员']);
|
|
$admin_account = AdminAccount::where('admin', $id)->where('del', 2)->first();
|
|
if (!$admin_account) Yo::error_echo(100001, ['管理员']);
|
|
$admin->nickname = $request->post('nickname');
|
|
$admin->avatar = $request->post('avatar');
|
|
$admin->admin_auth_group = $request->post('admin_auth_group');
|
|
$admin->initial_password = $request->post('initial_password');
|
|
$admin->status = $request->post('status');
|
|
$admin->save();
|
|
if ($admin_account->account != $account) {
|
|
$admin_account->account = $request->post('account');
|
|
$admin_account->save();
|
|
}
|
|
$admin_info = Admin::select(['id', 'nickname', 'avatar', 'status', 'admin_auth_group', 'initial_password'])
|
|
->selectRaw("IFNULL((select account from admin_accounts where admin_accounts.admin = admins.id and type = 1),'') as account")
|
|
->selectRaw("IFNULL((select name from admin_auth_groups where admin_auth_groups.id = admins.admin_auth_group),'') as admin_auth_group_name")
|
|
->where('id', $admin->id)->first();
|
|
return Yo::echo([
|
|
'info' => $admin_info
|
|
]);
|
|
}
|
|
|
|
public function delete(Request $request)
|
|
{
|
|
Login::admin([5]);
|
|
$id = $request->post('id');
|
|
$admin = Admin::where('id', $id)->where('del', 2)->first();
|
|
if (!$admin) Yo::error_echo(100001, ['管理员']);
|
|
$admin_account = AdminAccount::where('admin', $id)->where('del', 2)->first();
|
|
if (!$admin_account) Yo::error_echo(100001, ['管理员']);
|
|
$admin->del = 1;
|
|
$admin->save();
|
|
$admin_account->del = 1;
|
|
$admin_account->save();
|
|
return Yo::delete_echo($admin->id);
|
|
}
|
|
|
|
public function list(Request $request)
|
|
{
|
|
Login::admin([5]);
|
|
$status = request()->post('status');
|
|
$search = request()->post('search');
|
|
$admin_auth_group = request()->post('admin_auth_group');
|
|
$initial_password = request()->post('initial_password');
|
|
$admin_list = Admin::select(['id', 'nickname', 'avatar', 'status', 'admin_auth_group', 'initial_password'])
|
|
->selectRaw("IFNULL((select account from admin_accounts where admin_accounts.admin = admins.id and type = 1),'') as account")
|
|
->selectRaw("IFNULL((select name from admin_auth_groups where admin_auth_groups.id = admins.admin_auth_group),'') as admin_auth_group_name")
|
|
->where(function ($query) use ($status) {
|
|
if ($status != 0) $query->where('status', $status);
|
|
})
|
|
->where(function ($query) use ($admin_auth_group) {
|
|
if ($admin_auth_group != 0) $query->where('admin_auth_group', $admin_auth_group);
|
|
})
|
|
->where(function ($query) use ($initial_password) {
|
|
if ($initial_password != 0) $query->where('initial_password', $initial_password);
|
|
})
|
|
->where(function ($query) use ($search) {
|
|
if ($search != '') $query->where('nickname', 'like', "%$search%");
|
|
})
|
|
->where('del', 2)
|
|
->paginate(20);
|
|
return Yo::echo([
|
|
'list' => $admin_list
|
|
]);
|
|
}
|
|
|
|
public function quit()
|
|
{
|
|
Login::admin_check();
|
|
if (!!Login::$token) {
|
|
Login::$token->del = 1;
|
|
Login::$token->save();
|
|
}
|
|
return Yo::echo();
|
|
}
|
|
|
|
public function update_self(UpdateAdminInfo $request)
|
|
{
|
|
Login::admin();
|
|
$nickname = $request->post('nickname');
|
|
$avatar = $request->post('avatar');
|
|
Login::$info->nickname = $nickname;
|
|
Login::$info->avatar = $avatar;
|
|
Login::$info->save();
|
|
return Yo::update_echo(Login::$info->id);
|
|
}
|
|
|
|
public function login(Request $request)
|
|
{
|
|
$captcha_type_config = Config::where('name', '后台密码登录验证')->first();
|
|
if (!!$captcha_type_config) {
|
|
if ($captcha_type_config->value != '0') {
|
|
$hash = $request->post('hash');
|
|
$code = $request->post('code');
|
|
$time = $request->post('time');
|
|
$uuid = $request->post('uuid');
|
|
$captcha = null;
|
|
switch ($captcha_type_config->value) {
|
|
case '1':
|
|
$captcha = new ImageCaptchaController();
|
|
break;
|
|
}
|
|
$captcha_check = $captcha->check($hash, $code, $time, $uuid);
|
|
if ($captcha_check != 0) Yo::error_echo($captcha_check);
|
|
}
|
|
}
|
|
$account = $request->post('account');
|
|
$password = $request->post('password');
|
|
$type = 1;
|
|
$admin_account = AdminAccount::where('account', $account)
|
|
->where('type', $type)
|
|
->where('del', 2)
|
|
->first();
|
|
if (!$admin_account) Yo::error_echo(100007);
|
|
if (!password_verify($password, $admin_account->secret)) Yo::error_echo(100007);
|
|
$admin = Admin::where('id', $admin_account->admin)
|
|
->where('status', 1)
|
|
->where('del', 2)
|
|
->first();
|
|
if (!$admin) Yo::error_echo(100003);
|
|
Login::$info = $admin;
|
|
Login::$type = 'admin';
|
|
$token = $this->create_token($admin, $type);
|
|
return Yo::echo([
|
|
'token' => $token
|
|
]);
|
|
}
|
|
|
|
public function status()
|
|
{
|
|
Login::admin();
|
|
return Yo::echo();
|
|
}
|
|
|
|
public function info()
|
|
{
|
|
Login::admin();
|
|
return Yo::echo([
|
|
'info' => [
|
|
'id' => Login::$info->id,
|
|
'nickname' => Login::$info->nickname,
|
|
'avatar' => Login::$info->avatar,
|
|
'initial_password' => Login::$info->initial_password,
|
|
]
|
|
]);
|
|
}
|
|
|
|
public function create_token($info, $type = 1): string
|
|
{
|
|
if ($info->status != 1) Yo::error_echo(100003);
|
|
if ($info->del != 2) Yo::error_echo(100003);
|
|
$token_str = Str::orderedUuid();
|
|
$token = new AdminToken();
|
|
$token->admin = $info->id;
|
|
$token->token = $token_str;
|
|
// $type 1-密码登录
|
|
$token->type = $type;
|
|
$token->save();
|
|
return $token_str;
|
|
}
|
|
}
|