StoreMenu.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace addons\Store\common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%addons_store_menu}}".
  6. *
  7. * @property int $id
  8. * @property string $title 标题
  9. * @property string $app_id 应用id
  10. * @property int|null $pid 上级id
  11. * @property int|null $is_addon 是否插件[0=否,1=是]
  12. * @property string|null $addons_name 插件标识
  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 StoreMenu extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_store_menu}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['pid', 'is_addon', 'level', 'dev', 'sort', 'status', 'created_at', 'updated_at'], 'integer'],
  40. [['params'], 'safe'],
  41. [['title', 'icon', 'addons_name'], '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. 'is_addon' => '是否插件[0=否,1=是]',
  58. 'addons_name' => '插件标识',
  59. 'url' => '路由',
  60. 'icon' => '样式',
  61. 'level' => '级别',
  62. 'dev' => '开发者[0:都可见;开发模式可见]',
  63. 'sort' => '排序',
  64. 'params' => '参数',
  65. 'tree' => '树',
  66. 'status' => '状态[-1:删除;0:禁用;1启用]',
  67. 'created_at' => '添加时间',
  68. 'updated_at' => '修改时间',
  69. ];
  70. }
  71. //关联模型
  72. public function getChild()
  73. {
  74. return $this->hasMany(self::class, ['pid' => 'id'])->where(['<>', 'status', StatusEnum::DELETE])->orderBy('sort asc,id asc');
  75. }
  76. /**
  77. *
  78. * @Author bing
  79. * @DateTime 2021-08-26 10:56:02
  80. * @param integer $cate_id
  81. * @param integer $pid
  82. * @param string $app_id
  83. * @return array|null
  84. * @copyright: Copyright (c) 2020 广东七件事集团
  85. */
  86. public static function getMenus(array $cond, array $with = [], bool $is_array = true, string $select = '*')
  87. {
  88. $query = self::find()->select($select);
  89. self::paramPack(['where' => $cond, 'with' => $with, 'order' => 'sort asc, id asc'], $query);
  90. $menu = $query->asArray($is_array)->all();
  91. return $menu;
  92. }
  93. /**
  94. * 获取树状菜单
  95. * @Author bing
  96. * @DateTime 2021-08-10 18:22:16
  97. * @param array $menu 菜单
  98. * @param integer $parentId
  99. * @param string $app_id APP_ID
  100. * @return array
  101. * @copyright: Copyright (c) 2020 广东七件事集团
  102. */
  103. public static function getTreeMenu(array $menu, int $pid = 0, string $route = '',string $mall_sign = '')
  104. {
  105. $tree = [];
  106. foreach ($menu as $k => $v) {
  107. $v['value'] = $v['id'];
  108. $v['label'] = $v['title'];
  109. $v['mall_sign'] = $mall_sign;
  110. if ($v['pid'] == $pid) {
  111. $tmp = $v;
  112. // 当前路由的菜单被选中
  113. if ($route == $v['url']) $tmp['is_active'] = true;
  114. $children = self::getTreeMenu($menu, $v['id'], $route,$mall_sign);
  115. $children && $tmp['children'] = $children;
  116. //判断父级菜单是否被选中
  117. if (isset($tmp['children']) && !empty($tmp['children'])) {
  118. foreach ($tmp['children'] as $child) {
  119. if (isset($child['is_active']) && $child['is_active'] == true) {
  120. $tmp['is_active'] = true;
  121. }
  122. }
  123. }
  124. $tree[] = $tmp;
  125. }
  126. }
  127. return $tree;
  128. }
  129. /**
  130. * 冻结账户权限
  131. * @Author bing
  132. * @DateTime 2021-08-10 18:22:16
  133. * @param array $menu 菜单
  134. * @param integer $parentId
  135. * @param string $app_id APP_ID
  136. * @return array
  137. * @copyright: Copyright (c) 2020 广东七件事集团
  138. */
  139. public static function getFreezeMenu(array $menus,int $store_id)
  140. {
  141. $freeze_url_arr = [
  142. 'store/client/index/index',
  143. 'store/client/cash/index',
  144. 'store/client/finance/index',
  145. ];
  146. $store = Store::findOne(['id'=>$store_id]);
  147. if(!empty($store['is_freeze'])){
  148. $newMenus = [];
  149. foreach($menus as $item){
  150. if(in_array($item['url'],$freeze_url_arr)){
  151. $newMenus[] = $item;
  152. }
  153. }
  154. }else{
  155. $newMenus = $menus;
  156. }
  157. return $newMenus;
  158. }
  159. }