创建模型

This commit is contained in:
2025-05-12 09:04:46 +08:00
parent fc327e9f48
commit 8ee9ab1b92
19 changed files with 594 additions and 139 deletions

View File

@@ -4,23 +4,24 @@ namespace App\Service;
use App\BaseApp\BaseService;
use App\Enum\UserStatusEnum;
use App\Models\CollectionModel;
use App\Models\MenuModel;
use App\Models\RoleMenuRelationModel;
use App\Models\RoleModel;
use App\Models\UserModel;
use App\Models\UserPayModel;
use App\Models\AdminModel;
use App\Service\common\RedisService;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class UserService extends BaseService
class AdminService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->selectField = [ 'id', 'account', '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->model = UserModel::class;
$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->model = AdminModel::class;
$this->queryField = [
'account' => 'like',
'phone' => 'like',
'nick_name' => 'like',
'email' => 'like',
'role_id' => '=',
@@ -74,7 +75,7 @@ class UserService extends BaseService
{
$params['ip_table'] = json_encode([]);
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
$existsUser = $this->model::where('account', $params['account'])->whereOr('email', $params['email'])->exists();
$existsUser = $this->model::where('phone', $params['phone'])->whereOr('email', $params['email'])->exists();
if ($existsUser) {
$this->utils->errorThrow('账号或邮箱已存在');
}
@@ -176,35 +177,62 @@ class UserService extends BaseService
];
$result = $this->getDetail($this->userId);
$result['created_day_at'] = format_time(strtotime($result['created_at']), 'Y-m-d');
$result['account'] = desensitization($result['account']);
$result['phone'] = desensitization($result['phone']);
return $result;
}
/**
* 用户统计
* @return array[]
* 获取菜单列表
* @return array
*/
public function userStats()
public function menu(): array
{
$payProject = UserPayModel::where('user_id', $this->userId)->count();
$collection = CollectionModel::where('user_id', $this->userId)->count();
$role = $this->userInfo['role_name'];
return [
'pay_project' => [
'label' => '已购项目',
'value' => $payProject,
'icon' => 'shopping-bag'
],
'collection' => [
'label' => '我的收藏',
'value' => $collection,
'icon' => 'heart'
],
'role' => [
'label' => '用户类型',
'value' => $role,
'icon' => 'user'
],
if ($this->roleId !== 1) {
// 从redis获取菜单列表
$redis = RedisService::getInstance()->init(config('xk.redis.menu_key'))->get($this->roleId);
// $redis = null;
if (!empty($redis)) {
return json_decode($redis, true);
}
}
// 如果没有数据则从数据库获取菜单列表
$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'
];
if ($this->roleId === 1) {
$menuList = MenuModel::where('deleted_at', 0)->orderBy('sort', 'asc')->get($selectField);
} else {
$menuIds = RoleMenuRelationModel::where('role_id', $this->roleId)->pluck('menu_id');
$menuList = MenuModel::where('deleted_at', 0)->whereIn('id', $menuIds)->orderBy('sort', 'asc')->get($selectField);
}
$resultMenus = [];
foreach ($menuList as $menu) {
$resultMenus[] = [
'id' => $menu->id,
'name' => $menu->name,
'path' => $menu->path,
'component' => $menu->component,
'redirect' => $menu->redirect,
'pid' => $menu->pid,
'sort' => $menu->sort,
'meta' => [
'title' => $menu->title,
'icon' => $menu->icon,
'keepAlive' => !$menu->keep_alive,
'hideInMenu' => (bool)$menu->hide_in_menu,
'affixTab' => !$menu->affix_tab,
'order' => $menu->sort,
'iframeSrc' => $menu->iframe_src,
]
];
}
$result = $this->utils->tree($resultMenus);
// 根据sort排序多层
usort($result, function ($a, $b) {
return $a['meta']['order'] - $b['meta']['order'];
});
// 缓存菜单列表到redis
RedisService::getInstance()->init(config('xk.redis.menu_key'))->set($this->roleId, json_encode($result), 3600);
return $result;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace App\Service;
use App\BaseApp\BaseService;
use App\Models\FileModel;
use App\Models\ProjectModel;
use App\Models\RoleModel;
use App\Models\UserModel;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class FileService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->selectField = ['id', 'user_id', 'url', 'type', 'created_at', 'updated_at', 'deleted_at'];
$this->model = FileModel::class;
}
/**
* 获取文件列表
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
return $this->getPageList();
}
public function detail($id)
{
return $this->getDetail($id);
}
/**
* 文件下拉列表
* @return mixed
*/
public function option()
{
$this->optionField = ['id', 'url'];
return $this->getOption();
}
/**
* 创建文件
* @param $params
* @return mixed
* @throws Exception
*/
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 $ids
* @return mixed|true
* @throws Exception
*/
public function delete($ids): mixed
{
return $this->del($ids);
}
}

View File

@@ -2,7 +2,7 @@
namespace App\Service;
use App\Models\UserModel;
use App\Models\AdminModel;
use App\Service\common\JWTService;
use App\Service\common\UtilsService;
use Exception;
@@ -32,17 +32,17 @@ class LoginService
/**
* 登录
* @param $account
* @param $phone
* @param $password
* @return array
* @throws Exception
*/
public function login($account, $password): array
public function login($phone, $password): array
{
$userModel = UserModel::with(['roles:id,name,value'])->where('account', $account)->first();
$userModel = AdminModel::with(['roles:id,name,value'])->where('phone', $phone)->first();
if (empty($userModel)) {
UtilsService::getInstance()->errorThrow('用户或密码错误!');
// return $this->register($account, $password);
// return $this->register($phone, $password);
}
if (!password_verify($password, $userModel->password)) {
@@ -54,7 +54,7 @@ class LoginService
if (!in_array(get_ip(), $ipTable)) {
$ipTable[] = get_ip();
}
$update = UserModel::where('id', $userModel->id)->update([
$update = AdminModel::where('id', $userModel->id)->update([
'ip' => get_ip(),
'ip_table' => json_encode($ipTable),
'updated_at' => get_time()
@@ -64,11 +64,11 @@ class LoginService
UtilsService::getInstance()->errorThrow('更新失败!');
}
$userModel = UserModel::where('id', $userModel->id)->first();
$userModel = AdminModel::where('id', $userModel->id)->first();
}
$result = [
'id' => $userModel->id,
'account' => $userModel->account,
'phone' => $userModel->phone,
'nick_name' => $userModel->nick_name,
'avatar' => $userModel->avatar,
'email' => $userModel->email,
@@ -84,22 +84,22 @@ class LoginService
/**
* 注册
* @param $account
* @param $phone
* @param $password
* @param $email
* @param $code
* @return array
* @throws Exception
*/
public function register($account, $password, $email, $code): array
public function register($phone, $password, $email, $code): array
{
$userModel = UserModel::where('account', $account)->first();
$userModel = AdminModel::where('phone', $phone)->first();
if ($userModel) {
UtilsService::getInstance()->errorThrow('账号已被占用!');
}
$createModel = UserModel::create([
'account' => $account,
$createModel = AdminModel::create([
'phone' => $phone,
'email' => $email,
'password' => password_hash($password, PASSWORD_DEFAULT),
'nick_name' => '新用户'. Str::random(),
@@ -114,11 +114,11 @@ class LoginService
UtilsService::getInstance()->errorThrow('注册失败!');
}
$userInfo = UserModel::with(['roles:id,name,value'])->where('id', $createModel->id)->first();
$userInfo = AdminModel::with(['roles:id,name,value'])->where('id', $createModel->id)->first();
$result = [
'id' => $userInfo->id,
'account' => $userInfo->account,
'phone' => $userInfo->phone,
'nick_name' => $userInfo->nick_name,
'avatar' => $userInfo->avatar,
'email' => $userInfo->email?? '',

View File

@@ -5,7 +5,7 @@ namespace App\Service\common;
use App\BaseApp\BaseService;
use App\Models\ProjectModel;
use App\Models\RoleModel;
use App\Models\UserModel;
use App\Models\AdminModel;
use App\Service\common\upload\LocalhostStorageService;
use App\Service\common\upload\QiniuStorageService;
use App\Service\FileService;