12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace App\Modules\Admin\Services;
- use App\Base\BaseService;
- use App\Base\Utils;
- use App\Exceptions\ClientException;
- use App\Models\User\User;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Arr;
- class UserService extends BaseService
- {
- public function paginate($params)
- {
- $p = User::query()->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;
- }
- }
|