| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Module\User\Services;
- use App\Module\User\Models\UserInfo;
- use App\Module\User\Unit;
- use App\Module\User\Unit\UserPublic;
- use Dcore\Db\Arr;
- use Dcore\Exception\LogicException;
- class UserOInfo
- {
- static $ttl = 3600;
- /**
- * 读取单个用户O信息
- * @param $id
- * @return Unit\UserOInfo|null
- */
- static public function oinfo($id)
- {
- $cache = self::infoCache($id);
- if($cache === null){
- /**
- * @var \App\Module\User\Models\UserInfo[] $users
- */
- $user = \App\Module\User\Models\UserInfo::query()->where('user_id', $id)->with([
- 'merchant',
- 'user_phone'
- ])->first();
- if($user){
- $userp = self::userInfo2Oinfo($user);
- \Illuminate\Support\Facades\Cache::put(self::OInfoKey($user->user_id), $userp,60);
- return $userp;
- }
- return null;
- }
- return $cache;
- }
- /**
- * 数据库对象 转 oinfo对象
- *
- * @param UserInfo $info
- * @return Unit\UserOInfo
- */
- static protected function userInfo2Oinfo(UserInfo $info)
- {
- $userp = new \App\Module\User\Unit\UserOInfo();
- $userp->avatar = $info->avatar;
- $userp->user_id = $info->user_id;
- $userp->nick_name = $info->nickname;
- // dd($info->user_phone);
- if($info->user_phone){
- $userp->phone = $info->user_phone->phone;
- }
- if($info->merchant){
- // merchant_id
- $userp->merchant_id = (int) $info->merchant->id;
- }
- $userp->status = $info->status->value();
- return $userp;
- }
- /**
- * 获取用户订单信息
- *
- * @param $ids
- * @return array
- */
- static public function oinfos($ids = [])
- {
- if(empty($ids)){
- return [];
- }
- $ids2 = [];
- foreach ($ids as $id) {
- if(empty($id)){
- continue;
- }
- if(\App\Module\Sys\User::isSysUid($id)){
- continue;
- }
- $ids2[$id] = $id;
- }
- $res = self::infoCaches($ids2);
- if ($ids2) {
- /**
- * @var \App\Module\User\Models\UserInfo[] $users
- */
- $users = \App\Module\User\Models\UserInfo::query()->whereIn('user_id', $ids2)->with([
- 'merchant',
- 'user_phone'
- ])->get();
- foreach ($users as $user) {
- $userp= self::userInfo2Oinfo($user);
- $res[$userp->user_id] = $userp;
- \Illuminate\Support\Facades\Cache::put(self::OInfoKey($user->user_id), $userp,self::$ttl);
- unset($ids2[$userp->user_id]);
- }
- }
- if (count($ids2)) {
- foreach ($ids2 as $id) {
- $userp = new UserPublic();
- $userp->avatar = '';
- $userp->user_id = $id;
- $userp->nick_name = '已注销';
- $res[$userp->user_id] = $userp;
- \Illuminate\Support\Facades\Cache::put(self::OInfoKey($userp->user_id), $userp,self::$ttl);
- unset($ids2[$userp->user_id]);
- }
- }
- return $res;
- }
- protected static function add()
- {
- }
- /**
- * 获取用户O信息
- * @param $id
- * @return mixed|null
- */
- static public function infoCache($id)
- {
- $c = \Illuminate\Support\Facades\Cache::get(self::OInfoKey($id));
- if ($c) {
- return $c;
- }
- return null;
- }
- /**
- * 批量获取用户O信息
- *
- * @param $ids
- * @return void
- */
- static public function infoCaches(&$ids)
- {
- $res = [];
- foreach ($ids as $id) {
- $c = \Illuminate\Support\Facades\Cache::get(self::OInfoKey($id));
- if ($c) {
- $res[$id] = $c;
- unset($ids[$id]);
- }
- }
- return $res;
- }
- /**
- * 用户公共数据缓存key
- *
- * @param $uid
- * @return string
- */
- static private function OInfoKey($uid)
- {
- return 'lan_usero4info_' . $uid;
- }
- }
|