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

371 lines
11 KiB

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\middleware\CheckAgent;
use app\model\AgentDownScoresRecords;
use app\model\AgentUpScoresRecords;
use app\model\AgentUser;
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 think\exception\ValidateException;
use think\facade\Cache;
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;
# 代理id查询 启用用户
$agentData = $this->request->userInfo;
$where = ['aid' => $agentData['id'], 'status' => 1];
$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';
$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']);
}
return $this->renderSuccess('数据返回成功',['list' => $list, 'total' => $total]);
}
/**
* 充值记录
* @return array
* @throws \think\db\exception\DbException
*/
public function rechargeRecords()
{
$agentData = $this->request->userInfo;
$data = Request::param();
$limit = $data['limit'] ?: 10;
$list = Db::name('agent_recharge_records')
->where('aid',$agentData['id'])
->field('recharge_amount,residue_amount,trade_type,recharge_time')
->order('id' ,'desc')
->paginate($limit);
$listArr = $list->items();
$trade_type = [1 => '上分', 3 => '支付宝'];
foreach ($listArr as &$item) {
give_symbol($item['recharge_amount']);
$item['trade_type'] = $trade_type[$item['trade_type']];
$item['recharge_time'] = date("m月d日 H:i",strtotime($item['recharge_time']));
}
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);
$user_id = $request['user_id']; # 用户id
$quota = $request['quota']; # 额度
# 检测代理余额是否足够
$inspectRes = AgentUser::inspectBalance($agentUser['id'],$quota);
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
# 开启事务
$connection->startTrans();
# 扣减代理余额
$agentBalance = AgentUser::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);
$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);
$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 = AgentUser::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);
$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;
$user = AgentUser::find($agentUser['id']);
return $this->renderSuccess('数据返回成功',['code' => $user->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 = AgentUser::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 AgentUser();
$user = $userModel->retrieve(['password' => $data['password'],'aid' => $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'] = date("m月d日 H:i",strtotime($item['create_time']));
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'] = date("m月d日 H:i",strtotime($item['create_time']));
unset($item['user_id']);
}
return $this->renderSuccess('数据获取成功',[
'list' => $listArr,
'total' => $list->total()
]);
}
/**
* 返回用户联系客服信息
* @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_agent_user_data');
// $login_agent_user_data = Cache::store('redis')->get('login_agent_user_data');
// if ($login_agent_user_data) Cache::store('redis')->delete('login_agent_user_data');
return $this->renderSuccess('退出登陆成功');
}
}