123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?php
- namespace App\Services\Base;
- use App\Contracts\Repositories\Base\AdminRepository;
- use App\Http\Middleware\SingleLoginLimit;
- use App\Repositories\Criteria\Base\AdminCriteria;
- use App\Repositories\Eloquent\Base\AdminRepositoryEloquent;
- use App\Repositories\Enums\Base\AdminTypeEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Enums\RoleEnum;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Base\Auth;
- use App\Repositories\Models\Base\Department;
- use App\Repositories\Models\Base\User;
- use App\Repositories\Presenters\Base\AdminPresenter;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Str;
- use Tymon\JWTAuth\Facades\JWTAuth;
- use function PHPUnit\Framework\returnArgument;
- class AdminService
- {
- /**
- * @var AdminRepositoryEloquent
- */
- private $adminRepository;
- /**
- * AdminService constructor.
- *
- * @param AdminRepositoryEloquent $adminRepositoryEloquent
- */
- public function __construct(AdminRepositoryEloquent $adminRepositoryEloquent)
- {
- $this->adminRepository = $adminRepositoryEloquent;
- }
- /**
- * @param Request $request
- *
- * @return mixed
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- */
- public function handleList(Request $request)
- {
- $this->adminRepository->pushCriteria(new AdminCriteria($request));
- $this->adminRepository->setPresenter(AdminPresenter::class);
- return $this->adminRepository->searchAdminsByPage();
- }
- /**
- * @param $id
- *
- * @return \Illuminate\Database\Eloquent\Model
- */
- public function handleProfile($id)
- {
- $this->adminRepository->setPresenter(AdminPresenter::class);
- return $this->adminRepository->searchAdminBy($id);
- }
- /**
- * @param array $data
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleStore($data)
- {
- $data['password'] = Hash::make($data['password']);
- $admin = Admin::query()->where('mobile', $data['mobile'])->first();
- if (!$admin) {
- $admin = $this->adminRepository->create($data);
- } else {
- abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '该手机号已经存在');
- $admin->fill($data);
- $admin->save();
- }
- if (array_key_exists('role_ids', $data) && is_array($data['role_ids'])) $admin->syncRoles($data['role_ids']);
- return $admin;
- }
- /**
- * @param array $data
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleUpdate($data)
- {
- if (array_key_exists('password', $data)) unset($data['password']);
- $admin = $this->adminRepository->update($data, $data['id']);
- if (array_key_exists('role_ids', $data) && is_array($data['role_ids'])) $admin->syncRoles($data['role_ids']);
- return $admin;
- }
- /**
- * 删除用户
- * @param $id
- * @return int
- */
- public function handleDelete($id)
- {
- $admin = $this->adminRepository->find($id);
- // $admin->type = AdminTypeEnum::OTHER;
- return $admin->delete();
- }
- /**
- * 批量删除
- * @param $ids
- * @return int
- */
- public function handleBatchDelete($ids = [])
- {
- // return $this->adminRepository->deleteWhere([['id', 'in', $ids]]);
- $admins = Admin::query()->whereIn('id', $ids)->get();
- foreach ($admins as $admin) {
- // $admin->type = AdminTypeEnum::OTHER;
- $admin->delete();
- }
- return true;
- }
- /**
- * 重置密码
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleResetPassword(Request $request)
- {
- return $this->adminRepository->update([
- 'password' => Hash::make($request->get('password', '123456')),
- ], $request->get('id'));
- }
- /**
- * 重置密码
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleMeResetPassword($password, $admin_id = false)
- {
- if (!$admin_id) $admin_id = login_admin_id();
- return $this->adminRepository->update([
- 'password' => Hash::make($password),
- ], $admin_id);
- }
- /**
- * 选项数据
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- */
- public function handleSelectOptions(Request $request)
- {
- $this->adminRepository->pushCriteria(new AdminCriteria($request));
- // return $this->adminRepository->paginate(request('per_page', 15), ['id', 'name', 'mobile', 'username', 'sex']);
- return $this->adminRepository->all(['id', 'name', 'mobile', 'username', 'sex']);
- }
- /**
- * 账号密码登录
- * @param $request
- * @return bool
- */
- public function handleAccountLogin($credentials, $request)
- {
- $credentials['password'] = base64_decode($credentials['password']);
- $credentials['status'] = ModelStatusEnum::OK;
- if (!$token = auth()->guard('admins')->attempt($credentials)) {
- return false;
- }
- //单机登录限制
- $admin_id = login_admin_id();
- SingleLoginLimit::setToken('admins', $admin_id, $token);
- $this->adminRepository->updateLoginInfo($admin_id, $request->ip());
- return $token;
- }
- /**
- * 微信登录
- * @param $auth
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleAuthLogin($auth)
- {
- $user_id = $auth->user_id;
- if (!$user_id) abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
- $admin = $this->adminRepository->find($user_id);
- if ($admin['wechat_auth_id'] === 0) {
- abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
- }
- if ($admin['wechat_auth_id'] != $auth->id) {
- abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
- }
- $token = auth('admins')->login($admin);
- //单机登录限制
- $admin_id = $admin['id'];
- SingleLoginLimit::setToken('admins', $admin_id, $token);
- $this->adminRepository->updateLoginInfo($admin_id, \request()->ip());
- return $token;
- }
- /**
- * 微信登录
- * @param $auth
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleAuthMobileLogin($auth, $mobile)
- {
- $user_id = $auth->user_id;
- if (!$user_id) {
- $user = User::query()->updateOrCreate([
- 'mobile' => $mobile,
- ], [
- // 'username' => $mobile,
- // 'name' => $mobile,
- 'wechat_auth_id' => $auth->id,
- 'type' => AdminTypeEnum::STUDENT
- ]);
- $auth->user_id = $user->id;
- $auth->save();
- $user_id = $user->id;
- } else {
- $user = User::query()->where('id', $user_id)->first();
- if ($user->mobile != $mobile) {
- // $user->username = $mobile;
- $user->mobile = $mobile;
- $user->save();
- }
- }
- $admin = $this->adminRepository->find($user_id);
- if ($admin['wechat_auth_id'] === 0) {
- abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
- }
- if ($admin['wechat_auth_id'] != $auth->id) {
- abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
- }
- $token = auth('admins')->login($admin);
- //单机登录限制
- $admin_id = $admin['id'];
- SingleLoginLimit::setToken('admins', $admin_id, $token);
- $this->adminRepository->updateLoginInfo($admin_id, \request()->ip());
- return $token;
- }
- /**
- * 绑定微信号
- * @param $auth
- * @return bool
- */
- public function handleBindWechat($auth)
- {
- $admin_id = login_admin_id();
- $admin = $this->adminRepository->find($admin_id);
- $admin->wechat_auth_id = $auth->id;
- $auth->user_id = $admin_id;
- DB::beginTransaction();
- try {
- $auth->save();
- $admin->save();
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- exception($exception);
- }
- return true;
- }
- /**
- * 解绑微信号
- * @param $auth
- * @return bool
- */
- public function handleUnbindWechat()
- {
- $admin_id = login_admin_id();
- $admin = $this->adminRepository->find($admin_id);
- $auth = Auth::query()->find($admin->wechat_auth_id);
- if (!$auth) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '尚未绑定微信号');
- DB::beginTransaction();
- try {
- $auth->user_id = 0;
- $admin->wechat_auth_id = 0;
- $auth->save();
- $admin->save();
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- exception($exception);
- }
- return true;
- }
- /**
- * 获取登录用户信息
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- */
- public function handleMe()
- {
- $this->adminRepository->setPresenter(AdminPresenter::class);
- return $this->adminRepository->find(login_admin_id());
- }
- /**
- * @param Request $request
- * @return mixed
- */
- public function handleConfirmPassword(Request $request)
- {
- $credentials = $request->only(['id', 'password']);
- return $this->adminRepository->byIdConfirmPassword($credentials['id'], $credentials['password']);
- }
- /**
- * 更换手机号
- * @param $admin
- * @param $mobile
- * @return
- */
- public function handleBindMobile($admin_id, $mobile)
- {
- $admin = $this->adminRepository->find($admin_id);
- $type = $admin['type'];
- $admin->mobile = $mobile;
- switch ($type) {
- case AdminTypeEnum::SUPPLIER:
- $supplier = $admin->suppliers()->where('type', SupplierTypeEnum::PERSON)->first();
- if ($supplier) {
- $supplier->contact_mobile = $mobile;
- $supplier->save();
- }
- break;
- }
- $admin->save();
- return true;
- }
- /**
- * 更新个人数据
- * @param $data
- * @param $admin_id
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleMeUpdate($data, $admin_id = false)
- {
- if (!$admin_id) $admin_id = login_admin_id();
- return $this->adminRepository->update($data, $admin_id);
- }
- }
|