add 用户推广分润

This commit is contained in:
chenbo 2024-01-20 11:42:46 +08:00
parent 957bdf7f7f
commit ce8ad27cda
4 changed files with 129 additions and 42 deletions

View File

@ -258,30 +258,39 @@ class UserLogic extends BaseLogic
return User::where($where)->findOrEmpty();
}
public static function shareProfit($bridgeUser, $villageUser, $streetUser, $profit)
public static function shareProfit($bridgeUser, $villageUser, $streetUser, $orderMoney, $orderNo)
{
try {
Db::startTrans();
// 小队
$proportion = 0.03; // 分润比例
self::userProfit($bridgeUser, $profit, $proportion);
if (!empty($bridgeUser)) {
// 小队
self::userProfit($bridgeUser, $orderMoney, $proportion, $orderNo);
}
if (!empty($villageUser)) {
// 村
self::userProfit($villageUser, $profit, $proportion);
self::userProfit($villageUser, $orderMoney, $proportion, $orderNo);
}
if (!empty($streetUser)) {
// 镇
$proportion = 0.01; // 分润比例
self::userProfit($streetUser, $profit, $proportion);
self::userProfit($streetUser, $orderMoney, $proportion, $orderNo);
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
public static function userProfit($user, $profit, $proportion)
public static function userProfit($user, $orderMoney, $proportion, $orderNo)
{
$money = bcmul($profit, $proportion, 2);
$arr = [$user['id'], AccountLogEnum::UM_INC_TASK, AccountLogEnum::INC, $money, $user['sn'], '平台推广' . '获得劳务费' . $money . '元', ['company_id' => $data['company_id'], 'proportion' => $proportion], $data['status']];
$money = bcdiv(bcmul($orderMoney, $proportion, 2), 100, 2);
$arr = [$user['id'], AccountLogEnum::UM_INC_TASK, AccountLogEnum::INC, $money, $orderNo, '平台推广' . '获得劳务费' . $money . '元', ['company_id' => 0, 'proportion' => $proportion], 1];
self::addUserAccountLog($arr);
}
@ -293,4 +302,11 @@ class UserLogic extends BaseLogic
User::where('id', $data[0])->update(['user_money'=>Db::raw('user_money+' . $data[3])]);
return AccountLogLogic::add($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
}
public static function firstOrderLog($userIds, $orderMoney, $orderNo)
{
foreach ($userIds as $userId) {
Db::name('user_invite_first_order_log')->insert(['user_id' => $userId, 'order_money' => $orderMoney, 'order_no' => $orderNo, 'create_time' => time()]);
}
}
}

View File

@ -3,14 +3,7 @@
namespace app\api\controller;
use app\adminapi\logic\user\UserLogic;
use app\common\logic\task\TaskLogic;
use app\common\model\Company;
use app\common\model\dict\DictData;
use app\common\model\task\Task;
use app\common\model\task_template\TaskTemplate;
use think\Exception;
use think\facade\Db;
use think\facade\Log;
use think\exception\ValidateException;
/**
* 商城主动调用接口类
@ -18,14 +11,33 @@ use think\facade\Log;
class ShopCallController extends BaseApiController
{
public array $notNeedLogin = ['user_first_order_share_profit', 'user_order_share_profit'];
public function initialize()
{
parent::initialize();
$this->apiSecret = config('project.shop_api_secret');
}
/**
* 用户首单分润
* 镇三级分润
*/
public function user_first_order_share_profit()
{
$inviteCode = $this->request->get('promotion_code'); // 推广码
$profit = $this->request->get('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);
$requestDatas = decrypt($secretData, $this->apiSecret, $iv);
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$inviteCode = $requestDatas['promotion_code']; // 推广码
$orderMoney= $requestDatas['order_money']; // 订单金额
$orderNo= $requestDatas['order_no']; // 订单金额
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
@ -47,12 +59,15 @@ class ShopCallController extends BaseApiController
$streetUser = UserLogic::getUser($where1);
// 计算分润
UserLogic::shareProfit($userSelf, $villageUser, $streetUser, $profit);
UserLogic::shareProfit($userSelf, $villageUser, $streetUser, $orderMoney, $orderNo);
// 首单金额记录
UserLogic::firstOrderLog([$userSelf['id'], $villageUser['id'], $streetUser['id']], $orderMoney, $orderNo);
return $this->success('成功', [
['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $villageUser['id'], 'mobile' => $villageUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $streetUser['id'], 'mobile' => $streetUser['account'], 'user_profit' => bcmul($profit, 0.01, 2)],
['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
['user_id' => $villageUser['id'], 'account' => $villageUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
['user_id' => $streetUser['id'], 'account' => $streetUser['account'], 'user_profit' => bcmul($orderMoney, 0.01, 2)],
]);
}
@ -67,12 +82,15 @@ class ShopCallController extends BaseApiController
$streetUser = UserLogic::getUser($where);
// 计算分润
UserLogic::shareProfit($bridgeUser, $userSelf, $streetUser, $profit);
UserLogic::shareProfit($bridgeUser, $userSelf, $streetUser, $orderMoney);
// 首单金额记录
UserLogic::firstOrderLog([$userSelf['id'], $bridgeUser['id'], $streetUser['id']], $orderMoney, $orderNo);
return $this->success('成功', [
['user_id' => $bridgeUser['id'], 'mobile' => $bridgeUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $streetUser['id'], 'mobile' => $streetUser['account'], 'user_profit' => bcmul($profit, 0.01, 2)],
['user_id' => $bridgeUser['id'], 'account' => $bridgeUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
['user_id' => $streetUser['id'], 'account' => $streetUser['account'], 'user_profit' => bcmul($orderMoney, 0.01, 2)],
]);
}
@ -87,11 +105,15 @@ class ShopCallController extends BaseApiController
$bridgeUser = UserLogic::getUser($where);
// 计算分润
UserLogic::shareProfit($bridgeUser, $villageUser, $userSelf, $profit);
UserLogic::shareProfit($bridgeUser, $villageUser, $userSelf, $orderMoney);
// 首单金额记录
UserLogic::firstOrderLog([$userSelf['id'], $villageUser['id'], $userSelf['id']], $orderMoney, $orderNo);
return $this->success('成功', [
['user_id' => $bridgeUser['id'], 'mobile' => $bridgeUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $villageUser['id'], 'mobile' => $villageUser['account'], 'user_profit' => bcmul($profit, 0.03, 2)],
['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, 0.01, 2)],
['user_id' => $bridgeUser['id'], 'account' => $bridgeUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
['user_id' => $villageUser['id'], 'account' => $villageUser['account'], 'user_profit' => bcmul($orderMoney, 0.03, 2)],
['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, 0.01, 2)],
]);
}
}
@ -102,8 +124,17 @@ class ShopCallController extends BaseApiController
*/
public function user_order_share_profit()
{
$inviteCode = $this->request->get('promotion_code'); // 推广码
$profit = $this->request->get('profit'); // 订单金额
$params = $this->request->param();
$timestamp = $params['timestamp'];
$iv = substr(md5($this->apiSecret.$timestamp), 5, 16);
$secretData = encrypt($params['data'], $this->apiSecret, $iv);
$requestDatas = decrypt($secretData, $this->apiSecret, $iv);
if (null === $requestDatas) {
return $this->fail('非法访问,解析失败');
}
$inviteCode = $requestDatas['promotion_code']; // 推广码
$orderMoney = $requestDatas['order_money']; // 订单金额 分
$orderNo = $requestDatas['order_no']; // 订单金额
// 推广人
$userSelf = UserLogic::getUserByInviteCode($inviteCode);
@ -116,7 +147,7 @@ class ShopCallController extends BaseApiController
// 镇/街道合伙人
if ($userSelf['group_id'] == 15) {
// 计算分润
UserLogic::userProfit($userSelf, $profit, $proportion);
UserLogic::userProfit($userSelf, $orderMoney, $proportion, $orderNo);
}
// 小队/小区队长
@ -125,7 +156,7 @@ class ShopCallController extends BaseApiController
$where1 = ['group_id' => 15, 'street'=>$userSelf['street']];
$streetUser = UserLogic::getUser($where1);
// 计算分润
UserLogic::userProfit($streetUser, $profit, 0.01);
UserLogic::userProfit($streetUser, $orderMoney, 0.01, $orderNo);
}
// 村/社区合伙人
@ -135,8 +166,9 @@ class ShopCallController extends BaseApiController
$streetUser = UserLogic::getUser($where);
// 计算分润
UserLogic::userProfit($streetUser, $profit, 0.01);
UserLogic::userProfit($streetUser, $orderMoney, 0.01, $orderNo);
}
return $this->success('成功', ['user_id' => $userSelf['id'], 'mobile' => $userSelf['account'], 'user_profit' => bcmul($profit, $proportion, 2)]);
return $this->success('成功', ['user_id' => $userSelf['id'], 'account' => $userSelf['account'], 'user_profit' => bcmul($orderMoney, $proportion, 2)]);
}
}

View File

@ -542,3 +542,40 @@ function generate_rand_code($length = 8) {
return $code;
}
/**
* 使用对称密钥进行加密
* @param $plainText
* @param $secret
* @param $iv
* @return string
*/
function encrypt($plainText, $secret, $iv = null)
{
$cipher = 'aes-128-cbc';
$plainText = json_encode($plainText);
if (!empty($iv)) {
$encryptedData = openssl_encrypt($plainText, $cipher, $secret, OPENSSL_RAW_DATA, $iv);
} else {
$encryptedData = openssl_encrypt($plainText, $cipher, $secret);
}
return base64_encode($encryptedData);
}
/**
* 使用对称秘钥解密
* @param $plainText
* @param $secret
* @param $iv
* @return false|string
*/
function decrypt($plainText, $secret, $iv = null) {
$cipher = 'aes-128-cbc';
$plainText = base64_decode($plainText);
if (!empty($iv)) {
$data = openssl_decrypt($plainText,$cipher, $secret, OPENSSL_RAW_DATA, $iv);
$data = json_decode($data, true);
return $data;
}
return openssl_decrypt($plainText, $cipher, $secret);
}

View File

@ -99,6 +99,8 @@ return [
'decorate' => [
// 底部导航栏样式设置
'tabbar_style' => ['default_color' => '#999999', 'selected_color' => '#4173ff'],
]
],
'shop_api_secret' => 've2HSq011whZYgKE',// 商城api密钥
];