rabin 7 months ago
commit
a8bfe35dbc
6 changed files with 292 additions and 0 deletions
  1. 35 0
      README.MD
  2. 9 0
      index.php
  3. 41 0
      lib/Code.php
  4. 145 0
      lib/Relation.php
  5. 28 0
      table/code.php
  6. 34 0
      table/relation.php

+ 35 - 0
README.MD

@@ -0,0 +1,35 @@
+# 邀请组件
+
+使用方法:
+
+```
+# 1、获取uid等于1的邀请码
+$uid = 1;
+$code = Dever::load('code', 'invite')->get($uid);
+
+# 2、根据邀请码获取邀请人uid
+$code = '1234';
+$uid = Dever::load('code', 'invite')->getUid($code);
+
+# 3、设置邀请关系 uid为1的用户,下级用户uid为3
+$child = 3;
+$uid = Dever::load('relation', 'invite')->set($uid, $child);
+
+# 4、获取某个用户的下级列表
+level为关系级数,默认为6级
+$data = Dever::load('relation', 'invite')->getChild($uid, $level = 6);
+
+# 5、获取某个用户的上级
+level为关系级数,默认为1级
+$uid = Dever::load('relation', 'invite')->getParent($uid, $level = 1);
+
+# 6、获取某个用户的所有上级
+$data = Dever::load('relation', 'invite')->getParentAll($uid);
+
+# 7、获取来源用户uid(根据参数source_signature)
+uid 当前用户uid
+error为错误提示类型,默认为true,直接报错退出,false为return错误信息
+$uid = Dever::load('relation', 'invite')->getSourceUid($uid, $error);
+
+
+```

+ 9 - 0
index.php

@@ -0,0 +1,9 @@
+<?php
+
+if (!defined('DEVER_APP_NAME')) {
+	# 这样定义就可以将组件重复使用
+	define('DEVER_APP_NAME', 'invite');
+}
+define('DEVER_APP_LANG', '邀请组件');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+include(DEVER_APP_PATH . '../../boot.php');

+ 41 - 0
lib/Code.php

@@ -0,0 +1,41 @@
+<?php
+namespace Invite\Lib;
+use Dever;
+use Dever\Helper\Str;
+class Code 
+{
+    # 获取邀请码
+    public function get($uid)
+    {   
+        $info = Dever::db('code', 'invite')->find(array('uid' => $uid));
+        if ($info) {
+            return $info['value'];
+        } else {
+            //$code = Dever::uid($uid);
+            $code = Str::rand(5, 0);
+            return $this->createCode($uid, $code);
+        }
+    }
+
+    private function createCode($uid, $code)
+    {
+        $info = Dever::db('code', 'invite')->find(array('value' => $code));
+        if ($info) {
+            $code = Str::rand(5, 0);
+            return $this->createCode($uid, $code);
+        } else {
+            Dever::db('code', 'invite')->insert(array('value' => $code, 'uid' => $uid));
+            return $code;
+        }
+    }
+
+    # 根据邀请码获取邀请人uid
+    public function getUid($code)
+    {   
+        $info = Dever::db('code', 'invite')->find(array('value' => $code));
+        if ($info) {
+            return $info['uid'];
+        }
+        return false;
+    }
+}

+ 145 - 0
lib/Relation.php

@@ -0,0 +1,145 @@
+<?php
+# 关系类
+namespace Invite\Lib;
+use Dever;
+
+class Relation
+{
+    private $table = 'invite/relation';
+    public function __construct($invite = '')
+    {
+        if ($invite) {
+            $this->table = $invite;
+        }
+    }
+
+    # 只记录3级关系
+    private $total = 3;
+
+    # 通用的邀请方法:
+    # 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 replaceParent($uid, $old_parent, $new_parent, $call = true)
+    {
+        $info = Dever::db($this->table)->find(array('to_uid' => $uid, 'uid' => $old_parent));
+        if ($info) {
+            $state = Dever::db($this->table)->update($info['id'], array('uid' => $new_parent));
+            if ($state && $call) {
+                $child = $this->getChild($uid, false, false);
+                if ($child) {
+                    foreach ($child as $k => $v) {
+                        $this->replaceParent($v['to_uid'], $old_parent, $new_parent, false);
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    # 重置上级
+    public function resetParent($uid, $parent)
+    {
+        Dever::db($this->table)->delete(array('to_uid' => $uid));
+        $this->set($parent, $uid);
+
+        $child = $this->getChild($uid, 1, false);
+        if ($child) {
+            foreach ($child as $k => $v) {
+                $this->resetParent($v['to_uid'], $uid);
+            }
+        }
+        return true;
+    }
+
+    # 清理邀请关系
+    public function dropParent($uid, $parent)
+    {
+        return Dever::db($this->table)->delete(array('to_uid' => $uid, 'uid' => $parent));
+    }
+
+    # 获取某个用户的上级数据
+    public function getParent($uid, $level = 1)
+    {
+        return Dever::db($this->table)->find(array('to_uid' => $uid, 'level' => $level));
+    }
+
+    # 获取某个用户的所有上级数据
+    public function getParentAll($uid, $level = false)
+    {
+        $where['to_uid'] = $uid;
+        if ($level) {
+            $where['level'] = $level;
+        }
+        return Dever::db($this->table)->select($where);
+    }
+
+    # 获取某个用户的下级数据
+    public function getChild($uid, $level = false, $page = true)
+    {
+        $where['uid'] = $uid;
+        if ($level) {
+            $where['level'] = $level;
+        }
+        $set = array();
+        if ($page) {
+            $set['num'] = $page;
+        }
+        return Dever::db($this->table)->select($where, $set);
+    }
+
+    # 获取某个用户在x小时之内的下级数据
+    public function getChildNum($uid, $level = 1, $start = false, $end = false, $method = 'count')
+    {
+        $where['uid'] = $uid;
+        if ($level) {
+            $where['level'] = $level;
+        }
+        if ($start) {
+            $where['cdate#'] = array('>=', strtotime($start));
+        }
+        if ($end) {
+            $where['cdate##'] = array('<=', strtotime($end));
+        }
+        $method = 'select';
+        if ($method == 'count') {
+            $method = 'count';
+        }
+        return Dever::db($this->table)->$method($where);
+    }
+
+    # 插入数据
+    public function add($uid, $to_uid, $level = 1)
+    {   
+        $data['uid'] = $uid;
+        $data['to_uid'] = $to_uid;
+        $data['level'] = $level;
+        $info = Dever::db($this->table)->find($data);
+        if (!$info) {
+            return Dever::db($this->table)->insert($data);
+        }
+        
+        return false;
+    }
+}

+ 28 - 0
table/code.php

@@ -0,0 +1,28 @@
+<?php
+return array
+(
+    'name' => '邀请码',
+    'partition' => 'Dever::call("manage/common.system")',
+    # 数据结构
+    'struct' => array
+    (
+        'uid'       => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '用户',
+        ),
+
+        'value'      => array
+        (
+            'name'      => '名称',
+            'type'      => 'varchar(30)',
+        ),
+    ),
+
+    /*
+    'index' => array
+    (
+        'search' => 'uid,value',
+    ),
+    */
+);

+ 34 - 0
table/relation.php

@@ -0,0 +1,34 @@
+<?php
+return array
+(
+    'name' => '邀请关系',
+    'partition' => 'Dever::call("manage/common.system")',
+    # 数据结构
+    'struct' => array
+    (
+        'uid'       => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '邀请人',
+        ),
+
+        'to_uid'       => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '被邀请人',
+        ),
+
+        'level'       => array
+        (
+            'type'      => 'int(11)',
+            'name'      => '邀请级数',
+        ),
+    ),
+
+    /*
+    'index' => array
+    (
+        'search' => 'uid,value',
+    ),
+    */
+);