AdminService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Services;
  3. use App\Services\BaseService;
  4. use App\Models\Admin;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Services\RoleUserService;
  9. /**
  10. * 菜单
  11. */
  12. class AdminService extends BaseService
  13. {
  14. /**
  15. * @description: 模型
  16. * @return {string}
  17. */
  18. public static function model(): string
  19. {
  20. return Admin::class;
  21. }
  22. /**
  23. * @description: 枚举
  24. * @return {*}
  25. */
  26. public static function enum(): string
  27. {
  28. return '';
  29. }
  30. /**
  31. * @description: 获取查询条件
  32. * @param {array} $search 查询内容
  33. * @return {array}
  34. */
  35. public static function getWhere(array $search = []): array
  36. {
  37. $where = [];
  38. if (isset($search['id']) && !empty($search['id'])) {
  39. $where[] = ['id', '=', $search['id']];
  40. }
  41. if (isset($search['username']) && !empty($search['username'])) {
  42. $where[] = ['username', '=', $search['username']];
  43. }
  44. if (isset($search['nickname']) && !empty($search['nickname'])) {
  45. $where[] = ['nickname', 'like', '%'.$search['nickname'].'%'];
  46. }
  47. return $where;
  48. }
  49. /**
  50. * @description: 查询单条数据
  51. * @param array $search
  52. * @return \App\Models\Coin|null
  53. */
  54. public static function findOne(array $search): ?Admin
  55. {
  56. return self::model()::with('roles')->where(self::getWhere($search))->first();
  57. }
  58. /**
  59. * @description: 查询所有数据
  60. * @param array $search
  61. * @return \Illuminate\Database\Eloquent\Collection
  62. */
  63. public static function findAll(array $search = [])
  64. {
  65. return self::model()::with('roles')->where(self::getWhere($search))->get();
  66. }
  67. /**
  68. * @description: 分页查询
  69. * @param array $search
  70. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  71. */
  72. public static function paginate(array $search = [])
  73. {
  74. $limit = isset($search['limit']) ? $search['limit'] : 15;
  75. $paginator = self::model()::with('roles')->where(self::getWhere($search))
  76. // ->orderBy("sort", 'asc')
  77. ->paginate($limit);
  78. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  79. }
  80. /**
  81. * @description:
  82. * @param {*} $params
  83. * @return {*}
  84. */
  85. public static function submit($params = [])
  86. {
  87. $result = false;
  88. $msg['code'] = self::NOT;
  89. $msg['msg'] = '';
  90. if(isset($params['password']) && !empty($params['password'])){
  91. $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
  92. }else{
  93. unset($params['password']);
  94. }
  95. // 2. 判断是否是更新
  96. if (!empty($params['id'])) {
  97. // 更新
  98. $info = self::findOne(['id'=>$params['id']] );
  99. if (!$info) {
  100. $msg['msg'] = '账号不存在!';
  101. }else{
  102. $result = $info->update($params);
  103. $id = $params['id'];
  104. }
  105. } else {
  106. if(empty($params['password'])){
  107. $msg['msg'] = '新账号请设置密码!';
  108. }else{
  109. // 创建
  110. $result = $info = self::model()::create($params);
  111. $id = $result->id;
  112. }
  113. }
  114. if($result){
  115. if(is_array($params['roles_ids'])){
  116. $roles = $params['roles_ids'];
  117. }else{
  118. if(empty($params['roles_ids'])){
  119. $roles = [];
  120. }else{
  121. $roles = explode(',',$params['roles_ids']);
  122. }
  123. }
  124. RoleUserService::submit($id,$roles);
  125. $msg['code'] = self::YES;
  126. $msg['msg'] = '设置成功';
  127. }else{
  128. $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
  129. }
  130. return $msg;
  131. }
  132. }