123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Repositories\Models\Base;
- use App\Http\Middleware\SingleLoginLimit;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Dwbs\Zhengshu;
- use App\Repositories\Models\Model;
- use Carbon\Carbon;
- use Illuminate\Auth\Authenticatable;
- use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
- use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Crypt;
- use Laravel\Lumen\Auth\Authorizable;
- use Spatie\Permission\Traits\HasRoles;
- use Tymon\JWTAuth\Contracts\JWTSubject;
- class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
- {
- use Authenticatable, Authorizable, HasFactory, HasRoles;
- /**
- * @var string
- */
- protected $table = 'base_users';
- protected $guarded = [];
- const GroupMaps = [
- [
- 'id' => 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;
- }
- }
|