User.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Relations\MorphManyTagsTrait;
  4. use Carbon\Carbon;
  5. use Illuminate\Auth\Passwords\CanResetPassword;
  6. use Illuminate\Notifications\Notifiable;
  7. use Illuminate\Foundation\Auth\User as Authenticatable;
  8. use Illuminate\Support\Facades\App;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Config;
  11. class User extends Authenticatable
  12. {
  13. use CanResetPassword,MorphManyTagsTrait,Notifiable;
  14. /**
  15. * The database table used by the model.
  16. *
  17. * @var string
  18. */
  19. protected $table = 'users';
  20. protected $roles;
  21. /**
  22. * The attributes that are mass assignable.
  23. *
  24. * @var array
  25. */
  26. protected $fillable = ['name', 'email','mobile', 'password','status','site_notifications','email_notifications'];
  27. /**
  28. * The attributes excluded from the model's JSON form.
  29. *
  30. * @var array
  31. */
  32. protected $hidden = ['password', 'remember_token'];
  33. public static function boot()
  34. {
  35. parent::boot();
  36. static::saved(function($user){
  37. if(Setting()->get('xunsearch_open',0) == 1) {
  38. App::offsetGet('search')->update($user);
  39. }
  40. });
  41. static::deleted(function($user){
  42. /*删除用户扩展信息*/
  43. $user->userData()->delete();
  44. $user->userOauth()->delete();
  45. $user->authentication()->delete();
  46. /*删除用户提问*/
  47. $user->questions()->delete();
  48. /*删除用户回答*/
  49. $user->answers()->delete();
  50. /*删除用户文章*/
  51. $user->articles()->delete();
  52. /*删除粉丝*/
  53. $user->followers()->delete();
  54. /*删除收藏*/
  55. $user->collections()->delete();
  56. /*删除积分设置*/
  57. $user->credits()->delete();
  58. /*删除我的通知*/
  59. $user->notifications()->delete();
  60. /*删除动态*/
  61. $user->doings()->delete();
  62. /*删除积分兑换*/
  63. $user->exchanges()->delete();
  64. /*删除统计标签*/
  65. $user->userTags()->delete();
  66. /*删除问题邀请*/
  67. $user->questionInvitations()->delete();
  68. /*删除评论*/
  69. $user->comments()->delete();
  70. /*删除角色管理*/
  71. $user->detachAllRoles();
  72. if(Setting()->get('xunsearch_open',0) == 1) {
  73. App::offsetGet('search')->delete($user);
  74. }
  75. });
  76. }
  77. public static function getAvatarPath($userId,$size='big',$ext='jpg')
  78. {
  79. $avatarDir = self::getAvatarDir($userId);
  80. $avatarFileName = self::getAvatarFileName($userId,$size);
  81. return $avatarDir.'/'.$avatarFileName.'.'.$ext;
  82. }
  83. /**
  84. * 获取用户头像存储目录
  85. * @param $user_id
  86. * @return string
  87. */
  88. public static function getAvatarDir($userId,$rootPath='avatars')
  89. {
  90. $userId = sprintf("%09d", $userId);
  91. return $rootPath.'/'.substr($userId, 0, 3) . '/' . substr($userId, 3, 2) . '/' . substr($userId, 5, 2);
  92. }
  93. /**
  94. * 获取头像文件命名
  95. * @param string $size
  96. * @return mixed
  97. */
  98. public static function getAvatarFileName($userId,$size='big')
  99. {
  100. $avatarNames = [
  101. 'small'=>'user_small_'.$userId,
  102. 'middle'=>'user_middle_'.$userId,
  103. 'big'=>'user_big_'.$userId,
  104. 'origin'=>'user_origin_'.$userId
  105. ];
  106. return $avatarNames[$size];
  107. }
  108. /**
  109. * 从缓存中获取用户数据,主要用户问答文章等用户数据显示
  110. * @param $userId
  111. * @return mixed
  112. */
  113. public static function findFromCache($userId)
  114. {
  115. $data = Cache::remember('user_cache_'.$userId,Config::get('tipask.user_cache_time'),function() use($userId) {
  116. return self::select('name','title','gender')->find($userId);
  117. });
  118. return $data;
  119. }
  120. /*搜索*/
  121. public static function search($word,$size=16)
  122. {
  123. $list = self::where('name','like',"$word%")->paginate($size);
  124. return $list;
  125. }
  126. public function roles()
  127. {
  128. return $this->belongsToMany('App\Models\Role');
  129. }
  130. public function getRoles()
  131. {
  132. return (!$this->roles) ? $this->roles = $this->roles()->get() : $this->roles;
  133. }
  134. public function attachRole($role)
  135. {
  136. return (!$this->getRoles()->contains($role)) ? $this->roles()->attach($role) : true;
  137. }
  138. public function detachRole($role)
  139. {
  140. $this->roles = null;
  141. return $this->roles()->detach($role);
  142. }
  143. public function detachAllRoles()
  144. {
  145. $this->roles = null;
  146. return $this->roles()->detach();
  147. }
  148. /**
  149. * 检测用户是否有权限
  150. * @param string $permission
  151. * @return mixed
  152. */
  153. public function hasPermission($permission){
  154. $permissions = session('user_permissions',[]);
  155. return (in_array($permission,$permissions) || $this->isSuperAdmin());
  156. }
  157. public function getPermissionsAttribute(){
  158. return session('user_permissions',[]);
  159. }
  160. /**
  161. *获取用户数据
  162. * @param $userId
  163. */
  164. public function userData()
  165. {
  166. return $this->hasOne('App\Models\UserData');
  167. }
  168. public function userTag(){
  169. return $this->hasMany('App\Models\UserTag');
  170. }
  171. public function userOauth(){
  172. return $this->hasMany('App\Models\UserOauth');
  173. }
  174. /*用户认证信息*/
  175. public function authentication()
  176. {
  177. return $this->hasOne('App\Models\Authentication');
  178. }
  179. /*用户认证信息*/
  180. public function lecturer()
  181. {
  182. return $this->hasOne('App\Models\Lecturer');
  183. }
  184. /**
  185. * 获取用户问题
  186. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  187. */
  188. public function questions()
  189. {
  190. return $this->hasMany('App\Models\Question');
  191. }
  192. /**
  193. * 获取用户回答
  194. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  195. */
  196. public function answers()
  197. {
  198. return $this->hasMany('App\Models\Answer');
  199. }
  200. /**
  201. * 获取用户文章
  202. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  203. */
  204. public function articles()
  205. {
  206. return $this->hasMany('App\Models\Article');
  207. }
  208. /*我发起的通知*/
  209. public function notifications(){
  210. return $this->hasMany('App\Models\Notification');
  211. }
  212. /**
  213. * 获取用户动态
  214. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  215. */
  216. public function doings()
  217. {
  218. return $this->hasMany('App\Models\Doing');
  219. }
  220. /*我的评论*/
  221. public function comments(){
  222. return $this->hasMany('App\Models\Comment');
  223. }
  224. /*我的积分操作*/
  225. public function credits(){
  226. return $this->hasMany('App\Models\Credit');
  227. }
  228. /*获取用户收藏*/
  229. public function collections()
  230. {
  231. return $this->hasMany('App\Models\Collection');
  232. }
  233. /*用户关注*/
  234. public function attentions()
  235. {
  236. return $this->hasMany('App\Models\Attention');
  237. }
  238. /*用户粉丝*/
  239. public function followers()
  240. {
  241. return $this->morphToMany('App\Models\UserData', 'source','attentions','source_id','user_id');
  242. }
  243. /*邀请的回答*/
  244. public function questionInvitations()
  245. {
  246. return $this->hasMany('App\Models\QuestionInvitation');
  247. }
  248. /*我的商品兑换*/
  249. public function exchanges()
  250. {
  251. return $this->hasMany('App\Models\Exchange');
  252. }
  253. /*用户统计标签*/
  254. public function userTags(){
  255. return $this->hasMany('App\Models\UserTag','user_id');
  256. }
  257. public function hotTags(){
  258. $hotTagIds = $this->userTags()->select("tag_id")->distinct()->orderBy('supports','desc')->orderBy('answers','desc')->orderBy('created_at','desc')->take(5)->pluck('tag_id');
  259. $tags = [];
  260. foreach($hotTagIds as $hotTagId){
  261. $tag = Tag::find($hotTagId);
  262. if($tag){
  263. $tags[] = $tag;
  264. }
  265. }
  266. return $tags;
  267. }
  268. /*是否回答过问题*/
  269. public function isAnswered($questionId)
  270. {
  271. return boolval($this->answers()->where('question_id','=',$questionId)->count());
  272. }
  273. /*是否已经收藏过问题或文章*/
  274. public function isCollected($source_type,$source_id)
  275. {
  276. return $this->collections()->where('source_type','=',$source_type)->where('source_id','=',$source_id)->first();
  277. }
  278. /*是否已关注问题、用户*/
  279. public function isFollowed($source_type,$source_id)
  280. {
  281. return boolval($this->attentions()->where('source_type','=',$source_type)->where('source_id','=',$source_id)->count());
  282. }
  283. /**
  284. * 第三方账号是否绑定
  285. * @param $auth_type
  286. * @return bool
  287. */
  288. public function isOauthBind($auth_type){
  289. if($this->userOauth()->where("auth_type",'=',$auth_type)->count()){
  290. return true;
  291. }
  292. return false;
  293. }
  294. /*判断用户是否开启了邮件通知*/
  295. public function allowedEmailNotify($type){
  296. if(!in_array($type,explode(",",$this->email_notifications))){
  297. return false;
  298. }
  299. return true;
  300. }
  301. /*当前是否已签到*/
  302. public function isSigned(){
  303. $today = Carbon::parse(date('Y-m-d 00:00:00'));
  304. return $this->credits()->where('created_at','>',$today)->where('action','=','sign')->count();
  305. }
  306. public function isSuperAdmin(){
  307. return $this->id == config('tipask.super_admin_id');
  308. }
  309. public function openid(){
  310. $oauth = $this->userOauth()->where("auth_type","wechat-app")->first();
  311. if($oauth){
  312. return $oauth->id;
  313. }
  314. return '';
  315. }
  316. }