WokerTask/app/api/controller/ShopCallController.php

258 lines
11 KiB
PHP

<?php
namespace app\api\controller;
use app\adminapi\logic\user\UserLogic;
use app\common\logic\ShopRequestLogic;
use app\common\model\user\User;
use app\common\model\user\UserAccountLog;
use think\exception\ValidateException;
use think\facade\Log;
/**
* 商城主动调用接口类
*/
class ShopCallController extends BaseApiController
{
public array $notNeedLogin = ['user_first_order_share_profit', 'user_order_share_profit', 'handleRefund'];
public string $apiSecret = '';
public function initialize()
{
parent::initialize();
$this->apiSecret = config('project.shop_api_secret');
}
/**
* 用户直推首单分润
*
*/
public function user_first_order_share_profit()
{
$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);
// $secretData = encrypt($params['data'], $this->apiSecret, $iv); halt($secretData);
$requestDatas = decrypt($params['data'], $this->apiSecret, $iv);
Log::info('首单分润请求参数:'.json_encode($requestDatas));
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$inviteCode = $requestDatas['data']['promotion_code']; // 推广码
$orderMoney= $requestDatas['data']['order_money']; // 订单金额
$orderNo = $requestDatas['data']['order_sn']; // 订单金额
$orderId = $requestDatas['data']['order_id']; // 订单id
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
if ($userSelf->isEmpty()) {
return $this->fail('推广人不存在');
}
$type = 0;
if (in_array($userSelf['group_id'] , [2, 18])) {
$type = 1;
}
if ($userSelf['group_id'] == 3) {
$type = 2;
}
if ($userSelf['group_id'] == 15) {
$type = 3;
}
if($type === 0) {
return $this->fail('推广人不属于分佣角色');
}
$proportion = 0.03;
// 计算分润
UserLogic::userProfit($userSelf, $orderMoney, $proportion, $orderNo);
// 首单金额记录
UserLogic::firstOrderLog($userSelf['id'], $orderMoney, $orderNo);
// 通知商城
$request['user'] = [
['type' => $type, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.03)]
];
$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]);
Log::info('首单分润通知商城返回:'.json_encode($res));
return $this->success('成功');
// 推广人角色 - 小队/小区队长
// 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]);
// }
}
/**
* 用户订单分润
* 镇合伙人
*/
public function user_order_share_profit()
{
$params = $this->request->param();
$timestamp = $params['timestamp'];
$iv = substr(md5($this->apiSecret.$timestamp), 5, 16);
$requestDatas = decrypt($params['data'], $this->apiSecret, $iv);
Log::info('镇合伙人,配送员分润请求参数:'.json_encode($requestDatas));
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$streetCode = $requestDatas['data']['street_code']??''; // 镇街地区码
$orderMoney = $requestDatas['data']['order_money']; // 订单金额 分
$orderNo = $requestDatas['data']['order_sn']; // 订单金额
$orderId = $requestDatas['data']['order_id']; // 订单id
$courierPhone = $requestDatas['data']['courier_phone']??''; // 配送员账号
// 镇合伙人
$userSelf = new User();
if (!empty($streetCode)) {
$userSelf = UserLogic::getUser(['street'=>$streetCode, 'group_id'=>15]);
}
// 配送员
$courier = new User();
if (!empty($courierPhone)) {
$courier = UserLogic::getUser(['account'=>$courierPhone]);
}
$proportion = 0.01; // 比例
$noticeUser = [];
// 计算分润
if (!$userSelf->isEmpty()) {
UserLogic::userProfit($userSelf, $orderMoney, $proportion, $orderNo);
$noticeUser[] = ['type' => 3, 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.01)];
}
if (!$courier->isEmpty()) {
UserLogic::userProfit($courier, $orderMoney, $proportion, $orderNo);
$noticeUser[] = ['type' => 4, 'account' => $courier['account'], 'user_profit' => bcmul($orderMoney, 0.01)];
}
$request['user'] = $noticeUser;
// 通知商城
$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]);
Log::info('镇合伙人,配送员分润通知商城返回:'.json_encode($res));
return $this->success('成功', ['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, $proportion, 2)]);
}
// 退款扣除佣金
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);
Log::info('订单退款,扣除分润请求参数:'.json_encode($requestDatas));
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$orderNo = $requestDatas['data']['order_sn']; // 订单金额
UserLogic::handleProfitRefund($orderNo);
return $this->success('成功');
}
}