From 69e9f0604366f9b57135f3827fae2d5fb511b33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Wed, 19 Apr 2017 19:01:55 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E6=B7=BB=E5=8A=A0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E8=8F=9C=E5=8D=95=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common.php | 2 +- application/wechat/controller/Menu.php | 127 +++++++++- application/wechat/view/menu.index.html | 314 ++++++++++++++++++++++++ 3 files changed, 441 insertions(+), 2 deletions(-) create mode 100644 application/wechat/view/menu.index.html diff --git a/application/common.php b/application/common.php index 18e537f8f..3eff2c3b5 100644 --- a/application/common.php +++ b/application/common.php @@ -33,7 +33,7 @@ function p($data, $replace = false, $pathname = NULL) { /** * 获取微信操作对象 * @param string $type - * @return \Wechat\WechatReceive|\Wechat\WechatUser|\Wechat\WechatPay|\Wechat\WechatScript|\Wechat\WechatOauth + * @return \Wechat\WechatReceive|\Wechat\WechatUser|\Wechat\WechatPay|\Wechat\WechatScript|\Wechat\WechatOauth|\Wechat\WechatMenu */ function & load_wechat($type = '') { static $wechat = array(); diff --git a/application/wechat/controller/Menu.php b/application/wechat/controller/Menu.php index a94bc85fe..152874759 100644 --- a/application/wechat/controller/Menu.php +++ b/application/wechat/controller/Menu.php @@ -14,6 +14,8 @@ namespace app\wechat\controller; use controller\BasicAdmin; +use service\ToolsService; +use think\Db; /** * 微信菜单管理 @@ -24,7 +26,130 @@ use controller\BasicAdmin; */ class Menu extends BasicAdmin { - public function index() { + /** + * 指定当前页面标题 + * @var string + */ + protected $title = '微信菜单定制'; + /** + * 指定默认操作的数据表 + * @var string + */ + protected $table = 'WechatMenu'; + + /** + * 微信菜单的类型 + * @var array + */ + protected $menu_type = array( + 'view' => '跳转URL', + 'click' => '点击推事件', + 'scancode_push' => '扫码推事件', + 'scancode_waitmsg' => '扫码推事件且弹出“消息接收中”提示框', + 'pic_sysphoto' => '弹出系统拍照发图', + 'pic_photo_or_album' => '弹出拍照或者相册发图', + 'pic_weixin' => '弹出微信相册发图器', + 'location_select' => '弹出地理位置选择器', + ); + + /** + * 显示列表操作 + */ + public function index() { + parent::_list(Db::name($this->table), false, true); + } + + /** + * 列表数据处理 + * @param array $data + */ + protected function _index_data_filter(&$data) { + $data = ToolsService::arr2tree($data, 'index', 'pindex'); + } + + /** + * 微信菜单编辑 + */ + public function edit() { + if ($this->request->isPost()) { + $post = $this->request->post(); + !isset($post['data']) && $this->error('访问出错,请稍候再试!'); + $data = $post['data']; + if (empty($data)) { + Db::name($this->table)->where('1=1')->delete(); + load_wechat('Menu')->deleteMenu(); + $this->success('删除并取消微信菜单成功!', ''); + } + if (Db::name($this->table)->where('1=1')->delete() !== false && Db::name($this->table)->insertAll($data) !== false) { + $result = $this->_push(); + if ($result['status']) { + $this->success('保存发布菜单成功!', ''); + } + $this->error('菜单发布失败,' . $result['errmsg']); + } + $this->error('保存发布菜单失败!'); + } + } + + /** + * 取消菜单 + */ + public function cancel() { + $wehcat = &load_wechat('Menu'); + if (false !== $wehcat->deleteMenu()) { + $this->success('菜单取消成功,重新关注可立即生效!', ''); + } else { + $this->error('菜单取消失败,' . $wehcat->errMsg); + } + } + + /** + * 菜单推送 + */ + protected function _push() { + $result = Db::name($this->table) + ->field('id,index,pindex,name,type,content') + ->where('status', '1') + ->order('sort ASC,id ASC') + ->select(); + foreach ($result as &$row) { + empty($row['content']) && $row['content'] = uniqid(); + switch ($row['type']) { + case 'view': + $row['url'] = preg_match('#^(\w+:)?//#i', $row['content']) ? $row['content'] : url($row['content'], '', true, true); + break; + case 'event': + if (isset($this->menu_type[$row['content']])) { + $row['type'] = $row['content']; + $row['key'] = "wechat_menu#id#{$row['id']}"; + } + break; + case 'media_id': + $row['media_id'] = $row['content']; + break; + default : + (!in_array($row['type'], $this->menu_type)) && $row['type'] = 'click'; + $row['key'] = "wechat_menu#id#{$row['id']}"; + } + unset($row['content']); + } + $menus = ToolsService::arr2tree($result, 'index', 'pindex', 'sub_button'); + //去除无效的字段 + foreach ($menus as &$menu) { + unset($menu['index'], $menu['pindex'], $menu['id']); + if (empty($menu['sub_button'])) { + continue; + } + foreach ($menu['sub_button'] as &$submenu) { + unset($submenu['index'], $submenu['pindex'], $submenu['id']); + } + unset($menu['type']); + } + $wechat = &load_wechat('Menu'); + if (false !== $wechat->createMenu(['button' => $menus])) { + return array('status' => true, 'errmsg' => ''); + } + return array('status' => false, 'errmsg' => $wechat->errMsg); } } diff --git a/application/wechat/view/menu.index.html b/application/wechat/view/menu.index.html new file mode 100644 index 000000000..fe868d4a9 --- /dev/null +++ b/application/wechat/view/menu.index.html @@ -0,0 +1,314 @@ +{extend name='extra@admin/content' /} + +{block name="style"} + +{/block} + +{block name='content'} +
+
公众号
+
+ +
+
+ +
+ + +
+
+ {if auth("$classuri/edit")} + + {/if} + {if auth("$classuri/cancel")} + + {/if} +
+{/block} + +{block name="script"} + +{/block} \ No newline at end of file