UserService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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\ModelStatusEnum;
  7. use App\Repositories\Enums\ResponseCodeEnum;
  8. use App\Repositories\Models\Base\Auth;
  9. use App\Repositories\Presenters\Base\UserPresenter;
  10. use Carbon\Carbon;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Crypt;
  14. use Illuminate\Support\Facades\DB;
  15. use App\Repositories\Models\Base\User;
  16. use Illuminate\Support\Str;
  17. class UserService
  18. {
  19. /**
  20. * @var UserRepositoryEloquent
  21. */
  22. private $repository;
  23. /**
  24. * UserService constructor.
  25. *
  26. * @param UserRepositoryEloquent $userRepositoryEloquent
  27. */
  28. public function __construct(UserRepositoryEloquent $userRepositoryEloquent)
  29. {
  30. $this->repository = $userRepositoryEloquent;
  31. }
  32. /**
  33. * @param Request $request
  34. *
  35. * @return mixed
  36. * @throws \Prettus\Repository\Exceptions\RepositoryException
  37. */
  38. public function handleList(Request $request)
  39. {
  40. $this->repository->pushCriteria(new UserCriteria($request));
  41. $this->repository->setPresenter(UserPresenter::class);
  42. return $this->repository->searchUsersByPage();
  43. }
  44. /**
  45. * @param $id
  46. *
  47. * @return \Illuminate\Database\Eloquent\Model
  48. */
  49. public function handleProfile($id)
  50. {
  51. $this->repository->setPresenter(UserPresenter::class);
  52. return $this->repository->searchUserBy($id);
  53. }
  54. /**
  55. * @param array $data
  56. *
  57. * @return mixed
  58. * @throws \Prettus\Validator\Exceptions\ValidatorException
  59. */
  60. public function handleStore($data)
  61. {
  62. $data['mobile_encryption'] = Crypt::encryptString($data['mobile']);
  63. $data['mobile'] = mobile_hidden($data['mobile']);
  64. $user = $this->repository->create($data);
  65. return $user;
  66. }
  67. /**
  68. * @param array $data
  69. *
  70. * @return mixed
  71. * @throws \Prettus\Validator\Exceptions\ValidatorException
  72. */
  73. public function handleUpdate($data)
  74. {
  75. if (array_key_exists('mobile', $data) && strpos($data['mobile'], '*') === false) {
  76. $data['mobile_encryption'] = Crypt::encryptString($data['mobile']);
  77. $data['mobile'] = mobile_hidden($data['mobile']);
  78. }
  79. $user = $this->repository->update($data, $data['id']);
  80. return $user;
  81. }
  82. /**
  83. * @param Request $request
  84. *
  85. * @return mixed
  86. * @throws \Prettus\Validator\Exceptions\ValidatorException
  87. */
  88. public function handleDelete($id)
  89. {
  90. Auth::query()->where('user_id', $id)->update(['user_id' => 0]);
  91. return $this->repository->delete($id);
  92. }
  93. /**
  94. * 解绑微信号
  95. * @param $auth
  96. * @return bool
  97. */
  98. public function handleUnbindWechat($user_id = 0)
  99. {
  100. if (!$user_id) $user_id = login_user_id();
  101. $user = $this->repository->find($user_id);
  102. if (!$user->wechat_auth_id) return false;
  103. $auth = Auth::query()->find($user->wechat_auth_id);
  104. if (!$auth) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '尚未绑定微信号');
  105. DB::beginTransaction();
  106. try {
  107. $auth->user_id = 0;
  108. $user->wechat_auth_id = 0;
  109. //todo:是否解绑之后还要收取信息
  110. // $user->credential = null;
  111. $auth->save();
  112. $user->save();
  113. DB::commit();
  114. } catch (\Exception $exception) {
  115. DB::rollBack();
  116. exception($exception);
  117. }
  118. return true;
  119. }
  120. /**
  121. * 绑定微信
  122. * @param $auth_id
  123. * @param $user
  124. * @return bool
  125. */
  126. public function handleBindWechat($auth_id, $user)
  127. {
  128. $auth = Auth::query()->where('id', $auth_id)->first();
  129. if (!$auth) return false;
  130. $user = User::query()->where('id', $user['id'])->first();
  131. if (!$user) return false;
  132. $userInfo = Cache::get("cache:service:auth:userinfo:api:" . $auth['id'], false);
  133. DB::transaction(function () use ($user, $auth, $userInfo) {
  134. $auth->user_id = $user->id;
  135. $user->wechat_auth_id = $auth->id;
  136. $user->credential = $auth->credential;
  137. // if (isset($userInfo['name'])) $user->name = $userInfo['name'];
  138. if (isset($userInfo['nickname'])) $user->nickname = $userInfo['nickname'];
  139. if (isset($userInfo['headimg'])) {
  140. $filename = 'user-imgs/' . Str::random() . '.png';
  141. $filename = saveImageFromUrl($userInfo['headimg'], $filename);
  142. $user->headimg = $filename;
  143. }
  144. if (empty($user->last_update_time)) $user->last_update_time = Carbon::now()->toDateTimeString();
  145. $auth->save();
  146. $user->save();
  147. Auth::query()->where('user_id', $user->id)->where('id', '<>', $auth->id)->update(['user_id' => 0]);
  148. });
  149. return true;
  150. }
  151. /**
  152. * 获取登录用户信息
  153. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  154. */
  155. public function handleMe()
  156. {
  157. $this->repository->setPresenter(UserPresenter::class);
  158. return $this->repository->find(login_user_id());
  159. }
  160. /**
  161. * 更新个人数据
  162. * @param $data
  163. * @param $user_id
  164. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  165. * @throws \Prettus\Validator\Exceptions\ValidatorException
  166. */
  167. public function handleMeUpdate($data, $user_id = false)
  168. {
  169. if (!$user_id) $user_id = login_user_id();
  170. return $this->repository->update($data, $user_id);
  171. }
  172. /**
  173. * 手机号登录
  174. * @param $mobile
  175. * @return array
  176. * @throws \Prettus\Validator\Exceptions\ValidatorException
  177. */
  178. public function handleMobileLogin($mobile, $area_code = "+86")
  179. {
  180. $miMobile = mobile_hidden($mobile);
  181. $users = User::query()->where('mobile_code', $area_code)->where('mobile', $miMobile)->select(['id', 'mobile', 'mobile_encryption'])->where('status', ModelStatusEnum::OK)->get();
  182. $userNums = count($users);
  183. if (!$userNums) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
  184. $user_id = 0;
  185. foreach ($users as $user) {
  186. if (Crypt::decryptString($user['mobile_encryption']) === $mobile) {
  187. $user_id = $user['id'];
  188. break;
  189. }
  190. }
  191. if (!$user_id) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
  192. $user = User::query()->where('id', $user_id)->first();
  193. if (empty($user->last_update_time)) {
  194. $user->last_update_time = Carbon::now()->toDateTimeString();
  195. $user->save();
  196. }
  197. $token = auth('api')->login($user);
  198. //单机登录限制
  199. $user_id = $user['id'];
  200. SingleLoginLimit::setToken('api', $user_id, $token);
  201. $this->repository->updateLoginInfo($user_id, getClientIp());
  202. return [$token, $user];
  203. }
  204. /**
  205. * 手机号登录
  206. * @param $mobile
  207. * @return bool
  208. * @throws \Prettus\Validator\Exceptions\ValidatorException
  209. */
  210. public function handleCheckMobileIsExists($mobile, $area_code = "+86")
  211. {
  212. $miMobile = mobile_hidden($mobile);
  213. $users = User::query()->where('mobile_code', $area_code)->where('mobile', $miMobile)->select(['id', 'mobile', 'mobile_encryption'])->where('status', ModelStatusEnum::OK)->get();
  214. $userNums = count($users);
  215. if (!$userNums) return false;
  216. $user_id = 0;
  217. foreach ($users as $user) {
  218. if (Crypt::decryptString($user['mobile_encryption']) === $mobile) {
  219. $user_id = $user['id'];
  220. break;
  221. }
  222. }
  223. if (!$user_id) return false;
  224. $user = User::query()->where('id', $user_id)->where('status', ModelStatusEnum::OK)->exists();
  225. if (!$user) return false;
  226. return true;
  227. }
  228. /**
  229. * auth登录
  230. * @param $auth
  231. * @return array
  232. * @throws \Prettus\Validator\Exceptions\ValidatorException
  233. */
  234. public function handleAuthLogin($auth)
  235. {
  236. $user_id = $auth['user_id'];
  237. $user = User::query()->where('id', $user_id)->first();
  238. if (!$user) {
  239. // abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
  240. return [false, false];
  241. }
  242. Cache::remember("sercer:user:handleAuthLogin:{$user_id}", Carbon::now()->addDay(), function () use ($user, $auth) {
  243. $userInfo = Cache::get("cache:service:auth:userinfo:api:" . $auth['id'], false);
  244. // if (isset($userInfo['name'])) $user->name = $userInfo['name'];
  245. if (isset($userInfo['nickname'])) $user->nickname = $userInfo['nickname'];
  246. if (isset($userInfo['headimg'])) {
  247. $filename = 'user-imgs/' . Str::random() . '.png';
  248. $filename = saveImageFromUrl($userInfo['headimg'], $filename);
  249. $user->headimg = $filename;
  250. }
  251. $user->save();
  252. });
  253. $token = auth('api')->login($user);
  254. //单机登录限制
  255. $user_id = $user['id'];
  256. SingleLoginLimit::setToken('api', $user_id, $token);
  257. $this->repository->updateLoginInfo($user_id, getClientIp());
  258. return [$token, $user];
  259. }
  260. /**
  261. * 选项
  262. * @param Request $request
  263. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  264. * @throws \Prettus\Repository\Exceptions\RepositoryException
  265. */
  266. public function handleSelectOptions(Request $request)
  267. {
  268. $this->repository->pushCriteria(new UserCriteria($request));
  269. return $this->repository->all(['id', 'nickname', 'mobile']);
  270. }
  271. /**
  272. * 读信
  273. * @param $user_id
  274. * @return void
  275. */
  276. public function handleReadXin($user_id)
  277. {
  278. $user = User::query()->where('id', $user_id)->first();
  279. $user->is_read_xin = ModelStatusEnum::OK;
  280. $user->save();
  281. }
  282. }