81 lines
2.5 KiB
PHP
81 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\admin\controller\Base;
|
|
use \think\Request;
|
|
use think\facade\View;
|
|
use app\common\model\User;
|
|
use app\common\service\RankService;
|
|
|
|
class UserRank extends Base
|
|
{
|
|
/**
|
|
* 用户排行榜页面
|
|
*/
|
|
public function index()
|
|
{
|
|
// 当前选中的排行榜类型,默认为邀请新人排行榜
|
|
$type = request()->param('type', 'invite');
|
|
|
|
// 页码和每页显示条数
|
|
$page = request()->param('page/d', 1);
|
|
$limit = request()->param('limit/d', 15);
|
|
|
|
// 排序字段,主要用于亏损排行榜可按亏损金额或亏损率排序
|
|
$sortField = request()->param('sort_field', 'loss_money');
|
|
|
|
// 获取排行榜服务类
|
|
$rankService = new RankService();
|
|
|
|
// 获取排行榜数据
|
|
try {
|
|
// 亏损排行榜需要额外传入排序字段
|
|
if ($sortField != 'loss_money') {
|
|
$data = $rankService->getRankList('loss_desc', $page, $limit);
|
|
} else {
|
|
$data = $rankService->getRankList($type, $page, $limit);
|
|
}
|
|
|
|
// 向模板传递变量
|
|
View::assign('list', $data);
|
|
View::assign('type', $type);
|
|
View::assign('page', $page);
|
|
View::assign('limit', $limit);
|
|
View::assign('sort_field', $sortField);
|
|
|
|
return View::fetch('user_rank/index');
|
|
} catch (\Exception $e) {
|
|
// 异常处理
|
|
return $this->renderError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 异步加载排行榜数据接口
|
|
*/
|
|
public function getRankData()
|
|
{
|
|
// 获取参数
|
|
$type = request()->param('type', 'invite');
|
|
$page = request()->param('page/d', 1);
|
|
$limit = request()->param('limit/d', 15);
|
|
$sortField = request()->param('sort_field', 'loss_money');
|
|
|
|
// 获取排行榜服务类
|
|
$rankService = new RankService();
|
|
|
|
try {
|
|
// 亏损排行榜需要额外传入排序字段
|
|
if ($type == 'loss') {
|
|
$data = $rankService->getLossRank(0, 0, $page, $limit, $sortField);
|
|
} else {
|
|
$data = $rankService->getRankList($type, $page, $limit);
|
|
}
|
|
|
|
return $this->renderSuccess('获取成功', $data);
|
|
} catch (\Exception $e) {
|
|
return $this->renderError($e->getMessage());
|
|
}
|
|
}
|
|
}
|