User.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. const SEX_UNKNOW = 0;
  42. const SEX_MAN = 1;
  43. const SEX_WOMAN = 2;
  44. const SEXS = [
  45. self::SEX_UNKNOW => '未知',
  46. self::SEX_MAN => '男',
  47. self::SEX_WOMAN => '女'
  48. ];
  49. //sex_text
  50. public function getSexTextAttr($value, $data)
  51. {
  52. return self::SEXS[$data['sex']]??'';
  53. }
  54. /**
  55. * 是否为会员
  56. * @return bool
  57. */
  58. public function isVip()
  59. {
  60. return !empty($this->level) && !$this->isVipExpire();
  61. }
  62. /**
  63. * 是否过期,如果stop_time为空不算过期
  64. * @return bool
  65. */
  66. public function isVipExpire()
  67. {
  68. if (empty($this->stop_time))
  69. return false;
  70. return $this->stop_time < time();
  71. }
  72. /**
  73. * 是否为代理
  74. * @return bool
  75. */
  76. public function isAgent()
  77. {
  78. return $this->user_cate == self::CATE_AGENT;
  79. }
  80. /**
  81. * 是否为管理
  82. * @return bool
  83. */
  84. public function isManage()
  85. {
  86. return $this->user_cate == self::CATE_MANAGE;
  87. }
  88. /**
  89. * 计算会员到期时间
  90. * @param $vipId
  91. * @return false|int|mixed
  92. */
  93. public function createStopTime($vipId)
  94. {
  95. $starttime = $this->isVip() ? $this->stop_time : time();
  96. switch ($vipId) {
  97. case 1: //月
  98. return $starttime + 31 * 24 * 3600;
  99. case 2: //季度
  100. return $starttime + 93 * 24 * 3600;
  101. case 3: //年
  102. return strtotime('+1 year', $starttime);
  103. default:
  104. return $starttime;
  105. }
  106. }
  107. //nickname
  108. public function getNicknameAttr($value, $data)
  109. {
  110. return htmlspecialchars($value);
  111. }
  112. //level_text
  113. public function getLevelTextAttr($value, $data)
  114. {
  115. return self::GRADE[$data['level']]??'';
  116. }
  117. //status_text
  118. public function getStatusTextAttr($value, $data)
  119. {
  120. return self::STATUS[$data['status']]??'';
  121. }
  122. //stop_time_text
  123. public function getStopTimeTextAttr($value, $data)
  124. {
  125. if (empty($this->level)) {
  126. return '';
  127. }
  128. if (empty($this->stop_time)) {
  129. return '永久';
  130. }
  131. if ($this->stop_time < time()) {
  132. return '已过期';
  133. }
  134. return $this->stop_time ? date('Y-m-d H:i:s', $this->stop_time) : '';
  135. }
  136. //register_time
  137. public function getRegisterTimeAttr($value, $data)
  138. {
  139. return $value ? date('Y-m-d H:i:s', $value) : '';
  140. }
  141. //login_time
  142. public function getLoginTimeAttr($value, $data)
  143. {
  144. return $value ? date('Y-m-d H:i:s', $value) : '';
  145. }
  146. //user_type_text 账号类型
  147. public function getUserTypeTextAttr($value, $data)
  148. {
  149. return self::USER_TYPES[$data['user_type']]??"";
  150. }
  151. //user_cate_text 用户角色
  152. public function getUserCateTextAttr($value, $data)
  153. {
  154. return self::USER_CATES[$data['user_cate']]??"";
  155. }
  156. //invite_code 邀请码
  157. public function getInviteCodeAttr($value, $data)
  158. {
  159. return inviteEncode($this->id);
  160. }
  161. //child_count 邀请人数
  162. public function getChildCountAttr()
  163. {
  164. return $this->hasMany('User', 'pid')->where('status', self::STATUS_PASS)->count();
  165. }
  166. //推荐人
  167. public function parent()
  168. {
  169. return $this->belongsTo('User', 'pid');
  170. }
  171. }