diff --git a/app/common/dao/store/consumption/CommissionDao.php b/app/common/dao/store/consumption/CommissionDao.php new file mode 100644 index 00000000..a4a1be03 --- /dev/null +++ b/app/common/dao/store/consumption/CommissionDao.php @@ -0,0 +1,164 @@ +where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find(); + if (empty($consumption)) { + return $finance; + } + $storeConsumptionDao = new StoreConsumptionUserDao(); + $isFirstOrder = $storeConsumptionDao->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']); + if (!$isFirstOrder) { + return $finance; + } + $commission = bcmul($order['pay_price'], $consumption['config']['commission_rate'], 2); + if ($commission > 0) { + $finance[] = [ + 'order_id' => $order->order_id, + 'order_sn' => $order->order_sn, + 'user_info' => $order->user->nickname, + 'user_id' => $order['uid'], + 'financial_type' => 'first_order_commission', + 'financial_pm' => 0, + 'type' => 2, + 'number' => $commission, + 'mer_id' => $order->mer_id, + 'financial_record_sn' => $financeSn + ]; + app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission); + } + $redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2); + if ($redPack > 0) { + $userId = Merchant::where('mer_id', $order['mer_id'])->value('uid'); + $storeConsumptionDao->send($consumption, $consumption['config']['red_pack_rate'], $userId, $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); + $user = User::where('uid', $userId)->find(); + $user->red_pack_balance = bcadd($user->red_pack_balance, $redPack, 2); + if (!$user->save()) { + throw new \Exception('红包余额更新出错'); + } + } + $promotionCode = User::where('uid', $order['uid'])->value('promotion_code'); + if (!empty($promotionCode)) { + $this->sendCommission($order, $promotionCode); + } + return $finance; + } + + /** + * 活动首单推广人佣金(供应商平台异步回调) + * @param $data + * @return mixed + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function firstOrderBatchCommission($data) + { + $consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find(); + if (empty($consumption)) { + return []; + } + $financialRecordRepository = app()->make(FinancialRecordRepository::class); + $financeSn = $financialRecordRepository->getSn(); + $users = $data['user']; + $order = StoreOrder::where('order_id', $data['order_id'])->find(); + if (empty($order)) { + return []; + } + $finance = []; + $result = []; + foreach ($users as $k => $user) { + $user['user_profit'] = $user['commission']; + $user['account'] = $user['phone']; + $commission = bcdiv($user['user_profit'], 100, 2); + if ($commission > 0) { + $finance[] = [ + 'order_id' => $order->order_id, + 'order_sn' => $order->order_sn, + 'user_info' => $order->user->nickname, + 'user_id' => $order['uid'], + 'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission', + 'financial_pm' => 0, + 'type' => 2, + 'number' => $commission, + 'mer_id' => $order['mer_id'], + 'financial_record_sn' => $financeSn . ($k + 1) + ]; + $result[] = $user; + } + //用户是镇合伙人,不发放红包 + if ($user['type'] == 3) { + continue; + } + $redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2); + $user = User::where('phone', $user['account'])->find(); + if ($redPack > 0 && !empty($user)) { + try { + (new StoreConsumptionUserDao())->send($consumption, $consumption['config']['red_pack_rate'], $user['uid'], $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); + $user->red_pack_balance = bcadd($user->red_pack_balance, $redPack, 2); + if (!$user->save()) { + throw new \Exception('红包余额更新出错'); + } + } catch (\Exception $e) { + Log::error($e->getMessage()); + } + } + } + if (count($finance) > 0) { + $financialRecordRepository->insertAll($finance); + } + return $result; + } + + /** + * 给镇合伙人或推广人发放佣金(支付完成后调用,推广人仅首单奖励) + * 请求发送给供应商平台后异步回调 + * @param $order + * @param $promotionCode + * @param int $type 类型:1=>推广人,2=>镇合伙人 + * @return void + */ + public function sendCommission($order, $promotionCode, $type = 1) + { + $curl = new Curl(); + $timestamp = time(); + $json = ['timestamp' => $timestamp, 'data' => ['order_id' => $order['order_id'], 'order_sn' => $order['order_no'], 'order_money' => bcmul($order['pay_price'], 100), 'promotion_code' => $promotionCode]]; + if ($type == 2) { + $json['street_code'] = $promotionCode; + } else { + $json['promotion_code'] = $promotionCode; + } + $aes = new \AES(); + $iv = $aes->buildIv($timestamp); + $encrypt = $aes->encrypt($json, $iv); + $api = $type == 1 ? 'user_first_order_share_profit' : 'user_order_share_profit'; + $url = env('task.worker_host_url') . '/api/shop_call/' . $api; + $curl->post($url, ['timestamp' => $timestamp, 'data' => $encrypt]); + } + +} \ No newline at end of file diff --git a/app/common/dao/store/consumption/StoreConsumptionUserDao.php b/app/common/dao/store/consumption/StoreConsumptionUserDao.php index f7c9033d..d0ebcd95 100644 --- a/app/common/dao/store/consumption/StoreConsumptionUserDao.php +++ b/app/common/dao/store/consumption/StoreConsumptionUserDao.php @@ -9,9 +9,7 @@ use app\common\model\store\consumption\StoreConsumption; use app\common\model\store\consumption\StoreConsumptionUser; use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\StoreActivityOrder; -use app\common\model\system\merchant\Merchant; use app\common\model\user\User; -use app\common\repositories\system\merchant\MerchantRepository; use think\facade\Db; class StoreConsumptionUserDao extends BaseDao @@ -227,53 +225,4 @@ class StoreConsumptionUserDao extends BaseDao return intval($count == 1); } - /** - * 活动首单商户佣金 - * @param $order - * @param $finance - * @param $financeSn - * @return mixed - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - public function firstOrderCommission($order, $finance, $financeSn) - { - $consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find(); - if (empty($consumption)) { - return $finance; - } - $isFirstOrder = $this->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']); - if (!$isFirstOrder) { - return $finance; - } - $commission = bcmul($order['pay_price'], $consumption['config']['commission_rate'], 2); - if ($commission > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $order->user->nickname, - 'user_id' => $order['uid'], - 'financial_type' => 'first_order_commission', - 'financial_pm' => 0, - 'type' => 2, - 'number' => $commission, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn - ]; - app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission); - } - $redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2); - if ($redPack > 0) { - $userId = Merchant::where('mer_id', $order['mer_id'])->value('uid'); - $this->send($consumption, $consumption['config']['red_pack_rate'], $userId, $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); - $user = User::where('uid', $userId)->find(); - $user->red_pack_balance = bcadd($user->red_pack_balance, $redPack, 2); - if (!$user->save()) { - throw new \Exception('红包余额更新出错'); - } - } - return $finance; - } - } \ No newline at end of file diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 62cf0742..c0533256 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -11,6 +11,7 @@ // +---------------------------------------------------------------------- namespace app\common\repositories\store\order; +use app\common\dao\store\consumption\CommissionDao; use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\order\StoreCartDao; use app\common\dao\store\order\StoreOrderDao; @@ -422,8 +423,11 @@ class StoreOrderRepository extends BaseRepository ], $order->mer_id); //自动打印订单 $this->autoPrinter($order->order_id, $order->mer_id); - // "惠农供销,谱写数字新篇章"活动首单分润 - $finance = (new StoreConsumptionUserDao())->firstOrderCommission($order, $finance, $financeSn . ($i++)); + // "惠农供销,谱写数字新篇章"活动首单分润,商户和村、小组合伙人 + $finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn . ($i++)); + $addressCode = explode(',', $order['user_address_code']); + // "惠农供销,谱写数字新篇章"活动首单分润,镇合伙人 + (new CommissionDao())->sendCommission($order, $addressCode[3], 2); } //分销判断 // if ($groupOrder->user->spread_uid) { diff --git a/app/controller/api/Open.php b/app/controller/api/Open.php new file mode 100644 index 00000000..27b4809f --- /dev/null +++ b/app/controller/api/Open.php @@ -0,0 +1,34 @@ +request->post('timestamp'); + $data = $this->request->post('data'); + $aes = new \AES(); + $iv = !empty($timestamp) ? $aes->buildIv($timestamp) : ''; + $decrypted = $aes->decrypt($data, $iv); + if (!empty($decrypted)) { + $storeConsumptionUserDao = new CommissionDao(); + // "惠农供销,谱写数字新篇章"活动首单分润 + $result = $storeConsumptionUserDao->firstOrderBatchCommission($decrypted); + return app('json')->success($result); + } + return app('json')->fail('解密失败'); + } + +} \ No newline at end of file diff --git a/extend/AES.php b/extend/AES.php index 68773b70..804e271e 100644 --- a/extend/AES.php +++ b/extend/AES.php @@ -4,21 +4,21 @@ class AES { public $cipher = 'aes-128-cbc'; + public $secret = 've2HSq011whZYgKE'; /** * 使用对称密钥进行加密 * @param $plainText - * @param $secret * @param $iv * @return string */ - function encrypt($plainText, $secret, $iv = null) + function encrypt($plainText, $iv = null) { $plainText = json_encode($plainText); if (!empty($iv)) { - $encryptedData = openssl_encrypt($plainText, $this->cipher, $secret, OPENSSL_RAW_DATA, $iv); + $encryptedData = openssl_encrypt($plainText, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv); } else { - $encryptedData = openssl_encrypt($plainText, $this->cipher, $secret); + $encryptedData = openssl_encrypt($plainText, $this->cipher, $this->secret); } return base64_encode($encryptedData); } @@ -26,19 +26,27 @@ class AES /** * 使用对称秘钥解密 * @param $plainText - * @param $secret * @param $iv * @return false|string */ - function decrypt($plainText, $secret, $iv = null) { + function decrypt($plainText, $iv = null) { $plainText = base64_decode($plainText); if (!empty($iv)) { - $data = openssl_decrypt($plainText, $this->cipher, $secret, OPENSSL_RAW_DATA, $iv); + $data = openssl_decrypt($plainText, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $iv); $data = json_decode($data, true); return $data; } - return openssl_decrypt($plainText, $this->cipher, $secret); + return openssl_decrypt($plainText, $this->cipher, $this->secret); } + /** + * 生成iv + * @param int $timestamp 时间戳 + * @return false|string + */ + public function buildIv(int $timestamp) + { + return substr(md5($this->secret . $timestamp), 5, 16); + } } \ No newline at end of file diff --git a/route/api.php b/route/api.php index d8b2a647..c6f85dc5 100644 --- a/route/api.php +++ b/route/api.php @@ -644,6 +644,7 @@ Route::group('api/', function () { Route::resource('store/product/cloudWarehouse', 'api.store.product.CloudWarehouse'); Route::get('store/product/town_cloud', 'api.store.product.CloudWarehouse/town'); Route::get('storeActivity/consumption', 'api.store.StoreActivity/consumption'); //消费金列表 + Route::post('open/activityCommission', 'api.open/activityCommission'); //活动佣金回调 })->middleware(UserTokenMiddleware::class, false); //微信支付回调