User.php 5.7 KB

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