MenuCateService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use common\helpers\Auth;
  5. use common\helpers\ArrayHelper;
  6. use common\enums\StatusEnum;
  7. use common\components\Service;
  8. use common\enums\WhetherEnum;
  9. use common\models\backend\MenuCate;
  10. /**
  11. * Class MenuCateService
  12. * @package services\common
  13. * @author qimall
  14. */
  15. class MenuCateService extends Service
  16. {
  17. /**
  18. * @param string $addons_name 插件名称
  19. */
  20. public function delByAddonsName($addons_name)
  21. {
  22. MenuCate::deleteAll(['addons_name' => $addons_name]);
  23. }
  24. /**
  25. * @param $title
  26. * @param $icon
  27. * @return MenuCate
  28. */
  29. public function createByAddons($appId, array $info, $icon)
  30. {
  31. MenuCate::deleteAll(['app_id' => $appId, 'addons_name' => $info['name']]);
  32. $model = new MenuCate();
  33. $model->app_id = $appId;
  34. $model->addons_name = $info['name'];
  35. $model->is_addon = WhetherEnum::ENABLED;
  36. $model->title = $info['title'];
  37. $model->icon = $icon;
  38. $model->save();
  39. return $model;
  40. }
  41. /**
  42. * 查询 - 获取授权成功的全部分类
  43. *
  44. * @return array|\yii\db\ActiveRecord[]
  45. */
  46. public function getOnAuthList()
  47. {
  48. $models = $this->findAll();
  49. foreach ($models as $key => $model) {
  50. if ($model['is_addon'] == WhetherEnum::DISABLED && !Auth::verify('cate:' . $model['id'])) {
  51. unset($models[$key]);
  52. }
  53. if ($model['is_addon'] == WhetherEnum::ENABLED && !Auth::verify($model['addons_name'])) {
  54. unset($models[$key]);
  55. }
  56. }
  57. return $models;
  58. }
  59. /**
  60. * 编辑 - 获取正常分类Map列表
  61. *
  62. * @return array
  63. */
  64. public function getDefaultMap($app_id)
  65. {
  66. return ArrayHelper::map($this->findDefault($app_id), 'id', 'title');
  67. }
  68. /**
  69. * 编辑 - 获取正常的分类
  70. *
  71. * @return array|\yii\db\ActiveRecord[]
  72. */
  73. public function findDefault($app_id)
  74. {
  75. return MenuCate::find()
  76. ->where(['addon_centre' => StatusEnum::DISABLED])
  77. ->andWhere(['app_id' => $app_id])
  78. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  79. ->orderBy('sort asc')
  80. ->asArray()
  81. ->all();
  82. }
  83. /**
  84. * 查询 - 获取全部分类
  85. *
  86. * @return array|\yii\db\ActiveRecord[]
  87. */
  88. public function findAll()
  89. {
  90. return MenuCate::find()
  91. ->where(['status' => StatusEnum::ENABLED])
  92. ->andWhere(['app_id' => Yii::$app->id])
  93. ->orderBy('sort asc, id asc')
  94. ->asArray()
  95. ->all();
  96. }
  97. /**
  98. * @param $id
  99. * @return MenuCate|null
  100. */
  101. public function findById($id)
  102. {
  103. return MenuCate::findOne($id);
  104. }
  105. /**
  106. * 获取首个显示的分类
  107. *
  108. * @return false|null|string
  109. */
  110. public function findFirstId($app_id)
  111. {
  112. return MenuCate::find()
  113. ->where(['status' => StatusEnum::ENABLED])
  114. ->andWhere(['app_id' => $app_id])
  115. ->orderBy('sort asc')
  116. ->select(['id'])
  117. ->scalar();
  118. }
  119. }