Menu.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\controller;
  14. use controller\BasicAdmin;
  15. use service\LogService;
  16. use service\ToolsService;
  17. use think\Db;
  18. /**
  19. * 微信菜单管理
  20. * Class Menu
  21. * @package app\wechat\controller
  22. * @author Anyon <zoujingli@qq.com>
  23. * @date 2017/03/27 14:43
  24. */
  25. class Menu extends BasicAdmin
  26. {
  27. /**
  28. * 指定当前页面标题
  29. * @var string
  30. */
  31. public $title = '微信菜单定制';
  32. /**
  33. * 指定默认操作的数据表
  34. * @var string
  35. */
  36. public $table = 'WechatMenu';
  37. /**
  38. * 微信菜单的类型
  39. * @var array
  40. */
  41. protected $menuType = [
  42. 'view' => '跳转URL',
  43. 'click' => '点击推事件',
  44. 'scancode_push' => '扫码推事件',
  45. 'scancode_waitmsg' => '扫码推事件且弹出“消息接收中”提示框',
  46. 'pic_sysphoto' => '弹出系统拍照发图',
  47. 'pic_photo_or_album' => '弹出拍照或者相册发图',
  48. 'pic_weixin' => '弹出微信相册发图器',
  49. 'location_select' => '弹出地理位置选择器',
  50. ];
  51. /**
  52. * 显示列表操作
  53. */
  54. public function index()
  55. {
  56. return parent::_list(Db::name($this->table), false, true);
  57. }
  58. /**
  59. * 列表数据处理
  60. * @param array $data
  61. */
  62. protected function _index_data_filter(&$data)
  63. {
  64. $data = ToolsService::arr2tree($data, 'index', 'pindex');
  65. }
  66. /**
  67. * 微信菜单编辑
  68. */
  69. public function edit()
  70. {
  71. if ($this->request->isPost()) {
  72. $post = $this->request->post();
  73. !isset($post['data']) && $this->error('访问出错,请稍候再试!');
  74. $data = $post['data'];
  75. if (empty($data)) {
  76. Db::name($this->table)->where('1=1')->delete();
  77. load_wechat('Menu')->deleteMenu();
  78. $this->success('删除并取消微信菜单成功!', '');
  79. }
  80. foreach ($data as &$vo) {
  81. if (isset($vo['content'])) {
  82. $vo['content'] = str_replace('"', "'", $vo['content']);
  83. }
  84. }
  85. if (Db::name($this->table)->where('1=1')->delete() !== false && Db::name($this->table)->insertAll($data) !== false) {
  86. $result = $this->_push();
  87. if ($result['status']) {
  88. LogService::write('微信管理', '发布微信菜单成功');
  89. $this->success('保存发布菜单成功!', '');
  90. }
  91. $this->error('菜单发布失败,' . $result['errmsg']);
  92. }
  93. $this->error('保存发布菜单失败!');
  94. }
  95. }
  96. /**
  97. * 取消菜单
  98. */
  99. public function cancel()
  100. {
  101. $wehcat = load_wechat('Menu');
  102. if (false !== $wehcat->deleteMenu()) {
  103. $this->success('菜单取消成功,重新关注可立即生效!', '');
  104. }
  105. $this->error('菜单取消失败,' . $wehcat->getError());
  106. }
  107. /**
  108. * 菜单推送
  109. */
  110. protected function _push()
  111. {
  112. list($map, $fields) = [['status' => '1'], 'id,index,pindex,name,type,content'];
  113. $result = (array)Db::name($this->table)->field($fields)->where($map)->order('sort ASC,id ASC')->select();
  114. foreach ($result as &$row) {
  115. empty($row['content']) && $row['content'] = uniqid();
  116. if ($row['type'] === 'miniprogram') :
  117. list($row['appid'], $row['url'], $row['pagepath']) = explode(',', "{$row['content']},,");
  118. elseif ($row['type'] === 'view') :
  119. if (preg_match('#^(\w+:)?//#', $row['content'])) {
  120. $row['url'] = $row['content'];
  121. } else {
  122. $row['url'] = url($row['content'], '', true, true);
  123. }
  124. elseif ($row['type'] === 'event') :
  125. if (isset($this->menuType[$row['content']])) {
  126. list($row['type'], $row['key']) = [$row['content'], "wechat_menu#id#{$row['id']}"];
  127. };
  128. elseif ($row['type'] === 'media_id'):
  129. $row['media_id'] = $row['content'];
  130. else :
  131. $row['key'] = "wechat_menu#id#{$row['id']}";
  132. !in_array($row['type'], $this->menuType) && $row['type'] = 'click';
  133. endif;
  134. unset($row['content']);
  135. }
  136. $menus = ToolsService::arr2tree($result, 'index', 'pindex', 'sub_button');
  137. //去除无效的字段
  138. foreach ($menus as &$menu) {
  139. unset($menu['index'], $menu['pindex'], $menu['id']);
  140. if (empty($menu['sub_button'])) {
  141. continue;
  142. }
  143. foreach ($menu['sub_button'] as &$submenu) {
  144. unset($submenu['index'], $submenu['pindex'], $submenu['id']);
  145. }
  146. unset($menu['type']);
  147. }
  148. $wechat = load_wechat('Menu');
  149. if (false !== $wechat->createMenu(['button' => $menus])) {
  150. return ['status' => true, 'errmsg' => ''];
  151. }
  152. return ['status' => false, 'errmsg' => $wechat->getError()];
  153. }
  154. }