FansLists.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\lists;
  20. use app\common\model\User;
  21. /**
  22. * 粉丝列表
  23. * Class FansLists
  24. * @package app\shopapi\lists
  25. */
  26. class FansLists extends BaseShopDataLists
  27. {
  28. /**
  29. * @notes 粉丝列表
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @author Tab
  35. * @date 2021/8/5 18:45
  36. */
  37. public function lists(): array
  38. {
  39. // 设置搜索
  40. $where = $this->setWhere();
  41. $lists = User::field('id,avatar,nickname,mobile, create_time, id as fans, id as order_amount,id as order_num')
  42. ->where($where)
  43. ->order('id', 'desc')
  44. ->select()
  45. ->toArray();
  46. // 自定义排序
  47. $lists = $this->customizeOrder($lists);
  48. // 取指定页数据
  49. return array_slice($lists, $this->limitOffset, $this->limitLength);
  50. }
  51. /**
  52. * @notes 粉丝数量
  53. * @return int
  54. * @author Tab
  55. * @date 2021/8/5 18:45
  56. */
  57. public function count(): int
  58. {
  59. // 设置搜索
  60. $where = $this->setWhere();
  61. $count = User::field('avatar,nickname,mobile, create_time, id as fans, id as order_amount,id as order_num')
  62. ->where($where)
  63. ->count();
  64. return $count;
  65. }
  66. /**
  67. * @notes 设置搜索
  68. * @return array
  69. * @author Tab
  70. * @date 2021/8/5 18:29
  71. */
  72. public function setWhere()
  73. {
  74. // 粉丝类型
  75. $type = $this->params['type'] ?? 'all';
  76. switch ($type) {
  77. // 一级
  78. case 'first':
  79. $where[] = ['first_leader', '=', $this->userId];
  80. break;
  81. // 二级
  82. case 'second':
  83. $where[] = ['second_leader', '=', $this->userId];
  84. break;
  85. // 默认
  86. default:
  87. $where[] = ['first_leader|second_leader', '=', $this->userId];
  88. }
  89. // 关键字搜索
  90. if (isset($this->params['keyword']) && !empty($this->params['keyword'])) {
  91. $where[] = ['nickname|mobile', 'like', '%' . $this->params['keyword'] .'%'];
  92. }
  93. return $where;
  94. }
  95. /**
  96. * @notes 自定义排序
  97. * @return array
  98. * @author Tab
  99. * @date 2021/8/5 18:36
  100. */
  101. public function customizeOrder($lists) {
  102. foreach($lists as $key => $row) {
  103. $fans[$key] = $row['fans'];
  104. $orderAmount[$key] = $row['order_amount'];
  105. $orderNum[$key] = $row['order_num'];
  106. }
  107. // 排序 SORT_DESC = 3 倒序 , SORT_ASC = 4 升序
  108. // 粉丝数
  109. if(isset($this->params['fans']) && isset($fans)) {
  110. array_multisort($fans, strtolower($this->params['fans']) == 'asc' ? SORT_ASC : SORT_DESC, $lists);
  111. }
  112. // 已支付订单总金额
  113. if(isset($this->params['order_amount']) && isset($fans)) {
  114. array_multisort($orderAmount, strtolower($this->params['order_amount']) == 'asc' ? SORT_ASC : SORT_DESC, $lists);
  115. }
  116. // 已支付订单数量
  117. if(isset($this->params['order_num']) && isset($fans)) {
  118. array_multisort($orderNum, strtolower($this->params['order_num']) == 'asc' ? SORT_ASC : SORT_DESC, $lists);
  119. }
  120. return $lists;
  121. }
  122. }