190 lines
6.0 KiB
PHP
190 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Controllers;
|
||
|
||
use App\Admin\Actions\AllotCard;
|
||
use App\Admin\Exporters\CardExporters;
|
||
use App\Models\Card;
|
||
use App\Models\Goods;
|
||
use App\Models\GoodsParams;
|
||
use Encore\Admin\Controllers\AdminController;
|
||
use Encore\Admin\Grid;
|
||
use Encore\Admin\Widgets\Box;
|
||
use Encore\Admin\Widgets\Form;
|
||
use Illuminate\Http\Request;
|
||
|
||
class CardController extends AdminController
|
||
{
|
||
protected $title = '卡管理';
|
||
|
||
protected function grid()
|
||
{
|
||
$grid = new Grid(new Card);
|
||
$grid->disableCreateButton();
|
||
$grid->disableRowSelector();
|
||
$grid->disableActions();
|
||
$grid->disableColumnSelector();
|
||
$grid->tools(function (Grid\Tools $tools) {
|
||
$tools->append(new AllotCard());
|
||
});
|
||
|
||
$grid->header(function ($query) {
|
||
$form = new Form();
|
||
$form->action('card/createCard');
|
||
$form->hidden('_token')->default(csrf_token());
|
||
$form->disableReset();
|
||
$form->text('num', '生成数量')->rules('required|integer|min:1');
|
||
$form->select('type', '类型')->options([
|
||
'L' => '联【L】',
|
||
'K' => '卡【K】',
|
||
'F' => '蝠【F】',
|
||
])->load('goods', 'card/getGoods')->rules('required');
|
||
$form->select('goods', '专属商品')->load('param', 'card/getParam');
|
||
$form->select('param', '专属规格');
|
||
|
||
$box = new Box();
|
||
$box = new Box('生成卡', $form->render());
|
||
$box->collapsable();
|
||
$box->style('success');
|
||
$box->solid();
|
||
return $box->render();
|
||
});
|
||
|
||
$grid->model()->orderBy('id', 'desc');
|
||
|
||
$grid->column('id', '卡序号')->sortable();
|
||
$grid->column('归属')->display(function () {
|
||
return ($this->user->username ?? '---') . "<br>" . ($this->user->info->nickname ?? '---');
|
||
});
|
||
$grid->column('code', '卡号')->sortable();
|
||
$grid->column('打印卡号')->display(function () {
|
||
return $this->type . $this->code;
|
||
});
|
||
$grid->column('pass', '卡密');
|
||
$grid->column('type', '类型')->using([
|
||
'L' => '联【L】',
|
||
'K' => '卡【K】',
|
||
'F' => '蝠【F】',
|
||
]);
|
||
$grid->column('专项')->display(function () {
|
||
if ($this->type == 'K') {
|
||
return '商品ID:' . $this->source['goods'] . ';规格ID:' . $this->source['param'];
|
||
} else {
|
||
return '非专项';
|
||
}
|
||
});
|
||
|
||
$grid->column('status', '状态')->display(function () {
|
||
return $this->status_text;
|
||
})->label([
|
||
0 => 'warning',
|
||
1 => 'success',
|
||
2 => 'success',
|
||
3 => 'success',
|
||
4 => 'success',
|
||
5 => 'info',
|
||
]);
|
||
$grid->column('激活用户')->display(function () {
|
||
return $this->activeUser->username ?? '无';
|
||
});
|
||
$grid->column('actived_at', '激活时间');
|
||
$grid->column('created_at', '创建时间');
|
||
$grid->column('updated_at', '更改时间');
|
||
$grid->filter(function ($filter) {
|
||
$filter->disableIdFilter();
|
||
$filter->column(4, function ($filter) {
|
||
$filter->equal('code', '卡号');
|
||
});
|
||
$filter->column(4, function ($filter) {
|
||
$filter->equal('type', '类型')->select([
|
||
'L' => '联【L】',
|
||
'K' => '卡【K】',
|
||
'F' => '蝠【F】',
|
||
]);
|
||
});
|
||
|
||
$filter->column(4, function ($filter) {
|
||
|
||
});
|
||
|
||
$filter->column(4, function ($filter) {
|
||
|
||
});
|
||
});
|
||
|
||
$grid->disableExport(false);
|
||
$grid->exporter(new CardExporters());
|
||
|
||
return $grid;
|
||
}
|
||
|
||
public function createCard(Request $request)
|
||
{
|
||
$request->validate([
|
||
'num' => 'required|integer|min:1',
|
||
'type' => 'required',
|
||
], [
|
||
'num.required' => '数量必须填写',
|
||
'num.integer' => '数量必须是整数',
|
||
'num.min' => '数量最小为1',
|
||
'type.required' => '类型必须填写',
|
||
]);
|
||
|
||
$num = $request->num;
|
||
$type = $request->type;
|
||
$source = [
|
||
'goods' => $request->goods ?: 0,
|
||
'param' => $request->param ?: 0,
|
||
];
|
||
$data = [];
|
||
$codeStart = (Card::max('id') ?? 0) + 1;
|
||
$source = json_encode($source);
|
||
while ($num > 0) {
|
||
$data[] = [
|
||
'code' => sprintf("%'.08d", $codeStart++),
|
||
'pass' => sprintf("%'.04d", mt_rand(0, 9999)) . '-' . sprintf("%'.04d", mt_rand(0, 9999)) . '-' . sprintf("%'.04d", mt_rand(0, 9999)) . '-' . sprintf("%'.04d", mt_rand(0, 9999)),
|
||
'type' => $type,
|
||
'status' => 1,
|
||
'source' => $source,
|
||
'created_at' => now(),
|
||
'updated_at' => now(),
|
||
];
|
||
--$num;
|
||
}
|
||
Card::insert($data);
|
||
admin_success('成功生成', $request->num . '个码已经生成。');
|
||
return back();
|
||
}
|
||
|
||
public function getGoods()
|
||
{
|
||
$type = request('q');
|
||
$data = [];
|
||
if ($type == 'K') {
|
||
$plucks = Goods::where('status', 1)->get(['id', 'title as text']);
|
||
return $plucks;
|
||
} else {
|
||
$data[] = [
|
||
'id' => 0,
|
||
'text' => '非专属卡',
|
||
];
|
||
return $data;
|
||
}
|
||
}
|
||
|
||
public function getParam()
|
||
{
|
||
$goods_id = request('q');
|
||
if ($goods_id) {
|
||
$plucks = GoodsParams::where('goods_id', $goods_id)->where('status', 1)->get(['id', 'value as text']);
|
||
return $plucks;
|
||
} else {
|
||
$data[] = [
|
||
'id' => 0,
|
||
'text' => '非专属卡',
|
||
];
|
||
return $data;
|
||
}
|
||
}
|
||
}
|