dever 7 years ago
commit
6700bd9ff9
6 changed files with 337 additions and 0 deletions
  1. 27 0
      README.MD
  2. 72 0
      database/code.php
  3. 91 0
      database/relation.php
  4. 8 0
      index.php
  5. 67 0
      lib/Relation.php
  6. 72 0
      src/Api.php

+ 27 - 0
README.MD

@@ -0,0 +1,27 @@
+# 邀请组件
+
+使用方法:
+
+```
+# 1、获取uid等于1的邀请码
+$uid = 1;
+$code = Dever::load('invite/api')->code($uid);
+
+# 2、根据邀请码获取邀请人uid
+$code = '1234';
+$uid = Dever::load('invite/api')->getUid($code);
+
+# 3、设置邀请关系 uid为1的用户,上级用户uid为3;如果$parant不存在,也可以输入邀请码
+$parent = 3;
+$uid = Dever::load('invite/api')->setRelation($uid, $parent, $code = false);
+
+# 4、获取某个用户的下级列表
+level为关系级数,默认为6级
+$data = Dever::load('invite/api')->getChild($uid, $level = 6);
+
+# 5、获取某个用户的上级
+level为关系级数,默认为1级
+$uid = Dever::load('invite/api')->getParent($uid, $level = 1);
+
+
+```

+ 72 - 0
database/code.php

@@ -0,0 +1,72 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'code',
+	# 显示给用户看的名称
+	'lang' => '邀请码',
+	# 是否显示在后台菜单
+	'order' => 10,
+	'menu'	=> false,
+
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			//'list'		=> true,
+		),
+
+		'uid' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '用户',
+			'default' 	=> '',
+			'desc' 		=> '用户ID',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
+
+		'value'		=> array
+		(
+			'type' 		=> 'varchar-50',
+			'name' 		=> '邀请码',
+			'default' 	=> '',
+			'desc' 		=> '邀请码',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'search'	=> 'date',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+	),
+
+	'request' => array
+	(
+		
+	)
+);

+ 91 - 0
database/relation.php

@@ -0,0 +1,91 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'relation',
+	# 显示给用户看的名称
+	'lang' => '邀请关系',
+	# 是否显示在后台菜单
+	'order' => 9,
+	'menu'	=> false,
+
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			//'list'		=> true,
+		),
+
+		'uid' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '邀请人',
+			'default' 	=> '',
+			'desc' 		=> '邀请人',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
+
+		'to_uid' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '被邀请人',
+			'default' 	=> '',
+			'desc' 		=> '被邀请人',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
+
+		'level' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '邀请级数',
+			'default' 	=> '',
+			'desc' 		=> '邀请级数',
+			'match' 	=> 'is_numeric',
+			'list'		=> true,
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'search'	=> 'date',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+	(
+		'delete' => false,
+		'edit' => false,
+		'insert' => false,
+	),
+
+	'request' => array
+	(
+		'getChild' => array
+		(
+			# 匹配的正则或函数 选填项
+			'where' => array
+			(
+				'uid' => 'yes',
+				'level' => array('yes', '<='),
+			),
+			'type' => 'all',
+			'order' => array('id' => 'desc'),
+			'col' => '*',
+		),
+	)
+);

+ 8 - 0
index.php

@@ -0,0 +1,8 @@
+<?php
+
+define('DEVER_APP_NAME', 'invite');
+define('DEVER_APP_LANG', '邀请管理');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+define('DEVER_MANAGE_ORDER', 1);
+define('DEVER_MANAGE_ICON', 'glyphicon glyphicon-cutlery');
+include(DEVER_APP_PATH . '../boot.php');

+ 67 - 0
lib/Relation.php

@@ -0,0 +1,67 @@
+<?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;
+    }
+}

+ 72 - 0
src/Api.php

@@ -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;
+	}
+}