User.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 app\common\model\recharge\RechargeOrder;
  19. use think\model\concern\SoftDelete;
  20. /**
  21. * 用户模型
  22. * Class User
  23. * @package app\common\model\user
  24. */
  25. class User extends BaseModel
  26. {
  27. use SoftDelete;
  28. protected $deleteTime = 'delete_time';
  29. /**
  30. * @notes 关联用户授权模型
  31. * @return \think\model\relation\HasOne
  32. * @author 段誉
  33. * @date 2022/9/22 16:03
  34. */
  35. public function userAuth()
  36. {
  37. return $this->hasOne(UserAuth::class, 'user_id');
  38. }
  39. public function agricultural()
  40. {
  41. return $this->hasOne(RechargeOrder::class, 'id', 'agricultural_id')
  42. ->field('*');
  43. }
  44. public function bake()
  45. {
  46. return $this->hasOne(RechargeOrder::class, 'id', 'bake_id')
  47. ->field('*');
  48. }
  49. public function air()
  50. {
  51. return $this->hasOne(RechargeOrder::class, 'id', 'air_id')
  52. ->field('*');
  53. }
  54. /**
  55. * @notes 搜索器-用户信息
  56. * @param $query
  57. * @param $value
  58. * @param $data
  59. * @author 段誉
  60. * @date 2022/9/22 16:12
  61. */
  62. public function searchKeywordAttr($query, $value, $data)
  63. {
  64. if ($value) {
  65. $query->where('sn|nickname|mobile|account', 'like', '%' . $value . '%');
  66. }
  67. }
  68. /**
  69. * @notes 搜索器-注册来源
  70. * @param $query
  71. * @param $value
  72. * @param $data
  73. * @author 段誉
  74. * @date 2022/9/22 16:13
  75. */
  76. public function searchChannelAttr($query, $value, $data)
  77. {
  78. if ($value) {
  79. $query->where('channel', '=', $value);
  80. }
  81. }
  82. /**
  83. * @notes 搜索器-注册时间
  84. * @param $query
  85. * @param $value
  86. * @param $data
  87. * @author 段誉
  88. * @date 2022/9/22 16:13
  89. */
  90. public function searchCreateTimeStartAttr($query, $value, $data)
  91. {
  92. if ($value) {
  93. $query->where('create_time', '>=', strtotime($value));
  94. }
  95. }
  96. /**
  97. * @notes 搜索器-注册时间
  98. * @param $query
  99. * @param $value
  100. * @param $data
  101. * @author 段誉
  102. * @date 2022/9/22 16:13
  103. */
  104. public function searchCreateTimeEndAttr($query, $value, $data)
  105. {
  106. if ($value) {
  107. $query->where('create_time', '<=', strtotime($value));
  108. }
  109. }
  110. /**
  111. * @notes 头像获取器 - 用于头像地址拼接域名
  112. * @param $value
  113. * @return string
  114. * @author Tab
  115. * @date 2021/7/17 14:28
  116. */
  117. public function getAvatarAttr($value)
  118. {
  119. return trim($value) ? FileService::getFileUrl($value) : '';
  120. }
  121. /**
  122. * @notes 获取器-性别描述
  123. * @param $value
  124. * @param $data
  125. * @return string|string[]
  126. * @author 段誉
  127. * @date 2022/9/7 15:15
  128. */
  129. public function getSexAttr($value, $data)
  130. {
  131. return UserEnum::getSexDesc($value);
  132. }
  133. /**
  134. * @notes 登录时间
  135. * @param $value
  136. * @return string
  137. * @author 段誉
  138. * @date 2022/9/23 18:15
  139. */
  140. public function getLoginTimeAttr($value)
  141. {
  142. return $value ? date('Y-m-d H:i:s', $value) : '';
  143. }
  144. /**
  145. * @notes 生成用户编码
  146. * @param string $prefix
  147. * @param int $length
  148. * @return string
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * @author 段誉
  153. * @date 2022/9/16 10:33
  154. */
  155. public static function createUserSn($prefix = '', $length = 8)
  156. {
  157. $rand_str = '';
  158. for ($i = 0; $i < $length; $i++) {
  159. $rand_str .= mt_rand(1, 9);
  160. }
  161. $sn = $prefix . $rand_str;
  162. if (User::where(['sn' => $sn])->find()) {
  163. return self::createUserSn($prefix, $length);
  164. }
  165. return $sn;
  166. }
  167. }