更新分组密码
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化商品信息
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user