From b2cd493af4dc1a278bc81b4c9a88560f30bd9537 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Thu, 6 Jan 2022 11:05:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=BF=90=E8=B4=B9=E8=A7=84?= =?UTF-8?q?=E5=88=99=E5=9F=8E=E5=B8=82=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ShippingRuleController.php | 15 ++++---- app/Admin/Renderable/ShippingRuleTable.php | 23 +++++++------ app/Models/ShippingRule.php | 15 ++++---- app/Services/Push/UniPushService.php | 9 +++-- app/Services/ShippingService.php | 7 ++-- ...4030_add_zones_to_shipping_rules_table.php | 34 +++++++++++++++++++ 6 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 database/migrations/2022_01_06_094030_add_zones_to_shipping_rules_table.php diff --git a/app/Admin/Controllers/ShippingRuleController.php b/app/Admin/Controllers/ShippingRuleController.php index 7cc09169..b556a36c 100644 --- a/app/Admin/Controllers/ShippingRuleController.php +++ b/app/Admin/Controllers/ShippingRuleController.php @@ -52,7 +52,8 @@ class ShippingRuleController extends AdminController */ protected function form() { - $builder = ShippingRule::with('zones'); + // $builder = ShippingRule::with('zones'); + $builder = new ShippingRule(); return Form::make($builder, function (Form $form) { $templateId = Request::get('template_id', 0); $form->display('id'); @@ -66,12 +67,12 @@ class ShippingRuleController extends AdminController $form->tree('zones') ->nodes(Zone::get()->toArray()) // 设置所有节点 ->setTitleColumn('name') - ->customFormat(function ($v) { - if (!$v) { - return []; - } - return array_column($v, 'id'); - }) + // ->customFormat(function ($v) { + // if (!$v) { + // return []; + // } + // return array_column($v, 'id'); + // }) ->expand(false)->required(); $form->radio('type') ->when(1, function (Form $form) { diff --git a/app/Admin/Renderable/ShippingRuleTable.php b/app/Admin/Renderable/ShippingRuleTable.php index ee179cce..3ef3e1df 100644 --- a/app/Admin/Renderable/ShippingRuleTable.php +++ b/app/Admin/Renderable/ShippingRuleTable.php @@ -11,9 +11,10 @@ class ShippingRuleTable extends Grid { public static function grid(int $templateId = null) { - $builder = ShippingRule::with('zones'); - - $grid = parent::make($builder, function (Grid $grid) { + // $builder = ShippingRule::with('zones'); + $builder = new ShippingRule(); + $zones = Zone::get()->toArray(); + $grid = parent::make($builder, function (Grid $grid) use ($zones) { $grid->setResource('shipping-rules'); $grid->column('id')->sortable(); $grid->column('type')->using([ @@ -29,13 +30,15 @@ class ShippingRuleTable extends Grid return '首重:'.$value['first_weight'].'KG内'.$value['first_w_amount'].'元,续重:每'.$value['continue_weight'].'KG'.$value['continue_w_amount'].'元'; } })->label(); - $grid->column('zones', __('shipping-rule.fields.zones'))->display(function ($value) { - return $value->pluck('id')->toArray(); - })->showTreeInDialog(function (Grid\Displayers\DialogTree $tree) { - $tree->title('城市'); - $tree->nodes(Zone::get()->toArray()); - $tree->setTitleColumn('name'); - }); + $grid->column('zones', __('shipping-rule.fields.zones')) + // ->display(function ($value) { + // return $value->pluck('id')->toArray(); + // }) + ->showTreeInDialog(function (Grid\Displayers\DialogTree $tree) use ($zones) { + $tree->title('城市'); + $tree->nodes($zones); + $tree->setTitleColumn('name'); + }); $grid->column('created_at'); $grid->column('updated_at')->sortable(); diff --git a/app/Models/ShippingRule.php b/app/Models/ShippingRule.php index 00551b3e..a7d84652 100644 --- a/app/Models/ShippingRule.php +++ b/app/Models/ShippingRule.php @@ -15,12 +15,13 @@ class ShippingRule extends Model public const TYPE_FREE = 1;//包邮 public const TYPE_WEIGHT = 2;//计算重量 - // protected $casts = [ - // 'info' => JsonArray::class, - // ]; + protected $casts = [ + // 'info' => JsonArray::class, + 'zones' => JsonArray::class, + ]; - public function zones() - { - return $this->belongsToMany(Zone::class, 'shipping_rule_zones', 'rule_id', 'zone_id')->withTimestamps(); - } + // public function zones() + // { + // return $this->belongsToMany(Zone::class, 'shipping_rule_zones', 'rule_id', 'zone_id')->withTimestamps(); + // } } diff --git a/app/Services/Push/UniPushService.php b/app/Services/Push/UniPushService.php index 96d75bdc..c5eb5170 100644 --- a/app/Services/Push/UniPushService.php +++ b/app/Services/Push/UniPushService.php @@ -192,7 +192,10 @@ class UniPushService protected function createPushPayloadData($title, $body, $params = []) { - $payload = json_encode($params); + $payload = json_encode(array_merge($params, [ + 'title' => $title, + 'body' => $body, + ])); return [ 'settings'=>[ 'ttl'=> 3600000, @@ -205,7 +208,7 @@ class UniPushService 'transmission'=> json_encode([ // 'title' => $title, // 'content'=> $body, - 'payload'=> $title, + 'payload'=> $payload, ]), // 'notification'=>[ // 'title' => $title, @@ -220,7 +223,7 @@ class UniPushService 'transmission'=> json_encode([ // 'title' => $title, // 'content'=> $body, - 'payload'=> $title, + 'payload'=> $payload, ]), // 'notification'=>[ // 'title' => $title, diff --git a/app/Services/ShippingService.php b/app/Services/ShippingService.php index 5b1fa660..d6e5a202 100644 --- a/app/Services/ShippingService.php +++ b/app/Services/ShippingService.php @@ -27,9 +27,10 @@ class ShippingService if (ShippingRule::where('template_id', $templateId)->count() > 0) { $canShipping = false; //只拿有这个地区配置的模板规则,减少sql查询市区数量 - $rules = ShippingRule::with('zones')->where('template_id', $templateId)->whereHas('zones', function ($q) use ($zoneId) { - return $q->where('zones.id', $zoneId); - })->get(); + // $rules = ShippingRule::with('zones')->where('template_id', $templateId)->whereHas('zones', function ($q) use ($zoneId) { + // return $q->where('zones.id', $zoneId); + // })->get(); + $rules = ShippingRule::where('template_id', $templateId)->where('zones', 'like', '%"'.$zoneId.'"%')->get(); //按包邮/计重 $rules = $rules->sortBy('type');//优先计算包邮 foreach ($rules as $rule) { diff --git a/database/migrations/2022_01_06_094030_add_zones_to_shipping_rules_table.php b/database/migrations/2022_01_06_094030_add_zones_to_shipping_rules_table.php new file mode 100644 index 00000000..6097f74e --- /dev/null +++ b/database/migrations/2022_01_06_094030_add_zones_to_shipping_rules_table.php @@ -0,0 +1,34 @@ +text('zones')->nullable()->comment('地区IDs'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('shipping_rules', function (Blueprint $table) { + // + $table->dropColumn(['zones']); + }); + } +}