Menu.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\wechat\controller;
  15. use app\wechat\service\WechatService;
  16. use library\Controller;
  17. use think\Db;
  18. use think\exception\HttpResponseException;
  19. /**
  20. * 微信菜单管理
  21. * Class Menu
  22. * @package app\wechat\controller
  23. */
  24. class Menu extends Controller
  25. {
  26. /**
  27. * 微信菜单的类型
  28. * @var array
  29. */
  30. protected $menuType = [
  31. 'click' => '匹配规则',
  32. 'view' => '跳转网页',
  33. 'miniprogram' => '打开小程序',
  34. // 'customservice' => '转多客服',
  35. 'scancode_push' => '扫码推事件',
  36. 'scancode_waitmsg' => '扫码推事件且弹出“消息接收中”提示框',
  37. 'pic_sysphoto' => '弹出系统拍照发图',
  38. 'pic_photo_or_album' => '弹出拍照或者相册发图',
  39. 'pic_weixin' => '弹出微信相册发图器',
  40. 'location_select' => '弹出地理位置选择器',
  41. ];
  42. /**
  43. * 微信菜单管理
  44. * @auth true
  45. * @menu true
  46. * @throws \think\Exception
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. * @throws \think\exception\PDOException
  51. */
  52. public function index()
  53. {
  54. if ($this->request->get('output') === 'json') {
  55. $where = [['keys', 'notin', ['subscribe', 'default']], ['status', 'eq', '1']];
  56. $keys = Db::name('WechatKeys')->where($where)->order('sort desc,id desc')->select();
  57. $this->success('获取数据成功!', ['menudata' => sysdata('menudata'), 'keysdata' => $keys]);
  58. } else {
  59. $this->title = '微信菜单定制';
  60. $this->menuTypes = $this->menuType;
  61. $this->fetch();
  62. }
  63. }
  64. /**
  65. * 编辑微信菜单
  66. * @auth true
  67. */
  68. public function edit()
  69. {
  70. if ($this->request->isPost()) {
  71. $data = $this->request->post('data');
  72. if (empty($data)) { // 删除菜单
  73. try {
  74. WechatService::WeChatMenu()->delete();
  75. sysoplog('微信管理', '删除微信菜单成功');
  76. $this->success('删除微信菜单成功!', '');
  77. } catch (HttpResponseException $exception) {
  78. throw $exception;
  79. } catch (\Exception $e) {
  80. sysoplog('微信管理', "删除微信菜单失败:{$e->getMessage()}");
  81. $this->error('删除微信菜单失败,请稍候再试!' . $e->getMessage());
  82. }
  83. } else {
  84. try {
  85. sysdata('menudata', $this->_buildMenuData($menudata = json_decode($data, true)));
  86. WechatService::WeChatMenu()->create(['button' => sysdata('menudata')]);
  87. sysoplog('微信管理', '发布微信菜单成功');
  88. $this->success('保存发布菜单成功!', '');
  89. } catch (HttpResponseException $exception) {
  90. throw $exception;
  91. } catch (\Exception $e) {
  92. sysoplog('微信管理', "发布微信菜单失败:{$e->getMessage()}");
  93. $this->error("微信菜单发布失败,请稍候再试!<br> {$e->getMessage()}");
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * 菜单数据处理
  100. * @param array $list
  101. * @return array
  102. */
  103. private function _buildMenuData(array $list)
  104. {
  105. foreach ($list as &$vo) {
  106. unset($vo['active'], $vo['show']);
  107. if (empty($vo['sub_button'])) {
  108. $vo = $this->_buildMenuItemData($vo);
  109. } else {
  110. $item = ['name' => $vo['name'], 'sub_button' => []];
  111. foreach ($vo['sub_button'] as &$sub) {
  112. unset($sub['active'], $sub['show']);
  113. array_push($item['sub_button'], $this->_buildMenuItemData($sub));
  114. }
  115. $vo = $item;
  116. }
  117. }
  118. return $list;
  119. }
  120. /**
  121. * 单个微信菜单数据处理
  122. * @param array $item
  123. * @return array
  124. */
  125. private function _buildMenuItemData(array $item)
  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' => isset($item['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. }
  143. }
  144. /**
  145. * 取消微信菜单
  146. * @auth true
  147. */
  148. public function cancel()
  149. {
  150. try {
  151. WechatService::WeChatMenu()->delete();
  152. $this->success('菜单取消成功,重新关注可立即生效!', '');
  153. } catch (HttpResponseException $exception) {
  154. sysoplog('微信管理', '取消微信菜单成功');
  155. throw $exception;
  156. } catch (\Exception $e) {
  157. $this->error("菜单取消失败,请稍候再试!<br> {$e->getMessage()}");
  158. }
  159. }
  160. }