| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 | 
							- <?php
 
- # 关系类
 
- namespace Invite\Lib;
 
- use Dever;
 
- class Relation
 
- {
 
- 	# 只记录6级关系
 
- 	private $total = 100;
 
-     # 通用的邀请方法:
 
-     # uid 当前用户的上级,需要通过code邀请码来得到
 
-     # to_uid 被邀请人,当前登录用户,注册后得到
 
-     public function set($uid, $to_uid)
 
-     {
 
-         $this->setParent($uid, $to_uid);
 
-         $this->add($uid, $to_uid, 1);
 
-         return true;
 
-     }
 
-     public function setParent($uid, $to_uid, $level = 1)
 
-     {
 
-         $parent = $this->getParent($uid);
 
-         if ($parent) {
 
-             $level = $level + 1;
 
-             if ($level > $this->total) {
 
-                 return;
 
-             }
 
-             $this->add($parent['uid'], $to_uid, $level);
 
-             $this->setParent($parent['uid'], $to_uid, $level);
 
-         }
 
-     }
 
-     # 重置上级
 
-     public function resetParent($uid, $parent)
 
-     {
 
-         Dever::db('invite/relation')->delete(array('to_uid' => $uid));
 
-         $this->set($parent, $uid);
 
-         $child = $this->getChild($uid);
 
-         if ($child) {
 
-             foreach ($child as $k => $v) {
 
-                 $child_parent = $this->getParentAll($v['to_uid']);
 
-                 if ($child_parent) {
 
-                     foreach ($child_parent as $k1 => $v1) {
 
-                         if ($v1['level'] > $v['level']) {
 
-                             Dever::db('invite/relation')->update(array('where_id' => $v['id'], 'uid' => $parent));
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return true;
 
-     }
 
-     # 清理邀请关系
 
-     public function dropParent($uid, $parent)
 
-     {
 
-         return Dever::db('invite/relation')->delete(array('to_uid' => $uid, 'uid' => $parent));
 
-     }
 
-     # 获取某个用户的上级数据
 
-     public function getParent($uid, $level = 1)
 
-     {
 
-         return Dever::db('invite/relation')->one(array('to_uid' => $uid, 'level' => $level));
 
-     }
 
-     # 获取某个用户的所有上级数据
 
-     public function getParentAll($uid, $level = false)
 
-     {
 
-         $where['to_uid'] = $uid;
 
-         if ($level) {
 
-             $where['level'] = $level;
 
-         }
 
-         return Dever::db('invite/relation')->getParent($where);
 
-     }
 
-     # 获取某个用户的下级数据
 
-     public function getChild($uid, $level = false, $page = true)
 
-     {
 
-         $where['uid'] = $uid;
 
-         if ($level) {
 
-             $where['level'] = $level;
 
-         }
 
-         if ($page) {
 
-             return Dever::db('invite/relation')->getChildByPage($where);
 
-         } else {
 
-             return Dever::db('invite/relation')->getChild($where);
 
-         }
 
-         
 
-     }
 
-     # 获取某个用户在x小时之内的下级数据
 
-     public function getChildNum($uid, $level = 1, $time = false, $curtime = false, $method = 'count')
 
-     {
 
-         $where['uid'] = $uid;
 
-         if ($level) {
 
-             $where['level'] = $level;
 
-         }
 
-         if ($time) {
 
-             $time = $time * 3600;
 
-             if ($curtime) {
 
-                 if (strstr($curtime, '-')) {
 
-                     $curtime = maketime($curtime);
 
-                 }
 
-                 $cur = $curtime;
 
-             } else {
 
-                 $cur = time();
 
-             }
 
-             $where['end'] = $cur + $time;
 
-         }
 
-         if ($method == 'count') {
 
-             $method = 'getChildCount';
 
-         } else {
 
-             $method = 'getChild';
 
-         }
 
-         
 
-         return Dever::db('invite/relation')->$method($where);
 
-     }
 
-     # 插入数据
 
-     public function add($uid, $to_uid, $level = 1)
 
-     {   
 
-         $data['uid'] = $uid;
 
-         $data['to_uid'] = $to_uid;
 
-         $data['level'] = $level;
 
-         $data['clear'] = true;
 
-         $info = Dever::db('invite/relation')->one($data);
 
-         if (!$info) {
 
-             return Dever::db('invite/relation')->insert($data);
 
-         }
 
-         
 
-         return false;
 
-     }
 
- }
 
 
  |