WokerTask/app/api/controller/ShopCallController.php

188 lines
7.5 KiB
PHP

<?php
namespace app\api\controller;
use app\adminapi\logic\user\UserLogic;
use app\common\logic\ShopRequestLogic;
use think\exception\ValidateException;
/**
* 商城主动调用接口类
*/
class ShopCallController extends BaseApiController
{
public array $notNeedLogin = ['user_first_order_share_profit', 'user_order_share_profit'];
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);
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$inviteCode = $requestDatas['promotion_code']; // 推广码
$orderMoney= $requestDatas['order_money']; // 订单金额
$orderNo = $requestDatas['order_no']; // 订单金额
$orderId = $requestDatas['order_id']; // 订单id
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
if (empty($userSelf)) {
return $this->fail('推广人不存在');
}
// 推广人角色 - 小队/小区队长
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]);
}
return $this->success('成功');
}
/**
* 用户订单分润
* 镇合伙人
*/
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);
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$streetCode = $requestDatas['street_code']; // 镇街地区码
$orderMoney = $requestDatas['order_money']; // 订单金额 分
$orderNo = $requestDatas['order_no']; // 订单金额
$orderId = $requestDatas['order_id']; // 订单id
// 镇合伙人
$userSelf = UserLogic::getUser(['street'=>$streetCode, 'group_id'=>15]);
if (empty($userSelf)) {
return $this->fail('镇合伙人不存在');
}
$proportion = 0.01; // 比例
// 计算分润
UserLogic::userProfit($userSelf, $orderMoney, $proportion, $orderNo);
// 通知商城
$request['user'] = [
['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]);
return $this->success('成功', ['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, $proportion, 2)]);
}
}