Relation.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. # 关系类
  3. namespace Invite\Lib;
  4. use Dever;
  5. class Relation
  6. {
  7. # 只记录6级关系
  8. private $total = 20;
  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 dropParent($uid, $parent)
  32. {
  33. return Dever::db('invite/relation')->delete(array('to_uid' => $uid, 'uid' => $parent));
  34. }
  35. # 获取某个用户的上级数据
  36. public function getParent($uid, $level = 1)
  37. {
  38. return Dever::db('invite/relation')->one(array('to_uid' => $uid, 'level' => $level));
  39. }
  40. # 获取某个用户的所有上级数据
  41. public function getParentAll($uid, $level = false)
  42. {
  43. $where['to_uid'] = $uid;
  44. if ($level) {
  45. $where['level'] = $level;
  46. }
  47. return Dever::db('invite/relation')->getParent($where);
  48. }
  49. # 获取某个用户的下级数据
  50. public function getChild($uid, $level = false)
  51. {
  52. $where['uid'] = $uid;
  53. if ($level) {
  54. $where['level'] = $level;
  55. }
  56. return Dever::db('invite/relation')->getChild($where);
  57. }
  58. # 获取某个用户在x小时之内的下级数据
  59. public function getChildNum($uid, $level = 1, $time = false, $curtime = false, $method = 'count')
  60. {
  61. $where['uid'] = $uid;
  62. if ($level) {
  63. $where['level'] = $level;
  64. }
  65. if ($time) {
  66. $time = $time * 3600;
  67. if ($curtime) {
  68. if (strstr($curtime, '-')) {
  69. $curtime = maketime($curtime);
  70. }
  71. $cur = $curtime;
  72. } else {
  73. $cur = time();
  74. }
  75. $where['end'] = $cur + $time;
  76. }
  77. if ($method == 'count') {
  78. $method = 'getChildCount';
  79. } else {
  80. $method = 'getChild';
  81. }
  82. return Dever::db('invite/relation')->$method($where);
  83. }
  84. # 插入数据
  85. public function add($uid, $to_uid, $level = 1)
  86. {
  87. $data['uid'] = $uid;
  88. $data['to_uid'] = $to_uid;
  89. $data['level'] = $level;
  90. $info = Dever::db('invite/relation')->one($data);
  91. if (!$info) {
  92. $result = Dever::db('invite/relation')->insert($data);
  93. }
  94. return true;
  95. }
  96. }