MchMenu.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace addons\Mch\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_mch_menu".
  8. *
  9. * @property int $id
  10. * @property string $title 标题
  11. * @property string $app_id 应用id
  12. * @property int|null $pid 上级id
  13. * @property string|null $url 路由
  14. * @property string|null $icon 样式
  15. * @property int|null $level 级别
  16. * @property int|null $dev 开发者[0:都可见;开发模式可见]
  17. * @property int|null $sort 排序
  18. * @property string|null $params 参数
  19. * @property string $tree 树
  20. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int|null $created_at 添加时间
  22. * @property int|null $updated_at 修改时间
  23. */
  24. class MchMenu extends BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_mch_menu}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['pid', 'level', 'dev', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['params'], 'safe'],
  41. [['title', 'icon'], 'string', 'max' => 50],
  42. [['app_id'], 'string', 'max' => 20],
  43. [['url'], 'string', 'max' => 100],
  44. [['tree'], 'string', 'max' => 300],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'title' => '标题',
  55. 'app_id' => '应用id',
  56. 'pid' => '上级id',
  57. 'url' => '路由',
  58. 'icon' => '样式',
  59. 'level' => '级别',
  60. 'dev' => '开发者[0:都可见;开发模式可见]',
  61. 'sort' => '排序',
  62. 'params' => '参数',
  63. 'tree' => '树',
  64. 'status' => '状态[-1:删除;0:禁用;1启用]',
  65. 'created_at' => '添加时间',
  66. 'updated_at' => '修改时间',
  67. ];
  68. }
  69. //关联模型
  70. public function getChild()
  71. {
  72. return $this->hasMany(self::class, ['pid' => 'id'])->where(['<>', 'status', StatusEnum::DELETE])->orderBy('sort asc,id asc');
  73. }
  74. /**
  75. *
  76. * @Author bing
  77. * @DateTime 2021-08-26 10:56:02
  78. * @param integer $cate_id
  79. * @param integer $pid
  80. * @param string $app_id
  81. * @return array|null
  82. * @copyright: Copyright (c) 2020 广东七件事集团
  83. */
  84. public static function getMenus(array $cond, array $with = [], bool $is_array = true, string $select = '*')
  85. {
  86. $query = self::find()->select($select);
  87. self::paramPack(['where' => $cond, 'with' => $with, 'order' => 'sort asc, id asc'], $query);
  88. $menu = $query->asArray($is_array)->all();
  89. return $menu;
  90. }
  91. /**
  92. * 获取树状菜单
  93. * @Author bing
  94. * @DateTime 2021-08-10 18:22:16
  95. * @param array $menu 菜单
  96. * @param integer $parentId
  97. * @param string $app_id APP_ID
  98. * @return array
  99. * @copyright: Copyright (c) 2020 广东七件事集团
  100. */
  101. public static function getTreeMenu(array $menu, int $pid = 0, string $route = '',string $mall_sign = '')
  102. {
  103. $tree = [];
  104. foreach ($menu as $k => $v) {
  105. $v['value'] = $v['id'];
  106. $v['label'] = $v['title'];
  107. $v['mall_sign'] = $mall_sign;
  108. if ($v['pid'] == $pid) {
  109. $tmp = $v;
  110. // 当前路由的菜单被选中
  111. if ($route == $v['url']) $tmp['is_active'] = true;
  112. $children = self::getTreeMenu($menu, $v['id'], $route,$mall_sign);
  113. $children && $tmp['children'] = $children;
  114. //判断父级菜单是否被选中
  115. if (isset($tmp['children']) && !empty($tmp['children'])) {
  116. foreach ($tmp['children'] as $child) {
  117. if (isset($child['is_active']) && $child['is_active'] == true) {
  118. $tmp['is_active'] = true;
  119. }
  120. }
  121. }
  122. $tree[] = $tmp;
  123. }
  124. }
  125. return $tree;
  126. }
  127. }