UserService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. namespace App\Services\Base;
  3. use App\Http\Middleware\SingleLoginLimit;
  4. use App\Repositories\Criteria\Base\UserCriteria;
  5. use App\Repositories\Eloquent\Base\UserRepositoryEloquent;
  6. use App\Repositories\Enums\Base\UserBindMobileEnum;
  7. use App\Repositories\Enums\ModelStatusEnum;
  8. use App\Repositories\Enums\ResponseCodeEnum;
  9. use App\Repositories\Models\Api\Base\Banks;
  10. use App\Repositories\Models\Api\Base\User;
  11. use App\Repositories\Models\Base\Auth;
  12. use App\Repositories\Models\Human\Dispatch;
  13. use App\Repositories\Models\Human\Human;
  14. use App\Repositories\Models\Human\Interview;
  15. use App\Repositories\Models\Human\MoveRecord;
  16. use App\Repositories\Models\Human\Staff;
  17. use App\Repositories\Presenters\Base\UserPresenter;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Hash;
  21. use Illuminate\Support\Str;
  22. class UserService
  23. {
  24. /**
  25. * @var UserRepositoryEloquent
  26. */
  27. private $userRepository;
  28. /**
  29. * UserService constructor.
  30. *
  31. * @param UserRepositoryEloquent $userRepositoryEloquent
  32. */
  33. public function __construct(UserRepositoryEloquent $userRepositoryEloquent)
  34. {
  35. $this->userRepository = $userRepositoryEloquent;
  36. }
  37. /**
  38. * @param Request $request
  39. *
  40. * @return mixed
  41. * @throws \Prettus\Repository\Exceptions\RepositoryException
  42. */
  43. public function handleList(Request $request)
  44. {
  45. $this->userRepository->pushCriteria(new UserCriteria($request));
  46. $this->userRepository->setPresenter(UserPresenter::class);
  47. return $this->userRepository->searchUsersByPage();
  48. }
  49. /**
  50. * @param $id
  51. *
  52. * @return \Illuminate\Database\Eloquent\Model
  53. */
  54. public function handleProfile($id)
  55. {
  56. $this->userRepository->setPresenter(UserPresenter::class);
  57. return $this->userRepository->searchUserBy($id);
  58. }
  59. /**
  60. * @param array $data
  61. *
  62. * @return mixed
  63. * @throws \Prettus\Validator\Exceptions\ValidatorException
  64. */
  65. public function handleStore($data)
  66. {
  67. $user = $this->userRepository->create($data);
  68. return $user;
  69. }
  70. /**
  71. * @param array $data
  72. *
  73. * @return mixed
  74. * @throws \Prettus\Validator\Exceptions\ValidatorException
  75. */
  76. public function handleUpdate($data)
  77. {
  78. $user = $this->userRepository->update($data, $data['id']);
  79. return $user;
  80. }
  81. /**
  82. * @param Request $request
  83. *
  84. * @return mixed
  85. * @throws \Prettus\Validator\Exceptions\ValidatorException
  86. */
  87. public function handleDelete($id)
  88. {
  89. return $this->userRepository->delete($id);
  90. }
  91. /**
  92. * 账号密码登录
  93. * @param $request
  94. * @return bool
  95. */
  96. public function handleAccountLogin($credentials, $request)
  97. {
  98. $credentials['password'] = base64_decode($credentials['password']);
  99. $credentials['status'] = ModelStatusEnum::OK;
  100. if (!$token = auth()->guard('api')->attempt($credentials)) {
  101. return false;
  102. }
  103. //单机登录限制
  104. $user_id = login_user_id();
  105. SingleLoginLimit::setToken('api', $user_id, $token);
  106. $this->userRepository->updateLoginInfo($user_id, $request->ip());
  107. return $token;
  108. }
  109. /**
  110. * 微信登录
  111. * @param $auth
  112. * @return mixed
  113. * @throws \Prettus\Validator\Exceptions\ValidatorException
  114. */
  115. public function handleAuthLogin($auth)
  116. {
  117. $user_id = $auth->user_id;
  118. if (!$user_id) {
  119. $user = $this->userRepository->create([
  120. 'nickname' => '用户-' . rand(10000, 99999),
  121. 'wechat_auth_id' => $auth->id,
  122. 'password' => Hash::make(Str::random(8)),
  123. ]);
  124. Auth::query()->where('id', $auth->id)->update([
  125. 'user_id' => $user['id']
  126. ]);
  127. } else {
  128. $user = $this->userRepository->find($user_id);
  129. }
  130. $token = auth('api')->login($user);
  131. //单机登录限制
  132. $user_id = $user['id'];
  133. SingleLoginLimit::setToken('api', $user_id, $token);
  134. $this->userRepository->updateLoginInfo($user_id, \request()->ip());
  135. return $token;
  136. }
  137. /**
  138. * 绑定微信号
  139. * @param $auth
  140. * @return bool
  141. */
  142. public function handleBindWechat($auth)
  143. {
  144. $user_id = login_user_id();
  145. $user = $this->userRepository->find($user_id);
  146. $user->wechat_auth_id = $auth->id;
  147. $auth->user_id = $user_id;
  148. DB::beginTransaction();
  149. try {
  150. $auth->save();
  151. $user->save();
  152. DB::commit();
  153. } catch (\Exception $exception) {
  154. DB::rollBack();
  155. exception($exception);
  156. }
  157. return true;
  158. }
  159. /**
  160. * 解绑微信号
  161. * @param $auth
  162. * @return bool
  163. */
  164. public function handleUnbindWechat()
  165. {
  166. $user_id = login_user_id();
  167. $user = $this->userRepository->find($user_id);
  168. $auth = Auth::query()->find($user->wechat_auth_id);
  169. if (!$auth) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '尚未绑定微信号');
  170. DB::beginTransaction();
  171. try {
  172. $auth->user_id = 0;
  173. $user->wechat_auth_id = 0;
  174. $auth->save();
  175. $user->save();
  176. DB::commit();
  177. } catch (\Exception $exception) {
  178. DB::rollBack();
  179. exception($exception);
  180. }
  181. return true;
  182. }
  183. /**
  184. * 获取登录用户信息
  185. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  186. */
  187. public function handleMe()
  188. {
  189. $this->userRepository->setPresenter(\App\Repositories\Presenters\Base\UserPresenter::class);
  190. return $this->userRepository->find(login_user_id());
  191. }
  192. /**
  193. * @param Request $request
  194. * @return mixed
  195. */
  196. public function handleConfirmPassword(Request $request)
  197. {
  198. $credentials = $request->only(['id', 'password']);
  199. return $this->userRepository->byIdConfirmPassword($credentials['id'], $credentials['password']);
  200. }
  201. /**
  202. * 更换手机号
  203. * @param $user
  204. * @param $mobile
  205. * @return
  206. */
  207. public function handleBindMobile($user_id, $mobile)
  208. {
  209. $user = $this->userRepository->find($user_id);
  210. $user->mobile = $mobile;
  211. $user->is_bind_mobile = UserBindMobileEnum::YES;
  212. $user->band_mobile_time = time();
  213. $user->save();
  214. return true;
  215. }
  216. /**
  217. * 更新个人数据
  218. * @param $data
  219. * @param $user_id
  220. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  221. * @throws \Prettus\Validator\Exceptions\ValidatorException
  222. */
  223. public function handleMeUpdate($data, $user_id = false)
  224. {
  225. if (!$user_id) $user_id = login_user_id();
  226. return $this->userRepository->update($data, $user_id);
  227. }
  228. /**
  229. * 实名认证
  230. * @return bool
  231. */
  232. public function handleRealName($data, $user_id = false)
  233. {
  234. if (!$user_id) $user_id = login_user_id();
  235. $user = User::query()->find($user_id);
  236. DB::beginTransaction();
  237. try {
  238. $human = Human::query()->where('card_id', $data['card_id'])->first();
  239. if (!$human) {
  240. $human = Human::query()->create([
  241. 'name' => $data['name'],
  242. 'card_id' => $data['card_id'],
  243. 'nation' => $data['nation'],
  244. 'card_start_date' => $data['start_date'],
  245. 'card_end_date' => $data['end_date'],
  246. 'card_positive_path' => $data['card_positive_path'],
  247. 'card_reverse_path' => $data['card_reverse_path'],
  248. 'address' => $data['address'],
  249. 'native' => $data['native'],
  250. 'is_real_name' => 1,
  251. 'user_id' => $user_id,
  252. ]);
  253. $user->humen_id = $human->id;
  254. if ($user->is_bind_mobile) {
  255. $human->mobile = $user->mobile;
  256. $human->band_mobile_time = $user->band_mobile_time;
  257. $human->is_band_mobile = 1;
  258. }
  259. DB::commit();
  260. return true;
  261. }
  262. $human->fill([
  263. 'name' => $data['name'],
  264. 'card_id' => $data['card_id'],
  265. 'nation' => $data['nation'],
  266. 'card_start_date' => $data['start_date'],
  267. 'card_end_date' => $data['end_date'],
  268. 'card_positive_path' => $data['card_positive_path'],
  269. 'card_reverse_path' => $data['card_reverse_path'],
  270. 'address' => $data['address'],
  271. 'native' => $data['native'],
  272. 'is_real_name' => 1,
  273. 'user_id' => $user_id,
  274. ]);
  275. $old_user = User::query()->where('human_id', $human->id)->where('status', ModelStatusEnum::OK)->first();
  276. $user->human_id = $human->id;
  277. if ($user->is_bind_mobile && $user->band_mobile_time > $human->band_mobile_time) {
  278. $human->mobile = $user->mobile;
  279. $human->band_mobile_time = $user->band_mobile_time;
  280. $human->is_band_mobile = 1;
  281. }
  282. if ($old_user) {
  283. Auth::query()->where('user_id', $user_id)->update(['user_id' => $old_user->id]);
  284. $user->status = ModelStatusEnum::PAUSE;
  285. $user->delete();
  286. } else {
  287. $user->save();
  288. Dispatch::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]);
  289. Interview::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]);
  290. Staff::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]);
  291. MoveRecord::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]);
  292. }
  293. $human->save();
  294. DB::commit();
  295. } catch (\Exception $exception) {
  296. DB::rollBack();
  297. exception($exception);
  298. return false;
  299. }
  300. return true;
  301. }
  302. /**
  303. * 手机号登录
  304. * @param $mobile
  305. * @return string
  306. * @throws \Prettus\Validator\Exceptions\ValidatorException
  307. */
  308. public function handleMobileLogin($mobile)
  309. {
  310. $user = User::query()->where('mobile', $mobile)->first();
  311. if (!$user) {
  312. $user = User::query()->create([
  313. 'nickname' => '用户-' . rand(10000, 99999),
  314. 'password' => Hash::make(Str::random(8)),
  315. 'mobile' => $mobile,
  316. 'is_bind_mobile' => 1,
  317. ]);
  318. }
  319. $token = auth('api')->login($user);
  320. //单机登录限制
  321. $user_id = $user['id'];
  322. SingleLoginLimit::setToken('api', $user_id, $token);
  323. $this->userRepository->updateLoginInfo($user_id, \request()->ip());
  324. return $token;
  325. }
  326. /**
  327. * 更新银行卡
  328. * @param $data
  329. * @return bool
  330. */
  331. public function handleBank($data)
  332. {
  333. $user = User::query()->find(login_user_id());
  334. if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名');
  335. $human = Human::query()->find($user['human_id']);
  336. DB::beginTransaction();
  337. try {
  338. $human->fill([
  339. 'bank_path' => $data['path'],
  340. 'bank_no' => $data['bank_card_number'],
  341. 'bank_name' => $data['bank_name'],
  342. 'bank_valid_date' => $data['valid_date'],
  343. 'bank_type' => $data['bank_card_type'],
  344. 'bank_holder_name' => $data['holder_name'],
  345. ]);
  346. $human->save();
  347. Banks::query()->create([
  348. 'path' => $data['path'],
  349. 'no' => $data['bank_card_number'],
  350. 'name' => $data['bank_name'],
  351. 'valid_date' => $data['valid_date'],
  352. 'type' => $data['bank_card_type'],
  353. 'holder_name' => $data['holder_name'],
  354. ]);
  355. DB::commit();
  356. } catch (\Exception $exception) {
  357. DB::rollBack();
  358. exception($exception);
  359. }
  360. return true;
  361. }
  362. /**
  363. * 更新登记注册表
  364. * @param $data
  365. * @return bool
  366. */
  367. public function handleUpdateRegisterInfo($data)
  368. {
  369. $user = User::query()->find(login_user_id());
  370. if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名');
  371. $human = Human::query()->find($user['human_id']);
  372. DB::beginTransaction();
  373. try {
  374. $human->fill($data);
  375. $human->save();
  376. DB::commit();
  377. } catch (\Exception $exception) {
  378. DB::rollBack();
  379. exception($exception);
  380. }
  381. return true;
  382. }
  383. /**
  384. * 实名信息
  385. * @return array
  386. */
  387. public function handleRealNameInfo()
  388. {
  389. $user = User::query()->find(login_user_id());
  390. if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名');
  391. $human = Human::query()->find($user['human_id']);
  392. return [
  393. 'name' => $human['name'],
  394. 'card_id' => $human['card_id'],
  395. 'card_start_date' => $human['card_start_date'],
  396. 'card_end_date' => $human['card_end_date'],
  397. ];
  398. }
  399. /**
  400. * 实名信息
  401. * @return array
  402. */
  403. public function handleRegisterInfo()
  404. {
  405. $user = User::query()->find(login_user_id());
  406. if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名');
  407. $human = Human::query()->find($user['human_id']);
  408. return [
  409. 'nation' => $human['nation'],
  410. 'eye' => $human['eye'],
  411. 'height' => $human['height'],
  412. 'health_status' => $human['health_status'],
  413. 'marriage_status' => $human['marriage_status'],
  414. 'education_status' => $human['education_status'],
  415. 'major' => $human['major'],
  416. 'address' => $human['address'],
  417. 'contact_name' => $human['contact_name'],
  418. 'contact_type' => $human['contact_type'],
  419. 'contact_mobile' => $human['contact_mobile'],
  420. ];
  421. }
  422. /**
  423. * 银行卡信息
  424. * @return array
  425. */
  426. public function handleBankInfo()
  427. {
  428. $user = User::query()->find(login_user_id());
  429. if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名');
  430. $human = Human::query()->find($user['human_id']);
  431. return [
  432. 'bank_no' => $human['bank_no'],
  433. 'bank_name' => $human['bank_name'],
  434. 'bank_holder_name' => $human['bank_holder_name'],
  435. 'bank_is_me' => $human['bank_is_me'],
  436. ];
  437. }
  438. }