UserFriendsServices.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user;
  12. use app\dao\user\UserFriendsDao;
  13. use app\services\BaseServices;
  14. use app\services\user\level\SystemUserLevelServices;
  15. use crmeb\traits\ServicesTrait;
  16. /**
  17. * 获取好友列表
  18. * Class UserFriendsServices
  19. * @package app\services\user
  20. * @mixin UserFriendsDao
  21. */
  22. class UserFriendsServices extends BaseServices
  23. {
  24. use ServicesTrait;
  25. public function __construct(UserFriendsDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * 保存好友关系
  31. * @param int $uid
  32. * @param int $friends_uid
  33. * @return bool
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function saveFriend(int $uid, int $friends_uid)
  39. {
  40. $data = [
  41. 'uid' => $uid,
  42. 'friends_uid' => $friends_uid
  43. ];
  44. $userFriend = $this->dao->get($data);
  45. $res1 = true;
  46. if (!$userFriend) {
  47. $data['add_time'] = time();
  48. $res1 = $this->dao->save($data);
  49. }
  50. return $res1;
  51. }
  52. /**
  53. * 获取好友uids 我推广的 推广我的
  54. * @param int $uid
  55. * @return array
  56. */
  57. public function getFriendUids(int $uid)
  58. {
  59. $result = [];
  60. if ($uid) {
  61. $spread = $this->dao->getColumn(['uid' => $uid], 'friends_uid');
  62. $sup_spread = $this->dao->getColumn(['friends_uid' => $uid], 'uid');
  63. $result = array_unique(array_merge($spread, $sup_spread));
  64. }
  65. return $result;
  66. }
  67. /**
  68. * 获取好友
  69. * @param int $id
  70. * @param string $field
  71. * @return array
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function getFriendList(int $uid, string $field = 'uid,nickname,level,add_time')
  77. {
  78. $uids = $this->getFriendUids($uid);
  79. $list = [];
  80. $count = 0;
  81. if ($uids) {
  82. [$page, $limit] = $this->getPageValue();
  83. /** @var UserServices $userServices */
  84. $userServices = app()->make(UserServices::class);
  85. $list = $userServices->getList(['uid' => $uids], $field, $page, $limit);
  86. /** @var SystemUserLevelServices $systemLevelServices */
  87. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  88. $systemLevelList = $systemLevelServices->getWhereLevelList([], 'id,name');
  89. if ($systemLevelList) $systemLevelList = array_combine(array_column($systemLevelList, 'id'), $systemLevelList);
  90. foreach ($list as &$item) {
  91. $item['type'] = $systemLevelList[$item['level']]['name'] ?? '暂无';
  92. $item['add_time'] = $item['add_time'] && is_numeric($item['add_time']) ? date('Y-m-d H:i:s', $item['add_time']) : '';
  93. }
  94. $count = $userServices->count(['uid' => $uids]);
  95. }
  96. return compact('list', 'count');
  97. }
  98. }