170 lines
5.6 KiB
PHP
170 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\{Vip, Coupon};
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use EasyWeChat\Factory;
|
|
use EasyWeChat\Kernel\Http\StreamResponse;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class VipController extends AdminController
|
|
{
|
|
/**
|
|
* 维信小程序码
|
|
*/
|
|
public function mpQrcode()
|
|
{
|
|
$disk = Storage::disk('public');
|
|
|
|
$directory = '';
|
|
$filename = 'buy_vip_qrcode.png';
|
|
$filepath = ($directory ? "{$directory}/" : '').$filename;
|
|
|
|
if (! $disk->exists($filepath)) {
|
|
$scene = http_build_query(['vip' => 1]);
|
|
|
|
$app = Factory::miniProgram(config('wechat.mini_program.default'));
|
|
|
|
$response = $app->app_code->getUnlimit($scene, [
|
|
'page' => 'pages/welcome/index',
|
|
'check_path' => false,
|
|
'env_version' => app()->isProduction() ? 'release' : 'trial',
|
|
'width' => 200,
|
|
]);
|
|
|
|
if (! $response instanceof StreamResponse) {
|
|
throw new BizException('小程序码获取失败');
|
|
}
|
|
|
|
$response->saveAs($disk->path($directory), $filename);
|
|
}
|
|
|
|
return $disk->download($filepath);
|
|
}
|
|
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Vip(), function (Grid $grid) {
|
|
|
|
$grid->column('id');
|
|
$grid->column('name');
|
|
$grid->column('times')->display(function ($v) {
|
|
return data_get($v, 'text');
|
|
});
|
|
$grid->column('price');
|
|
$grid->column('points')->display(fn() => data_get($this->gift, 'points', 0));
|
|
$grid->column('sort')->editable();
|
|
$grid->column('status')->switch();
|
|
|
|
$grid->tools(function (Grid\Tools $tools) {
|
|
if (Admin::user()->can('dcat.admin.vip.mp_qrcode')) {
|
|
$tools->append('<a class="btn btn-primary btn-outline" href="'.route('dcat.admin.vip.mp_qrcode').'" target="_blank"><i class="feather grid-action-icon icon-download"></i> 下载小程序码</a>');
|
|
}
|
|
});
|
|
|
|
$grid->disableViewButton(false);
|
|
//新增
|
|
$grid->showCreateButton(Admin::user()->can('dcat.admin.vip.create'));
|
|
//修改
|
|
$grid->showEditButton(Admin::user()->can('dcat.admin.vip.edit'));
|
|
$grid->showDeleteButton(Admin::user()->can('dcat.admin.vip.destroy'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Vip(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('name');
|
|
$show->field('times')->as(function ($v) {
|
|
return data_get($v, 'text');
|
|
});
|
|
$show->filed('price');
|
|
$show->filed('sort');
|
|
$show->field('status');
|
|
$show->field('description');
|
|
$show->field('gift')->view('admin.vip.gift');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
|
|
$show->disableEditButton();
|
|
$show->disableDeleteButton();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Vip(), function (Form $form) {
|
|
$form->display('id');
|
|
$form->text('name')->required();
|
|
$form->embeds('times', function ($form) {
|
|
$form->number('num', '数量')->min(0)->required();
|
|
$form->select('unit', '单位')->options(Vip::$timeMap)->required();
|
|
$form->text('text', '描述')->required();
|
|
});
|
|
$form->number('price')->min(0);
|
|
$form->number('sort')->min(0);
|
|
$form->switch('status')->default(1);
|
|
$form->textarea('description');
|
|
|
|
$form->number('points')
|
|
->min(0)
|
|
->customFormat(fn () => data_get($this->gift, 'points'));
|
|
$form->table('coupon', function ($table) {
|
|
$table->select('id', '优惠券')->options(Coupon::class)->ajax('api/coupons');
|
|
$table->number('amount', '数量')->min(1)->default(1);
|
|
})->customFormat(fn() => data_get($this->gift, 'coupon'));
|
|
|
|
$form->hidden('gift')->customFormat(fn($v) => json_encode($v));
|
|
|
|
$form->disableCreatingCheck();
|
|
$form->disableEditingCheck();
|
|
$form->disableDeleteButton();
|
|
|
|
$form->saving(function (Form $form) {
|
|
$gift = $form->model()?->gift ?: [];
|
|
|
|
$points = str_replace(',', '', $form->input('points'));
|
|
if ($points > 0) {
|
|
$gift['points'] = $points;
|
|
} else {
|
|
unset($gift['points']);
|
|
}
|
|
|
|
if ($form->coupon) {
|
|
// [id, amount, name]
|
|
$couponList = [];
|
|
foreach($form->coupon as $item) {
|
|
if (!data_get($item, '_remove_') && $item['amount'] > 0) {
|
|
$coupon = Coupon::findOrFail($item['id']);
|
|
array_push($couponList, ['id' => $coupon->id, 'amount' => $item['amount'], 'name' => $coupon->name]);
|
|
}
|
|
}
|
|
$gift['coupon'] = $couponList;
|
|
}
|
|
|
|
$form->gift = $gift;
|
|
$form->deleteInput(['points', 'coupon']);
|
|
});
|
|
});
|
|
}
|
|
}
|