更新分组密码
This commit is contained in:
@@ -26,6 +26,7 @@ class GoodsGroupController extends AdminController
|
||||
$grid->column('id')->sortable();
|
||||
$grid->column('gp_name')->editable();
|
||||
$grid->column('is_open')->switch();
|
||||
$grid->column('is_open_group_pwd')->switch();
|
||||
$grid->column('ord')->editable();
|
||||
$grid->column('created_at');
|
||||
$grid->column('updated_at')->sortable();
|
||||
@@ -66,6 +67,16 @@ class GoodsGroupController extends AdminController
|
||||
return admin_trans('dujiaoka.status_close');
|
||||
}
|
||||
});
|
||||
$show->field('is_open_group_pwd')->as(function ($isOpenGroupPwd) {
|
||||
if ($isOpenGroupPwd == GoodsGroupModel::STATUS_OPEN) {
|
||||
return admin_trans('dujiaoka.status_open');
|
||||
} else {
|
||||
return admin_trans('dujiaoka.status_close');
|
||||
}
|
||||
});
|
||||
$show->field('group_pwd')->as(function ($groupPwd) {
|
||||
return empty($groupPwd) ? '' : '******';
|
||||
});
|
||||
$show->field('ord');
|
||||
$show->field('created_at');
|
||||
$show->field('updated_at');
|
||||
@@ -83,6 +94,8 @@ class GoodsGroupController extends AdminController
|
||||
$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->display('created_at');
|
||||
$form->display('updated_at');
|
||||
|
||||
@@ -65,6 +65,7 @@ class HomeController extends BaseController
|
||||
try {
|
||||
$goods = $this->goodsService->detail($id);
|
||||
$this->goodsService->validatorGoodsStatus($goods);
|
||||
$this->goodsService->validatorGoodsGroupAccess($goods);
|
||||
// 有没有优惠码可以展示
|
||||
if (count($goods->coupon)) {
|
||||
$goods->open_coupon = 1;
|
||||
@@ -83,6 +84,26 @@ class HomeController extends BaseController
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证商品分类访问密码
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function verifyGroupPassword(Request $request)
|
||||
{
|
||||
try {
|
||||
$request->validate([
|
||||
'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')]);
|
||||
} catch (RuleValidationException $ruleValidationException) {
|
||||
return response()->json(['code' => 400, 'msg' => $ruleValidationException->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 极验行为验证
|
||||
*
|
||||
|
||||
@@ -17,6 +17,14 @@ class GoodsGroup extends BaseModel
|
||||
'deleted' => GoodsGroupDeleted::class
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'group_pwd',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_open_group_pwd' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* 关联商品
|
||||
*
|
||||
|
||||
@@ -40,6 +40,7 @@ class GoodsService
|
||||
public function withGroup(): ?array
|
||||
{
|
||||
$goods = GoodsGroup::query()
|
||||
->select(['id', 'gp_name', 'is_open', 'is_open_group_pwd', 'group_pwd', 'ord', 'created_at', 'updated_at', 'deleted_at'])
|
||||
->with(['goods' => function($query) {
|
||||
$query->withCount(['carmis' => function($query) {
|
||||
$query->where('status', Carmis::STATUS_UNSOLD);
|
||||
@@ -48,6 +49,15 @@ class GoodsService
|
||||
->where('is_open', GoodsGroup::STATUS_OPEN)
|
||||
->orderBy('ord', 'DESC')
|
||||
->get();
|
||||
|
||||
$goods->each(function (GoodsGroup $group) {
|
||||
$isLocked = $this->isGroupPasswordProtected($group) && !$this->hasGroupAccess($group->id);
|
||||
$group->setAttribute('is_group_locked', $isLocked ? GoodsGroup::STATUS_OPEN : GoodsGroup::STATUS_CLOSE);
|
||||
if ($isLocked) {
|
||||
$group->setRelation('goods', collect());
|
||||
}
|
||||
});
|
||||
|
||||
// 将自动
|
||||
return $goods ? $goods->toArray() : null;
|
||||
}
|
||||
@@ -65,13 +75,98 @@ class GoodsService
|
||||
public function detail(int $id)
|
||||
{
|
||||
$goods = Goods::query()
|
||||
->with(['coupon'])
|
||||
->with(['coupon', 'group'])
|
||||
->withCount(['carmis' => function($query) {
|
||||
$query->where('status', Carmis::STATUS_UNSOLD);
|
||||
}])->where('id', $id)->first();
|
||||
return $goods;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分类是否开启密码访问
|
||||
*
|
||||
* @param GoodsGroup|null $group
|
||||
* @return bool
|
||||
*/
|
||||
public function isGroupPasswordProtected(?GoodsGroup $group): bool
|
||||
{
|
||||
return !empty($group)
|
||||
&& $group->is_open_group_pwd == GoodsGroup::STATUS_OPEN
|
||||
&& !empty($group->group_pwd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前会话是否已解锁分类
|
||||
*
|
||||
* @param int $groupID 分类id
|
||||
* @return bool
|
||||
*/
|
||||
public function hasGroupAccess(int $groupID): bool
|
||||
{
|
||||
$groupIDs = session('dujiaoka_group_pwd_access', []);
|
||||
return in_array($groupID, $groupIDs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证分类访问密码
|
||||
*
|
||||
* @param int $groupID 分类id
|
||||
* @param string $password 访问密码
|
||||
* @return bool
|
||||
*/
|
||||
public function verifyGroupPassword(int $groupID, string $password): bool
|
||||
{
|
||||
$group = GoodsGroup::query()
|
||||
->where('is_open', GoodsGroup::STATUS_OPEN)
|
||||
->where('id', $groupID)
|
||||
->first();
|
||||
|
||||
if (empty($group)) {
|
||||
throw new RuleValidationException(__('dujiaoka.prompt.goods_group_does_not_exist'));
|
||||
}
|
||||
|
||||
if (!$this->isGroupPasswordProtected($group)) {
|
||||
$this->grantGroupAccess($group->id);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!hash_equals((string) $group->group_pwd, (string) $password)) {
|
||||
throw new RuleValidationException(__('dujiaoka.prompt.goods_group_password_error'));
|
||||
}
|
||||
|
||||
$this->grantGroupAccess($group->id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证商品所属分类访问权限
|
||||
*
|
||||
* @param Goods $goods 商品模型
|
||||
* @return void
|
||||
*/
|
||||
public function validatorGoodsGroupAccess(Goods $goods): void
|
||||
{
|
||||
$group = $goods->group;
|
||||
if ($this->isGroupPasswordProtected($group) && !$this->hasGroupAccess($group->id)) {
|
||||
throw new RuleValidationException(__('dujiaoka.prompt.goods_group_password_required'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权当前会话访问分类
|
||||
*
|
||||
* @param int $groupID 分类id
|
||||
* @return void
|
||||
*/
|
||||
private function grantGroupAccess(int $groupID): void
|
||||
{
|
||||
$groupIDs = session('dujiaoka_group_pwd_access', []);
|
||||
if (!in_array($groupID, $groupIDs)) {
|
||||
$groupIDs[] = $groupID;
|
||||
session(['dujiaoka_group_pwd_access' => $groupIDs]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化商品信息
|
||||
*
|
||||
|
||||
@@ -106,6 +106,8 @@ class OrderService
|
||||
$goods = $this->goodsService->detail($request->input('gid'));
|
||||
// 商品状态验证
|
||||
$this->goodsService->validatorGoodsStatus($goods);
|
||||
// 商品所属分类访问权限验证
|
||||
$this->goodsService->validatorGoodsGroupAccess($goods);
|
||||
// 如果有限购
|
||||
if ($goods->buy_limit_num > 0 && $request->input('by_amount') > $goods->buy_limit_num) {
|
||||
throw new RuleValidationException(__('dujiaoka.prompt.purchase_limit_exceeded'));
|
||||
|
||||
Reference in New Issue
Block a user