UserOInfo.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Module\User;
  3. use App\Module\User\Enums\STATUS2;
  4. use App\Module\User\Models\UserInfo;
  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 UserOInfo
  10. {
  11. static $ttl = 3600;
  12. /**
  13. * 读取单个用户O信息
  14. * @param $id
  15. * @return Unit\UserOInfo|null
  16. */
  17. static public function oinfo($id)
  18. {
  19. $cache = self::infoCache($id);
  20. if($cache === null){
  21. /**
  22. * @var \App\Module\User\Models\UserInfo[] $users
  23. */
  24. $user = \App\Module\User\Models\UserInfo::query()->where('user_id', $id)->with([
  25. 'merchant',
  26. 'user_phone'
  27. ])->first();
  28. if($user){
  29. $userp = self::userInfo2Oinfo($user);
  30. \Illuminate\Support\Facades\Cache::put(self::OInfoKey($user->user_id), $userp,60);
  31. return $userp;
  32. }
  33. return null;
  34. }
  35. return $cache;
  36. }
  37. /**
  38. * 数据库对象 转 oinfo对象
  39. *
  40. * @param UserInfo $info
  41. * @return Unit\UserOInfo
  42. */
  43. static protected function userInfo2Oinfo(UserInfo $info)
  44. {
  45. $userp = new \App\Module\User\Unit\UserOInfo();
  46. $userp->avatar = $info->avatar;
  47. $userp->user_id = $info->user_id;
  48. $userp->nick_name = $info->nickname;
  49. // dd($info->user_phone);
  50. if($info->user_phone){
  51. $userp->phone = $info->user_phone->phone;
  52. }
  53. if($info->merchant){
  54. // merchant_id
  55. $userp->merchant_id = (int) $info->merchant->id;
  56. }
  57. $userp->status = $info->status->value();
  58. return $userp;
  59. }
  60. /**
  61. * 获取用户订单信息
  62. *
  63. * @param $ids
  64. * @return array
  65. */
  66. static public function oinfos($ids = [])
  67. {
  68. if(empty($ids)){
  69. return [];
  70. }
  71. $ids2 = [];
  72. foreach ($ids as $id) {
  73. if(empty($id)){
  74. continue;
  75. }
  76. if(\App\Module\Sys\User::isSysUid($id)){
  77. continue;
  78. }
  79. $ids2[$id] = $id;
  80. }
  81. $res = self::infoCaches($ids2);
  82. if ($ids2) {
  83. /**
  84. * @var \App\Module\User\Models\UserInfo[] $users
  85. */
  86. $users = \App\Module\User\Models\UserInfo::query()->whereIn('user_id', $ids2)->with([
  87. 'merchant',
  88. 'user_phone'
  89. ])->get();
  90. foreach ($users as $user) {
  91. $userp= self::userInfo2Oinfo($user);
  92. $res[$userp->user_id] = $userp;
  93. \Illuminate\Support\Facades\Cache::put(self::OInfoKey($user->user_id), $userp,self::$ttl);
  94. unset($ids2[$userp->user_id]);
  95. }
  96. }
  97. if (count($ids2)) {
  98. foreach ($ids2 as $id) {
  99. $userp = new UserPublic();
  100. $userp->avatar = '';
  101. $userp->user_id = $id;
  102. $userp->nick_name = '已注销';
  103. $res[$userp->user_id] = $userp;
  104. \Illuminate\Support\Facades\Cache::put(self::OInfoKey($userp->user_id), $userp,self::$ttl);
  105. unset($ids2[$userp->user_id]);
  106. }
  107. }
  108. return $res;
  109. }
  110. protected static function add()
  111. {
  112. }
  113. /**
  114. * 获取用户O信息
  115. * @param $id
  116. * @return mixed|null
  117. */
  118. static public function infoCache($id)
  119. {
  120. $c = \Illuminate\Support\Facades\Cache::get(self::OInfoKey($id));
  121. if ($c) {
  122. return $c;
  123. }
  124. return null;
  125. }
  126. /**
  127. * 批量获取用户O信息
  128. *
  129. * @param $ids
  130. * @return void
  131. */
  132. static public function infoCaches(&$ids)
  133. {
  134. $res = [];
  135. foreach ($ids as $id) {
  136. $c = \Illuminate\Support\Facades\Cache::get(self::OInfoKey($id));
  137. if ($c) {
  138. $res[$id] = $c;
  139. unset($ids[$id]);
  140. }
  141. }
  142. return $res;
  143. }
  144. /**
  145. * 用户公共数据缓存key
  146. *
  147. * @param $uid
  148. * @return string
  149. */
  150. static private function OInfoKey($uid)
  151. {
  152. return 'lan_usero4info_' . $uid;
  153. }
  154. }