Menu.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\wechat\controller;
  3. use app\wechat\service\WechatService;
  4. use think\admin\Controller;
  5. use think\exception\HttpResponseException;
  6. /**
  7. * 微信菜单管理
  8. * Class Menu
  9. * @package app\wechat\controller
  10. */
  11. class Menu extends Controller
  12. {
  13. /**
  14. * 存储数据名称
  15. * @var string
  16. */
  17. protected $ckey = 'wechat_menu_data';
  18. /**
  19. * 微信菜单的类型
  20. * @var array
  21. */
  22. protected $menuTypes = [
  23. 'click' => '匹配规则',
  24. 'view' => '跳转网页',
  25. 'miniprogram' => '打开小程序',
  26. 'customservice' => '转多客服',
  27. 'scancode_push' => '扫码推事件',
  28. 'scancode_waitmsg' => '扫码推事件且弹出“消息接收中”提示框',
  29. 'pic_sysphoto' => '弹出系统拍照发图',
  30. 'pic_photo_or_album' => '弹出拍照或者相册发图',
  31. 'pic_weixin' => '弹出微信相册发图器',
  32. 'location_select' => '弹出地理位置选择器',
  33. ];
  34. /**
  35. * 微信菜单管理
  36. * @auth true
  37. * @menu true
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function index()
  43. {
  44. if ($this->request->get('output') === 'json') {
  45. $map = [['keys', 'notin', ['subscribe', 'default']], ['status', '=', 1]];
  46. $result = $this->app->db->name('WechatKeys')->where($map)->order('sort desc,id desc')->select();
  47. $this->success('获取数据成功!', ['menudata' => sysdata($this->ckey), 'keysdata' => $result->toArray()]);
  48. } else {
  49. $this->title = '微信菜单定制';
  50. $this->fetch();
  51. }
  52. }
  53. /**
  54. * 取消微信菜单
  55. * @auth true
  56. */
  57. public function cancel()
  58. {
  59. try {
  60. WechatService::WeChatMenu()->delete();
  61. $this->success('菜单取消成功,重新订阅可立即生效!');
  62. } catch (HttpResponseException $exception) {
  63. sysoplog('微信管理', '取消微信菜单成功');
  64. throw $exception;
  65. } catch (\Exception $exception) {
  66. $this->error("菜单取消失败,请稍候再试!<br> {$exception->getMessage()}");
  67. }
  68. }
  69. /**
  70. * 编辑微信菜单
  71. * @auth true
  72. */
  73. public function push()
  74. {
  75. if ($this->request->isPost()) {
  76. $data = $this->request->post('data');
  77. if (empty($data)) try {
  78. WechatService::WeChatMenu()->delete();
  79. sysoplog('微信菜单管理', '删除微信菜单成功');
  80. $this->success('删除微信菜单成功!', '');
  81. } catch (HttpResponseException $exception) {
  82. throw $exception;
  83. } catch (\Exception $exception) {
  84. sysoplog('微信菜单管理', "删除微信菜单失败:{$exception->getMessage()}");
  85. $this->error("删除微信菜单失败,请稍候再试!<br>{$exception->getMessage()}");
  86. }
  87. else try {
  88. sysdata($this->ckey, $this->_buildMenuData(json_decode($data, true)));
  89. WechatService::WeChatMenu()->create(['button' => sysdata($this->ckey)]);
  90. sysoplog('微信菜单管理', '发布微信菜单成功');
  91. $this->success('保存发布菜单成功!', '');
  92. } catch (HttpResponseException $exception) {
  93. throw $exception;
  94. } catch (\Exception $exception) {
  95. sysoplog('微信菜单管理', "发布微信菜单失败:{$exception->getMessage()}");
  96. $this->error("微信菜单发布失败,请稍候再试!<br> {$exception->getMessage()}");
  97. }
  98. }
  99. }
  100. /**
  101. * 菜单数据处理
  102. * @param array $list
  103. * @return array
  104. */
  105. private function _buildMenuData(array $list): array
  106. {
  107. foreach ($list as $key => &$item) {
  108. if (empty($item['sub_button'])) {
  109. $item = $this->_buildMenuDataItem($item);
  110. } else {
  111. $button = ['name' => $item['name'], 'sub_button' => []];
  112. foreach ($item['sub_button'] as &$sub) {
  113. $button['sub_button'][] = $this->_buildMenuDataItem($sub);
  114. }
  115. $item = $button;
  116. }
  117. }
  118. return $list;
  119. }
  120. /**
  121. * 单个微信菜单数据处理
  122. * @param array $item
  123. * @return array
  124. */
  125. private function _buildMenuDataItem(array $item): array
  126. {
  127. switch (strtolower($item['type'])) {
  128. case 'pic_weixin':
  129. case 'pic_sysphoto':
  130. case 'scancode_push':
  131. case 'location_select':
  132. case 'scancode_waitmsg':
  133. case 'pic_photo_or_album':
  134. return ['name' => $item['name'], 'type' => $item['type'], 'key' => $item['key'] ?? $item['type']];
  135. case 'click':
  136. if (empty($item['key'])) $this->error('匹配规则存在空的选项');
  137. return ['name' => $item['name'], 'type' => $item['type'], 'key' => $item['key']];
  138. case 'view':
  139. return ['name' => $item['name'], 'type' => $item['type'], 'url' => $item['url']];
  140. case 'miniprogram':
  141. return ['name' => $item['name'], 'type' => $item['type'], 'url' => $item['url'], 'appid' => $item['appid'], 'pagepath' => $item['pagepath']];
  142. default:
  143. return [];
  144. }
  145. }
  146. }