User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\model\user;
  15. use app\common\enum\user\UserEnum;
  16. use app\common\model\BaseModel;
  17. use app\common\service\FileService;
  18. use think\model\concern\SoftDelete;
  19. /**
  20. * 用户模型
  21. * Class User
  22. * @package app\common\model\user
  23. */
  24. class User extends BaseModel
  25. {
  26. use SoftDelete;
  27. protected $deleteTime = 'delete_time';
  28. /**
  29. * @notes 关联用户授权模型
  30. * @return \think\model\relation\HasOne
  31. * @author 段誉
  32. * @date 2022/9/22 16:03
  33. */
  34. public function userAuth()
  35. {
  36. return $this->hasOne(UserAuth::class, 'user_id');
  37. }
  38. /**
  39. * @notes 搜索器-用户信息
  40. * @param $query
  41. * @param $value
  42. * @param $data
  43. * @author 段誉
  44. * @date 2022/9/22 16:12
  45. */
  46. public function searchKeywordAttr($query, $value, $data)
  47. {
  48. if ($value) {
  49. $query->where('sn|nickname|mobile|account', 'like', '%' . $value . '%');
  50. }
  51. }
  52. /**
  53. * @notes 搜索器-注册来源
  54. * @param $query
  55. * @param $value
  56. * @param $data
  57. * @author 段誉
  58. * @date 2022/9/22 16:13
  59. */
  60. public function searchChannelAttr($query, $value, $data)
  61. {
  62. if ($value) {
  63. $query->where('channel', '=', $value);
  64. }
  65. }
  66. /**
  67. * @notes 搜索器-注册时间
  68. * @param $query
  69. * @param $value
  70. * @param $data
  71. * @author 段誉
  72. * @date 2022/9/22 16:13
  73. */
  74. public function searchCreateTimeStartAttr($query, $value, $data)
  75. {
  76. if ($value) {
  77. $query->where('create_time', '>=', strtotime($value));
  78. }
  79. }
  80. /**
  81. * @notes 搜索器-注册时间
  82. * @param $query
  83. * @param $value
  84. * @param $data
  85. * @author 段誉
  86. * @date 2022/9/22 16:13
  87. */
  88. public function searchCreateTimeEndAttr($query, $value, $data)
  89. {
  90. if ($value) {
  91. $query->where('create_time', '<=', strtotime($value));
  92. }
  93. }
  94. /**
  95. * @notes 头像获取器 - 用于头像地址拼接域名
  96. * @param $value
  97. * @return string
  98. * @author Tab
  99. * @date 2021/7/17 14:28
  100. */
  101. public function getAvatarAttr($value)
  102. {
  103. return trim($value) ? FileService::getFileUrl($value) : '';
  104. }
  105. /**
  106. * @notes 获取器-性别描述
  107. * @param $value
  108. * @param $data
  109. * @return string|string[]
  110. * @author 段誉
  111. * @date 2022/9/7 15:15
  112. */
  113. public function getSexAttr($value, $data)
  114. {
  115. return UserEnum::getSexDesc($value);
  116. }
  117. /**
  118. * @notes 登录时间
  119. * @param $value
  120. * @return string
  121. * @author 段誉
  122. * @date 2022/9/23 18:15
  123. */
  124. public function getLoginTimeAttr($value)
  125. {
  126. return $value ? date('Y-m-d H:i:s', $value) : '';
  127. }
  128. /**
  129. * @notes 生成用户编码
  130. * @param string $prefix
  131. * @param int $length
  132. * @return string
  133. * @throws \think\db\exception\DataNotFoundException
  134. * @throws \think\db\exception\DbException
  135. * @throws \think\db\exception\ModelNotFoundException
  136. * @author 段誉
  137. * @date 2022/9/16 10:33
  138. */
  139. public static function createUserSn($prefix = '', $length = 8)
  140. {
  141. $rand_str = '';
  142. for ($i = 0; $i < $length; $i++) {
  143. $rand_str .= mt_rand(1, 9);
  144. }
  145. $sn = $prefix . $rand_str;
  146. if (User::where(['sn' => $sn])->find()) {
  147. return self::createUserSn($prefix, $length);
  148. }
  149. return $sn;
  150. }
  151. }