12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- # 关系类
- namespace Invite\Lib;
- use Dever;
- class Relation
- {
- # 只记录6级关系
- private $total = 6;
- # 通用的邀请方法:
- # 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 getParent($uid, $level = 1)
- {
- return Dever::db('invite/relation')->one(array('to_uid' => $uid, 'level' => $level));
- }
- # 获取某个用户的下级数据
- public function getChild($uid, $level = false)
- {
- if (!$level) {
- $level = $this->level;
- }
- return Dever::db('invite/relation')->getChild(array('uid' => $uid, 'level' => $level));
- }
- # 插入数据
- public function add($uid, $to_uid, $level = 1)
- {
- $data['uid'] = $uid;
- $data['to_uid'] = $to_uid;
- $data['level'] = $level;
- $info = Dever::db('invite/relation')->one($data);
- if (!$info) {
- $result = Dever::db('invite/relation')->insert($data);
- }
-
- return true;
- }
- }
|