User.php 5.7 KB

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