128 lines
3.6 KiB
PHP
128 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\model\user\User;
|
|
|
|
class ShareController extends BaseApiController
|
|
{
|
|
public array $notNeedLogin = ['getShareInfo'];
|
|
|
|
/**
|
|
* @notes 获取分享信息(当前用户的邀请码、昵称、头像等)
|
|
*/
|
|
public function getShareInfo()
|
|
{
|
|
$h5Domain = \app\common\service\ConfigService::get('website', 'h5_domain', '');
|
|
if (empty($h5Domain)) {
|
|
$h5Domain = request()->domain() . '/mobile';
|
|
}
|
|
|
|
// 未登录:返回通用分享信息
|
|
if (empty($this->userId)) {
|
|
return $this->data([
|
|
'invite_code' => '',
|
|
'nickname' => '世博头条',
|
|
'avatar' => '',
|
|
'h5_domain' => $h5Domain,
|
|
]);
|
|
}
|
|
|
|
$user = User::findOrEmpty($this->userId);
|
|
if ($user->isEmpty()) {
|
|
return $this->data([
|
|
'invite_code' => '',
|
|
'nickname' => '世博头条',
|
|
'avatar' => '',
|
|
'h5_domain' => $h5Domain,
|
|
]);
|
|
}
|
|
|
|
// 如果用户还没有邀请码,自动生成
|
|
$inviteCode = $user->getData('invite_code');
|
|
if (empty($inviteCode)) {
|
|
$inviteCode = User::createInviteCode();
|
|
$user->save(['invite_code' => $inviteCode]);
|
|
}
|
|
|
|
// 递归获取所有下级用户数
|
|
$teamCount = self::getTeamCount($this->userId);
|
|
|
|
return $this->data([
|
|
'invite_code' => $inviteCode,
|
|
'nickname' => $user->getData('nickname'),
|
|
'avatar' => $user->avatar,
|
|
'h5_domain' => $h5Domain,
|
|
'team_count' => $teamCount,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 递归统计所有下级用户总数
|
|
*/
|
|
private static function getTeamCount(int $userId): int
|
|
{
|
|
$directIds = User::where('inviter_id', $userId)->column('id');
|
|
if (empty($directIds)) {
|
|
return 0;
|
|
}
|
|
$count = count($directIds);
|
|
foreach ($directIds as $id) {
|
|
$count += self::getTeamCount($id);
|
|
}
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* @notes 获取我的邀请列表
|
|
*/
|
|
public function getInviteList()
|
|
{
|
|
$page = $this->request->get('page/d', 1);
|
|
$size = $this->request->get('size/d', 20);
|
|
$level = $this->request->get('level/d', 1);
|
|
|
|
// 未登录:返回空列表
|
|
if (empty($this->userId)) {
|
|
return $this->data([
|
|
'list' => [],
|
|
'total' => 0,
|
|
'page' => $page,
|
|
'size' => $size,
|
|
]);
|
|
}
|
|
|
|
if ($level == 2) {
|
|
// 二级:我的一级用户邀请的人
|
|
$firstLevelIds = User::where('inviter_id', $this->userId)->column('id');
|
|
if (empty($firstLevelIds)) {
|
|
return $this->data([
|
|
'list' => [],
|
|
'total' => 0,
|
|
'page' => $page,
|
|
'size' => $size,
|
|
]);
|
|
}
|
|
$query = User::whereIn('inviter_id', $firstLevelIds);
|
|
} else {
|
|
// 一级:直接邀请的人
|
|
$query = User::where('inviter_id', $this->userId);
|
|
}
|
|
|
|
$list = $query->field('id, sn, nickname, avatar, create_time')
|
|
->order('id', 'desc')
|
|
->page($page, $size)
|
|
->select()
|
|
->toArray();
|
|
|
|
$count = $query->count();
|
|
|
|
return $this->data([
|
|
'list' => $list,
|
|
'total' => $count,
|
|
'page' => $page,
|
|
'size' => $size,
|
|
]);
|
|
}
|
|
}
|