截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\common\model\user;
  3. use app\common\basics\Models;
  4. use app\common\model\distribution\Distribution;
  5. use app\common\model\user\UserLevel;
  6. use app\common\model\distribution\DistributionOrderGoods;
  7. use app\common\server\UrlServer;
  8. use app\common\enum\ClientEnum;
  9. class User extends Models
  10. {
  11. public function getSexAttr($value)
  12. {
  13. switch ($value){
  14. case 1:
  15. return '男';
  16. case 2:
  17. return '女';
  18. default:
  19. return '未知';
  20. }
  21. }
  22. public function getBirthdayAttr($value)
  23. {
  24. return $value ? date('Y-m-d', $value) : $value;
  25. }
  26. public function getLoginTimeAttr($value)
  27. {
  28. return date('Y-m-d H:i:s', $value);
  29. }
  30. public function getAbsAvatarAttr($value)
  31. {
  32. return empty($value) ? '' : UrlServer::getFileUrl($value);
  33. }
  34. public function getClientDescAttr($value, $data)
  35. {
  36. return ClientEnum::getClient($data['client']);
  37. }
  38. public function getLevelNameAttr($value, $data)
  39. {
  40. $levelName = '-';
  41. if($data['level']) {
  42. $levelName = UserLevel::where(['id'=>$data['level'], 'del'=>0])->value('name');
  43. }
  44. return $levelName;
  45. }
  46. public function getGroupNameAttr($value, $data) {
  47. $groupName = '-';
  48. if($data['group_id']) {
  49. $groupName = UserGroup::where(['id'=>$data['group_id'], 'del'=>0])->value('name');
  50. }
  51. return $groupName;
  52. }
  53. public function getFansAttr($value, $data){
  54. $fans = User::where('find_in_set('. $data['id']. ',ancestor_relation)')->count();
  55. return $fans;
  56. }
  57. public function getDistributionOrderAttr($value, $data) {
  58. $info = DistributionOrderGoods::alias('d')
  59. ->leftJoin('order_goods g', 'g.id=d.order_goods_id')
  60. ->leftJoin('order o', 'o.id=g.order_id')
  61. ->field('count(d.id) as num, sum(d.money) as money, sum(o.order_amount) as amount')
  62. ->where([
  63. 'd.user_id' => $data['id'],
  64. 'd.status' => 2, // 已结算
  65. ])
  66. ->find();
  67. return $info;
  68. }
  69. // 获取上级
  70. public function getLeaderAttr($value, $data) {
  71. $firstLeader = [];
  72. if($data['first_leader']) {
  73. $firstLeader = self::field('id,sn,nickname,mobile,level')->where('id', $data['first_leader'])->find()->toArray();
  74. if($firstLeader['level']) {
  75. $firstLeader['levelName'] = UserLevel::where(['id'=>$firstLeader['level'], 'del'=>0])->value('name');
  76. }else{
  77. $firstLeader['levelName'] = '-';
  78. }
  79. }
  80. return $firstLeader;
  81. }
  82. public function level()
  83. {
  84. return $this->hasOne(UserLevel::class,'id', 'level');
  85. }
  86. /**
  87. * 根据user_id确认分销资格
  88. */
  89. public function confirmDistribution($user_id)
  90. {
  91. if ($this->where(['id'=>$user_id,'is_distribution'=>1,'freeze_distribution'=>0])->find()){
  92. return true;
  93. }
  94. return false;
  95. }
  96. public function getDistributionAttr($value)
  97. {
  98. $distribution = Distribution::where('user_id', $value)->findOrEmpty()->toArray();
  99. if (!empty($distribution) && $distribution['is_distribution'] == 1) {
  100. return '是';
  101. }
  102. return '否';
  103. }
  104. public function searchDistributionAttr($query, $value, $params)
  105. {
  106. // 非分销会员
  107. if (isset($params['is_distribution']) && $params['is_distribution'] != 'all' && $params['is_distribution'] == 0) {
  108. $ids = Distribution::where('is_distribution', 1)->column('user_id');
  109. $query->where('id', 'not in', $ids);
  110. }
  111. // 分销会员
  112. if (isset($params['is_distribution']) && $params['is_distribution'] != 'all' && $params['is_distribution'] == 1) {
  113. $ids = Distribution::where('is_distribution', 1)->column('user_id');
  114. $query->where('id', 'in', $ids);
  115. }
  116. }
  117. public static function getUserInfo($userId)
  118. {
  119. $user = self::field('id,sn,nickname,avatar')->findOrEmpty($userId)->toArray();
  120. if (empty($user)) {
  121. return '系统';
  122. }
  123. $user['avatar'] = empty($user['avatar']) ? '' : UrlServer::getFileUrl($user['avatar']);
  124. return $user;
  125. }
  126. static function UserIsDelete($id): bool
  127. {
  128. return (bool) User::where('id', $id)->value('user_delete');
  129. }
  130. }