76 lines
2.0 KiB
PHP
76 lines
2.0 KiB
PHP
<?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;
|
|
}
|
|
|
|
}
|
|
}
|