Menu.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use addons\wechat\library\Config;
  4. use app\common\controller\Backend;
  5. use app\admin\model\WechatResponse;
  6. use EasyWeChat\Factory;
  7. use think\Exception;
  8. /**
  9. * 菜单管理
  10. *
  11. * @icon fa fa-list-alt
  12. */
  13. class Menu extends Backend
  14. {
  15. protected $wechatcfg = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->wechatcfg = \app\admin\model\WechatConfig::get(['name' => 'menu']);
  20. }
  21. /**
  22. * 查看
  23. */
  24. public function index()
  25. {
  26. $responselist = array();
  27. $all = WechatResponse::all();
  28. foreach ($all as $k => $v) {
  29. $responselist[$v['eventkey']] = $v['title'];
  30. }
  31. $this->view->assign('responselist', $responselist);
  32. $this->view->assign('menu', (array)json_decode($this->wechatcfg->value, true));
  33. return $this->view->fetch();
  34. }
  35. /**
  36. * 修改
  37. */
  38. public function edit($ids = null)
  39. {
  40. $menu = $this->request->post("menu");
  41. $menu = (array)json_decode($menu, true);
  42. foreach ($menu as $index => &$item) {
  43. if (isset($item['sub_button'])) {
  44. foreach ($item['sub_button'] as &$subitem) {
  45. if ($subitem['type'] == 'view') {
  46. $allowFields = ['type', 'name', 'url'];
  47. $subitem = ['type' => $subitem['type'], 'name' => $subitem['name'], 'url' => $subitem['url']];
  48. } else {
  49. if ($subitem['type'] == 'miniprogram') {
  50. $allowFields = ['type', 'name', 'url', 'appid', 'pagepath'];
  51. $subitem = ['type' => $subitem['type'], 'name' => $subitem['name'], 'url' => $subitem['url'], 'appid' => $subitem['appid'], 'pagepath' => $subitem['pagepath']];
  52. } else {
  53. $allowFields = ['type', 'name', 'key'];
  54. $subitem = ['type' => $subitem['type'], 'name' => $subitem['name'], 'key' => $subitem['key']];
  55. }
  56. }
  57. $subitem = array_intersect_key($subitem, array_flip($allowFields));
  58. }
  59. } else {
  60. if ($item['type'] == 'view') {
  61. $allowFields = ['type', 'name', 'url'];
  62. } else {
  63. if ($item['type'] == 'miniprogram') {
  64. $allowFields = ['type', 'name', 'url', 'appid', 'pagepath'];
  65. } else {
  66. $allowFields = ['type', 'name', 'key'];
  67. }
  68. }
  69. $item = array_intersect_key($item, array_flip($allowFields));
  70. }
  71. }
  72. $this->wechatcfg->value = json_encode($menu, JSON_UNESCAPED_UNICODE);
  73. $this->wechatcfg->save();
  74. $this->success();
  75. }
  76. /**
  77. * 加载远程菜单
  78. */
  79. public function remote()
  80. {
  81. $app = Factory::officialAccount(Config::load());
  82. try {
  83. $list = $app->menu->list();
  84. } catch (\Exception $e) {
  85. $this->error($e->getMessage());
  86. }
  87. if (isset($list['menu']['button'])) {
  88. $buttons = $list['menu']['button'];
  89. foreach ($buttons as $index => &$item) {
  90. if (isset($item['sub_button'])) {
  91. if ($item['sub_button']) {
  92. foreach ($item['sub_button'] as $key => &$value) {
  93. if (!isset($value['sub_button']) || !$value['sub_button']) {
  94. unset($value['sub_button']);
  95. }
  96. }
  97. } else {
  98. unset($item['sub_button']);
  99. }
  100. }
  101. }
  102. $this->wechatcfg->value = json_encode($buttons, JSON_UNESCAPED_UNICODE);
  103. $this->wechatcfg->save();
  104. $this->success();
  105. } else {
  106. $this->error("加载菜单失败");
  107. }
  108. }
  109. /**
  110. * 同步到服务器
  111. */
  112. public function sync($ids = null)
  113. {
  114. $app = Factory::officialAccount(Config::load());
  115. try {
  116. $hasError = false;
  117. $menu = json_decode($this->wechatcfg->value, true);
  118. foreach ($menu as $k => $v) {
  119. if (isset($v['sub_button'])) {
  120. foreach ($v['sub_button'] as $m => $n) {
  121. if ($n['type'] == 'click' && isset($n['key']) && !$n['key']) {
  122. $hasError = true;
  123. break 2;
  124. }
  125. }
  126. } else {
  127. if ($v['type'] == 'click' && isset($v['key']) && !$v['key']) {
  128. $hasError = true;
  129. break;
  130. }
  131. }
  132. }
  133. if (!$hasError) {
  134. try {
  135. $ret = $app->menu->create($menu);
  136. } catch (\Exception $e) {
  137. $this->error($e->getMessage());
  138. }
  139. if ($ret['errcode'] == 0) {
  140. $this->success();
  141. } else {
  142. $this->error($ret['errmsg']);
  143. }
  144. } else {
  145. $this->error(__('Invalid parameters'));
  146. }
  147. } catch (Exception $e) {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. }