美文网首页PHP经验分享首页投稿(暂停使用,暂停投稿)
thinkcmf中实现微信自定义菜单在线编辑生成

thinkcmf中实现微信自定义菜单在线编辑生成

作者: geeooooz | 来源:发表于2017-11-02 14:39 被阅读27次
    由于我需要的实现的功能比较少 所以 就没做获取当前公众号菜单以及 区分标识 场景等点击推 各位看官这么聪明 应该是可以自行添加上的哈

    先上个数据表的图(结构还是不太合理 等待大家修改)

    数据表生成代码:
    /*
    Navicat MySQL Data Transfer
    
    Source Server         : 七海服务器
    Source Server Version : 50625
    Source Host           : 182.92.215.27:3306
    Source Database       : bmcs
    
    Target Server Type    : MYSQL
    Target Server Version : 50625
    File Encoding         : 65001
    
    Date: 2017-11-02 14:35:28
    */
    
    SET FOREIGN_KEY_CHECKS=0;
    
    -- ----------------------------
    -- Table structure for cmf_we_menu
    -- ----------------------------
    DROP TABLE IF EXISTS `cmf_we_menu`;
    CREATE TABLE `cmf_we_menu` (
      `id` tinyint(11) NOT NULL AUTO_INCREMENT,
      `we_menu_name` varchar(20) NOT NULL COMMENT '菜单名称',
      `we_menu_leftid` int(11) NOT NULL COMMENT '菜单上级ID',
      `we_menu_type` tinyint(2) NOT NULL COMMENT '菜单类型 1为弹出 2为链接',
      `we_menu_typeval` varchar(200) NOT NULL COMMENT '菜单类型值',
      `is_del` tinyint(2) NOT NULL DEFAULT '0' COMMENT '假删',
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of cmf_we_menu
    -- ----------------------------
    INSERT INTO `cmf_we_menu` VALUES ('10', '菜单一', '0', '2', 'https://www.baidu.com/', '0');
    INSERT INTO `cmf_we_menu` VALUES ('11', '菜单二', '0', '1', '', '0');
    INSERT INTO `cmf_we_menu` VALUES ('17', '3', '11', '2', 'http://www.baidu.com', '0');
    INSERT INTO `cmf_we_menu` VALUES ('15', '1', '11', '2', 'https://www.baidu.com/', '0');
    INSERT INTO `cmf_we_menu` VALUES ('16', '2', '11', '2', 'http://www.baidu.com', '0');
    

    生成自定义菜单代码:

    <?php
    namespace Wechat\Controller;
    use Common\Controller\AdminbaseController;
    class CreateMenuController extends AdminbaseController{
        private $_appid;
        private $_appsecret;
        private $we_menu;
        
        public function __construct($_appid,$_appsecret,$_token){//构造函数  就因为少写了一个     _  fuck
            $this->_appid = 'wx6761adab1c78b126';
            $this->_appsecret = '753042aee9f6682eb399e36138a813c9';
            $this->we_menu = M('we_menu');
        }
        //创建自定义菜单
        public function create(){
            $url_get='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->_appid.'&secret='.$this->_appsecret;
            $json=json_decode(file_get_contents("$url_get"));
            
            
            if($this->_appid==false||$this->_appsecret==false){
                $this->error('必须先填写【AppId】【 AppSecret】');exit;
            }
                //开始拼接数据
                $data = '{"button":[';
    
                $class=$this->we_menu->where(array('we_menu_leftid'=>'0'))->order('id asc')->select();//取出顶级菜单
                $k=1;
                foreach($class as $key=>$vo){
                    //主菜单
    
                    $data.='{"name":"'.$vo['we_menu_name'].'",';
    
                    //取出二级菜单
    
                    $c=$this->we_menu->where(array('we_menu_leftid'=>$vo['id']))->order('id asc')->limit(5)->select();
    
                    $count=$this->we_menu->where(array('we_menu_leftid'=>$vo['id']))->limit(5)->count();
                    $num=1;
                    //拼接子菜单
                    if($c!=false){
    
                        $data.='"sub_button":[';
                        foreach($c as $voo){
                            if($num==$count) $data.='{"type":"view","name":"'.$voo['we_menu_name'].'","url":"'.$voo['we_menu_typeval'].'"}';
                            else $data.='{"type":"view","name":"'.$voo['we_menu_name'].'","url":"'.$voo['we_menu_typeval'].'"},';
                            $num++;
                        }
    
                        $data.=']';
    
                    }else{
    
                        $data.='"type":"view","url":"'.$vo['we_menu_typeval'].'"';
    
                    }
    
                    if($k==3){
    
                        $data.='}';
    
                    }else{
    
                        $data.='},';
    
                    }
    
                    $k++;
                }
    
                $data.=']}';
                $url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$json->access_token;
                file_get_contents('https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$json->access_token);
                if($this->api_notice_increment($url,$data)==false){
                    return false;
                }else{
                    return true;
                }
        }
    
        //POST提交方法
    
        function api_notice_increment($url, $data){
    
            $ch = curl_init();
    
            $header = "Accept-Charset: utf-8";
    
            curl_setopt($ch, CURLOPT_URL, $url);
    
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
            $tmpInfo = curl_exec($ch);
            // p($tmpInfo);
    
            if (curl_errno($ch)) {
    
                echo '失败';
    
            }else{
    
                echo '成功';
    
            }
    
        }
    
    }
    

    ok 完了。

    相关文章

      网友评论

        本文标题:thinkcmf中实现微信自定义菜单在线编辑生成

        本文链接:https://www.haomeiwen.com/subject/xcbvpxtx.html