Menu.php 6.3 KB

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