admin/plugins/new-poster/src/services/RecommendReward.php

148 lines
5.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Yunshop\NewPoster\services;
use app\common\exceptions\ShopException;
use app\common\models\CouponLog;
use app\common\models\MemberCoupon;
use app\common\services\credit\ConstService;
use app\common\services\finance\BalanceChange;
use app\common\services\finance\PointService;
use app\common\services\WechatPay;
use Illuminate\Support\Str;
class RecommendReward
{
protected $type_change = [
'1' => 'point',
'2' => 'bonus_red',
'3' => 'wechat_red',
'4' => 'coupon',
];
protected $type;
protected $poster;
protected $recommendMemberId;
protected $subscriberMemberId;
protected $recommendOpenid;
public function index($type, $poster = '', $recommendMemberId = '', $subscriberMemberId = '', $recommendOpenid = '')
{
$this->type = $type;
$this->poster = $poster;
$this->recommendMemberId = $recommendMemberId;
$this->subscriberMemberId = $subscriberMemberId;
$this->recommendOpenid = $recommendOpenid;
$method = $this->type_change[$this->type];
$method = Str::camel($method);
if (!method_exists(new self(), $method)) {
throw new ShopException('方法' . $method . '不存在');
}
self::$method();
}
protected function point()
{
$pointData = array(
'uniacid' => \YunShop::app()->uniacid,
'point_income_type' => 1,
'member_id' => $this->recommendMemberId,
'point_mode' => 3,
'point' => $this->poster->supplement->recommender_credit,
'remark' => '超级海报: uid 为 ' . $this->recommendMemberId . ' 的用户推荐uid 为 ' . $this->subscriberMemberId . ' 的用户, 获得推送海报积分奖励 ' . $this->poster->supplement->recommender_credit . ' 个',
);
try {
$pointService = new PointService($pointData);
$pointService->changePoint();
} catch (\Exception $e) {
\Log::error('超级海报扫码注册的积分奖励出错:' . $e->getMessage());
}
}
protected function bonusRed()
{
$data = [
'member_id' => $this->recommendMemberId,
'remark' => '超级海报: uid 为 ' . $this->recommendMemberId . ' 的用户推荐 uid 为 ' . $this->subscriberMemberId . ' 的用户, 因此获得余额奖励' . $this->poster->supplement->recommender_bonus . '元',
'source' => ConstService::SOURCE_AWARD,
'relation' => '',
'operator' => 1,
'operator_id' => $this->poster->id,
'change_value' => $this->poster->supplement->recommender_bonus, //奖励金额
];
try {
if (bccomp($this->poster->supplement->recommender_bonus, 0, 2) == 1) {
(new BalanceChange())->award($data);
}
} catch (\Exception $e) {
\Log::error('超级海报扫码注册的余额红包奖励出错:' . $e->getMessage());
}
}
protected function wechatRed()
{
$wechatPay = new WechatPay();
$pay = \Setting::get('shop.pay');
$shop_name = \Setting::get('shop.shop.name');
$notify_url = ''; //回调地址
$app = $wechatPay->getEasyWeChatApp($pay, $notify_url);
$luckyMoney = $app->redpack;
$luckyMoneyData = [
'mch_billno' => $pay['weixin_mchid'] . date('YmdHis') . rand(1000, 9999),
'send_name' => mb_substr($shop_name, 0, 10, 'utf-8'),
're_openid' => $this->recommendOpenid,
'total_num' => 1,
'total_amount' => $this->poster->supplement->recommender_bonus * 100,
'wishing' => '超级海报推荐奖励',
'client_ip' => \Request::getClientIp(),
'act_name' => '超级海报推荐奖励',
'remark' => '超级海报: 推荐者奖励 ' . $this->poster->supplement->recommender_bonus . ' 元',
];
try {
$luckyMoney->sendNormal($luckyMoneyData);
} catch (\Exception $e) {
\Log::error('超级海报扫码注册的微信红包奖励出错:' . $e->getMessage());
}
}
protected function coupon()
{
$couponData = [
'uniacid' => \YunShop::app()->uniacid,
'uid' => $this->recommendMemberId,
'coupon_id' => $this->poster->supplement->recommender_coupon_id,
'get_type' => 3,
'used' => 0,
'get_time' => strtotime('now'),
];
for($i = 0; $i<$this->poster->supplement->recommender_coupon_num; $i++) {
$memberCoupon = new MemberCoupon();
$res = $memberCoupon->create($couponData);
//写入log
if ($res) {
//发放优惠券成功
$log = '超级海报优惠券奖励: 奖励 ' . $this->poster->supplement->recommender_coupon_num . ' 张优惠券( ID为 ' . $couponData['coupon_id'] . ' )给用户( Member ID 为 ' . $couponData['uid'] . ' )';
//超级海报优惠券奖励奖励1张优惠券ID为123给用户ID为12
} else { //发放优惠券失败
$log = '超级海报优惠券奖励: 奖励优惠券( ID为 ' . $couponData['coupon_id'] . ' )给用户( Member ID 为 ' . $couponData['uid'] . ' )时失败!';
}
$logData = [
'uniacid' => \YunShop::app()->uniacid,
'logno' => $log,
'member_id' => $couponData['uid'],
'couponid' => $couponData['coupon_id'],
'paystatus' => 0,
'creditstatus' => 0,
'paytype' => 0,
'getfrom' => 0,
'status' => 0,
'createtime' => time(),
];
CouponLog::create($logData);
}
}
}