UsersController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Models\Role;
  5. use App\Models\User;
  6. use App\Repositories\Eloquent\UserRepositoryEloquent;
  7. use App\Validators\UserValidator;
  8. use Illuminate\Contracts\Routing\ResponseFactory;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. /**
  12. * Class UsersController.
  13. *
  14. * @package namespace App\Http\Controllers;
  15. */
  16. class UsersController extends AdminBaseController
  17. {
  18. /**
  19. * UsersController constructor.
  20. *
  21. * @param UserRepositoryEloquent $repository
  22. * @param UserValidator $validator
  23. */
  24. public function __construct(UserRepositoryEloquent $repository, UserValidator $validator)
  25. {
  26. parent::__construct($repository, $validator);
  27. }
  28. protected function _indexScopeQuery()
  29. {
  30. return function($query){
  31. $query->where('id', '<>', 1);
  32. };
  33. }
  34. protected function _indexPost($datum)
  35. {
  36. foreach ( $datum as &$data ) {
  37. $data->role_name = Role::where('id',$data->{'roles_id'})->value('name')?:'--';
  38. }
  39. unset($data);
  40. return $datum;
  41. }
  42. protected function _storeGet($request)
  43. {
  44. // 角色
  45. $roles = Role::where('status',1)->orderBy('id', 'asc')->get();
  46. $option = [
  47. 'checked' => '请选择角色',
  48. 'attr' => [
  49. [
  50. 'value' => 0,
  51. 'label' => "请选择角色",
  52. 'disabled' => false,
  53. 'notice' => '*'
  54. ]
  55. ]
  56. ];
  57. foreach ( $roles as $category ) {
  58. $option['attr'][] = [
  59. 'value' => $category->id,
  60. 'label' => $category->name,
  61. 'disabled' => false,
  62. 'notice' => "*"
  63. ];
  64. }
  65. $params['options'] = json_encode($option,JSON_UNESCAPED_SLASHES);
  66. return $params;
  67. }
  68. protected function _storePost($request)
  69. {
  70. $email = $request->input('email', "");
  71. $password = $request->input('password', "");
  72. $roleID = $request->input('role_id', "");
  73. if ( empty($email) ) {
  74. $this->errorMsg = "注册账号必填";
  75. return false;
  76. }
  77. if ( empty($password) ) {
  78. $this->errorMsg = "登录密码必填";
  79. return false;
  80. }
  81. if ( $roleID <= 0 ) {
  82. $this->errorMsg = "请选择角色";
  83. return false;
  84. }
  85. $count = User::where('email', '=', $email)->count();
  86. if ( $count > 0 ) {
  87. $this->errorMsg = "账号已注册";
  88. return false;
  89. }
  90. $user = $this->repository->create([
  91. 'name' => $request->input('name', ""),
  92. 'email' => $email,
  93. 'password' => bcrypt($password),
  94. 'roles_id' =>$roleID,
  95. ]);
  96. // // 添加角色
  97. // DB::table('user_has_roles')->insert([
  98. // 'role_id' => $roleID,
  99. // 'user_id' => $user->id,
  100. // ]);
  101. return true;
  102. }
  103. protected function _editGet($id)
  104. {
  105. $params['model'] = $this->repository->find($id);
  106. $userRole = DB::table('users')->where('id', $id)->first();
  107. if ( !empty($userRole) ) {
  108. $userRole = Role::where('id', $userRole->roles_id)->first();
  109. }
  110. // 角色
  111. $roles = Role::where('status',1)->orderBy('id', 'asc')->get();
  112. $option = [
  113. 'checked' => empty($userRole) ? "" : $userRole->name,
  114. 'attr' => []
  115. ];
  116. foreach ( $roles as $category ) {
  117. $option['attr'][] = [
  118. 'value' => $category->id,
  119. 'label' => $category->name,
  120. 'disabled' => false,
  121. 'notice' => "*"
  122. ];
  123. }
  124. $params['options'] = json_encode($option,JSON_UNESCAPED_SLASHES);
  125. return $params;
  126. }
  127. protected function _updatePost($request, $id)
  128. {
  129. $model = $this->repository->find($id);
  130. $email = $request->input('email', "");
  131. $password = $request->input('password', "");
  132. $roleID = $request->input('role_id', "");
  133. if ( empty($email) ) {
  134. $this->errorMsg = "注册账号必填";
  135. return false;
  136. }
  137. $role = Role::where('id', $roleID)->orWhere('name',$roleID)->first();
  138. if ( empty($role) ) {
  139. $this->errorMsg = "请选择角色";
  140. return false;
  141. }
  142. $roleID = $role['id'];
  143. $count = User::where('email', '=', $email)->where('id', '<>', $id)->count();
  144. if ( $count > 0 ) {
  145. $this->errorMsg = "账号已注册";
  146. return false;
  147. }
  148. $update = array();
  149. if ( $model->name != $request->input('name', "") ) {
  150. $update['name'] = $request->input('name', "");
  151. }
  152. if ( $model->email != $email ) {
  153. $update['email'] = $email;
  154. }
  155. if ( !empty($password) ) {
  156. $update['password'] = bcrypt($password);
  157. }
  158. if(!empty($roleID)){
  159. $update['roles_id'] = $roleID;
  160. }
  161. if ( !empty($update) ) {
  162. $this->repository->update($update, $id);
  163. }
  164. // $userRole = DB::table('user_has_roles')->where('user_id', $id)->first();
  165. // if ( $userRole->role_id != $role->{'id'} ) {
  166. // // 更新角色
  167. // DB::table('user_has_roles')->where('user_id', $id)->where('role_id', $userRole->role_id)->update([
  168. // 'role_id' => $roleID,
  169. // ]);
  170. // }
  171. return true;
  172. }
  173. /**
  174. * @param Request $request
  175. * @param ResponseFactory $response
  176. * @param $id
  177. * @return \Illuminate\Http\JsonResponse
  178. *
  179. * 删除一行数据
  180. */
  181. protected function destroy(Request $request,ResponseFactory $response,$id = null)
  182. {
  183. if( $request->isMethod('post') && $request->ajax() ){
  184. if ( $id <= 1 ) {
  185. $data = [
  186. 'status' => 1,
  187. 'message' => '超级账号不能删除'
  188. ];
  189. }else{
  190. $deleted = $this->repository->delete($id);
  191. $deleted === false ? $data = [
  192. 'status' => 1,
  193. 'message'=> self::ERROR_MSG
  194. ]: $data = [
  195. 'status' => 0,
  196. 'message' => ''
  197. ];
  198. }
  199. if( $request->wantsJson() ){
  200. return $response->json($data);
  201. }
  202. }
  203. }
  204. }