OfficialAccountMenuLogic.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\channel;
  15. use app\common\enum\OfficialAccountEnum;
  16. use app\common\logic\BaseLogic;
  17. use app\common\service\ConfigService;
  18. use app\common\service\wechat\WeChatOaService;
  19. /**
  20. * 微信公众号菜单逻辑层
  21. * Class OfficialAccountMenuLogic
  22. * @package app\adminapi\logic\wechat
  23. */
  24. class OfficialAccountMenuLogic extends BaseLogic
  25. {
  26. /**
  27. * @notes 保存
  28. * @param $params
  29. * @return bool
  30. * @author 段誉
  31. * @date 2022/3/29 10:43
  32. */
  33. public static function save($params)
  34. {
  35. try {
  36. self::checkMenu($params);
  37. ConfigService::set('oa_setting', 'menu', $params);
  38. return true;
  39. } catch (\Exception $e) {
  40. OfficialAccountMenuLogic::setError($e->getMessage());
  41. return false;
  42. }
  43. }
  44. /**
  45. * @notes 一级菜单校验
  46. * @param $menu
  47. * @throws \Exception
  48. * @author 段誉
  49. * @date 2022/3/29 10:55
  50. */
  51. public static function checkMenu($menu)
  52. {
  53. if (empty($menu) || !is_array($menu)) {
  54. throw new \Exception('请设置正确格式菜单');
  55. }
  56. if (count($menu) > 3) {
  57. throw new \Exception('一级菜单超出限制(最多3个)');
  58. }
  59. foreach ($menu as $item) {
  60. if (!is_array($item)) {
  61. throw new \Exception('一级菜单项须为数组格式');
  62. }
  63. if (empty($item['name'])) {
  64. throw new \Exception('请输入一级菜单名称');
  65. }
  66. if (mb_strlen($item['name']) > 4) {
  67. throw new \Exception("一级菜单名称字数不能超过4个字符");
  68. }
  69. if (false == $item['has_menu']) {
  70. if (empty($item['type'])) {
  71. throw new \Exception('一级菜单未选择菜单类型');
  72. }
  73. if (!in_array($item['type'], OfficialAccountEnum::MENU_TYPE)) {
  74. throw new \Exception('一级菜单类型错误');
  75. }
  76. self::checkType($item);
  77. }
  78. if (true == $item['has_menu'] && empty($item['sub_button'])) {
  79. throw new \Exception('请配置子菜单');
  80. }
  81. if (!empty($item['sub_button'])) {
  82. self::checkSubButton($item['sub_button']);
  83. }
  84. }
  85. }
  86. /**
  87. * @notes 二级菜单校验
  88. * @param $subButtion
  89. * @throws \Exception
  90. * @author 段誉
  91. * @date 2022/3/29 10:55
  92. */
  93. public static function checkSubButton($subButtion)
  94. {
  95. if (!is_array($subButtion)) {
  96. throw new \Exception('二级菜单须为数组格式');
  97. }
  98. if (count($subButtion) > 5) {
  99. throw new \Exception('二级菜单超出限制(最多5个)');
  100. }
  101. foreach ($subButtion as $subItem) {
  102. if (!is_array($subItem)) {
  103. throw new \Exception('二级菜单项须为数组');
  104. }
  105. if (empty($subItem['name'])) {
  106. throw new \Exception('请输入二级菜单名称');
  107. }
  108. if (mb_strlen($subItem['name']) > 8) {
  109. throw new \Exception("二级菜单名称字数不能超过8个字符");
  110. }
  111. if (empty($subItem['type']) || !in_array($subItem['type'], OfficialAccountEnum::MENU_TYPE)) {
  112. throw new \Exception('二级未选择菜单类型或菜单类型错误');
  113. }
  114. self::checkType($subItem);
  115. }
  116. }
  117. /**
  118. * @notes 菜单类型校验
  119. * @param $item
  120. * @throws \Exception
  121. * @author 段誉
  122. * @date 2022/3/29 10:55
  123. */
  124. public static function checkType($item)
  125. {
  126. switch ($item['type']) {
  127. // 关键字
  128. case 'click':
  129. if (empty($item['key'])) {
  130. throw new \Exception('请输入关键字');
  131. }
  132. break;
  133. // 跳转网页链接
  134. case 'view':
  135. if (empty($item['url'])) {
  136. throw new \Exception('请输入网页链接');
  137. }
  138. break;
  139. // 小程序
  140. case 'miniprogram':
  141. if (empty($item['url'])) {
  142. throw new \Exception('请输入网页链接');
  143. }
  144. if (empty($item['appid'])) {
  145. throw new \Exception('请输入appid');
  146. }
  147. if (empty($item['pagepath'])) {
  148. throw new \Exception('请输入小程序路径');
  149. }
  150. break;
  151. }
  152. }
  153. /**
  154. * @notes 保存发布菜单
  155. * @param $params
  156. * @return bool
  157. * @throws \GuzzleHttp\Exception\GuzzleException
  158. * @author 段誉
  159. * @date 2022/3/29 10:55
  160. */
  161. public static function saveAndPublish($params)
  162. {
  163. try {
  164. self::checkMenu($params);
  165. $result = (new WeChatOaService())->createMenu($params);
  166. if ($result['errcode'] == 0) {
  167. ConfigService::set('oa_setting', 'menu', $params);
  168. return true;
  169. }
  170. self::setError('保存发布菜单失败' . json_encode($result->getContent()));
  171. return false;
  172. } catch (\Exception $e) {
  173. self::setError($e->getMessage());
  174. return false;
  175. }
  176. }
  177. /**
  178. * @notes 查看菜单详情
  179. * @return array|int|mixed|string|null
  180. * @author 段誉
  181. * @date 2022/3/29 10:56
  182. */
  183. public static function detail()
  184. {
  185. $data = ConfigService::get('oa_setting', 'menu', []);
  186. if (!empty($data)) {
  187. foreach ($data as &$item) {
  188. $item['has_menu'] = !empty($item['has_menu']);
  189. }
  190. }
  191. return $data;
  192. }
  193. }