Menu.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller\xmwechat\offiaccount;
  3. use app\common\controller\Backend;
  4. use app\common\library\xmwechat\offiaccount\MenuService;
  5. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  6. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  7. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  9. class Menu extends Backend
  10. {
  11. protected array $menuType = ['article_id', 'media_id', 'click', 'view', 'miniprogram'];
  12. public function initialize(): void
  13. {
  14. parent::initialize();
  15. }
  16. /**
  17. * 获取菜单
  18. * @return void
  19. * @author jsjxsz
  20. */
  21. public function getMenu(): void
  22. {
  23. try {
  24. $data = MenuService::getInstance()->getMenu();
  25. $this->success('success', $data);
  26. } catch (ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface|TransportExceptionInterface $e) {
  27. $this->error($e->getMessage());
  28. }
  29. }
  30. /**
  31. * 保存并发布菜单
  32. * @return void
  33. * @author jsjxsz
  34. */
  35. public function saveMenu(): void
  36. {
  37. $params = $this->request->param();
  38. $result = false;
  39. try {
  40. $this->checkMenu($params['button']);
  41. $result = MenuService::getInstance()->saveAndPublish($params);
  42. } catch (\Exception $e) {
  43. $this->error($e->getMessage());
  44. }
  45. if ($result) {
  46. $this->success('发布成功');
  47. } else {
  48. $this->error('发布失败');
  49. }
  50. }
  51. /**
  52. * 一级菜单校验
  53. * @param $menuParams
  54. * @return void
  55. * @author jsjxsz
  56. */
  57. public function checkMenu($menuParams): void
  58. {
  59. if (empty($menuParams) || !is_array($menuParams)) {
  60. $this->result(0, "请正确设置菜单格式");
  61. }
  62. if (count($menuParams) > 3) {
  63. $this->result(0, "一级菜单最多设置3个");
  64. }
  65. foreach ($menuParams as $item) {
  66. if (!is_array($item)) {
  67. $this->result(0, "一级菜单数据格式有误");
  68. }
  69. if (empty($item['name'])) {
  70. $this->result(0, "请输入一级菜单名称");
  71. }
  72. if (isset($item['sub_button'])) {
  73. if (!empty($item['sub_button'])) {
  74. $this->checkSubMenu($item['sub_button']);
  75. }
  76. } else {
  77. if (empty($item['type'])) {
  78. $this->result(0, "一级菜单未选择菜单类型");
  79. }
  80. if (!in_array($item['type'], $this->menuType)) {
  81. $this->result(0, "一级菜单类型错误");
  82. }
  83. $this->checkMenuType($item);
  84. }
  85. }
  86. }
  87. /**
  88. * 二级菜单校验
  89. * @param $subMenuParams
  90. * @return void
  91. * @author jsjxsz
  92. */
  93. public function checkSubMenu($subMenuParams): void
  94. {
  95. if (!is_array($subMenuParams)) {
  96. $this->result(0, "二级菜单数据格式有误");
  97. }
  98. if (count($subMenuParams) > 5) {
  99. $this->result(0, "二级菜单最多设置5个");
  100. }
  101. foreach ($subMenuParams as $subItem) {
  102. if (!is_array($subItem)) {
  103. $this->result(0, "二级菜单数据格式有误");
  104. }
  105. if (empty($subItem['name'])) {
  106. $this->result(0, "请输入二级菜单名称");
  107. }
  108. if (empty($subItem['type']) || !in_array($subItem['type'], $this->menuType)) {
  109. $this->result(0, "二级菜单类型有误");
  110. }
  111. $this->checkMenuType($subItem);
  112. }
  113. }
  114. /**
  115. * 检查菜单类型
  116. * @param $item
  117. * @return void
  118. * @author jsjxsz
  119. */
  120. public function checkMenuType($item): void
  121. {
  122. switch ($item['type']) {
  123. case 'click':
  124. if (empty($item['key'])) {
  125. $this->result(0, "菜单KEY不能为空");
  126. }
  127. break;
  128. case 'view':
  129. if (empty($item['url'])) {
  130. $this->result(0, "请输入网页链接");
  131. }
  132. break;
  133. case 'miniprogram':
  134. if (empty($item['url'])) {
  135. $this->result(0, "请输入网页链接");
  136. }
  137. if (empty($item['appid'])) {
  138. $this->result(0, "请输入appid");
  139. }
  140. if (empty($item['pagepath'])) {
  141. $this->result(0, "请输入小程序路径");
  142. }
  143. break;
  144. }
  145. }
  146. }