2, 'name' => '经销商', ], [ 'id' => 3, 'name' => '批发商', ] ]; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = [ 'password', 'mobile_encryption' ]; protected $casts = [ 'extra_fields' => 'json' ]; /** * Get the identifier that will be stored in the subject claim of the JWT. * * @return mixed */ public function getJWTIdentifier() { return $this->getKey(); } /** * Return a key value array, containing any custom claims to be added to the JWT. * * @return array */ public function getJWTCustomClaims() { return ['role' => 'user']; } protected static function booted() { parent::booted(); // TODO: Change the autogenerated stub self::updated(function (User $user) { if ($user->isDirty('status')) { //退出登录 if ($user->status == ModelStatusEnum::PAUSE) { SingleLoginLimit::delToken('api', $user->id); } } }); self::deleted(function (User $user) { SingleLoginLimit::delToken('api', $user->id); }); } public function wechat_auth() { return $this->belongsTo(Auth::class, 'wechat_auth_id')->select(['id', 'credential']); } /** * 当前名次 * @return int */ public function getMingciAttribute() { $isOpenGroup = (bool)Setting::byCodeGetSetting('h5_paihangbang_is_group_user'); $meXuefen = $this->attributes['xuefen']; if (!$meXuefen) return 0; $group_id = 0; if ($isOpenGroup) { if (!array_key_exists('group_id', $this->attributes)) { $group_id = self::query()->where('id', $this->attributes['id'])->value('group_id') ?? 0; } else { $group_id = $this->attributes['group_id']; } } $last_update_time = $this->attributes['last_update_time']; $ranking = User::query()->when($isOpenGroup, function ($query) use ($group_id) { return $query->where('group_id', $group_id); })->where(function ($query) use ($meXuefen, $last_update_time) { return $query->orWhere('xuefen', '>', $meXuefen)->orWhere(function ($q) use ($last_update_time, $meXuefen) { return $q->where('xuefen', '=', $meXuefen)->where('last_update_time', '<', $last_update_time); }); })->where('status', ModelStatusEnum::OK)->count(); $ranking += 1; return $ranking; } public function getHeadimgAttribute($val) { if (empty($val)) { return config('app.url') . '/default/headimg.png'; } return path_to_url($val); } public function zhengshu() { return $this->belongsTo(Zhengshu::class, 'tag', 'id')->select(['name', 'id', 'min_xuefen', 'star_settings']); } public static function refreshUserTag($user_id = 0) { $user = self::query()->where('id', $user_id)->select(['id', 'xuefen', 'tag', 'tag_star'])->first(); $zhengshus = Zhengshu::query()->where('status', ModelStatusEnum::OK)->select(['id', 'is_open_star', 'min_xuefen', 'status', 'star_settings'])->orderBy('min_xuefen')->get(); $tag = 0; $tag_star = 0; foreach ($zhengshus as $zhengshu) { if ($zhengshu['min_xuefen'] > $user['xuefen']) break; $tag_star = 0; $tag = $zhengshu['id']; if ($zhengshu['is_open_star']) { if (isset($zhengshu['star_settings'])) { $settings = arraySort($zhengshu['star_settings'], 'min_xuefen', SORT_ASC); foreach ($settings as $setting) { if ($setting['min_xuefen'] > $user['xuefen']) break; $tag_star = $setting['star']; } } } } if ($user->tag === $tag && $user->tag_star === $tag_star) return true; $user->tag = $tag; $user->tag_star = $tag_star; $user->save(); //todo:获取学位通知 return true; } public function getMMobileAttribute() { if (isset($this->attributes['mobile_encryption'])) { return Crypt::decryptString($this->attributes['mobile_encryption']); } return ''; } /** * 根据用户手机号查询用户 * @param $mobile * @return mixed */ public static function byMobileGetIds($mobile) { // return Cache::remember("model:user:byMobileGetIds:{$mobile}", Carbon::now()->addHours(4), function () use ($mobile) { if (strlen($mobile) == 11) { $users = self::query()->where('mobile', mobile_hidden($mobile))->select(['id', 'mobile', 'mobile_encryption'])->get(); $len = count($users); if (!$len) return []; $ids = []; foreach ($users as $user) { if ($mobile === $user->m_mobile) { $ids[] = $user->id; } } return $ids; } return self::query()->where('mobile', 'like', "%{$mobile}")->pluck('id'); // }); } public static function byGroupIdGetName($group_id) { return Cache::remember("model:User:byGroupIdGetName:{$group_id}", Carbon::now()->addDay(), function () use ($group_id) { if (!$group_id) return '全部'; $name = '未知'; foreach (self::GroupMaps as $map) { if ($map['id'] == $group_id) { $name = $map['name']; break; } } return $name; }); } public static function byIdGetWechatInfo($user_id) { $key = "model:user:byIdGetWechatInfo:{$user_id}"; Cache::forget($key); $user = Cache::remember($key, Carbon::now()->addHours(8), function () use ($user_id) { $user = self::query()->where('id', $user_id)->whereNotNull('credential')->select(['id', 'nickname', 'credential'])->first(); if (!$user) return false; return $user; }); if (!$user) Cache::forget($key); return $user; } }