核销回调+修改活动规则

This commit is contained in:
2020-08-13 13:32:48 +08:00
parent ce775fd9b5
commit 60744fed7c
9 changed files with 150 additions and 19 deletions

View File

@@ -3,9 +3,12 @@
namespace App\Admin\Controllers\Activity; namespace App\Admin\Controllers\Activity;
use App\Models\ActivityRule; use App\Models\ActivityRule;
use App\Models\User;
use Encore\Admin\Controllers\AdminController; use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form; use Encore\Admin\Form;
use Encore\Admin\Grid; use Encore\Admin\Grid;
use Illuminate\Support\MessageBag;
use RuLong\Identity\Models\Identity;
class RuleController extends AdminController class RuleController extends AdminController
{ {
@@ -46,10 +49,31 @@ class RuleController extends AdminController
$form->text('title', '规则名称')->required(); $form->text('title', '规则名称')->required();
$form->text('code', '规则编号')->required()->help('列YSD-full100-15'); $form->text('code', '规则编号')->required()->help('列YSD-full100-15');
$form->decimal('full', '满足金额')->required()->default(0); $form->hidden('full', '满足金额')->required()->default(0);
$form->decimal('take', '抵扣金额')->required()->default(0); $form->hidden('take', '抵扣金额')->required()->default(0);
$form->switch('status', '状态')->default(1); $form->switch('status', '状态')->default(1);
$form->saving(function (Form $form) {
$code = $form->code;
$ticket = explode('-', $code);
if (!is_array($ticket) || count($ticket) != 3) {
$error = new MessageBag([
'title' => '错误',
'message' => '规则编号格式错误',
]);
return back()->withInput()->with(compact('error'));
}
$full = $ticket[1]; //full100
$price = $ticket[2];
preg_match('/\d+/', $full, $match);
$form->full = $match[0];
$form->take = $price;
});
return $form; return $form;
} }

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Events;
use App\Models\ActivityCoupon;
/**
* 核券之后的回调
*/
class ConponCallback
{
public $acticity_coupon;
public function __construct(ActivityCoupon $acticity_coupon)
{
$this->acticity_coupon = $acticity_coupon;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Listeners;
use App\Events\ConponCallback;
use App\Models\ActivityCouponLog;
use GuzzleHttp\Client;
use Illuminate\Contracts\Queue\ShouldQueue;
class ConponCallbackListener implements ShouldQueue
{
public $queue = 'LISTENER';
/**
* Handle the event.
* @param RoomLoginDone $event
* @return void
*/
public function handle(ConponCallback $event)
{
$acticity_coupon = $event->acticity_coupon;
$user = $acticity_coupon->outlet->parent;
if ($user->callback) {
$client = new Client();
$response = $client->request('post', $user->callback, [
'timeout' => 30,
'query' => [
'code' => $acticity_coupon->code,
],
]);
$data = [
'code' => $acticity_coupon->code,
'url' => $user->callback,
];
if ($response->getStatusCode() == 200) {
$body = $response->getBody();
$result = json_decode($body->getContents(), true);
$data['status'] = $result['code'] ?? '';
$data['source'] = $result;
$data['remark'] = $result['message'] ?? '';
} else {
$data['status'] = 0;
$data['source'] = '';
$data['remark'] = '接口错误';
}
ActivityCouponLog::create($data);
}
}
}

View File

@@ -2,9 +2,13 @@
namespace App\Models; namespace App\Models;
use App\Models\Traits\BelongsToOutlet;
class ActivityCoupon extends Model class ActivityCoupon extends Model
{ {
use BelongsToOutlet;
protected $dates = [ protected $dates = [
'start_at', 'start_at',
'end_at', 'end_at',

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Models;
class ActivityCouponLog extends Model
{
protected $casts = [
'source' => 'array',
];
}

View File

@@ -2,12 +2,13 @@
namespace App\Models; namespace App\Models;
use App\Models\Traits\BelongsToOutlet;
use App\Models\Traits\BelongsToUser; use App\Models\Traits\BelongsToUser;
class Coupon extends Model class Coupon extends Model
{ {
use BelongsToUser; use BelongsToUser, BelongsToOutlet;
protected $dates = [ protected $dates = [
'paid_at', 'paid_at',
@@ -20,11 +21,6 @@ class Coupon extends Model
self::TYPE_YSD => '自有券', self::TYPE_YSD => '自有券',
]; ];
public function outlet()
{
return $this->belongsTo(User::class, 'outletId', 'outlet_id');
}
public function getStatusTextAttribute() public function getStatusTextAttribute()
{ {
switch ($this->status) { switch ($this->status) {

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Models\Traits;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
trait BelongsToOutlet
{
public function outlet()
{
return $this->belongsTo(User::class, 'outletId', 'outlet_id');
}
}

View File

@@ -2,27 +2,25 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Auth\Events\Registered; use App\Events\ConponCallback;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use App\Listeners\ConponCallbackListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
/** /**
* The event listener mappings for the application. * The event listener mappings for the application.
*
* @var array * @var array
*/ */
protected $listen = [ protected $listen = [
Registered::class => [ ConponCallback::class => [
SendEmailVerificationNotification::class, ConponCallbackListener::class,
], ],
]; ];
/** /**
* Register any events for your application. * Register any events for your application.
*
* @return void * @return void
*/ */
public function boot() public function boot()
@@ -31,4 +29,5 @@ class EventServiceProvider extends ServiceProvider
// //
} }
} }

View File

@@ -2,6 +2,7 @@
namespace XuanChen\Coupon\Action\ysd; namespace XuanChen\Coupon\Action\ysd;
use App\Events\ConponCallback;
use App\Models\ActivityCoupon; use App\Models\ActivityCoupon;
use App\Models\Coupon; use App\Models\Coupon;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@@ -30,6 +31,7 @@ class YsdVerification extends Init
$this->query_coupon = (new YsdQuery)->setOutletId($this->outletId) $this->query_coupon = (new YsdQuery)->setOutletId($this->outletId)
->setCode($this->redemptionCode) ->setCode($this->redemptionCode)
->start(); ->start();
if (is_string($this->query_coupon)) { if (is_string($this->query_coupon)) {
return $this->query_coupon; return $this->query_coupon;
} }
@@ -74,6 +76,8 @@ class YsdVerification extends Init
$this->coupon->profit(); $this->coupon->profit();
DB::commit(); DB::commit();
event(new ConponCallback($this->query_coupon));
return $resdata; return $resdata;
} catch (Exception $e) { } catch (Exception $e) {
DB::rollback(); DB::rollback();
@@ -115,10 +119,7 @@ class YsdVerification extends Init
} }
$rule_code = $this->query_coupon->activity->rule->code; $rule_code = $this->query_coupon->activity->rule->code;
info($rule_code);
$code = $this->user->code->where('code', $rule_code)->first(); $code = $this->user->code->where('code', $rule_code)->first();
info($this->user->code);
info($code);
if (!$code) { if (!$code) {
return "核销失败,您没有权限使用此卡券优惠活动。"; return "核销失败,您没有权限使用此卡券优惠活动。";