2019-09-22 22:10:04 +08:00

70 lines
2.2 KiB
PHP

<?php
namespace wstmart\app\controller;
use wstmart\app\model\Users;
use wstmart\common\model\UserLevel as UL;
use wstmart\common\model\UserTrees as UT;
use think\Collection;
/**
* ============================================================================
* 用户控制器
*/
class UserLevel extends Base
{
protected $beforeActionList = [
'checkAuth' => ['except'=>'']// 访问这些except下的方法不需要执行前置操作
];
/**
* index
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author 倪苍华 <canghua.cc@gmail.com>
* Date 2019/9/10 10:37
*/
public function index()
{
$userId = get_my_id();
$User = UT::getMyLevel($userId);
return $User;
}
/**
* TreeList
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @author 倪苍华 <canghua.cc@gmail.com>
* Date 2019/9/10 11:18
*/
public function TreeList()
{
$userId = get_my_id();
$count = UL::where(['uid' => $userId])->group("level")->count();
$treeArr = [];
$newLevel = 1;
do {
if ($newLevel > 10) break;
$treeArr[$newLevel]['level'] = UT::$level[$newLevel];
$ptree = UT::where(['uid' => $userId])->find();// 找到我的层级
$userId = $ptree->pid;// 上级uid
if ($userId == 0) $userId = 1;// 上级uid
$plevel = UL::where(['uid' => $userId, 'level' => $newLevel])->find();// 查看上级是否有权限助购
if ($plevel) {// 如果有,则使用上级信息
$treeArr[$newLevel]['userId'] = $userId;
$user = Users::where(['userId' => $plevel->uid])->find();
$treeArr[$newLevel]['userName'] = $user->userName ?: $user->loginName;
$newLevel++;
}
// 如果没有,则递归查询上级信息
} while ($newLevel < ($count + 2));
// pd($treeArr);
return $treeArr;
}
}