UserService.php 5.7 KB

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