更新代码
This commit is contained in:
75
app/Models/WechatMenu.php
Normal file
75
app/Models/WechatMenu.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class WechatMenu extends Model
|
||||
{
|
||||
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(WechatMenu::class, 'parent_id')->orderBy('sort', 'asc');
|
||||
}
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(WechatMenu::class)->withDefault();
|
||||
}
|
||||
|
||||
public static function getPublishArray()
|
||||
{
|
||||
$buttons = [];
|
||||
|
||||
$menus = self::where('parent_id', 0)->orderBy('sort', 'asc')->get();
|
||||
|
||||
foreach ($menus as $key => $menu) {
|
||||
$button['name'] = $menu->name;
|
||||
if ($menu->children->isEmpty()) {
|
||||
$button = array_merge($button, $menu->type_value);
|
||||
} else {
|
||||
foreach ($menu->children as $k => $child) {
|
||||
$subButton['name'] = $child->name;
|
||||
$subButton = array_merge($subButton, $child->type_value);
|
||||
$button['sub_button'][$k] = $subButton;
|
||||
$subButton = [];
|
||||
}
|
||||
}
|
||||
$buttons[] = $button;
|
||||
$button = [];
|
||||
}
|
||||
|
||||
return $buttons;
|
||||
}
|
||||
|
||||
public function getTypeValueAttribute()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case 'click':
|
||||
return [
|
||||
'type' => $this->type,
|
||||
'key' => $this->value,
|
||||
];
|
||||
break;
|
||||
case 'view':
|
||||
return [
|
||||
'type' => $this->type,
|
||||
'url' => $this->value,
|
||||
];
|
||||
break;
|
||||
case 'miniprogram':
|
||||
return [
|
||||
'type' => $this->type,
|
||||
'url' => explode($this->value)[0],
|
||||
'appid' => explode($this->value)[1],
|
||||
'pagepath' => explode($this->value)[2],
|
||||
];
|
||||
break;
|
||||
default:
|
||||
return [
|
||||
'type' => 'view',
|
||||
'url' => $this->value,
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user