修复分类锁的bug
This commit is contained in:
@@ -92,13 +92,26 @@ class GoodsGroupController extends AdminController
|
||||
{
|
||||
return Form::make(new GoodsGroup(), function (Form $form) {
|
||||
$form->display('id');
|
||||
$form->text('gp_name');
|
||||
$form->switch('is_open')->default(GoodsGroupModel::STATUS_OPEN);
|
||||
$form->switch('is_open_group_pwd')->default(GoodsGroupModel::STATUS_CLOSE);
|
||||
$form->text('group_pwd')->help(admin_trans('goods-group.fields.group_pwd_help'));
|
||||
$form->number('ord')->default(1)->help(admin_trans('dujiaoka.ord'));
|
||||
$form->text('gp_name', admin_trans('goods-group.fields.gp_name'));
|
||||
$form->switch('is_open', admin_trans('goods-group.fields.is_open'))->default(GoodsGroupModel::STATUS_OPEN);
|
||||
|
||||
$form->divider('分类密码访问');
|
||||
$form->switch('is_open_group_pwd', admin_trans('goods-group.fields.is_open_group_pwd'))->default(GoodsGroupModel::STATUS_CLOSE);
|
||||
$form->text('group_pwd', admin_trans('goods-group.fields.group_pwd'))
|
||||
->help(admin_trans('goods-group.fields.group_pwd_help'));
|
||||
|
||||
$form->number('ord', admin_trans('goods-group.fields.ord'))->default(1)->help(admin_trans('dujiaoka.ord'));
|
||||
$form->display('created_at');
|
||||
$form->display('updated_at');
|
||||
|
||||
$form->saving(function (Form $form) {
|
||||
if ((int) $form->is_open_group_pwd !== GoodsGroupModel::STATUS_OPEN) {
|
||||
$form->group_pwd = null;
|
||||
} elseif ($form->isEditing() && $form->group_pwd === null) {
|
||||
$form->deleteInput('group_pwd');
|
||||
}
|
||||
});
|
||||
|
||||
$form->disableViewButton();
|
||||
$form->footer(function ($footer) {
|
||||
// 去掉`查看`checkbox
|
||||
|
||||
@@ -46,6 +46,9 @@ class HomeController extends BaseController
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
// 每次打开/刷新首页都重新要求输入分类访问密码
|
||||
session()->forget('dujiaoka_group_pwd_access');
|
||||
|
||||
$goods = $this->goodsService->withGroup();
|
||||
return $this->render('static_pages/home', ['data' => $goods], __('dujiaoka.page-title.home'));
|
||||
}
|
||||
@@ -97,13 +100,34 @@ class HomeController extends BaseController
|
||||
'group_id' => 'required|integer',
|
||||
'password' => 'required|string',
|
||||
]);
|
||||
$this->goodsService->verifyGroupPassword((int) $request->input('group_id'), (string) $request->input('password'));
|
||||
return response()->json(['code' => 200, 'msg' => __('dujiaoka.prompt.goods_group_password_success')]);
|
||||
$group = $this->goodsService->verifyGroupPassword((int) $request->input('group_id'), (string) $request->input('password'));
|
||||
return response()->json([
|
||||
'code' => 200,
|
||||
'msg' => __('dujiaoka.prompt.goods_group_password_success'),
|
||||
'data' => $group,
|
||||
]);
|
||||
} catch (RuleValidationException $ruleValidationException) {
|
||||
return response()->json(['code' => 400, 'msg' => $ruleValidationException->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消商品分类密码访问授权
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function forgetGroupPasswordAccess(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'group_id' => 'required|integer',
|
||||
]);
|
||||
|
||||
$this->goodsService->forgetGroupAccess((int) $request->input('group_id'));
|
||||
|
||||
return response()->json(['code' => 200, 'msg' => 'ok']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 极验行为验证
|
||||
*
|
||||
|
||||
@@ -17,10 +17,6 @@ class GoodsGroup extends BaseModel
|
||||
'deleted' => GoodsGroupDeleted::class
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'group_pwd',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_open_group_pwd' => 'integer',
|
||||
];
|
||||
|
||||
@@ -112,11 +112,16 @@ class GoodsService
|
||||
*
|
||||
* @param int $groupID 分类id
|
||||
* @param string $password 访问密码
|
||||
* @return bool
|
||||
* @return array
|
||||
*/
|
||||
public function verifyGroupPassword(int $groupID, string $password): bool
|
||||
public function verifyGroupPassword(int $groupID, string $password): array
|
||||
{
|
||||
$group = GoodsGroup::query()
|
||||
->with(['goods' => function($query) {
|
||||
$query->withCount(['carmis' => function($query) {
|
||||
$query->where('status', Carmis::STATUS_UNSOLD);
|
||||
}])->where('is_open', Goods::STATUS_OPEN)->orderBy('ord', 'DESC');
|
||||
}])
|
||||
->where('is_open', GoodsGroup::STATUS_OPEN)
|
||||
->where('id', $groupID)
|
||||
->first();
|
||||
@@ -127,7 +132,7 @@ class GoodsService
|
||||
|
||||
if (!$this->isGroupPasswordProtected($group)) {
|
||||
$this->grantGroupAccess($group->id);
|
||||
return true;
|
||||
return $this->formatGroupForResponse($group);
|
||||
}
|
||||
|
||||
if (!hash_equals((string) $group->group_pwd, (string) $password)) {
|
||||
@@ -135,7 +140,24 @@ class GoodsService
|
||||
}
|
||||
|
||||
$this->grantGroupAccess($group->id);
|
||||
return true;
|
||||
return $this->formatGroupForResponse($group);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化分类接口返回数据
|
||||
*
|
||||
* @param GoodsGroup $group 分类模型
|
||||
* @return array
|
||||
*/
|
||||
private function formatGroupForResponse(GoodsGroup $group): array
|
||||
{
|
||||
$data = $group->toArray();
|
||||
foreach ($data['goods'] as &$goods) {
|
||||
$goods['picture_url'] = picture_ulr($goods['picture']);
|
||||
}
|
||||
unset($goods);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,6 +174,22 @@ class GoodsService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消当前会话访问指定分类的授权
|
||||
*
|
||||
* @param int $groupID 分类id
|
||||
* @return void
|
||||
*/
|
||||
public function forgetGroupAccess(int $groupID): void
|
||||
{
|
||||
$groupIDs = session('dujiaoka_group_pwd_access', []);
|
||||
$groupIDs = array_values(array_filter($groupIDs, function ($id) use ($groupID) {
|
||||
return (int) $id !== $groupID;
|
||||
}));
|
||||
|
||||
session(['dujiaoka_group_pwd_access' => $groupIDs]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权当前会话访问分类
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user