|
@@ -0,0 +1,72 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Invite\Src;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+use Invite\Lib\Relation;
|
|
|
+
|
|
|
+class Api
|
|
|
+{
|
|
|
+ # 获取邀请码
|
|
|
+ public function code($uid)
|
|
|
+ {
|
|
|
+ $info = Dever::db('invite/code')->one(array('uid' => $uid));
|
|
|
+ if ($info) {
|
|
|
+ return $info['value'];
|
|
|
+ } else {
|
|
|
+ $code = Dever::uid($uid);
|
|
|
+ return $this->createCode($uid, $code);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function createCode($uid, $code)
|
|
|
+ {
|
|
|
+ $info = Dever::db('invite/code')->one(array('value' => $code));
|
|
|
+ if ($info) {
|
|
|
+ $code = Dever::code(6);
|
|
|
+ return $this->createCode($uid, $code);
|
|
|
+ } else {
|
|
|
+ Dever::db('invite/code')->insert(array('value' => $code, 'uid' => $uid));
|
|
|
+ return $code;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ # 根据邀请码获取邀请人uid
|
|
|
+ public function getUid($code)
|
|
|
+ {
|
|
|
+ $info = Dever::db('invite/code')->one(array('value' => $code));
|
|
|
+ if ($info) {
|
|
|
+ return $info['uid'];
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 设置邀请关系
|
|
|
+ public function setRelation($uid, $parent, $code = false)
|
|
|
+ {
|
|
|
+ if ($code) {
|
|
|
+ $parent = $this->getCode($code);
|
|
|
+ }
|
|
|
+ $relation = new Relation();
|
|
|
+ $relation->set($parent, $uid);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 获取某个用户的下级
|
|
|
+ public function getChild($uid, $level = false)
|
|
|
+ {
|
|
|
+ $relation = new Relation();
|
|
|
+ return $relation->getChild($uid, $level);
|
|
|
+ }
|
|
|
+
|
|
|
+ # 获取某个用户的上级
|
|
|
+ public function getParent($uid, $level = 1)
|
|
|
+ {
|
|
|
+ $relation = new Relation();
|
|
|
+ $info = $relation->get($uid, $level);
|
|
|
+ if ($info) {
|
|
|
+ return $info['uid'];
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|