123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace App\Http\Controllers\User;
- use App\Http\Controllers\AdminBaseController;
- use App\Models\Role;
- use App\Models\User;
- use App\Repositories\Eloquent\UserRepositoryEloquent;
- use App\Validators\UserValidator;
- use Illuminate\Contracts\Routing\ResponseFactory;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- /**
- * Class UsersController.
- *
- * @package namespace App\Http\Controllers;
- */
- class UsersController extends AdminBaseController
- {
- /**
- * UsersController constructor.
- *
- * @param UserRepositoryEloquent $repository
- * @param UserValidator $validator
- */
- public function __construct(UserRepositoryEloquent $repository, UserValidator $validator)
- {
- parent::__construct($repository, $validator);
- }
- protected function _indexScopeQuery()
- {
- return function($query){
- $query->where('id', '<>', 1);
- };
- }
- protected function _indexPost($datum)
- {
- foreach ( $datum as &$data ) {
- $data->role_name = Role::where('id',$data->{'roles_id'})->value('name')?:'--';
- }
- unset($data);
- return $datum;
- }
- protected function _storeGet($request)
- {
- // 角色
- $roles = Role::where('status',1)->orderBy('id', 'asc')->get();
- $option = [
- 'checked' => '请选择角色',
- 'attr' => [
- [
- 'value' => 0,
- 'label' => "请选择角色",
- 'disabled' => false,
- 'notice' => '*'
- ]
- ]
- ];
- foreach ( $roles as $category ) {
- $option['attr'][] = [
- 'value' => $category->id,
- 'label' => $category->name,
- 'disabled' => false,
- 'notice' => "*"
- ];
- }
- $params['options'] = json_encode($option,JSON_UNESCAPED_SLASHES);
- return $params;
- }
- protected function _storePost($request)
- {
- $email = $request->input('email', "");
- $password = $request->input('password', "");
- $roleID = $request->input('role_id', "");
- if ( empty($email) ) {
- $this->errorMsg = "注册账号必填";
- return false;
- }
- if ( empty($password) ) {
- $this->errorMsg = "登录密码必填";
- return false;
- }
- if ( $roleID <= 0 ) {
- $this->errorMsg = "请选择角色";
- return false;
- }
- $count = User::where('email', '=', $email)->count();
- if ( $count > 0 ) {
- $this->errorMsg = "账号已注册";
- return false;
- }
- $user = $this->repository->create([
- 'name' => $request->input('name', ""),
- 'email' => $email,
- 'password' => bcrypt($password),
- 'roles_id' =>$roleID,
- ]);
- // // 添加角色
- // DB::table('user_has_roles')->insert([
- // 'role_id' => $roleID,
- // 'user_id' => $user->id,
- // ]);
- return true;
- }
- protected function _editGet($id)
- {
- $params['model'] = $this->repository->find($id);
- $userRole = DB::table('users')->where('id', $id)->first();
- if ( !empty($userRole) ) {
- $userRole = Role::where('id', $userRole->roles_id)->first();
- }
- // 角色
- $roles = Role::where('status',1)->orderBy('id', 'asc')->get();
- $option = [
- 'checked' => empty($userRole) ? "" : $userRole->name,
- 'attr' => []
- ];
- foreach ( $roles as $category ) {
- $option['attr'][] = [
- 'value' => $category->id,
- 'label' => $category->name,
- 'disabled' => false,
- 'notice' => "*"
- ];
- }
- $params['options'] = json_encode($option,JSON_UNESCAPED_SLASHES);
- return $params;
- }
- protected function _updatePost($request, $id)
- {
- $model = $this->repository->find($id);
- $email = $request->input('email', "");
- $password = $request->input('password', "");
- $roleID = $request->input('role_id', "");
- if ( empty($email) ) {
- $this->errorMsg = "注册账号必填";
- return false;
- }
- $role = Role::where('id', $roleID)->orWhere('name',$roleID)->first();
- if ( empty($role) ) {
- $this->errorMsg = "请选择角色";
- return false;
- }
- $roleID = $role['id'];
- $count = User::where('email', '=', $email)->where('id', '<>', $id)->count();
- if ( $count > 0 ) {
- $this->errorMsg = "账号已注册";
- return false;
- }
- $update = array();
- if ( $model->name != $request->input('name', "") ) {
- $update['name'] = $request->input('name', "");
- }
- if ( $model->email != $email ) {
- $update['email'] = $email;
- }
- if ( !empty($password) ) {
- $update['password'] = bcrypt($password);
- }
- if(!empty($roleID)){
- $update['roles_id'] = $roleID;
- }
- if ( !empty($update) ) {
- $this->repository->update($update, $id);
- }
- // $userRole = DB::table('user_has_roles')->where('user_id', $id)->first();
- // if ( $userRole->role_id != $role->{'id'} ) {
- // // 更新角色
- // DB::table('user_has_roles')->where('user_id', $id)->where('role_id', $userRole->role_id)->update([
- // 'role_id' => $roleID,
- // ]);
- // }
- return true;
- }
- /**
- * @param Request $request
- * @param ResponseFactory $response
- * @param $id
- * @return \Illuminate\Http\JsonResponse
- *
- * 删除一行数据
- */
- protected function destroy(Request $request,ResponseFactory $response,$id = null)
- {
- if( $request->isMethod('post') && $request->ajax() ){
- if ( $id <= 1 ) {
- $data = [
- 'status' => 1,
- 'message' => '超级账号不能删除'
- ];
- }else{
- $deleted = $this->repository->delete($id);
- $deleted === false ? $data = [
- 'status' => 1,
- 'message'=> self::ERROR_MSG
- ]: $data = [
- 'status' => 0,
- 'message' => ''
- ];
- }
- if( $request->wantsJson() ){
- return $response->json($data);
- }
- }
- }
- }
|