You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
6.8 KiB
234 lines
6.8 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\event\UserPasswordChange;
|
|
use app\middleware\CheckAdmin;
|
|
use app\model\AdminDownScoresRecords;
|
|
use app\model\AdminUpScoresRecords;
|
|
use app\model\RechargeRecords;
|
|
use app\model\User;
|
|
use app\model\WithdrawalRecords;
|
|
use think\facade\Db;
|
|
use think\facade\Event;
|
|
use think\facade\Request;
|
|
use think\facade\Session;
|
|
use app\validate\Admin;
|
|
use app\validate\User as UserValidate;
|
|
use think\exception\ValidateException;
|
|
|
|
class AdminUserTeam extends BaseController
|
|
{
|
|
|
|
protected $middleware = [CheckAdmin::class];
|
|
|
|
/**
|
|
* 获取用户列表
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function userList()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$limit = $request['limit'] ?? 10;
|
|
|
|
$where = [];
|
|
|
|
$UserModel = new User();
|
|
|
|
# 用户id / 手机号码查询
|
|
if (isset($request['title']) && !empty($request['title'])) {
|
|
$queryUser = $UserModel
|
|
->whereOr(['phone' => $request['title'], 'id' => $request['title']])
|
|
->field('id')
|
|
->select()
|
|
->toArray();
|
|
if ($queryUser) {
|
|
foreach ($queryUser as $queryUserRow) {
|
|
$where['id'][] = $queryUserRow['id'];
|
|
}
|
|
} else {
|
|
$where['id'] = 0;
|
|
}
|
|
}
|
|
|
|
# 查询用户列表
|
|
$field = 'id,aid,phone,avatar,balance,withdrawal_balance,status';
|
|
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
|
|
|
|
$list = $userRes->items();
|
|
$total = $userRes->total();
|
|
|
|
foreach ($list as &$item) {
|
|
|
|
$item['avatar'] = get_image_url($item['avatar']);
|
|
$item['status'] = $item['status'] == 1 ? '正常' : '停用';
|
|
}
|
|
|
|
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]);
|
|
}
|
|
|
|
/**
|
|
* 新增用户
|
|
* @return array
|
|
*/
|
|
public function addUser()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
|
|
validate(UserValidate::class)->scene('register')->check($param);
|
|
|
|
$User = new User();
|
|
$param['aid'] = 0;
|
|
$result = $User->register($param);
|
|
|
|
if (!$result) throw new ValidateException('代理已存在');
|
|
|
|
return $this->renderSuccess('添加成功');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 编辑用户信息
|
|
*/
|
|
public function editUser()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
|
|
validate(UserValidate::class)->scene('editUser')->check($param);
|
|
|
|
$UserModel = new User();
|
|
$result = $UserModel->retrieve($param);
|
|
|
|
if (!$result['status']) throw new ValidateException($result['msg']);
|
|
|
|
$event = new UserPasswordChange($param['user_id']);
|
|
Event::trigger(UserPasswordChange::class,$event);
|
|
|
|
return $this->renderSuccess($result['msg']);
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除用户
|
|
* @return array
|
|
*/
|
|
public function deleteUser()
|
|
{
|
|
$param = Request::param();
|
|
|
|
try {
|
|
|
|
validate(UserValidate::class)->scene('delUser')->check($param);
|
|
|
|
$result = User::destroy($param['user_id']);
|
|
|
|
if (!$result) throw new ValidateException('删除失败');
|
|
|
|
return $this->renderSuccess('已删除');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户上分
|
|
* @return array
|
|
*/
|
|
public function upScores()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
$adminUser = $this->request->userInfo;
|
|
|
|
validate(Admin::class)->scene('scoresUser')->check($request);
|
|
|
|
$user_id = $request['user_id']; # 用户id
|
|
$quota = $request['quota']; # 额度
|
|
|
|
# 开启事务
|
|
$connection->startTrans();
|
|
|
|
# 用户上分
|
|
$userBalance = User::incrBalance($user_id,$quota);
|
|
|
|
# 管理用户上分记录
|
|
AdminUpScoresRecords::createRecords($user_id,$adminUser['id'],$quota,$userBalance);
|
|
|
|
# 用户充值记录-上分
|
|
RechargeRecords::createRecords($user_id,$quota,$userBalance,1,2);
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('上分成功');
|
|
} catch (ValidateException $e) {
|
|
return $this->renderError($e->getMessage());
|
|
} catch (\Exception $exception) {
|
|
$connection->rollback();
|
|
return $this->renderError($exception->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户下分
|
|
* @return array|void
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function downScores()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
$adminUser = $this->request->userInfo;
|
|
validate(Admin::class)->scene('scoresUser')->check($request);
|
|
|
|
$user_id = $request['user_id']; # 用户id
|
|
$quota = $request['quota']; # 额度
|
|
|
|
# 检测用户余额是否足够
|
|
$inspectRes = User::inspectUserBalance($user_id,$quota);
|
|
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
|
|
|
|
# 开启事务
|
|
$connection->startTrans();
|
|
|
|
# 用户下分
|
|
$userBalance = User::decrWithdrawalBalance($user_id,$quota);
|
|
|
|
# 管理给用户下分记录
|
|
AdminDownScoresRecords::createRecords($user_id,$adminUser['id'],$quota,$userBalance);
|
|
|
|
# 用户提现记录-下分
|
|
WithdrawalRecords::createRecords($user_id,$quota,$userBalance,1,2);
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('下分成功');
|
|
} catch (ValidateException $e) {
|
|
return $this->renderError($e->getMessage());
|
|
} catch (\Exception $exception) {
|
|
$connection->rollback();
|
|
return $this->renderError($exception->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|