Relation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. # 关系类
  3. namespace Invite\Lib;
  4. use Dever;
  5. class Relation
  6. {
  7. # 只记录6级关系
  8. private $total = 6;
  9. # 通用的邀请方法:
  10. # uid 当前用户的上级,需要通过code邀请码来得到
  11. # to_uid 被邀请人,当前登录用户,注册后得到
  12. public function set($uid, $to_uid)
  13. {
  14. $this->setParent($uid, $to_uid);
  15. $this->add($uid, $to_uid, 1);
  16. return true;
  17. }
  18. public function setParent($uid, $to_uid, $level = 1)
  19. {
  20. $parent = $this->getParent($uid);
  21. if ($parent) {
  22. $level = $level + 1;
  23. if ($level > $this->total) {
  24. return;
  25. }
  26. $this->add($parent['uid'], $to_uid, $level);
  27. $this->setParent($parent['uid'], $to_uid, $level);
  28. }
  29. }
  30. # 获取某个用户的上级数据
  31. public function getParent($uid, $level = 1)
  32. {
  33. return Dever::db('invite/relation')->one(array('to_uid' => $uid, 'level' => $level));
  34. }
  35. # 获取某个用户的所有上级数据
  36. public function getParentAll($uid, $level = false)
  37. {
  38. $where['to_uid'] = $uid;
  39. if ($level) {
  40. $where['level'] = $level;
  41. }
  42. return Dever::db('invite/relation')->getParent($where);
  43. }
  44. # 获取某个用户的下级数据
  45. public function getChild($uid, $level = false)
  46. {
  47. $where['uid'] = $uid;
  48. if ($level) {
  49. $where['level'] = $level;
  50. }
  51. return Dever::db('invite/relation')->getChild($where);
  52. }
  53. # 获取某个用户在x小时之内的下级数据
  54. public function getChildNum($uid, $level = 1, $time = false, $curtime = false, $method = 'count')
  55. {
  56. $where['uid'] = $uid;
  57. if ($level) {
  58. $where['level'] = $level;
  59. }
  60. if ($time) {
  61. $time = $time * 3600;
  62. if ($curtime) {
  63. if (strstr($curtime, '-')) {
  64. $curtime = maketime($curtime);
  65. }
  66. $cur = $curtime;
  67. } else {
  68. $cur = time();
  69. }
  70. $where['end'] = $cur + $time;
  71. }
  72. if ($method == 'count') {
  73. $method = 'getChildCount';
  74. } else {
  75. $method = 'getChild';
  76. }
  77. return Dever::db('invite/relation')->$method($where);
  78. }
  79. # 插入数据
  80. public function add($uid, $to_uid, $level = 1)
  81. {
  82. $data['uid'] = $uid;
  83. $data['to_uid'] = $to_uid;
  84. $data['level'] = $level;
  85. $info = Dever::db('invite/relation')->one($data);
  86. if (!$info) {
  87. $result = Dever::db('invite/relation')->insert($data);
  88. }
  89. return true;
  90. }
  91. }