MemberModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Models;
  12. /**
  13. * 会员-模型
  14. * @author laravel开发员
  15. * @since 2020/11/11
  16. * Class MemberModel
  17. * @package App\Models
  18. */
  19. class MemberModel extends BaseModel
  20. {
  21. // 设置数据表
  22. protected $table = 'member';
  23. protected $appends = ['time_text','level_name','idcard_text','mobile_text'];
  24. // 头像
  25. public function getAvatarAttribute($value)
  26. {
  27. $value = $value ? get_image_url($value) : '';
  28. return $value;
  29. }
  30. // 背景
  31. public function getHomeBgAttribute($value)
  32. {
  33. $value = $value ? get_image_url($value) : get_image_url('/images/member/home_bg.png');
  34. return $value;
  35. }
  36. // 时间
  37. public function getTimeTextAttribute()
  38. {
  39. return $this->create_time? datetime($this->create_time,'Y-m-d H:i:s') : '';
  40. }
  41. // VIP等级名称
  42. public function getLevelNameAttribute()
  43. {
  44. $vipTypes = ['普通会员','黄金会员','白金会员'];
  45. return ($this->vip_expired >= date('Y-m-d H:i:s') || $this->vip_expired=='永久有效' || $this->vip_expired=='0000-00-00 00:00:00') && $this->member_level>0? $vipTypes[$this->member_level]: '普通会员';
  46. }
  47. public function getMemberLevelAttribute($value)
  48. {
  49. return ($this->vip_expired >= date('Y-m-d H:i:s') || $this->vip_expired=='永久有效' || $this->vip_expired=='0000-00-00 00:00:00') && $value>0? $value: 0;
  50. }
  51. public function getVipExpiredAttribute($value)
  52. {
  53. if($value >= date('Y-m-d H:i:s')){
  54. return $value;
  55. }else if(!$value || $value=='0000-00-00 00:00:00'){
  56. return '永久有效';
  57. }else{
  58. return 0;
  59. }
  60. }
  61. // 时间
  62. public function getLoginTimeAttribute($value)
  63. {
  64. return $value? datetime($value,'Y-m-d H:i:s') : '';
  65. }
  66. // 身份证
  67. public function getIdcardTextAttribute()
  68. {
  69. return $this->idcard? substr($this->idcard,0,4).'**************' : '';
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getmobileTextAttribute()
  75. {
  76. return $this->mobile? format_mobile($this->mobile):'';
  77. }
  78. /**
  79. * 推荐人
  80. */
  81. public function parent()
  82. {
  83. return $this->hasOne(MemberModel::class, 'id','parent_id')
  84. ->where(['status'=>1,'mark'=>1])
  85. ->select(['id', 'nickname','realname','avatar', 'mobile', 'status']);
  86. }
  87. /**
  88. * 邀请用户
  89. */
  90. public function invites()
  91. {
  92. return $this->hasOne(MemberModel::class, 'parent_id','id')
  93. ->where(['mark'=>1])
  94. ->select(['id', 'nickname','avatar', 'realname','parent_id', 'mobile', 'status']);
  95. }
  96. /**
  97. * 获取会员信息
  98. * @param int $id 会员ID
  99. * @return array|string
  100. * @author laravel开发员
  101. * @since 2020/11/11
  102. */
  103. public function getInfo($id)
  104. {
  105. $info = parent::getInfo($id); // TODO: Change the autogenerated stub
  106. if ($info) {
  107. // 头像
  108. if ($info['avatar']) {
  109. $info['avatar'] = get_image_url($info['avatar']);
  110. }
  111. }
  112. return $info;
  113. }
  114. }