User.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Http\Middleware\SingleLoginLimit;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use App\Repositories\Models\Dwbs\Zhengshu;
  6. use App\Repositories\Models\Model;
  7. use Carbon\Carbon;
  8. use Illuminate\Auth\Authenticatable;
  9. use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
  10. use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
  11. use Illuminate\Database\Eloquent\Factories\HasFactory;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\Crypt;
  14. use Laravel\Lumen\Auth\Authorizable;
  15. use Spatie\Permission\Traits\HasRoles;
  16. use Tymon\JWTAuth\Contracts\JWTSubject;
  17. class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
  18. {
  19. use Authenticatable, Authorizable, HasFactory, HasRoles;
  20. /**
  21. * @var string
  22. */
  23. protected $table = 'base_users';
  24. protected $guarded = [];
  25. const GroupMaps = [
  26. [
  27. 'id' => 2,
  28. 'name' => '经销商',
  29. ],
  30. [
  31. 'id' => 3,
  32. 'name' => '批发商',
  33. ]
  34. ];
  35. /**
  36. * The attributes excluded from the model's JSON form.
  37. *
  38. * @var array
  39. */
  40. protected $hidden = [
  41. 'password',
  42. 'mobile_encryption'
  43. ];
  44. protected $casts = [
  45. 'extra_fields' => 'json'
  46. ];
  47. /**
  48. * Get the identifier that will be stored in the subject claim of the JWT.
  49. *
  50. * @return mixed
  51. */
  52. public function getJWTIdentifier()
  53. {
  54. return $this->getKey();
  55. }
  56. /**
  57. * Return a key value array, containing any custom claims to be added to the JWT.
  58. *
  59. * @return array
  60. */
  61. public function getJWTCustomClaims()
  62. {
  63. return ['role' => 'user'];
  64. }
  65. protected static function booted()
  66. {
  67. parent::booted(); // TODO: Change the autogenerated stub
  68. self::updated(function (User $user) {
  69. if ($user->isDirty('status')) {
  70. //退出登录
  71. if ($user->status == ModelStatusEnum::PAUSE) {
  72. SingleLoginLimit::delToken('api', $user->id);
  73. }
  74. }
  75. });
  76. self::deleted(function (User $user) {
  77. SingleLoginLimit::delToken('api', $user->id);
  78. });
  79. }
  80. public function wechat_auth()
  81. {
  82. return $this->belongsTo(Auth::class, 'wechat_auth_id')->select(['id', 'credential']);
  83. }
  84. /**
  85. * 当前名次
  86. * @return int
  87. */
  88. public function getMingciAttribute()
  89. {
  90. $isOpenGroup = (bool)Setting::byCodeGetSetting('h5_paihangbang_is_group_user');
  91. $meXuefen = $this->attributes['xuefen'];
  92. if (!$meXuefen) return 0;
  93. $group_id = 0;
  94. if ($isOpenGroup) {
  95. if (!array_key_exists('group_id', $this->attributes)) {
  96. $group_id = self::query()->where('id', $this->attributes['id'])->value('group_id') ?? 0;
  97. } else {
  98. $group_id = $this->attributes['group_id'];
  99. }
  100. }
  101. $last_update_time = $this->attributes['last_update_time'];
  102. $ranking = User::query()->when($isOpenGroup, function ($query) use ($group_id) {
  103. return $query->where('group_id', $group_id);
  104. })->where(function ($query) use ($meXuefen, $last_update_time) {
  105. return $query->orWhere('xuefen', '>', $meXuefen)->orWhere(function ($q) use ($last_update_time, $meXuefen) {
  106. return $q->where('xuefen', '=', $meXuefen)->where('last_update_time', '<', $last_update_time);
  107. });
  108. })->where('status', ModelStatusEnum::OK)->count();
  109. $ranking += 1;
  110. return $ranking;
  111. }
  112. public function getHeadimgAttribute($val)
  113. {
  114. if (empty($val)) {
  115. return config('app.url') . '/default/headimg.png';
  116. }
  117. return path_to_url($val);
  118. }
  119. public function zhengshu()
  120. {
  121. return $this->belongsTo(Zhengshu::class, 'tag', 'id')->select(['name', 'id', 'min_xuefen', 'star_settings']);
  122. }
  123. public static function refreshUserTag($user_id = 0)
  124. {
  125. $user = self::query()->where('id', $user_id)->select(['id', 'xuefen', 'tag', 'tag_star'])->first();
  126. $zhengshus = Zhengshu::query()->where('status', ModelStatusEnum::OK)->select(['id', 'is_open_star', 'min_xuefen', 'status', 'star_settings'])->orderBy('min_xuefen')->get();
  127. $tag = 0;
  128. $tag_star = 0;
  129. foreach ($zhengshus as $zhengshu) {
  130. if ($zhengshu['min_xuefen'] > $user['xuefen']) break;
  131. $tag_star = 0;
  132. $tag = $zhengshu['id'];
  133. if ($zhengshu['is_open_star']) {
  134. if (isset($zhengshu['star_settings'])) {
  135. $settings = arraySort($zhengshu['star_settings'], 'min_xuefen', SORT_ASC);
  136. foreach ($settings as $setting) {
  137. if ($setting['min_xuefen'] > $user['xuefen']) break;
  138. $tag_star = $setting['star'];
  139. }
  140. }
  141. }
  142. }
  143. if ($user->tag === $tag && $user->tag_star === $tag_star) return true;
  144. $user->tag = $tag;
  145. $user->tag_star = $tag_star;
  146. $user->save();
  147. //todo:获取学位通知
  148. return true;
  149. }
  150. public function getMMobileAttribute()
  151. {
  152. if (isset($this->attributes['mobile_encryption'])) {
  153. return Crypt::decryptString($this->attributes['mobile_encryption']);
  154. }
  155. return '';
  156. }
  157. /**
  158. * 根据用户手机号查询用户
  159. * @param $mobile
  160. * @return mixed
  161. */
  162. public static function byMobileGetIds($mobile)
  163. {
  164. // return Cache::remember("model:user:byMobileGetIds:{$mobile}", Carbon::now()->addHours(4), function () use ($mobile) {
  165. if (strlen($mobile) == 11) {
  166. $users = self::query()->where('mobile', mobile_hidden($mobile))->select(['id', 'mobile', 'mobile_encryption'])->get();
  167. $len = count($users);
  168. if (!$len) return [];
  169. $ids = [];
  170. foreach ($users as $user) {
  171. if ($mobile === $user->m_mobile) {
  172. $ids[] = $user->id;
  173. }
  174. }
  175. return $ids;
  176. }
  177. return self::query()->where('mobile', 'like', "%{$mobile}")->pluck('id');
  178. // });
  179. }
  180. public static function byGroupIdGetName($group_id)
  181. {
  182. return Cache::remember("model:User:byGroupIdGetName:{$group_id}", Carbon::now()->addDay(), function () use ($group_id) {
  183. if (!$group_id) return '全部';
  184. $name = '未知';
  185. foreach (self::GroupMaps as $map) {
  186. if ($map['id'] == $group_id) {
  187. $name = $map['name'];
  188. break;
  189. }
  190. }
  191. return $name;
  192. });
  193. }
  194. public static function byIdGetWechatInfo($user_id)
  195. {
  196. $key = "model:user:byIdGetWechatInfo:{$user_id}";
  197. Cache::forget($key);
  198. $user = Cache::remember($key, Carbon::now()->addHours(8), function () use ($user_id) {
  199. $user = self::query()->where('id', $user_id)->whereNotNull('credential')->select(['id', 'nickname', 'credential'])->first();
  200. if (!$user) return false;
  201. return $user;
  202. });
  203. if (!$user) Cache::forget($key);
  204. return $user;
  205. }
  206. }