AdminService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace App\Services\Base;
  3. use App\Contracts\Repositories\Base\AdminRepository;
  4. use App\Http\Middleware\SingleLoginLimit;
  5. use App\Repositories\Criteria\Base\AdminCriteria;
  6. use App\Repositories\Eloquent\Base\AdminRepositoryEloquent;
  7. use App\Repositories\Enums\Base\AdminTypeEnum;
  8. use App\Repositories\Enums\ModelStatusEnum;
  9. use App\Repositories\Enums\ResponseCodeEnum;
  10. use App\Repositories\Enums\RoleEnum;
  11. use App\Repositories\Models\Base\Admin;
  12. use App\Repositories\Models\Base\Auth;
  13. use App\Repositories\Models\Base\Department;
  14. use App\Repositories\Models\Base\User;
  15. use App\Repositories\Presenters\Base\AdminPresenter;
  16. use Carbon\Carbon;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\Cache;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Hash;
  21. use Illuminate\Support\Str;
  22. use Tymon\JWTAuth\Facades\JWTAuth;
  23. use function PHPUnit\Framework\returnArgument;
  24. class AdminService
  25. {
  26. /**
  27. * @var AdminRepositoryEloquent
  28. */
  29. private $adminRepository;
  30. /**
  31. * AdminService constructor.
  32. *
  33. * @param AdminRepositoryEloquent $adminRepositoryEloquent
  34. */
  35. public function __construct(AdminRepositoryEloquent $adminRepositoryEloquent)
  36. {
  37. $this->adminRepository = $adminRepositoryEloquent;
  38. }
  39. /**
  40. * @param Request $request
  41. *
  42. * @return mixed
  43. * @throws \Prettus\Repository\Exceptions\RepositoryException
  44. */
  45. public function handleList(Request $request)
  46. {
  47. $this->adminRepository->pushCriteria(new AdminCriteria($request));
  48. $this->adminRepository->setPresenter(AdminPresenter::class);
  49. return $this->adminRepository->searchAdminsByPage();
  50. }
  51. /**
  52. * @param $id
  53. *
  54. * @return \Illuminate\Database\Eloquent\Model
  55. */
  56. public function handleProfile($id)
  57. {
  58. $this->adminRepository->setPresenter(AdminPresenter::class);
  59. return $this->adminRepository->searchAdminBy($id);
  60. }
  61. /**
  62. * @param array $data
  63. *
  64. * @return mixed
  65. * @throws \Prettus\Validator\Exceptions\ValidatorException
  66. */
  67. public function handleStore($data)
  68. {
  69. $data['password'] = Hash::make($data['password']);
  70. $admin = Admin::query()->where('mobile', $data['mobile'])->first();
  71. if (!$admin) {
  72. $admin = $this->adminRepository->create($data);
  73. } else {
  74. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '该手机号已经存在');
  75. $admin->fill($data);
  76. $admin->save();
  77. }
  78. if (array_key_exists('role_ids', $data) && is_array($data['role_ids'])) $admin->syncRoles($data['role_ids']);
  79. return $admin;
  80. }
  81. /**
  82. * @param array $data
  83. *
  84. * @return mixed
  85. * @throws \Prettus\Validator\Exceptions\ValidatorException
  86. */
  87. public function handleUpdate($data)
  88. {
  89. if (array_key_exists('password', $data)) unset($data['password']);
  90. $admin = $this->adminRepository->update($data, $data['id']);
  91. if (array_key_exists('role_ids', $data) && is_array($data['role_ids'])) $admin->syncRoles($data['role_ids']);
  92. return $admin;
  93. }
  94. /**
  95. * 删除用户
  96. * @param $id
  97. * @return int
  98. */
  99. public function handleDelete($id)
  100. {
  101. $admin = $this->adminRepository->find($id);
  102. // $admin->type = AdminTypeEnum::OTHER;
  103. return $admin->delete();
  104. }
  105. /**
  106. * 批量删除
  107. * @param $ids
  108. * @return int
  109. */
  110. public function handleBatchDelete($ids = [])
  111. {
  112. // return $this->adminRepository->deleteWhere([['id', 'in', $ids]]);
  113. $admins = Admin::query()->whereIn('id', $ids)->get();
  114. foreach ($admins as $admin) {
  115. // $admin->type = AdminTypeEnum::OTHER;
  116. $admin->delete();
  117. }
  118. return true;
  119. }
  120. /**
  121. * 重置密码
  122. * @param Request $request
  123. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  124. * @throws \Prettus\Validator\Exceptions\ValidatorException
  125. */
  126. public function handleResetPassword(Request $request)
  127. {
  128. return $this->adminRepository->update([
  129. 'password' => Hash::make($request->get('password', '123456')),
  130. ], $request->get('id'));
  131. }
  132. /**
  133. * 重置密码
  134. * @param Request $request
  135. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  136. * @throws \Prettus\Validator\Exceptions\ValidatorException
  137. */
  138. public function handleMeResetPassword($password, $admin_id = false)
  139. {
  140. if (!$admin_id) $admin_id = login_admin_id();
  141. return $this->adminRepository->update([
  142. 'password' => Hash::make($password),
  143. ], $admin_id);
  144. }
  145. /**
  146. * 选项数据
  147. * @param Request $request
  148. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  149. * @throws \Prettus\Repository\Exceptions\RepositoryException
  150. */
  151. public function handleSelectOptions(Request $request)
  152. {
  153. $this->adminRepository->pushCriteria(new AdminCriteria($request));
  154. // return $this->adminRepository->paginate(request('per_page', 15), ['id', 'name', 'mobile', 'username', 'sex']);
  155. return $this->adminRepository->all(['id', 'name', 'mobile', 'username', 'sex']);
  156. }
  157. /**
  158. * 账号密码登录
  159. * @param $request
  160. * @return bool
  161. */
  162. public function handleAccountLogin($credentials, $request)
  163. {
  164. $credentials['password'] = base64_decode($credentials['password']);
  165. $credentials['status'] = ModelStatusEnum::OK;
  166. if (!$token = auth()->guard('admins')->attempt($credentials)) {
  167. return false;
  168. }
  169. //单机登录限制
  170. $admin_id = login_admin_id();
  171. SingleLoginLimit::setToken('admins', $admin_id, $token);
  172. $this->adminRepository->updateLoginInfo($admin_id, $request->ip());
  173. return $token;
  174. }
  175. /**
  176. * 微信登录
  177. * @param $auth
  178. * @return mixed
  179. * @throws \Prettus\Validator\Exceptions\ValidatorException
  180. */
  181. public function handleAuthLogin($auth)
  182. {
  183. $user_id = $auth->user_id;
  184. if (!$user_id) abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
  185. $admin = $this->adminRepository->find($user_id);
  186. if ($admin['wechat_auth_id'] === 0) {
  187. abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
  188. }
  189. if ($admin['wechat_auth_id'] != $auth->id) {
  190. abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
  191. }
  192. $token = auth('admins')->login($admin);
  193. //单机登录限制
  194. $admin_id = $admin['id'];
  195. SingleLoginLimit::setToken('admins', $admin_id, $token);
  196. $this->adminRepository->updateLoginInfo($admin_id, \request()->ip());
  197. return $token;
  198. }
  199. /**
  200. * 微信登录
  201. * @param $auth
  202. * @return mixed
  203. * @throws \Prettus\Validator\Exceptions\ValidatorException
  204. */
  205. public function handleAuthMobileLogin($auth, $mobile)
  206. {
  207. $user_id = $auth->user_id;
  208. if (!$user_id) {
  209. $user = User::query()->updateOrCreate([
  210. 'mobile' => $mobile,
  211. ], [
  212. // 'username' => $mobile,
  213. // 'name' => $mobile,
  214. 'wechat_auth_id' => $auth->id,
  215. 'type' => AdminTypeEnum::STUDENT
  216. ]);
  217. $auth->user_id = $user->id;
  218. $auth->save();
  219. $user_id = $user->id;
  220. } else {
  221. $user = User::query()->where('id', $user_id)->first();
  222. if ($user->mobile != $mobile) {
  223. // $user->username = $mobile;
  224. $user->mobile = $mobile;
  225. $user->save();
  226. }
  227. }
  228. $admin = $this->adminRepository->find($user_id);
  229. if ($admin['wechat_auth_id'] === 0) {
  230. abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
  231. }
  232. if ($admin['wechat_auth_id'] != $auth->id) {
  233. abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号');
  234. }
  235. $token = auth('admins')->login($admin);
  236. //单机登录限制
  237. $admin_id = $admin['id'];
  238. SingleLoginLimit::setToken('admins', $admin_id, $token);
  239. $this->adminRepository->updateLoginInfo($admin_id, \request()->ip());
  240. return $token;
  241. }
  242. /**
  243. * 绑定微信号
  244. * @param $auth
  245. * @return bool
  246. */
  247. public function handleBindWechat($auth)
  248. {
  249. $admin_id = login_admin_id();
  250. $admin = $this->adminRepository->find($admin_id);
  251. $admin->wechat_auth_id = $auth->id;
  252. $auth->user_id = $admin_id;
  253. DB::beginTransaction();
  254. try {
  255. $auth->save();
  256. $admin->save();
  257. DB::commit();
  258. } catch (\Exception $exception) {
  259. DB::rollBack();
  260. exception($exception);
  261. }
  262. return true;
  263. }
  264. /**
  265. * 解绑微信号
  266. * @param $auth
  267. * @return bool
  268. */
  269. public function handleUnbindWechat()
  270. {
  271. $admin_id = login_admin_id();
  272. $admin = $this->adminRepository->find($admin_id);
  273. $auth = Auth::query()->find($admin->wechat_auth_id);
  274. if (!$auth) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '尚未绑定微信号');
  275. DB::beginTransaction();
  276. try {
  277. $auth->user_id = 0;
  278. $admin->wechat_auth_id = 0;
  279. $auth->save();
  280. $admin->save();
  281. DB::commit();
  282. } catch (\Exception $exception) {
  283. DB::rollBack();
  284. exception($exception);
  285. }
  286. return true;
  287. }
  288. /**
  289. * 获取登录用户信息
  290. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  291. */
  292. public function handleMe()
  293. {
  294. $this->adminRepository->setPresenter(AdminPresenter::class);
  295. return $this->adminRepository->find(login_admin_id());
  296. }
  297. /**
  298. * @param Request $request
  299. * @return mixed
  300. */
  301. public function handleConfirmPassword(Request $request)
  302. {
  303. $credentials = $request->only(['id', 'password']);
  304. return $this->adminRepository->byIdConfirmPassword($credentials['id'], $credentials['password']);
  305. }
  306. /**
  307. * 更换手机号
  308. * @param $admin
  309. * @param $mobile
  310. * @return
  311. */
  312. public function handleBindMobile($admin_id, $mobile)
  313. {
  314. $admin = $this->adminRepository->find($admin_id);
  315. $type = $admin['type'];
  316. $admin->mobile = $mobile;
  317. switch ($type) {
  318. case AdminTypeEnum::SUPPLIER:
  319. $supplier = $admin->suppliers()->where('type', SupplierTypeEnum::PERSON)->first();
  320. if ($supplier) {
  321. $supplier->contact_mobile = $mobile;
  322. $supplier->save();
  323. }
  324. break;
  325. }
  326. $admin->save();
  327. return true;
  328. }
  329. /**
  330. * 更新个人数据
  331. * @param $data
  332. * @param $admin_id
  333. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  334. * @throws \Prettus\Validator\Exceptions\ValidatorException
  335. */
  336. public function handleMeUpdate($data, $admin_id = false)
  337. {
  338. if (!$admin_id) $admin_id = login_admin_id();
  339. return $this->adminRepository->update($data, $admin_id);
  340. }
  341. }