WokerTask/app/api/controller/ShopCallController.php

258 lines
11 KiB
PHP
Raw Permalink Normal View History

2023-12-27 14:06:33 +08:00
<?php
namespace app\api\controller;
2024-01-19 14:35:51 +08:00
use app\adminapi\logic\user\UserLogic;
2024-01-20 16:34:12 +08:00
use app\common\logic\ShopRequestLogic;
2024-01-26 11:59:11 +08:00
use app\common\model\user\User;
2024-01-29 16:18:41 +08:00
use app\common\model\user\UserAccountLog;
2024-01-20 11:42:46 +08:00
use think\exception\ValidateException;
2024-01-26 11:48:29 +08:00
use think\facade\Log;
2023-12-27 14:06:33 +08:00
/**
* 商城主动调用接口类
*/
class ShopCallController extends BaseApiController
{
2024-01-29 16:18:41 +08:00
public array $notNeedLogin = ['user_first_order_share_profit', 'user_order_share_profit', 'handleRefund'];
2024-01-20 16:34:12 +08:00
public string $apiSecret = '';
2024-01-20 11:42:46 +08:00
public function initialize()
{
parent::initialize();
$this->apiSecret = config('project.shop_api_secret');
}
2024-01-19 14:35:51 +08:00
/**
2024-01-26 11:48:29 +08:00
* 用户直推首单分润
*
2024-01-19 14:35:51 +08:00
*/
public function user_first_order_share_profit()
{
2024-01-20 11:42:46 +08:00
$params = $this->request->param();
if (!isset($params['data']) || !isset($params['timestamp'])) {
return $this->fail('未传入参数');
}
$timestamp = $params['timestamp'];
$iv = substr(md5($this->apiSecret.$timestamp), 5, 16);
2024-01-20 16:34:12 +08:00
// $secretData = encrypt($params['data'], $this->apiSecret, $iv); halt($secretData);
$requestDatas = decrypt($params['data'], $this->apiSecret, $iv);
2024-01-26 11:48:29 +08:00
Log::info('首单分润请求参数:'.json_encode($requestDatas));
2024-01-20 11:42:46 +08:00
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
2024-01-23 16:41:47 +08:00
$inviteCode = $requestDatas['data']['promotion_code']; // 推广码
$orderMoney= $requestDatas['data']['order_money']; // 订单金额
$orderNo = $requestDatas['data']['order_sn']; // 订单金额
$orderId = $requestDatas['data']['order_id']; // 订单id
2023-12-27 14:06:33 +08:00
2024-01-19 14:35:51 +08:00
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
2024-01-26 11:48:29 +08:00
if ($userSelf->isEmpty()) {
2024-01-19 14:35:51 +08:00
return $this->fail('推广人不存在');
}
2024-02-04 09:55:09 +08:00
$type = 0;
2024-01-21 11:23:34 +08:00
if (in_array($userSelf['group_id'] , [2, 18])) {
$type = 1;
2024-01-19 14:35:51 +08:00
}
if ($userSelf['group_id'] == 3) {
2024-01-21 11:23:34 +08:00
$type = 2;
2024-01-19 14:35:51 +08:00
}
if ($userSelf['group_id'] == 15) {
2024-01-21 11:23:34 +08:00
$type = 3;
}
2024-02-04 09:55:09 +08:00
if($type === 0) {
return $this->fail('推广人不属于分佣角色');
}
$proportion = 0.03;
// 计算分润
UserLogic::userProfit($userSelf, $orderMoney, $proportion, $orderNo);
// 首单金额记录
UserLogic::firstOrderLog($userSelf['id'], $orderMoney, $orderNo);
2024-01-21 11:23:34 +08:00
// 通知商城
$request['user'] = [
2024-01-26 12:03:08 +08:00
['type' => $type, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.03)]
2024-01-21 11:23:34 +08:00
];
$request['order_id'] = $orderId;
$timestamp = time();
$iv1 = substr(md5($this->apiSecret.$timestamp), 5, 16);
$encryptStr = encrypt($request, $this->apiSecret, $iv1);
$res = ShopRequestLogic::userProfitNotice(['data' => $encryptStr, 'timestamp' => $timestamp]);
2024-01-26 11:48:29 +08:00
Log::info('首单分润通知商城返回:'.json_encode($res));
2024-01-21 11:23:34 +08:00
return $this->success('成功');
2024-01-20 16:34:12 +08:00
2024-01-21 11:23:34 +08:00
// 推广人角色 - 小队/小区队长
// if (in_array($userSelf['group_id'] , [2, 18])) {
//
// //村/社区合伙人
// $where = ['group_id' => 3, 'village'=>$userSelf['village']];
// $villageUser = UserLogic::getUser($where);
//
// //镇/街道合伙人
// $where1 = ['group_id' => 15, 'street'=>$userSelf['street']];
// $streetUser = UserLogic::getUser($where1);
//
// // 计算分润
// UserLogic::shareProfit($userSelf, $villageUser, $streetUser, $orderMoney, $orderNo);
//
// // 首单金额记录
// UserLogic::firstOrderLog([$userSelf['id'], $villageUser['id'], $streetUser['id']], $orderMoney, $orderNo);
//
// $request['user'] = [
// ['type' => 1, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
// ['type' => 2, 'account' => $villageUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
// ['type' => 3, 'account' => $streetUser['account'], 'user_profit' => bcmul($orderMoney, 0.01, 2)],
// ];
// $request['order_id'] = $orderId;
// $timestamp = time();
// $iv1 = substr(md5($this->apiSecret.$timestamp), 5, 16);
// $encryptStr = encrypt($request, $this->apiSecret, $iv1);
//
// // 通知商城
// $res = ShopRequestLogic::userProfitNotice(['data' => $encryptStr, 'timestamp' => $timestamp]);
// }
//
// // 村/社区合伙人
// if ($userSelf['group_id'] == 3) {
// // 查下级 小队
// $where = ['group_id' => [2, 18], 'village'=>$userSelf['village']];
// $bridgeUser = UserLogic::getUser($where);
//
// //镇/街道合伙人
// $where = ['group_id' => 15, 'village'=>$userSelf['village']];
// $streetUser = UserLogic::getUser($where);
//
// // 计算分润
// UserLogic::shareProfit($bridgeUser, $userSelf, $streetUser, $orderMoney);
//
// // 首单金额记录
// UserLogic::firstOrderLog([$userSelf['id'], $bridgeUser['id'], $streetUser['id']], $orderMoney, $orderNo);
//
// $request['user'] = [
// ['type' => 1, 'account' => $bridgeUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
// ['type' => 2, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
// ['type' => 3, 'account' => $streetUser['account'], 'user_profit' => bcmul($orderMoney, 0.01, 2)],
// ];
// $request['order_id'] = $orderId;
// $timestamp = time();
// $iv1 = substr(md5($this->apiSecret.$timestamp), 5, 16);
// $encryptStr = encrypt($request, $this->apiSecret, $iv1);
//
// // 通知商城
// $res = ShopRequestLogic::userProfitNotice(['data' => $encryptStr, 'timestamp' => $timestamp]);
// }
//
// // 镇/街道合伙人
// if ($userSelf['group_id'] == 15) {
// // 查下级村
// $where = ['group_id' => 3, 'village'=>$userSelf['village']];
// $villageUser = UserLogic::getUser($where);
//
// //和小队
// $where = ['group_id' => [2, 18], 'village'=>$userSelf['village']];
// $bridgeUser = UserLogic::getUser($where);
//
// // 计算分润
// UserLogic::shareProfit($bridgeUser, $villageUser, $userSelf, $orderMoney);
//
// // 首单金额记录
// UserLogic::firstOrderLog([$userSelf['id'], $villageUser['id'], $userSelf['id']], $orderMoney, $orderNo);
//
// $request['user'] = [
// ['type' => 1, 'account' => $bridgeUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
// ['type' => 2, 'account' => $villageUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
// ['type' => 3, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.01, 2)],
// ];
// $request['order_id'] = $orderId;
// $timestamp = time();
// $iv1 = substr(md5($this->apiSecret.$timestamp), 5, 16);
// $encryptStr = encrypt($request, $this->apiSecret, $iv1);
//
// // 通知商城
// $res = ShopRequestLogic::userProfitNotice(['data' => $encryptStr, 'timestamp' => $timestamp]);
// }
2024-01-20 16:34:12 +08:00
2024-01-19 14:35:51 +08:00
}
/**
* 用户订单分润
* 镇合伙人
*/
public function user_order_share_profit()
{
2024-01-20 11:42:46 +08:00
$params = $this->request->param();
$timestamp = $params['timestamp'];
$iv = substr(md5($this->apiSecret.$timestamp), 5, 16);
2024-01-20 16:34:12 +08:00
$requestDatas = decrypt($params['data'], $this->apiSecret, $iv);
2024-01-26 11:48:29 +08:00
Log::info('镇合伙人,配送员分润请求参数:'.json_encode($requestDatas));
2024-01-20 11:42:46 +08:00
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
2024-01-26 11:59:11 +08:00
$streetCode = $requestDatas['data']['street_code']??''; // 镇街地区码
2024-01-26 11:54:21 +08:00
$orderMoney = $requestDatas['data']['order_money']; // 订单金额 分
$orderNo = $requestDatas['data']['order_sn']; // 订单金额
$orderId = $requestDatas['data']['order_id']; // 订单id
2024-01-26 11:59:11 +08:00
$courierPhone = $requestDatas['data']['courier_phone']??''; // 配送员账号
2024-01-19 14:35:51 +08:00
2024-01-20 16:34:12 +08:00
// 镇合伙人
2024-01-26 11:59:11 +08:00
$userSelf = new User();
if (!empty($streetCode)) {
$userSelf = UserLogic::getUser(['street'=>$streetCode, 'group_id'=>15]);
}
2024-01-19 14:35:51 +08:00
2024-01-21 16:16:30 +08:00
// 配送员
2024-01-26 11:59:11 +08:00
$courier = new User();
if (!empty($courierPhone)) {
$courier = UserLogic::getUser(['account'=>$courierPhone]);
}
2024-01-19 14:35:51 +08:00
$proportion = 0.01; // 比例
2024-01-21 17:50:49 +08:00
$noticeUser = [];
2024-01-20 16:34:12 +08:00
// 计算分润
2024-01-26 11:59:11 +08:00
if (!$userSelf->isEmpty()) {
2024-01-21 17:50:49 +08:00
UserLogic::userProfit($userSelf, $orderMoney, $proportion, $orderNo);
2024-01-26 12:02:44 +08:00
$noticeUser[] = ['type' => 3, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.01)];
2024-01-21 17:50:49 +08:00
}
2024-01-26 11:59:11 +08:00
if (!$courier->isEmpty()) {
2024-01-21 17:50:49 +08:00
UserLogic::userProfit($courier, $orderMoney, $proportion, $orderNo);
2024-01-26 12:02:44 +08:00
$noticeUser[] = ['type' => 4, 'account' => $courier['account'], 'user_profit' => bcmul($orderMoney, 0.01)];
2024-01-21 17:50:49 +08:00
}
2024-01-19 14:35:51 +08:00
2024-01-21 17:50:49 +08:00
$request['user'] = $noticeUser;
2024-01-20 16:34:12 +08:00
// 通知商城
$request['order_id'] = $orderId;
$timestamp = time();
$iv1 = substr(md5($this->apiSecret.$timestamp), 5, 16);
$encryptStr = encrypt($request, $this->apiSecret, $iv1);
$res = ShopRequestLogic::userProfitNotice(['data' => $encryptStr, 'timestamp' => $timestamp]);
2024-01-26 11:48:29 +08:00
Log::info('镇合伙人,配送员分润通知商城返回:'.json_encode($res));
2024-01-20 11:42:46 +08:00
return $this->success('成功', ['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, $proportion, 2)]);
2024-01-19 14:35:51 +08:00
}
2024-01-29 16:18:41 +08:00
// 退款扣除佣金
public function handleRefund()
{
$params = $this->request->param();
$timestamp = $params['timestamp'];
$iv = substr(md5($this->apiSecret.$timestamp), 5, 16);
// $secretData = encrypt($params, $this->apiSecret, $iv); halt($secretData);
// halt($secretData);
$requestDatas = decrypt($params['data'], $this->apiSecret, $iv);
2024-01-29 16:22:35 +08:00
Log::info('订单退款,扣除分润请求参数:'.json_encode($requestDatas));
2024-01-29 16:18:41 +08:00
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$orderNo = $requestDatas['data']['order_sn']; // 订单金额
UserLogic::handleProfitRefund($orderNo);
return $this->success('成功');
}
2023-12-27 14:06:33 +08:00
}