with(["group", "company"])->when($params['status'], function (Builder $query) use ($params) { return $query->where("status", $params['status']); })->when($params['userinfo'], function (Builder $query) use ($params) { return $query->where(function (Builder $query) use ($params) { return $query->where("phone", $params['userinfo'])->orWhere("email", $params['userinfo'])->orWhere("id", $params['userinfo']); }); })->orderByDesc("id")->paginate($params['page_size']); return [ "total" => $p->total(), "page_total" => $p->lastPage(), "list" => array_map(function (User $u) { return [ "id" => Utils::idNo4($u->id), "phone" => $u->phone, "email" => $u->email, "status" => $u->status, "created_at" => $u->created_at->format("Y-m-d"), "company" => optional($u->company)->name, "group_id" => $u->group_id, "group_name" => optional($u->group)->name ?? "", 'permissions' => Arr::get($u->extra, 'permissions'), ]; }, $p->items()), ]; } public function changeStatus($data) { $u = User::findOrFail($data['id']); if ($u->id == \Auth::id()) { throw new ClientException("你不能禁用自己的账号"); } $u->status = $u->status == User::STATUS_OK ? User::STATUS_STOP : User::STATUS_OK; $u->save(); return true; } public function saveSetting($data) { $u = User::findOrFail($data['id']); $u->group_id = $data['group_id']; if ($data['password']) { $u->password = $u->hashPassword($data['password']); } $u->extra = array_merge($u->extra ?? [], [ "permissions" => $data['permissions'] ?? [], ]); $u->save(); return true; } }