Menu.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\common\library\wechat\mp;
  10. use app\common\library\wechat\Factory;
  11. /**
  12. * 公众号菜单管理类
  13. * Class Menu
  14. * @package app\common\library\wechat\mp
  15. */
  16. class Menu {
  17. /**
  18. * 保存菜单
  19. * @param array $data
  20. * @return array
  21. * @throws \Exception
  22. */
  23. public static function save($data=[]){
  24. if(empty($data)){
  25. try{
  26. Factory::wechat()->menu->delete();
  27. }catch(\Exception $e){
  28. throw new \Exception($e->getMessage(),0);
  29. }
  30. }else{
  31. try{
  32. $value = self::buildMenuData($data);
  33. Factory::wechat()->menu->create(['button' => $value]);
  34. return $value;
  35. }catch(\Exception $e){
  36. throw new \Exception($e->getMessage(),0);
  37. }
  38. }
  39. }
  40. /**
  41. * 菜单数据处理
  42. * @param array $list
  43. * @return array
  44. */
  45. private static function buildMenuData(array $list){
  46. foreach ($list as &$vo) {
  47. unset($vo['active'], $vo['show']);
  48. if (empty($vo['sub_button'])) {
  49. $vo = self::buildMenuItemData($vo);
  50. } else {
  51. $item = ['name' => $vo['name'], 'sub_button' => []];
  52. foreach ($vo['sub_button'] as &$sub) {
  53. unset($sub['active'], $sub['show']);
  54. array_push($item['sub_button'],self::buildMenuItemData($sub));
  55. }
  56. $vo = $item;
  57. }
  58. }
  59. return $list;
  60. }
  61. /**
  62. * 单个微信菜单数据处理
  63. * @param array $item
  64. * @return array
  65. */
  66. private static function buildMenuItemData(array $item){
  67. switch (strtolower($item['type'])) {
  68. case 'pic_weixin':
  69. case 'pic_sysphoto':
  70. case 'scancode_push':
  71. case 'location_select':
  72. case 'scancode_waitmsg':
  73. case 'pic_photo_or_album':
  74. return [
  75. 'name' => $item['name'], 'type' => $item['type'],
  76. 'key' => isset($item['key']) ? $item['key'] : $item['type']
  77. ];
  78. case 'click':
  79. if (empty($item['key'])) {
  80. throw new \Exception("匹配规则存在空的选项",0);
  81. }
  82. return ['name' => $item['name'], 'type' => $item['type'], 'key' => $item['key']];
  83. case 'view':
  84. return ['name' => $item['name'], 'type' => $item['type'], 'url' => $item['url']];
  85. case 'miniprogram':
  86. return [
  87. 'name' => $item['name'], 'type' => $item['type'],
  88. 'url' => $item['url'], 'appid' => $item['appid'],
  89. 'pagepath' => $item['pagepath']
  90. ];
  91. }
  92. }
  93. }