OfficialAccountMenuLogic.php 6.6 KB

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