User.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2020/2/4
  6. * Time: 12:47
  7. */
  8. namespace app\common\model;
  9. use think\Model;
  10. class User extends Model
  11. {
  12. protected $autoWriteTimestamp = false;
  13. const TYPE_ACCOUNT = 0;
  14. const TYPE_WECHAT = 1;
  15. const TYPE_QQ = 2;
  16. const TYPE_PHONE = 3;
  17. const TYPE_EMAIL = 4;
  18. const USER_TYPES = [
  19. self::TYPE_ACCOUNT => '账号密码',
  20. self::TYPE_WECHAT => '微信',
  21. self::TYPE_QQ => 'QQ',
  22. self::TYPE_PHONE => '手机号',
  23. self::TYPE_EMAIL => '邮箱',
  24. ];
  25. const CATE_USER = 0;
  26. const CATE_AGENT = 1;
  27. const CATE_MANAGE = 2;
  28. const USER_CATES = [
  29. self::CATE_USER => '用户',
  30. self::CATE_MANAGE => '管理',
  31. ];
  32. const STATUS_WAIT = 0;
  33. const STATUS_PASS = 1;
  34. const STATUS_UNPASS = -1;
  35. const STATUS = [
  36. self::STATUS_WAIT => '待激活',
  37. self::STATUS_PASS => '已激活',
  38. self::STATUS_UNPASS => '已被封'
  39. ];
  40. const GRADE = ["非会员", "月度会员", "季度会员", "年度会员"];
  41. /**
  42. * 是否为会员
  43. * @return bool
  44. */
  45. public function isVip()
  46. {
  47. return !empty($this->level) && !$this->isVipExpire();
  48. }
  49. /**
  50. * 是否过期,如果stop_time为空不算过期
  51. * @return bool
  52. */
  53. public function isVipExpire()
  54. {
  55. if (empty($this->stop_time))
  56. return false;
  57. return $this->stop_time < time();
  58. }
  59. /**
  60. * 是否为代理
  61. * @return bool
  62. */
  63. public function isAgent()
  64. {
  65. return $this->user_cate == self::CATE_AGENT;
  66. }
  67. /**
  68. * 是否为管理
  69. * @return bool
  70. */
  71. public function isManage()
  72. {
  73. return $this->user_cate == self::CATE_MANAGE;
  74. }
  75. /**
  76. * 计算会员到期时间
  77. * @param $vipId
  78. * @return false|int|mixed
  79. */
  80. public function createStopTime($vipId)
  81. {
  82. $starttime = $this->isVip() ? $this->stop_time : time();
  83. switch ($vipId) {
  84. case 1: //月
  85. return $starttime + 31 * 24 * 3600;
  86. case 2: //季度
  87. return $starttime + 93 * 24 * 3600;
  88. case 3: //年
  89. return strtotime('+1 year', $starttime);
  90. default:
  91. return $starttime;
  92. }
  93. }
  94. //nickname
  95. public function getNicknameAttr($value, $data)
  96. {
  97. return htmlspecialchars($value);
  98. }
  99. //level_text
  100. public function getLevelTextAttr($value, $data)
  101. {
  102. return self::GRADE[$data['level']]??'';
  103. }
  104. //status_text
  105. public function getStatusTextAttr($value, $data)
  106. {
  107. return self::STATUS[$data['status']]??'';
  108. }
  109. //stop_time_text
  110. public function getStopTimeTextAttr($value, $data)
  111. {
  112. if (empty($this->level)) {
  113. return '';
  114. }
  115. if (empty($this->stop_time)) {
  116. return '永久';
  117. }
  118. if ($this->stop_time < time()) {
  119. return '已过期';
  120. }
  121. return $this->stop_time ? date('Y-m-d H:i:s', $this->stop_time) : '';
  122. }
  123. //register_time
  124. public function getRegisterTimeAttr($value, $data)
  125. {
  126. return $value ? date('Y-m-d H:i:s', $value) : '';
  127. }
  128. //login_time
  129. public function getLoginTimeAttr($value, $data)
  130. {
  131. return $value ? date('Y-m-d H:i:s', $value) : '';
  132. }
  133. //user_type_text 账号类型
  134. public function getUserTypeTextAttr($value, $data)
  135. {
  136. return self::USER_TYPES[$data['user_type']]??"";
  137. }
  138. //user_cate_text 用户角色
  139. public function getUserCateTextAttr($value, $data)
  140. {
  141. return self::USER_CATES[$data['user_cate']]??"";
  142. }
  143. //invite_code 邀请码
  144. public function getInviteCodeAttr($value, $data)
  145. {
  146. return inviteEncode($this->id);
  147. }
  148. //child_count 邀请人数
  149. public function getChildCountAttr()
  150. {
  151. return $this->hasMany('User', 'pid')->where('status', self::STATUS_PASS)->count();
  152. }
  153. //推荐人
  154. public function parent()
  155. {
  156. return $this->belongsTo('User', 'pid');
  157. }
  158. }