基础功能、图片上传
This commit is contained in:
@@ -72,4 +72,18 @@ class AdminController extends BaseController
|
||||
'获取成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单列表
|
||||
* @Method GET
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function menu(): JsonResponse
|
||||
{
|
||||
return jok(
|
||||
$this->service->menu(),
|
||||
'获取成功'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,12 @@ class LoginController extends BaseController
|
||||
public function login()
|
||||
{
|
||||
$this->insertField = [
|
||||
'phone', 'password', 'remember'
|
||||
'account', 'password'
|
||||
];
|
||||
$param = $this->checkRequiredFields(request()->post());
|
||||
|
||||
return jok(
|
||||
$this->service->login($param['phone'], $param['password']),
|
||||
$this->service->login($param['account'], $param['password']),
|
||||
'登录成功'
|
||||
);
|
||||
}
|
||||
@@ -54,4 +54,20 @@ class LoginController extends BaseController
|
||||
);
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
return jok(
|
||||
[],
|
||||
'退出成功'
|
||||
);
|
||||
}
|
||||
|
||||
public function codes()
|
||||
{
|
||||
return jok(
|
||||
[],
|
||||
'获取成功'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
35
app/Http/Controllers/Api/MenuController.php
Executable file
35
app/Http/Controllers/Api/MenuController.php
Executable file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\MenuService;
|
||||
|
||||
class MenuController extends BaseController
|
||||
{
|
||||
//
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->insertField = [ 'pid', 'title', 'icon', 'path', 'affix_tab', 'keep_alive', 'hide_in_menu', 'sort' ];
|
||||
$this->updateField = [ 'id', 'pid', 'title', 'icon', 'path', 'affix_tab', 'redirect', 'keep_alive', 'hide_in_menu', 'badge_variants', 'sort' ];
|
||||
$this->notRequest = [ 'name', 'badge', 'iframe_src', 'redirect', 'query', 'badge_type', 'badge_variants', 'component' ];
|
||||
$this->service = MenuService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单树形下拉列表
|
||||
* @Method GET
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \Psr\Container\ContainerExceptionInterface
|
||||
* @throws \Psr\Container\NotFoundExceptionInterface
|
||||
*/
|
||||
public function getTreeOption()
|
||||
{
|
||||
$isSelect = request()->get('is_select', false);
|
||||
return jok(
|
||||
$this->service->getTreeOption($isSelect)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
54
app/Http/Controllers/Api/RoleController.php
Executable file
54
app/Http/Controllers/Api/RoleController.php
Executable file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\RoleService;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class RoleController extends BaseController
|
||||
{
|
||||
//
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->insertField = ['name', 'value', 'desc'];
|
||||
$this->updateField = ['id', 'name', 'value', 'desc'];
|
||||
$this->service = RoleService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色菜单ID
|
||||
* @Method GET
|
||||
* @return JsonResponse
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function getMenuIdsByRoleIds(): JsonResponse
|
||||
{
|
||||
$roleId = request()->get('role_id', null);
|
||||
return jok(
|
||||
$this->service->getMenuIdsByRoleIds($roleId)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存角色菜单
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveRoleMenu(): JsonResponse
|
||||
{
|
||||
$this->insertField = ['role_id', 'menu_id'];
|
||||
$this->checkRequiredFields(request()->post());
|
||||
$roleId = request()->post('role_id');
|
||||
$menuIds = request()->post('menu_id', []);
|
||||
return jok(
|
||||
$this->service->saveRoleMenu($roleId, $menuIds)
|
||||
);
|
||||
}
|
||||
}
|
||||
47
app/Http/Controllers/Api/UploadController.php
Normal file
47
app/Http/Controllers/Api/UploadController.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\common\UploadService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class UploadController extends BaseController
|
||||
{
|
||||
//
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = UploadService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片上传
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function image()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
return jok(
|
||||
$this->service->uploadImage($file),
|
||||
'上传成功'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频上传
|
||||
* @Method POST
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function video()
|
||||
{
|
||||
$file = request()->file('file');
|
||||
return jok(
|
||||
$this->service->uploadVideo($file),
|
||||
'上传成功'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,29 @@ class AdminService extends BaseService
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->selectField = [ 'id', 'phone', 'nick_name', 'avatar', 'email', 'balance', 'is_sys_notifications', 'is_collection_notifications', 'is_marketing_notifications', 'qr_code', 'role_id', 'ip', 'ip_table', 'status', 'last_reset_password_at', 'created_at', 'updated_at', 'deleted_at', ];
|
||||
$this->selectField = [
|
||||
'id',
|
||||
'open_id',
|
||||
'avatar',
|
||||
'nick_name',
|
||||
'password',
|
||||
'phone',
|
||||
'email',
|
||||
'code',
|
||||
'role_id',
|
||||
'province_id',
|
||||
'city_id',
|
||||
'reg_ip',
|
||||
'last_login_time',
|
||||
'ip',
|
||||
'ip_table',
|
||||
'operation_password',
|
||||
'desc',
|
||||
'status',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
];
|
||||
$this->model = AdminModel::class;
|
||||
$this->queryField = [
|
||||
'phone' => 'like',
|
||||
@@ -45,7 +67,6 @@ class AdminService extends BaseService
|
||||
foreach ($result['items'] as &$v) {
|
||||
$v['ip_table'] = json_decode($v['ip_table'], true);
|
||||
$v['status_text'] = UserStatusEnum::from($v['status'])->description();
|
||||
$v['last_reset_password_at'] = format_time($v['last_reset_password_at']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -189,7 +210,7 @@ class AdminService extends BaseService
|
||||
{
|
||||
if ($this->roleId !== 1) {
|
||||
// 从redis获取菜单列表
|
||||
$redis = RedisService::getInstance()->init(config('xk.redis.menu_key'))->get($this->roleId);
|
||||
$redis = RedisService::getInstance()->init(config('nl.redis.menu_key'))->get($this->roleId);
|
||||
// $redis = null;
|
||||
if (!empty($redis)) {
|
||||
return json_decode($redis, true);
|
||||
@@ -232,7 +253,7 @@ class AdminService extends BaseService
|
||||
return $a['meta']['order'] - $b['meta']['order'];
|
||||
});
|
||||
// 缓存菜单列表到redis
|
||||
RedisService::getInstance()->init(config('xk.redis.menu_key'))->set($this->roleId, json_encode($result), 3600);
|
||||
RedisService::getInstance()->init(config('nl.redis.menu_key'))->set($this->roleId, json_encode($result), 3600);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ class LoginService extends BaseNotAuthService
|
||||
'nick_name' => $userModel->nick_name,
|
||||
'avatar' => $userModel->avatar,
|
||||
'email' => $userModel->email,
|
||||
'role_id' => $userModel->role_id,
|
||||
'role_name' => $userModel->role->name,
|
||||
'role_value' => $userModel->role->value,
|
||||
'ip' => $userModel->ip,
|
||||
|
||||
145
app/Service/MenuService.php
Executable file
145
app/Service/MenuService.php
Executable file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\MenuModel;
|
||||
use Exception;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* 角色服务类
|
||||
*/
|
||||
class MenuService extends BaseService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->model = MenuModel::class;
|
||||
$this->selectField = [
|
||||
'id', 'pid', 'title', 'icon', 'name', 'affix_tab', 'path', 'component', 'redirect', 'keep_alive', 'hide_in_menu', 'badge', 'badge_type', 'badge_variants', 'iframe_src', 'sort', 'query', 'created_at'
|
||||
];
|
||||
$this->queryField = [ 'title' => 'like', 'pid' => '=' ];
|
||||
$this->optionField = ['id', 'title'];
|
||||
$this->orderBy = [
|
||||
'name' => 'sort',
|
||||
'sort' => 'asc'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function list()
|
||||
{
|
||||
// /order/product-order
|
||||
$this->where[] = ['pid', '=', 0];
|
||||
$list = $this->getPageList();
|
||||
$ids = array_column($list['items'], 'id');
|
||||
$items = $this->model::whereIn('pid', $ids)->get($this->selectField);
|
||||
$childrenIds = [];
|
||||
foreach ($items as $v) {
|
||||
$list['items'][] = $v;
|
||||
$childrenIds[] = $v['id'];
|
||||
}
|
||||
$items = $this->model::whereIn('pid', $childrenIds)->get($this->selectField);
|
||||
foreach ($items as $v) {
|
||||
$list['items'][] = $v;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取树形结构的菜单下拉
|
||||
* @param bool $isSelect
|
||||
* @return array
|
||||
*/
|
||||
public function getTreeOption(bool $isSelect = false): array
|
||||
{
|
||||
$this->optionField = ['id', 'title', 'icon', 'pid'];
|
||||
$list = $this->model::get($this->optionField);
|
||||
$result = $this->utils->tree($list);
|
||||
if ($isSelect == 'true') {
|
||||
array_unshift($result, [ 'id' => 0, 'title' => '根目录', 'pid' => 0, 'icon' => '' ]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下拉列表
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function option(): mixed
|
||||
{
|
||||
$option = $this->getOption();
|
||||
if (request()->get('is_select', false)) {
|
||||
$result = [
|
||||
[ 'id' => 0, 'title' => '根目录' ]
|
||||
];
|
||||
foreach ($option as $item) {
|
||||
$result[] = [ 'id' => $item['id'], 'title' => $item['title'] ];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
return $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据详情
|
||||
* @param $id
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function detail($id): mixed
|
||||
{
|
||||
return $this->getDetail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
* @param $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function create($params): mixed
|
||||
{
|
||||
$params['badge_type'] = !empty($params['badge_type']) ? $params['badge_type']: 0;
|
||||
$params['badge_variants'] = !empty($params['badge_variants']) ? $params['badge_variants']: 0;
|
||||
$params['created_at'] = time();
|
||||
return $this->insert($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数据
|
||||
* @param $id
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update($id, $params): mixed
|
||||
{
|
||||
$params['badge_type'] = !empty($params['badge_type']) ? $params['badge_type']: 0;
|
||||
$params['badge_variants'] = !empty($params['badge_variants']) ? $params['badge_variants']: 0;
|
||||
$params['updated_at'] = time();
|
||||
return $this->save($id, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param $id
|
||||
* @return mixed|true
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($id): mixed
|
||||
{
|
||||
if (in_array($id, [1, 2])) {
|
||||
$this->utils->errorThrow('管理员角色禁止删除');
|
||||
}
|
||||
return $this->del($id);
|
||||
}
|
||||
}
|
||||
161
app/Service/RoleService.php
Executable file
161
app/Service/RoleService.php
Executable file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
use App\Models\RoleMenuRelationModel;
|
||||
use App\Models\RoleModel;
|
||||
use App\Service\common\RedisService;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* 角色服务类
|
||||
*/
|
||||
class RoleService extends BaseService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->model = RoleModel::class;
|
||||
$this->selectField = ['id', 'name', 'value', 'pid', 'desc', 'created_at'];
|
||||
$this->queryField = ['name' => 'like', 'value' => 'like', 'pid' => '='];
|
||||
$this->orderBy = ['name' => 'id', 'sort' => 'asc'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
return $this->getPageList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下拉列表
|
||||
* @return mixed
|
||||
*/
|
||||
public function option(): mixed
|
||||
{
|
||||
return $this->getOption();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色ID获取菜单ID
|
||||
* @param $roleId
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getMenuIdsByRoleIds($roleId): mixed
|
||||
{
|
||||
if (empty($roleId)) $this->utils->errorThrow('请选择角色');
|
||||
return RoleMenuRelationModel::where('role_id', $roleId)->pluck('menu_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存角色菜单关系
|
||||
* @param $roleId
|
||||
* @param $menuIds
|
||||
* @return true
|
||||
* @throws Exception
|
||||
*/
|
||||
public function saveRoleMenu($roleId, $menuIds): true
|
||||
{
|
||||
|
||||
$menus = array_unique($menuIds);
|
||||
|
||||
// 查询角色当前绑定的菜单
|
||||
$bindResources = RoleMenuRelationModel::where('role_id', $roleId)->pluck('menu_id');
|
||||
|
||||
// 开启事务
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
// 提取查询到的菜单ID
|
||||
$existingResourceIds = $bindResources->toArray();
|
||||
// 传值中不存在的菜单ID,从数据库中删除
|
||||
$menusToDelete = array_diff($existingResourceIds, $menus);
|
||||
if (!empty($menusToDelete)) {
|
||||
$delBindResources = RoleMenuRelationModel::where('role_id', $roleId)->whereIn('menu_id', array_values($menusToDelete))->delete();
|
||||
|
||||
if (!$delBindResources) {
|
||||
$this->utils->errorThrow('更新失败!');
|
||||
}
|
||||
}
|
||||
|
||||
// 传值中存在但数据库中不存在的菜单ID,新增到数据库
|
||||
$menusToAdd = array_diff($menus, $existingResourceIds);
|
||||
$insertData = [];
|
||||
foreach ($menusToAdd as $menusId) {
|
||||
$insertData[] = [
|
||||
'role_id' => $roleId,
|
||||
'menu_id' => $menusId,
|
||||
'created_at' => time()
|
||||
];
|
||||
}
|
||||
$userRoleBinding = RoleMenuRelationModel::insert($insertData);
|
||||
if (!$userRoleBinding) {
|
||||
$this->utils->errorThrow('更新失败!');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
RedisService::getInstance()->init(config('nl.redis.menu_key'))->del($roleId);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
$this->utils->errorThrow($e->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据详情
|
||||
* @param $id
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function detail($id): mixed
|
||||
{
|
||||
return $this->getDetail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
* @param $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function create($params): mixed
|
||||
{
|
||||
return $this->insert($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数据
|
||||
* @param $id
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update($id, $params): mixed
|
||||
{
|
||||
return $this->save($id, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param $id
|
||||
* @return mixed|true
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($id): mixed
|
||||
{
|
||||
if (in_array($id, [1, 2])) {
|
||||
$this->utils->errorThrow('管理员角色禁止删除');
|
||||
}
|
||||
return $this->del($id);
|
||||
}
|
||||
}
|
||||
@@ -48,14 +48,14 @@ class JWTService
|
||||
public function generateToken(array $data): string
|
||||
{
|
||||
$issuedAt = time();
|
||||
$expirationTime = $issuedAt + config('cc.redis.jwt_ttl', 600000);
|
||||
$expirationTime = $issuedAt + config('nl.redis.jwt_ttl', 600000);
|
||||
|
||||
$payload = [
|
||||
'iat' => $issuedAt,
|
||||
'exp' => $expirationTime,
|
||||
'data' => $data
|
||||
];
|
||||
RedisService::getInstance()->init(config('cc.redis.jwt'))->set($data['id'], json_encode($data));
|
||||
RedisService::getInstance()->init(config('nl.redis.jwt'))->set($data['id'], json_encode($data));
|
||||
return JWT::encode($payload, $this->secretKey, 'HS256');
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ class JWTService
|
||||
{
|
||||
try {
|
||||
$jwt = $this->parseToken();
|
||||
$user = RedisService::getInstance()->init(config('cc.redis.jwt'))->get($jwt->data->id);
|
||||
$user = RedisService::getInstance()->init(config('nl.redis.jwt'))->get($jwt->data->id);
|
||||
if (empty($user)) return UtilsService::getInstance()->notAuth('登录状态过期');
|
||||
return json_decode($user, true);
|
||||
} catch (Exception $e) {
|
||||
|
||||
@@ -22,10 +22,10 @@ class QiniuStorageService extends BaseService
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->accessKey = config('cc.oss.qiniu.access_key');
|
||||
$this->secretKey = config('cc.oss.qiniu.secret_key');
|
||||
$this->bucket = config('cc.oss.qiniu.bucket');
|
||||
$this->domain = config('cc.oss.qiniu.domain');
|
||||
$this->accessKey = config('nl.oss.qiniu.access_key');
|
||||
$this->secretKey = config('nl.oss.qiniu.secret_key');
|
||||
$this->bucket = config('nl.oss.qiniu.bucket');
|
||||
$this->domain = config('nl.oss.qiniu.domain');
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user