Relation.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. # 关系类
  3. namespace Invite\Lib;
  4. use Dever;
  5. class Relation
  6. {
  7. private $table = 'invite/relation';
  8. public function __construct()
  9. {
  10. if (Dever::config('base')->invite) {
  11. $this->table = Dever::config('base')->invite;
  12. }
  13. }
  14. # 只记录6级关系
  15. private $total = 100;
  16. # 通用的邀请方法:
  17. # uid 当前用户的上级,需要通过code邀请码来得到
  18. # to_uid 被邀请人,当前登录用户,注册后得到
  19. public function set($uid, $to_uid)
  20. {
  21. $this->setParent($uid, $to_uid);
  22. $this->add($uid, $to_uid, 1);
  23. return true;
  24. }
  25. public function setParent($uid, $to_uid, $level = 1)
  26. {
  27. $parent = $this->getParent($uid);
  28. if ($parent) {
  29. $level = $level + 1;
  30. if ($level > $this->total) {
  31. return;
  32. }
  33. $this->add($parent['uid'], $to_uid, $level);
  34. $this->setParent($parent['uid'], $to_uid, $level);
  35. }
  36. }
  37. # 更换上级 效率较高
  38. public function replaceParent($uid, $old_parent, $new_parent, $call = true)
  39. {
  40. $info = Dever::db($this->table)->one(array('to_uid' => $uid, 'uid' => $old_parent));
  41. if ($info) {
  42. $state = Dever::db($this->table)->update(array('where_id' => $info['id'], 'uid' => $new_parent));
  43. if ($state && $call) {
  44. $child = $this->getChild($uid, false, false);
  45. if ($child) {
  46. foreach ($child as $k => $v) {
  47. $this->replaceParent($v['to_uid'], $old_parent, $new_parent, false);
  48. }
  49. }
  50. }
  51. }
  52. return true;
  53. }
  54. # 重置上级 效率较低
  55. public function resetParent($uid, $parent)
  56. {
  57. Dever::db($this->table)->delete(array('to_uid' => $uid));
  58. $this->set($parent, $uid);
  59. $child = $this->getChild($uid, 1, false);
  60. if ($child) {
  61. foreach ($child as $k => $v) {
  62. $this->resetParent($v['to_uid'], $uid);
  63. /*
  64. $child_parent = $this->getParentAll($v['to_uid']);
  65. if ($child_parent) {
  66. foreach ($child_parent as $k1 => $v1) {
  67. if ($v1['level'] > $v['level']) {
  68. Dever::db($this->table)->update(array('where_id' => $v['id'], 'uid' => $parent));
  69. }
  70. }
  71. }*/
  72. }
  73. }
  74. return true;
  75. }
  76. # 清理邀请关系
  77. public function dropParent($uid, $parent)
  78. {
  79. return Dever::db($this->table)->delete(array('to_uid' => $uid, 'uid' => $parent));
  80. }
  81. # 获取某个用户的上级数据
  82. public function getParent($uid, $level = 1)
  83. {
  84. return Dever::db($this->table)->one(array('to_uid' => $uid, 'level' => $level));
  85. }
  86. # 获取某个用户的所有上级数据
  87. public function getParentAll($uid, $level = false, $order = 1)
  88. {
  89. $where['to_uid'] = $uid;
  90. if ($level) {
  91. $where['level'] = $level;
  92. }
  93. if ($order == 1) {
  94. return Dever::db($this->table)->getParent($where);
  95. } else {
  96. return Dever::db($this->table)->getParentLevel($where);
  97. }
  98. }
  99. # 获取某个用户的下级数据
  100. public function getChild($uid, $level = false, $page = true)
  101. {
  102. $where['uid'] = $uid;
  103. if ($level) {
  104. $where['level'] = $level;
  105. }
  106. if ($page) {
  107. return Dever::db($this->table)->getChildByPage($where);
  108. } else {
  109. return Dever::db($this->table)->getChild($where);
  110. }
  111. }
  112. # 获取某个用户在x小时之内的下级数据
  113. public function getChildNum($uid, $level = 1, $start = false, $end = false, $method = 'count')
  114. {
  115. $where['uid'] = $uid;
  116. if ($level) {
  117. $where['level'] = $level;
  118. }
  119. /*
  120. if ($time) {
  121. $time = $time * 3600;
  122. if ($curtime) {
  123. if (strstr($curtime, '-')) {
  124. $curtime = maketime($curtime);
  125. }
  126. $cur = $curtime;
  127. } else {
  128. $cur = time();
  129. }
  130. $where['end'] = $cur + $time;
  131. }
  132. */
  133. if ($start) {
  134. $where['start'] = Dever::maketime($start);
  135. }
  136. if ($end) {
  137. $where['end'] = Dever::maketime($end);
  138. }
  139. if ($method == 'count') {
  140. $method = 'getChildCount';
  141. } else {
  142. $method = 'getChild';
  143. }
  144. return Dever::db($this->table)->$method($where);
  145. }
  146. # 插入数据
  147. public function add($uid, $to_uid, $level = 1)
  148. {
  149. $data['uid'] = $uid;
  150. $data['to_uid'] = $to_uid;
  151. $data['level'] = $level;
  152. $data['clear'] = true;
  153. $info = Dever::db($this->table)->one($data);
  154. if (!$info) {
  155. return Dever::db($this->table)->insert($data);
  156. }
  157. return false;
  158. }
  159. }