UserOInfo.php 4.7 KB

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