MenuCateService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use common\enums\StatusEnum;
  6. use common\enums\WhetherEnum;
  7. use common\enums\AddonTypeEnum;
  8. use common\helpers\ArrayHelper;
  9. use common\helpers\Auth;
  10. use common\components\Service;
  11. use common\models\common\MenuCate;
  12. /**
  13. * Class MenuCateService
  14. * @package services\common
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class MenuCateService extends Service
  18. {
  19. /**
  20. * @param $appId
  21. * @param array $info
  22. * @param $icon
  23. * @param $location
  24. * @param $sort
  25. * @param $pattern
  26. * @return MenuCate
  27. * @throws \yii\web\UnprocessableEntityHttpException
  28. */
  29. public function createByAddon($appId, array $info, $icon, $location, $sort, $pattern = [])
  30. {
  31. MenuCate::deleteAll(['app_id' => $appId, 'addon_name' => $info['name']]);
  32. $model = new MenuCate();
  33. $model->app_id = $appId;
  34. $model->addon_name = $info['name'];
  35. $model->addon_location = $location;
  36. $model->is_addon = WhetherEnum::ENABLED;
  37. $model->title = $info['title'];
  38. $model->icon = $icon;
  39. $model->pattern = $pattern;
  40. $model->sort = $sort;
  41. if (!$model->save()) {
  42. $this->error($model);
  43. }
  44. return $model;
  45. }
  46. /**
  47. * 更新状态
  48. *
  49. * @param $addon_name
  50. * @return void
  51. */
  52. public function updateStatusByAddonName($addon_name, $status)
  53. {
  54. MenuCate::updateAll(['status' => $status], ['addon_name' => $addon_name, 'is_addon' => StatusEnum::ENABLED]);
  55. }
  56. /**
  57. * @param string $addon_name 插件名称
  58. */
  59. public function delByAddonName($addon_name)
  60. {
  61. MenuCate::deleteAll(['addon_name' => $addon_name]);
  62. }
  63. /**
  64. * 编辑 - 获取正常分类Map列表
  65. *
  66. * @return array
  67. */
  68. public function getDefaultMap($app_id)
  69. {
  70. return ArrayHelper::map($this->findDefault($app_id), 'id', 'title');
  71. }
  72. /**
  73. * @param $app_id
  74. * @return array|\yii\db\ActiveRecord[]
  75. * @throws \yii\web\UnauthorizedHttpException
  76. */
  77. public function findAllInAuth($app_id)
  78. {
  79. $models = $this->findAll($app_id);
  80. foreach ($models as $key => $model) {
  81. if ($model['addon_location'] === AddonTypeEnum::ADDONS) {
  82. unset($models[$key]);
  83. continue;
  84. }
  85. // 验证开发模式
  86. if (!empty($model['pattern'])) {
  87. !is_array($model['pattern']) && $model['pattern'] = Json::decode($model['pattern']);
  88. if (
  89. !empty($model['pattern']) &&
  90. !in_array(Yii::$app->params['devPattern'], $model['pattern'])
  91. ) {
  92. unset($models[$key]);
  93. continue;
  94. }
  95. }
  96. if (
  97. $model['is_addon'] == WhetherEnum::DISABLED &&
  98. !Auth::verify('menuCate:' . $model['id'])
  99. ) {
  100. unset($models[$key]);
  101. continue;
  102. }
  103. if (
  104. $model['is_addon'] == WhetherEnum::ENABLED &&
  105. !Auth::verify($model['addon_name'])
  106. ) {
  107. unset($models[$key]);
  108. }
  109. }
  110. return $models;
  111. }
  112. /**
  113. * @param $id
  114. * @return MenuCate|null
  115. */
  116. public function findById($id)
  117. {
  118. return MenuCate::findOne($id);
  119. }
  120. /**
  121. * 查询 - 获取全部分类
  122. *
  123. * @return array|\yii\db\ActiveRecord[]
  124. */
  125. public function findAll($app_id)
  126. {
  127. return MenuCate::find()
  128. ->where(['status' => StatusEnum::ENABLED])
  129. ->andWhere(['app_id' => $app_id])
  130. ->orderBy('sort asc, id asc')
  131. ->asArray()
  132. ->all();
  133. }
  134. /**
  135. * 编辑
  136. *
  137. * 获取正常的分类
  138. *
  139. * @param $app_id
  140. * @return array|\yii\db\ActiveRecord[]
  141. */
  142. public function findDefault($app_id)
  143. {
  144. $list = MenuCate::find()
  145. ->where([
  146. 'type' => StatusEnum::DISABLED,
  147. 'app_id' => $app_id
  148. ])
  149. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  150. ->andWhere(['in', 'addon_location', ['', AddonTypeEnum::DEFAULT]])
  151. ->orderBy('sort asc, id asc')
  152. ->asArray()
  153. ->all();
  154. // foreach ($list as $key => $item) {
  155. // if (in_array(DevPatternEnum::BLANK, Json::decode($item['pattern']))) {
  156. // unset($list[$key]);
  157. // }
  158. // }
  159. return array_merge($list);
  160. }
  161. /**
  162. * 获取首个显示的分类
  163. *
  164. * @return false|null|string
  165. */
  166. public function findAddon($app_id)
  167. {
  168. return MenuCate::find()
  169. ->where(['status' => StatusEnum::ENABLED, 'type' => StatusEnum::ENABLED])
  170. ->andWhere(['app_id' => $app_id])
  171. ->orderBy('sort asc, id asc')
  172. ->select(['id'])
  173. ->scalar();
  174. }
  175. /**
  176. * 获取首个显示的分类
  177. *
  178. * @return false|null|string
  179. */
  180. public function findFirstId($app_id)
  181. {
  182. return MenuCate::find()
  183. ->where(['status' => StatusEnum::ENABLED])
  184. ->andWhere(['app_id' => $app_id])
  185. ->andWhere(['in', 'addon_location', ['', AddonTypeEnum::DEFAULT]])
  186. ->orderBy('sort asc, id asc')
  187. ->select(['id'])
  188. ->scalar();
  189. }
  190. }
粤ICP备19079148号