diff --git a/app/Admin/Controllers/GoodsGroupController.php b/app/Admin/Controllers/GoodsGroupController.php index b17303d..5c2abef 100644 --- a/app/Admin/Controllers/GoodsGroupController.php +++ b/app/Admin/Controllers/GoodsGroupController.php @@ -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 diff --git a/app/Http/Controllers/Home/HomeController.php b/app/Http/Controllers/Home/HomeController.php index baf8692..0a0bafa 100644 --- a/app/Http/Controllers/Home/HomeController.php +++ b/app/Http/Controllers/Home/HomeController.php @@ -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']); + } + /** * 极验行为验证 * diff --git a/app/Models/GoodsGroup.php b/app/Models/GoodsGroup.php index 1a4a831..6c054f6 100644 --- a/app/Models/GoodsGroup.php +++ b/app/Models/GoodsGroup.php @@ -17,10 +17,6 @@ class GoodsGroup extends BaseModel 'deleted' => GoodsGroupDeleted::class ]; - protected $hidden = [ - 'group_pwd', - ]; - protected $casts = [ 'is_open_group_pwd' => 'integer', ]; diff --git a/app/Service/GoodsService.php b/app/Service/GoodsService.php index 649cb82..e1074c3 100644 --- a/app/Service/GoodsService.php +++ b/app/Service/GoodsService.php @@ -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]); + } + /** * 授权当前会话访问分类 * diff --git a/public/assets/luna/main.js b/public/assets/luna/main.js index 97ae5ed..31cc666 100644 --- a/public/assets/luna/main.js +++ b/public/assets/luna/main.js @@ -64,7 +64,15 @@ }, function (res) { if (res.code === 200) { layer.close(index); - window.location.reload(); + + res.data.key = group.key; + res.data.is_group_locked = 0; + goodsMsg[group.key] = res.data; + + laytpl(cateTpl).render(res.data, function (html) { + $('.cate-box').eq(group.key).replaceWith(html); + }); + changeCate(group.key, true); } else { layer.msg(res.msg); } diff --git a/resources/views/hyper/errors/error.blade.php b/resources/views/hyper/errors/error.blade.php index 9e18d11..fe9a64f 100644 --- a/resources/views/hyper/errors/error.blade.php +++ b/resources/views/hyper/errors/error.blade.php @@ -14,11 +14,11 @@
' + goods.gd_name + '
'; + html += '