UserService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace App\Module\User\Services;
  3. use App\Module\User\Dto\UserDto;
  4. use App\Module\User\Dto\UserInfoDto;
  5. use App\Module\User\Dto\UserPublicDto;
  6. use App\Module\User\Enums\STATUS2;
  7. use App\Module\User\Models;
  8. use App\Module\User\Unit\UserPublic;
  9. use Illuminate\Support\Facades\Hash;
  10. class UserService
  11. {
  12. /**
  13. * 创建用户
  14. *
  15. * @param string $username 用户名
  16. * @param string $password 密码
  17. * @return UserDto|string 成功返回用户DTO,失败返回错误信息
  18. * @throws \LogicException
  19. */
  20. static public function create($username, $password)
  21. {
  22. $user = new \App\Module\User\Models\User();
  23. $user->username = $username;
  24. $user->password = Hash::make($password);
  25. $user->status2 = STATUS2::Normal;
  26. $res = $user->save();
  27. if(\App\Module\System\Services\User::isSysUid($user->id)){
  28. $user->delete();
  29. throw new \LogicException("错误34");
  30. }
  31. if ($res === false) {
  32. return 'create-error';
  33. }
  34. UserService::infoinfo($user->id, true);
  35. // 返回DTO而不是模型
  36. return UserDto::fromModel($user);
  37. }
  38. /**
  39. * 使用手机号码注册
  40. *
  41. * @param string $phone 手机号
  42. * @param string $password 密码
  43. * @return UserDto|string 成功返回用户DTO,失败返回错误信息
  44. * @throws \LogicException
  45. */
  46. static public function createPhone($phone, $password)
  47. {
  48. $user = self::create($phone, $password);
  49. if (is_string($user)) {
  50. throw new \LogicException("create error");
  51. }
  52. $b = Phone::bind($user->id, $phone);
  53. if ($b === false) {
  54. throw new \LogicException("create bind error");
  55. }
  56. return $user;
  57. }
  58. /**
  59. * 更改密码
  60. *
  61. * @param $id
  62. * @param $password
  63. * @return string|true
  64. */
  65. static public function resetPassword($id, $password)
  66. {
  67. $user = \App\Module\User\Models\User::query()->find($id);
  68. if (!$user) {
  69. return 'user-notfind';
  70. }
  71. $user->password = Hash::make($password);
  72. $res = $user->save();
  73. if ($res === false) {
  74. return 'update-error';
  75. }
  76. return true;
  77. }
  78. /**
  79. * 列表
  80. *
  81. * @param $page
  82. * @param $limit
  83. * @param $where
  84. * @return \Illuminate\Pagination\LengthAwarePaginator
  85. */
  86. static public function lisss($page, $limit, $where)
  87. {
  88. $q = \App\Module\User\Models\User::query()->with([
  89. 'fund', 'merchant', 'info'
  90. ]);
  91. $wArr = new Arr($where, $q);
  92. $wArr->queryNumber('user_id')
  93. ->queryString('mobile');
  94. return $q->paginate($limit, [
  95. 'user_id',
  96. 'nick_name'
  97. ], '', $page);
  98. }
  99. /**
  100. * 获取用户信息
  101. *
  102. * @param int $id 用户ID
  103. * @param bool $create 如果不存在是否创建
  104. * @return UserDto|null 用户DTO或null
  105. */
  106. static public function info($id, $create = false)
  107. {
  108. $model = \App\Module\User\Logic\User::info($id, $create);
  109. if (!$model) {
  110. return null;
  111. }
  112. return UserDto::fromModel($model);
  113. }
  114. /**
  115. * 获取用户详细信息
  116. *
  117. * @param int $id 用户ID
  118. * @param bool $create 如果不存在是否创建
  119. * @return UserInfoDto|null 用户详细信息DTO或null
  120. */
  121. static public function infoinfo($id, $create = false)
  122. {
  123. $model = \App\Module\User\Logic\User::infoinfo($id, $create);
  124. if (!$model) {
  125. return null;
  126. }
  127. return UserInfoDto::fromModel($model);
  128. }
  129. /**
  130. * 获取用户公共信息
  131. *
  132. * @param array $ids 用户ID数组
  133. * @return array 用户公共信息DTO数组
  134. */
  135. static public function pinfos($ids = [])
  136. {
  137. if(empty($ids)){
  138. return [];
  139. }
  140. $ids2 = [];
  141. foreach ($ids as $id) {
  142. if(empty($id)){
  143. continue;
  144. }
  145. if(\App\Module\System\Services\User::isSysUid($id)){
  146. continue;
  147. }
  148. $ids2[$id] = $id;
  149. }
  150. $res = self::pinfoCaches($ids2);
  151. if ($ids2) {
  152. /**
  153. * @var \App\Module\User\Models\UserInfo[] $users
  154. */
  155. $users = \App\Module\User\Models\UserInfo::query()->whereIn('user_id', $ids2)->with([
  156. 'merchant'
  157. ])->get();
  158. foreach ($users as $user) {
  159. // 使用DTO而不是直接使用UserPublic
  160. $userDto = UserPublicDto::fromUserInfo($user);
  161. $res[$userDto->userId] = $userDto;
  162. \Illuminate\Support\Facades\Cache::put(self::publicInfoKey($user->user_id), $userDto);
  163. unset($ids2[$userDto->userId]);
  164. }
  165. }
  166. if (count($ids2)) {
  167. foreach ($ids2 as $id) {
  168. // 使用DTO而不是直接使用UserPublic
  169. $userDto = UserPublicDto::createDeleted($id);
  170. $res[$userDto->userId] = $userDto;
  171. \Illuminate\Support\Facades\Cache::put(self::publicInfoKey($id), $userDto);
  172. unset($ids2[$userDto->userId]);
  173. }
  174. }
  175. return $res;
  176. }
  177. /**
  178. * 从缓存中获取用户公共信息
  179. *
  180. * @param array $ids 用户ID数组
  181. * @return array 用户公共信息DTO数组
  182. */
  183. static public function pinfoCaches(&$ids)
  184. {
  185. $res = [];
  186. foreach ($ids as $id) {
  187. $c = \Illuminate\Support\Facades\Cache::get(self::publicInfoKey($id));
  188. if ($c) {
  189. // 如果缓存中的是旧的UserPublic对象,转换为DTO
  190. if ($c instanceof UserPublic) {
  191. $c = UserPublicDto::fromUserPublic($c);
  192. }
  193. $res[$id] = $c;
  194. unset($ids[$id]);
  195. }
  196. }
  197. return $res;
  198. }
  199. /**
  200. * 用户公共数据缓存key
  201. *
  202. * @param $uid
  203. * @return string
  204. */
  205. static public function publicInfoKey($uid)
  206. {
  207. return 'lan_userpublic_' . $uid;
  208. }
  209. static public function getPhone()
  210. {
  211. }
  212. }