|
@@ -5,8 +5,12 @@ namespace app\api\controller\commission;
|
|
|
use think\Db;
|
|
|
use app\common\model\User as UserModel;
|
|
|
use app\common\model\commission\Agent as AgentModel;
|
|
|
+use app\common\model\commission\Reward as RewardModel;
|
|
|
+use app\common\model\commission\Apply as ApplyModel;
|
|
|
use app\common\model\Goods as GoodsModel;
|
|
|
use app\common\Service\Wallet;
|
|
|
+use app\common\Enum\AgentType;
|
|
|
+use app\common\library\BcMath;
|
|
|
|
|
|
|
|
|
class Agent extends Commission
|
|
@@ -51,71 +55,99 @@ class Agent extends Commission
|
|
|
break;
|
|
|
}
|
|
|
$data = $this->service->agent;
|
|
|
-
|
|
|
- $this->success('分销商信息', $data);
|
|
|
- }
|
|
|
-
|
|
|
- // 分销商完善个人信息
|
|
|
- public function form()
|
|
|
- {
|
|
|
- if (!$this->service->config->isAgentApplyForm()) {
|
|
|
- $this->error('未开启分销商申请');
|
|
|
- }
|
|
|
-
|
|
|
- $agentForm = $this->service->config->getAgentForm();
|
|
|
- $protocol = $this->service->config->getApplyProtocol();
|
|
|
-
|
|
|
- $config = [
|
|
|
- 'form' => $agentForm['content'],
|
|
|
- 'status' => $this->service->getAgentStatus(true),
|
|
|
- 'background' => $agentForm['background_image'],
|
|
|
- 'protocol' => $protocol
|
|
|
- ];
|
|
|
-
|
|
|
- $this->success("", $config);
|
|
|
- }
|
|
|
-
|
|
|
- // 申请分销商/完善资料
|
|
|
- public function apply()
|
|
|
- {
|
|
|
- if (!$this->service->config->isAgentApplyForm()) {
|
|
|
- $this->error('未开启分销商申请');
|
|
|
- }
|
|
|
- $status = $this->service->getAgentStatus(true);
|
|
|
-
|
|
|
- if (!in_array($status, [AgentModel::AGENT_STATUS_NEEDINFO, AgentModel::AGENT_STATUS_REJECT, AgentModel::AGENT_STATUS_NORMAL])) {
|
|
|
- $this->error('您暂时不能申请');
|
|
|
- }
|
|
|
- Db::transaction(function () use ($status) {
|
|
|
- $data = $this->request->param('data/a');
|
|
|
- // 过滤无效表单字段数据
|
|
|
- $config = $this->service->config->getAgentForm();
|
|
|
- $form = (array)$config['content'];
|
|
|
- $data = array_column($data, 'value', 'name');
|
|
|
-
|
|
|
- foreach ($form as &$item) {
|
|
|
- if (!empty($data[$item['name']])) {
|
|
|
- $item['value'] = $data[$item['name']];
|
|
|
- } else {
|
|
|
- $this->error($item['name'] . '不能为空');
|
|
|
+
|
|
|
+ // 增强代理商数据信息
|
|
|
+ if ($data) {
|
|
|
+ $data = $data->toArray();
|
|
|
+
|
|
|
+ // 添加代理商类型描述(使用枚举类)
|
|
|
+ $data['agent_type_text'] = AgentType::getTypeText($data['agent_type']);
|
|
|
+
|
|
|
+ // 如果是区域代理商,添加管辖区域信息
|
|
|
+ if (AgentType::isRegionalAgent($data['agent_type'])) {
|
|
|
+ $areaInfo = [];
|
|
|
+
|
|
|
+ // 获取省份信息
|
|
|
+ if (!empty($data['manage_province_id'])) {
|
|
|
+ $provinceInfo = \app\common\model\Area::where('id', $data['manage_province_id'])->find();
|
|
|
+ if ($provinceInfo) {
|
|
|
+ $areaInfo['province_name'] = $provinceInfo['name'];
|
|
|
+ $areaInfo['province_id'] = $data['manage_province_id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取城市信息
|
|
|
+ if (!empty($data['manage_city_id'])) {
|
|
|
+ $cityInfo = \app\common\model\Area::where('id', $data['manage_city_id'])->find();
|
|
|
+ if ($cityInfo) {
|
|
|
+ $areaInfo['city_name'] = $cityInfo['name'];
|
|
|
+ $areaInfo['city_id'] = $data['manage_city_id'];
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ // 获取区域信息
|
|
|
+ if (!empty($data['manage_district_id'])) {
|
|
|
+ $districtInfo = \app\common\model\Area::where('id', $data['manage_district_id'])->find();
|
|
|
+ if ($districtInfo) {
|
|
|
+ $areaInfo['district_name'] = $districtInfo['name'];
|
|
|
+ $areaInfo['district_id'] = $data['manage_district_id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $data['manage_area'] = $areaInfo;
|
|
|
+
|
|
|
+ // 生成管辖区域描述文本
|
|
|
+ $areaText = [];
|
|
|
+ if (isset($areaInfo['province_name'])) $areaText[] = $areaInfo['province_name'];
|
|
|
+ if (isset($areaInfo['city_name'])) $areaText[] = $areaInfo['city_name'];
|
|
|
+ if (isset($areaInfo['district_name'])) $areaText[] = $areaInfo['district_name'];
|
|
|
+ $data['manage_area_text'] = implode('-', $areaText);
|
|
|
+ } else {
|
|
|
+ $data['manage_area'] = null;
|
|
|
+ $data['manage_area_text'] = '-';
|
|
|
}
|
|
|
- if ($status === AgentModel::AGENT_STATUS_NEEDINFO) {
|
|
|
- $this->service->createNewAgent('');
|
|
|
+
|
|
|
+ // 获取用户基本信息和佣金数据
|
|
|
+ $user = $this->service->user;
|
|
|
+ if ($user) {
|
|
|
+ $data['user_info'] = [
|
|
|
+ 'nickname' => $user->nickname,
|
|
|
+ 'avatar' => cdnurl($user->avatar), // 转换为完整CDN URL
|
|
|
+ 'username' => $user->username,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取代理商申请信息
|
|
|
+ $applyInfo = ApplyModel::where('user_id', $this->service->user->id)
|
|
|
+ ->with(['identity'])
|
|
|
+ ->order('id desc')
|
|
|
+ ->find();
|
|
|
+
|
|
|
+ if ($applyInfo) {
|
|
|
+ $data['apply_info'] = [
|
|
|
+ 'id' => $applyInfo->id,
|
|
|
+ 'apply_type' => $applyInfo->apply_type,
|
|
|
+ 'apply_type_text' => $applyInfo->apply_type_text,
|
|
|
+ 'status' => $applyInfo->status,
|
|
|
+ 'status_text' => $applyInfo->status_text,
|
|
|
+ 'identity_name' => $applyInfo->identity->name ?? '',
|
|
|
+ 'area_text' => implode('-', array_filter([
|
|
|
+ $applyInfo->province_name,
|
|
|
+ $applyInfo->city_name,
|
|
|
+ $applyInfo->district_name
|
|
|
+ ])),
|
|
|
+ 'createtime' => $applyInfo->createtime ? date('Y-m-d H:i:s', $applyInfo->createtime) : '',
|
|
|
+ 'audit_time' => $applyInfo->audit_time ? date('Y-m-d H:i:s', $applyInfo->audit_time) : '',
|
|
|
+ ];
|
|
|
} else {
|
|
|
- // 重置为审核中
|
|
|
- if ($status === AgentModel::AGENT_STATUS_REJECT) {
|
|
|
- $this->service->agent->status = AgentModel::AGENT_STATUS_PENDING;
|
|
|
- }
|
|
|
- // 保存分销商状态更新
|
|
|
- $this->service->agent->save();
|
|
|
+ $data['apply_info'] = null;
|
|
|
}
|
|
|
- });
|
|
|
-
|
|
|
+ }
|
|
|
|
|
|
- $this->success('提交成功');
|
|
|
+ $this->success('分销商信息', $data);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// 我的团队
|
|
|
public function team()
|
|
|
{
|
|
@@ -131,18 +163,45 @@ class Agent extends Commission
|
|
|
$this->success("", $data);
|
|
|
}
|
|
|
|
|
|
- // 佣金转余额
|
|
|
+ // 佣金转余额/提现
|
|
|
public function transfer()
|
|
|
{
|
|
|
$amount = $this->request->param('amount');
|
|
|
if ($amount <= 0) {
|
|
|
$this->error('请输入正确的金额');
|
|
|
}
|
|
|
- Db::transaction(function () use ($amount) {
|
|
|
+
|
|
|
+ $agent = $this->service->agent;
|
|
|
+ if (!$agent) {
|
|
|
+ $this->error('您还不是分销商');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用BcMath工具类检查可提现余额(累计收益 - 已提现金额)
|
|
|
+ $totalIncome = $agent->total_income ?? '0.00';
|
|
|
+ $withdrawnAmount = $agent->withdrawn_amount ?? '0.00';
|
|
|
+ $requestAmount = BcMath::format($amount);
|
|
|
+
|
|
|
+ $availableBalance = BcMath::sub($totalIncome, $withdrawnAmount);
|
|
|
+
|
|
|
+ // 使用BcMath比较函数检查余额是否足够
|
|
|
+ if (BcMath::comp($requestAmount, $availableBalance) > 0) {
|
|
|
+ $this->error('提现金额超过可用余额,可用余额:' . $availableBalance . '元');
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::transaction(function () use ($amount, $agent) {
|
|
|
$user = auth_user();
|
|
|
- Wallet::change($user, 'commission', -$amount, 'transfer_to_money');
|
|
|
- Wallet::change($user, 'money', $amount, 'transfer_by_commission');
|
|
|
+
|
|
|
+ // 转账到用户余额
|
|
|
+ Wallet::change($user, 'money', $amount, 'commission_withdraw');
|
|
|
+
|
|
|
+ // 更新代理商的已提现金额
|
|
|
+ AgentModel::where('user_id', $user->id)->setInc('withdrawn_amount', $amount);
|
|
|
});
|
|
|
- $this->success('');
|
|
|
+
|
|
|
+ $this->success('提现成功');
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|