'拆分活动的校验权限,分为发券和核券。'
This commit is contained in:
@@ -3,18 +3,72 @@
|
|||||||
namespace App\Admin\Controllers\Activity;
|
namespace App\Admin\Controllers\Activity;
|
||||||
|
|
||||||
use App\Models\Activity;
|
use App\Models\Activity;
|
||||||
|
use App\Models\ActivityGrant;
|
||||||
use App\Models\ActivityRule;
|
use App\Models\ActivityRule;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Encore\Admin\Controllers\AdminController;
|
use Encore\Admin\Controllers\HasResourceActions;
|
||||||
use Encore\Admin\Form;
|
use Encore\Admin\Form;
|
||||||
use Encore\Admin\Grid;
|
use Encore\Admin\Grid;
|
||||||
|
use Encore\Admin\Layout\Content;
|
||||||
use Illuminate\Support\MessageBag;
|
use Illuminate\Support\MessageBag;
|
||||||
|
use Illuminate\Routing\Controller as AdminController;
|
||||||
|
|
||||||
class IndexController extends AdminController
|
class IndexController extends AdminController
|
||||||
{
|
{
|
||||||
|
|
||||||
|
use HasResourceActions;
|
||||||
|
|
||||||
protected $title = '活动管理';
|
protected $title = '活动管理';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get content title.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function title()
|
||||||
|
{
|
||||||
|
return $this->title;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Index interface.
|
||||||
|
* @param Content $content
|
||||||
|
* @return Content
|
||||||
|
*/
|
||||||
|
public function index(Content $content)
|
||||||
|
{
|
||||||
|
return $content
|
||||||
|
->title($this->title())
|
||||||
|
->description($this->description['index'] ?? trans('admin.list'))
|
||||||
|
->body($this->grid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit interface.
|
||||||
|
* @param mixed $id
|
||||||
|
* @param Content $content
|
||||||
|
* @return Content
|
||||||
|
*/
|
||||||
|
public function edit($id, Content $content)
|
||||||
|
{
|
||||||
|
return $content
|
||||||
|
->title($this->title())
|
||||||
|
->description($this->description['edit'] ?? trans('admin.edit'))
|
||||||
|
->body($this->form($id)->edit($id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create interface.
|
||||||
|
* @param Content $content
|
||||||
|
* @return Content
|
||||||
|
*/
|
||||||
|
public function create(Content $content)
|
||||||
|
{
|
||||||
|
return $content
|
||||||
|
->title($this->title())
|
||||||
|
->description($this->description['create'] ?? trans('admin.create'))
|
||||||
|
->body($this->form());
|
||||||
|
}
|
||||||
|
|
||||||
protected function grid()
|
protected function grid()
|
||||||
{
|
{
|
||||||
$grid = new Grid(new Activity);
|
$grid = new Grid(new Activity);
|
||||||
@@ -78,21 +132,13 @@ class IndexController extends AdminController
|
|||||||
* Make a form builder.
|
* Make a form builder.
|
||||||
* @return Form
|
* @return Form
|
||||||
*/
|
*/
|
||||||
protected function form()
|
protected function form($id = '')
|
||||||
{
|
{
|
||||||
$form = new Form(new Activity);
|
$form = new Form(new Activity);
|
||||||
|
|
||||||
$form->text('title', '活动名称')->required();
|
$form->text('title', '活动名称')->required();
|
||||||
$form->textarea('description', '活动说明')->required();
|
$form->textarea('description', '活动说明')->required();
|
||||||
|
|
||||||
$form->select('user_id', '隶属渠道')
|
|
||||||
->options(function ($option, $info) {
|
|
||||||
return User::with('info')->whereHas('identity', function ($q) {
|
|
||||||
$q->where('identity_id', 1);
|
|
||||||
})->orderBy('id', 'desc')->get()->pluck('nickname', 'id');
|
|
||||||
})
|
|
||||||
->help('活动是否专属,不选择为所有渠道都可使用。');
|
|
||||||
|
|
||||||
$form->select('activity_rule_id', '所属规则')
|
$form->select('activity_rule_id', '所属规则')
|
||||||
->options(function ($option, $info) {
|
->options(function ($option, $info) {
|
||||||
return ActivityRule::where('status', 1)->pluck('title', 'id');
|
return ActivityRule::where('status', 1)->pluck('title', 'id');
|
||||||
@@ -114,6 +160,26 @@ class IndexController extends AdminController
|
|||||||
->default(1)
|
->default(1)
|
||||||
->help('同一订单,多次核销时校验,订单每满100元可核销一笔。');
|
->help('同一订单,多次核销时校验,订单每满100元可核销一笔。');
|
||||||
|
|
||||||
|
$grantdef = $verificationsdef = '';
|
||||||
|
if ($id) {
|
||||||
|
$grantdef = Activity::find($id)->grants()->pluck('user_id')->toArray();
|
||||||
|
$verificationsdef = Activity::find($id)->verifications()->pluck('user_id')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$users = User::with('info')->whereHas('identity', function ($q) {
|
||||||
|
$q->where('identity_id', 1);
|
||||||
|
})->orderBy('id', 'desc')->get()->pluck('nickname', 'id');
|
||||||
|
|
||||||
|
$form->listbox('grants.user_id', '可发券渠道')
|
||||||
|
->options($users)
|
||||||
|
->default($grantdef)
|
||||||
|
->required();
|
||||||
|
|
||||||
|
$form->listbox('verifications.user_id', '可核券渠道')
|
||||||
|
->options($users)
|
||||||
|
->default($verificationsdef)
|
||||||
|
->required();
|
||||||
|
|
||||||
$form->saving(function (Form $form) {
|
$form->saving(function (Form $form) {
|
||||||
$request = request();
|
$request = request();
|
||||||
if ($request->type == Activity::TYPE_EXTEND && empty($request->days)) {
|
if ($request->type == Activity::TYPE_EXTEND && empty($request->days)) {
|
||||||
@@ -142,6 +208,29 @@ class IndexController extends AdminController
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$form->saved(function (Form $form) {
|
||||||
|
$users = [];
|
||||||
|
foreach ($form->grants['user_id'] as $key => $user_id) {
|
||||||
|
if ($user_id) {
|
||||||
|
$form->model()->grants()->updateOrCreate([
|
||||||
|
'user_id' => $user_id,
|
||||||
|
]);
|
||||||
|
$users[] = $user_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$form->model()->grants()->whereNotIn('user_id', $users)->delete();
|
||||||
|
$users = [];
|
||||||
|
foreach ($form->verifications['user_id'] as $key => $user_id) {
|
||||||
|
if ($user_id) {
|
||||||
|
$form->model()->verifications()->updateOrCreate([
|
||||||
|
'user_id' => $user_id,
|
||||||
|
]);
|
||||||
|
$users[] = $user_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$form->model()->verifications()->whereNotIn('user_id', $users)->delete();
|
||||||
|
});
|
||||||
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,28 @@ class Activity extends Model
|
|||||||
return $this->hasMany(ActivityCoupon::class);
|
return $this->hasMany(ActivityCoupon::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes: 关联可发券渠道
|
||||||
|
* @Author: 玄尘
|
||||||
|
* @Date : 2020/8/21 11:09
|
||||||
|
*/
|
||||||
|
public function grants()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ActivityGrant::class);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes: 关联可核销渠道
|
||||||
|
* @Author: 玄尘
|
||||||
|
* @Date : 2020/8/21 11:09
|
||||||
|
*/
|
||||||
|
public function verifications()
|
||||||
|
{
|
||||||
|
return $this->hasMany(ActivityVerification::class);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//发券
|
//发券
|
||||||
public function grant($mobile, $outletId = null)
|
public function grant($mobile, $outletId = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Models\Traits\BelongsToActivity;
|
||||||
use App\Models\Traits\BelongsToOutlet;
|
use App\Models\Traits\BelongsToOutlet;
|
||||||
|
|
||||||
class ActivityCoupon extends Model
|
class ActivityCoupon extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
use BelongsToOutlet;
|
use BelongsToOutlet, BelongsToActivity;
|
||||||
|
|
||||||
protected $dates = [
|
protected $dates = [
|
||||||
'start_at',
|
'start_at',
|
||||||
@@ -30,17 +31,6 @@ class ActivityCoupon extends Model
|
|||||||
*/
|
*/
|
||||||
protected $with = ['activity'];
|
protected $with = ['activity'];
|
||||||
|
|
||||||
/**
|
|
||||||
* Notes: 关联活动
|
|
||||||
* @Author: 玄尘
|
|
||||||
* @Date : 2020/6/30 9:40
|
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
||||||
*/
|
|
||||||
public function activity()
|
|
||||||
{
|
|
||||||
return $this->belongsTo(Activity::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notes: 卡券状态
|
* Notes: 卡券状态
|
||||||
* @Author: 玄尘
|
* @Author: 玄尘
|
||||||
|
|||||||
12
app/Models/ActivityGrant.php
Normal file
12
app/Models/ActivityGrant.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Models\Traits\BelongsToActivity;
|
||||||
|
use App\Models\Traits\BelongsToUser;
|
||||||
|
|
||||||
|
class ActivityGrant extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
use BelongsToActivity, BelongsToUser;
|
||||||
|
}
|
||||||
12
app/Models/ActivityVerification.php
Normal file
12
app/Models/ActivityVerification.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Models\Traits\BelongsToActivity;
|
||||||
|
use App\Models\Traits\BelongsToUser;
|
||||||
|
|
||||||
|
class ActivityVerification extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
use BelongsToActivity, BelongsToUser;
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
class PinganToken extends Model
|
class PinganToken extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $dates = [
|
protected $dates = [
|
||||||
'get_token_time',
|
'get_token_time',
|
||||||
];
|
];
|
||||||
|
|||||||
23
app/Models/Traits/BelongsToActivity.php
Normal file
23
app/Models/Traits/BelongsToActivity.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Traits;
|
||||||
|
|
||||||
|
use App\Models\Activity;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
trait BelongsToActivity
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notes: 关联活动
|
||||||
|
* @Author: 玄尘
|
||||||
|
* @Date : 2020/6/30 9:40
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
*/
|
||||||
|
public function activity()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Activity::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,14 +16,12 @@ class User extends Authenticatable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that are mass assignable.
|
* The attributes that are mass assignable.
|
||||||
*
|
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be hidden for arrays.
|
* The attributes that should be hidden for arrays.
|
||||||
*
|
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ namespace App\Models;
|
|||||||
|
|
||||||
class UserCode extends Model
|
class UserCode extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'codes' => 'array',
|
'codes' => 'array',
|
||||||
'profit' => 'array',
|
'profit' => 'array',
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ use App\Models\Traits\BelongsToUser;
|
|||||||
|
|
||||||
class UserPingan extends Model
|
class UserPingan extends Model
|
||||||
{
|
{
|
||||||
use BelongsToUser;
|
|
||||||
|
|
||||||
|
use BelongsToUser;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ namespace App\Models;
|
|||||||
|
|
||||||
class WoCouponLog extends Model
|
class WoCouponLog extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
public function coupon()
|
public function coupon()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(WoCoupon::class, 'wo_coupon_id', 'id');
|
return $this->belongsTo(WoCoupon::class, 'wo_coupon_id', 'id');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,15 +18,18 @@ class YsdGrant extends Init
|
|||||||
return '发券失败,没有找到这个活动。';
|
return '发券失败,没有找到这个活动。';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($activity->user) {
|
$outlet = User::where('outlet_id', $this->outletId)->first();
|
||||||
$info = User::where('outlet_id', $this->outletId)->first();
|
if (!$outlet) {
|
||||||
if (!$info) {
|
|
||||||
return '发券失败,未查询到此网点信息。';
|
return '发券失败,未查询到此网点信息。';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($info->parent_id != $activity->user_id) {
|
$grants = $activity->grants()->pluck('user_id');
|
||||||
return '发券失败,您没有权限参加这个活动!';
|
if ($grants->isEmpty()) {
|
||||||
|
return '发券失败,此活动还没有配置可发券渠道,请联系相关人员进行配置。';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!in_array($outlet->parent_id, $grants->toArray())) {
|
||||||
|
return '发券失败,您没有权限发此类优惠券。';
|
||||||
}
|
}
|
||||||
|
|
||||||
$coupon = $activity->grant($this->mobile, $this->outletId);
|
$coupon = $activity->grant($this->mobile, $this->outletId);
|
||||||
|
|||||||
@@ -24,8 +24,19 @@ class YsdQuery extends Init
|
|||||||
throw new \Exception('卡券编号错误,未查询到卡券信息');
|
throw new \Exception('卡券编号错误,未查询到卡券信息');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($coupon->activity->user_id > 0 && $info->parent_id != $coupon->activity->user_id) {
|
$activity = $coupon->activity;
|
||||||
throw new \Exception('您没有权限查询此卡券信息。');
|
if (!$activity) {
|
||||||
|
throw new \Exception('操作失败,未查到活动信息');
|
||||||
|
}
|
||||||
|
|
||||||
|
$verifications = $activity->verifications()->pluck('user_id');
|
||||||
|
|
||||||
|
if ($verifications->isEmpty()) {
|
||||||
|
throw new \Exception('操作失败,此活动还没有配置可核券渠道,请联系相关人员进行配置。');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array($info->parent_id, $verifications->toArray())) {
|
||||||
|
throw new \Exception('操作失败,您没有权限查询此卡券信息。');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $coupon;
|
return $coupon;
|
||||||
|
|||||||
@@ -104,10 +104,6 @@ class YsdVerification extends Init
|
|||||||
return '核销失败,优惠券不可核销';
|
return '核销失败,优惠券不可核销';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->query_coupon->activity->user_id > 0 && $this->user->id !== $this->query_coupon->activity->user_id) {
|
|
||||||
return '核销失败,您没有权限核销此优惠券。';
|
|
||||||
}
|
|
||||||
|
|
||||||
$now = now();
|
$now = now();
|
||||||
|
|
||||||
if ($this->query_coupon->start_at->gt($now)) {
|
if ($this->query_coupon->start_at->gt($now)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user