'拆分活动的校验权限,分为发券和核券。'
This commit is contained in:
@@ -3,18 +3,72 @@
|
||||
namespace App\Admin\Controllers\Activity;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\ActivityGrant;
|
||||
use App\Models\ActivityRule;
|
||||
use App\Models\User;
|
||||
use Encore\Admin\Controllers\AdminController;
|
||||
use Encore\Admin\Controllers\HasResourceActions;
|
||||
use Encore\Admin\Form;
|
||||
use Encore\Admin\Grid;
|
||||
use Encore\Admin\Layout\Content;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Illuminate\Routing\Controller as AdminController;
|
||||
|
||||
class IndexController extends AdminController
|
||||
{
|
||||
|
||||
use HasResourceActions;
|
||||
|
||||
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()
|
||||
{
|
||||
$grid = new Grid(new Activity);
|
||||
@@ -78,21 +132,13 @@ class IndexController extends AdminController
|
||||
* Make a form builder.
|
||||
* @return Form
|
||||
*/
|
||||
protected function form()
|
||||
protected function form($id = '')
|
||||
{
|
||||
$form = new Form(new Activity);
|
||||
|
||||
$form->text('title', '活动名称')->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', '所属规则')
|
||||
->options(function ($option, $info) {
|
||||
return ActivityRule::where('status', 1)->pluck('title', 'id');
|
||||
@@ -114,6 +160,26 @@ class IndexController extends AdminController
|
||||
->default(1)
|
||||
->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) {
|
||||
$request = request();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user