123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Services;
- use App\Services\BaseService;
- use App\Models\Admin;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use App\Services\RoleUserService;
- /**
- * 菜单
- */
- class AdminService extends BaseService
- {
- /**
- * @description: 模型
- * @return {string}
- */
- public static function model(): string
- {
- return Admin::class;
- }
- /**
- * @description: 枚举
- * @return {*}
- */
- public static function enum(): string
- {
- return '';
- }
- /**
- * @description: 获取查询条件
- * @param {array} $search 查询内容
- * @return {array}
- */
- public static function getWhere(array $search = []): array
- {
- $where = [];
-
- if (isset($search['id']) && !empty($search['id'])) {
- $where[] = ['id', '=', $search['id']];
- }
- if (isset($search['username']) && !empty($search['username'])) {
- $where[] = ['username', '=', $search['username']];
- }
- if (isset($search['nickname']) && !empty($search['nickname'])) {
- $where[] = ['nickname', 'like', '%'.$search['nickname'].'%'];
- }
-
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return \App\Models\Coin|null
- */
- public static function findOne(array $search): ?Admin
- {
- return self::model()::with('roles')->where(self::getWhere($search))->first();
- }
- /**
- * @description: 查询所有数据
- * @param array $search
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public static function findAll(array $search = [])
- {
- return self::model()::with('roles')->where(self::getWhere($search))->get();
- }
- /**
- * @description: 分页查询
- * @param array $search
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public static function paginate(array $search = [])
- {
- $limit = isset($search['limit']) ? $search['limit'] : 15;
- $paginator = self::model()::with('roles')->where(self::getWhere($search))
- // ->orderBy("sort", 'asc')
- ->paginate($limit);
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
- }
- /**
- * @description:
- * @param {*} $params
- * @return {*}
- */
- public static function submit($params = [])
- {
- $result = false;
- $msg['code'] = self::NOT;
- $msg['msg'] = '';
- if(isset($params['password']) && !empty($params['password'])){
- $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
- }else{
- unset($params['password']);
- }
- // 2. 判断是否是更新
- if (!empty($params['id'])) {
- // 更新
- $info = self::findOne(['id'=>$params['id']] );
- if (!$info) {
- $msg['msg'] = '账号不存在!';
- }else{
- $result = $info->update($params);
- $id = $params['id'];
- }
-
- } else {
- if(empty($params['password'])){
- $msg['msg'] = '新账号请设置密码!';
- }else{
- // 创建
- $result = $info = self::model()::create($params);
- $id = $result->id;
- }
-
- }
- if($result){
- if(is_array($params['roles_ids'])){
- $roles = $params['roles_ids'];
- }else{
- if(empty($params['roles_ids'])){
- $roles = [];
- }else{
- $roles = explode(',',$params['roles_ids']);
- }
-
- }
- RoleUserService::submit($id,$roles);
- $msg['code'] = self::YES;
- $msg['msg'] = '设置成功';
- }else{
- $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
- }
- return $msg;
- }
- }
|