Relation.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 getChild($uid, $level = false)
  37. {
  38. if (!$level) {
  39. $level = $this->level;
  40. }
  41. return Dever::db('invite/relation')->getChild(array('uid' => $uid, 'level' => $level));
  42. }
  43. # 插入数据
  44. public function add($uid, $to_uid, $level = 1)
  45. {
  46. $data['uid'] = $uid;
  47. $data['to_uid'] = $to_uid;
  48. $data['level'] = $level;
  49. $info = Dever::db('invite/relation')->one($data);
  50. if (!$info) {
  51. $result = Dever::db('invite/relation')->insert($data);
  52. }
  53. return true;
  54. }
  55. }