Browse Source

Merge remote-tracking branch 'origin/master'

Panda 1 week ago
parent
commit
b209aed987
2 changed files with 107 additions and 0 deletions
  1. 19 0
      application/api/controller/Demo.php
  2. 88 0
      application/common/model/Intro.php

+ 19 - 0
application/api/controller/Demo.php

@@ -348,6 +348,25 @@ class Demo extends Api
 
     }
 
+    //注册时设置推荐人,顺带修改网体
+    public function set_invite($invite_id = 1){
+
+        $intro = Db::name('user')->where('id',$invite_id)->field('id,invite_id,intro_level,intro_ids')->find();
+        if(!$intro) {
+            return '无效推荐人';
+        }
+
+        $data_reg['invite_id'] = $intro['id']; //推荐人id
+        $data_reg['intro_ids'] = $intro['intro_ids'] ? ($intro['intro_ids'].','.$intro['id']) : $intro['id']; //所有上级
+        if(!empty($data_reg['intro_ids'])) {
+            $data_reg['intro_ids_new'] = ','.$data_reg['intro_ids'].','; //便于查询所有下级
+        }
+        $data_reg['intro_level'] = $intro['intro_level'] + 1; //推荐网层数
+
+        //更新直推数和团队数
+        model('Intro')->addIntroNum($intro['id'], 1, 1);
+    }
+
     /**
      * 需要登录的接口
      *

+ 88 - 0
application/common/model/Intro.php

@@ -0,0 +1,88 @@
+<?php
+
+namespace app\common\model;
+
+use think\Db;
+use think\Model;
+
+/**
+ * 邀请
+ */
+class User extends Model
+{
+    protected $table = 'user';
+
+    /**
+     * 更新邀请码数和推荐团队人数
+     * @param string $intro_id 用户id
+     * @param integer $num 直推增减人数
+     * @param integer $team 团队增减人数
+     */
+    public function addIntroNum($intro_id, $num, $team)
+    {
+        //更新直推人数
+        if($num < 0) {
+            $this->where(['id' => $intro_id])->setDec('intro_num', abs($num));
+        } else {
+            $this->where(['id' => $intro_id])->setInc('intro_num', $num);
+        }
+        //更新团队
+        $rstjr = $this->where(['id' => $intro_id])->field('id,intro_ids')->find();
+        if($rstjr) {
+            $tjstr = $rstjr['intro_ids'] . "," . $rstjr['id'];
+            $tjstr = trim($tjstr, ',');
+            $arr_intro = explode(',', $tjstr);
+
+
+            if($team < 0) {
+                $this->where(['id' => ['in', $arr_intro]])->setDec('intro_num_all', abs($team));
+            } else {
+                $this->where(['id' => ['in', $arr_intro]])->setInc('intro_num_all', $team);
+            }
+
+
+        }
+    }
+
+    /**
+     * 获取上几代推荐人
+     * @param string $ids 上级id
+     * @param int $max 代数
+     * @return false|\PDOStatement|string|\think\Collection
+     */
+    public function getUp($ids = '', $max = 1)
+    {
+        if($ids) {
+            $intro_arr = explode(',', $ids);
+            $intro_arr = array_filter($intro_arr);
+            $intro_arr = array_slice($intro_arr, -$max);
+
+            $info = Db::name('user')
+                ->where(['id' => ['in', $intro_arr]])
+                ->field('id,username,nickname,logo,mobile,email,rank')
+                ->order('intro_level desc') //按级别倒序,级别越大越靠近本人
+                ->limit($max)
+                ->select();
+        }
+        return $info ?? '';
+    }
+
+    /**
+     * 获取上几代推荐人
+     */
+    function getUpComplex($username, $min = 1, $max = 1, $str = '', $step = 0)
+    {
+        if($max >= $step) {
+            $user = Db::name('user')->where(['username' => $username])->field("username,intro")->find();
+            if($user['username']) {
+                if($step >= $min) {
+                    $str .= ",".$user["username"];
+                }
+                if($user['intro']) {
+                    return $this->getUpComplex($user['intro'], $min, $max, $str, $step + 1);
+                }
+            }
+        }
+        return $str;
+    }
+}