刮刮后端接口
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.

470 lines
15 KiB

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\middleware\CheckAgent;
use app\model\AgentDownScoresRecords;
use app\model\AgentInfo;
use app\model\AgentUpScoresRecords;
use app\model\RebateRecords;
use app\model\RechargeRecords;
use app\model\Setting as SettingModel;
use app\model\User;
use app\model\User as UserModel;
use app\model\CustomerService as CustomerServiceModel;
use app\model\WithdrawalRecords;
use app\validate\Agent;
use app\validate\User as UserValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
class AgentTeam extends BaseController
{
protected $middleware = [CheckAgent::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;
$identity = $request['identity'] ?? 3;
# 代理id查询 启用用户
$agentData = $this->request->userInfo;
$where = ['aid' => $agentData['id'], 'status' => 1, 'identity' => $identity];
$UserModel = new UserModel();
# 用户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,identity';
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
$list = $userRes->items();
$total = $userRes->total();
foreach ($list as &$item) {
#$item['phone'] = format_phone_number($item['phone']);
$item['avatar'] = get_image_url($item['avatar']);
$item['identity_str'] = $UserModel->identityArr[$item['identity']];
$rebate_ratio = AgentInfo::where('aid',$item['id'])->value('rebate_ratio');
$item['rebate_ratio'] = $rebate_ratio ? round($rebate_ratio*100,2) : '0';
unset($item['identity']);
}
return $this->renderSuccess('数据返回成功',['list' => $list, 'total' => $total]);
}
/**
* 充值记录
* @return array
* @throws \think\db\exception\DbException
*/
public function rechargeRecords()
{
$userData = $this->request->userInfo;
$data = Request::param();
$limit = $data['limit'] ?: 10;
$RechargeRecords = new RechargeRecords();
$list = Db::name('recharge_records')
->where('user_id',$userData['id'])
->field('recharge_amount,residue_amount,trade_type,trade_id,recharge_time')
->order('id' ,'desc')
->paginate($limit);
$listArr = $list->items();
foreach ($listArr as &$item) {
give_symbol($item['recharge_amount']);
$str = $item['trade_type'] == 4 ? 'ID:' . $item['trade_id'] . ' ' : '';
$item['trade_type'] = $str . $RechargeRecords->tradeType[$item['trade_type']];
$item['recharge_time'] = date("m月d日 H:i",strtotime($item['recharge_time']));
unset($item['trade_id']);
}
return $this->renderSuccess('数据获取成功',[
'list' => $listArr,
'total' => $list->total()
]);
}
/**
* 上分
* @return array
*/
public function upScores()
{
$request = Request::param();
$connection = Db::connect();
try {
$agentUser = $this->request->userInfo;
validate(Agent::class)->scene('scores')->check($request);
$validateUserInfo = validate(Agent::class)->validateUserInfo($agentUser['id'],$request['user_id']);
if ($validateUserInfo !== true) return $this->renderError($validateUserInfo);
$user_id = $request['user_id']; # 用户id
$quota = $request['quota']; # 额度
# 检测代理余额是否足够
$inspectRes = User::inspectBalance($agentUser['id'],$quota);
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
# 开启事务
$connection->startTrans();
# 扣减代理余额
$agentBalance = User::decrBalance($agentUser['id'],$quota);
# 用户上分记录
AgentUpScoresRecords::createRecords($agentUser['id'],$user_id,$quota,$agentBalance);
# 用户上分
$userBalance = User::incrBalance($user_id,$quota);
# 用户充值记录-上分
RechargeRecords::createRecords($user_id,$quota,$userBalance,1,1,$agentUser['id']);
$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 {
$agentUser = $this->request->userInfo;
validate(Agent::class)->scene('scores')->check($request);
$validateUserInfo = validate(Agent::class)->validateUserInfo($agentUser['id'],$request['user_id']);
if ($validateUserInfo !== true) return $this->renderError($validateUserInfo);
$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();
# 增加代理可提余额
$agentBalance = User::incrWithdrawalBalance($agentUser['id'],$quota);
# 用户下分记录
AgentDownScoresRecords::createRecords($agentUser['id'],$user_id,$quota,$agentBalance);
# 用户下分
$userBalance = User::decrWithdrawalBalance($user_id,$quota);
# 用户提现记录-下分
WithdrawalRecords::createRecords($user_id,$quota,$userBalance,1,1,$agentUser['id']);
$connection->commit();
return $this->renderSuccess('下分成功');
} catch (ValidateException $e) {
return $this->renderError($e->getMessage());
} catch (\Exception $exception) {
$connection->rollback();
return $this->renderError($exception->getMessage());
}
}
/**
* 返回代理邀请码
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAgentInviteCode()
{
$agentUser = $this->request->userInfo;
$userInfo = AgentInfo::where('aid',$agentUser['id'])->find();
return $this->renderSuccess('数据返回成功',['code' => $userInfo->invite_code]);
}
/**
* 代理登录个人信息
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getAgentUserInfo()
{
$agentUser = $this->request->userInfo;
$user = User::find($agentUser['id']);
return $this->renderSuccess('数据返回成功',[
'aid' => $user->id,
'avatar' => get_image_url($user->avatar),
'balance' => $user->balance,
'withdrawal_balance' => $user->withdrawal_balance,
'recharge' => SettingModel::settingLoad('agent_recharge_set') ?: 0
]);
}
/**
* 找回密码
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function modifyPassword()
{
$data = Request::param();
try {
// 验证代理输入
validate(Agent::class)->scene('modifyPassword')->check($data);
$userModel = new User();
$user = $userModel->retrieve(['password' => $data['password'],'user_id' => $this->request->userInfo['id']]);
if ($user['status']) {
return $this->renderSuccess('修改成功');
} else {
return $this->renderError($user['msg']);
}
} catch (ValidateException $exception) {
return $this->renderError($exception->getMessage());
}
}
/**
* 上分记录
* @return array
* @throws \think\db\exception\DbException
*/
public function upScoresList()
{
$agentData = $this->request->userInfo;
$data = Request::param();
$limit = $data['limit'] ?: 10;
$list = Db::name('agent_up_scores_records')
->where('aid',$agentData['id'])
->field('user_id,balance,residue_amount,create_time')
->order('id' ,'desc')
->paginate($limit);
$listArr = $list->items();
foreach ($listArr as &$item) {
$item['relation_id'] = $item['user_id'];
give_symbol($item['balance'],'-');
$item['create_time'] = get_datetime($item['create_time'],2);
unset($item['user_id']);
}
return $this->renderSuccess('数据获取成功',[
'list' => $listArr,
'total' => $list->total()
]);
}
/**
* 下分记录
* @return array
* @throws \think\db\exception\DbException
*/
public function downScoresList()
{
$agentData = $this->request->userInfo;
$data = Request::param();
$limit = $data['limit'] ?: 10;
$list = Db::name('agent_down_scores_records')
->where('aid',$agentData['id'])
->field('user_id,withdrawal_amount,withdrawal_balance,create_time')
->order('id' ,'desc')
->paginate($limit);
$listArr = $list->items();
foreach ($listArr as &$item) {
$item['relation_id'] = $item['user_id'];
give_symbol($item['withdrawal_amount']);
$item['create_time'] = get_datetime($item['create_time'],2);
unset($item['user_id']);
}
return $this->renderSuccess('数据获取成功',[
'list' => $listArr,
'total' => $list->total()
]);
}
/**
* 代理升级用户为代理
*/
public function upGradationAgent()
{
$param = Request::param();
$connection = Db::connect();
try {
$agentData = $this->request->userInfo;
validate(Agent::class)->scene('promotion')->check($param);
$user_id = $param['user_id'];
$rebate_ratio = $param['rebate_ratio'] ?? 0;
$validateUserInfo = validate(Agent::class)->validateUserInfo($agentData['id'],$user_id);
if ($validateUserInfo !== true) throw new ValidateException($validateUserInfo);
$validateUserIdentity = validate(UserValidate::class)->validateUserIdentity($user_id);
if ($validateUserIdentity !== true) throw new ValidateException($validateUserIdentity);
$validateRebateRatio = validate(Agent::class)->validateRebateRatio($agentData['id'],$rebate_ratio);
if ($validateRebateRatio !== true) throw new ValidateException($validateRebateRatio);
# 开启事务
$connection->startTrans();
# 创建代理信息
AgentInfo::createInfo($user_id,$rebate_ratio);
# 升级为代理
User::changeIdentity($user_id,2,$agentData['id']);
$connection->commit();
return $this->renderSuccess('成功');
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
} catch (\Exception $e) {
$connection->rollback();
return $this->renderError($e->getMessage());
}
}
/**
* 修改代理占比
*/
public function settingRebateRatio()
{
$param = Request::param();
try {
$agentData = $this->request->userInfo;
validate()->rule([
'user_id|用户id' => 'require|number',
'rebate_ratio|返点占比' => 'require|float|between:0.01,100'
])->check($param);
$user_id = $param['user_id'];
$rebate_ratio = $param['rebate_ratio'] ?? 0;
$validateUserInfo = validate(Agent::class)->validateUserInfo($agentData['id'],$user_id);
if ($validateUserInfo !== true) throw new ValidateException($validateUserInfo);
$validateRebateRatio = validate(Agent::class)->validateRebateRatio($agentData['id'],$rebate_ratio);
if ($validateRebateRatio !== true) throw new ValidateException($validateRebateRatio);
AgentInfo::updateRebateRatio($user_id,$rebate_ratio);
return $this->renderSuccess('成功');
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
}
}
/**
* 返回用户联系客服信息
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCustomerServiceList()
{
$where = [];
$CustomerServiceModel = new CustomerServiceModel();
# 查询用户列表
$field = 'id,name,wx_number';
$list = $CustomerServiceModel->field($field)->where($where)->order('id desc')->select()->toArray();
return $this->renderSuccess('数据返回成功', ['list' => $list]);
}
/**
* 退出登陆
*/
public function LogOut()
{
Session::delete('login_user_data');
return $this->renderSuccess('退出登陆成功');
}
/**
* 代理返点记录
* @return array
* @throws \think\db\exception\DbException
*/
public function getRebateRecordsList()
{
$param = Request::param();
$limit = $param['limit'] ?: 10;
$agentData = $this->request->userInfo;
$data = RebateRecords::getList(['aid' => $agentData['id']],$limit);
return $this->renderSuccess('数据获取成功',[
'list' => $data['list'],
'total' => $data['total']
]);
}
}