html
<?php if(!defined('PIGCMS_PATH')) exit('deny access!');?> <!doctype html> <html> <head> <meta charset="utf-8"/> <title>首页 - <?php echo $store_session['name']; ?> | <?php if (empty($_SESSION['sync_store'])) { ?><?php echo $config['site_name'];?><?php } else { ?>微店系统<?php } ?></title> <meta name="copyright" content="<?php echo $config['site_url'];?>"/> <link href="<?php echo TPL_URL;?>css/base.css" type="text/css" rel="stylesheet"/> <link href="<?php echo TPL_URL;?>css/store.css" type="text/css" rel="stylesheet"/> <link href="<?php echo TPL_URL;?>css/wx_menu.css?r=<?php echo time();?>" type="text/css" rel="stylesheet"/> <link href="<?php echo STATIC_URL;?>css/jquery.ui.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="<?php echo STATIC_URL;?>js/jquery.min.js"></script> </head> <body class="font14 usercenter"> <?php include display('public:header');?> <div class="wrap_1000 clearfix container"> <?php if (!empty($_SESSION['drp_diy_store'])) { ?> <?php include display('sidebar');?> <?php } ?> <div class="app" <?php if (empty($_SESSION['drp_diy_store'])) { ?>style=" 100%;"<?php } ?>> <div class="app-inner clearfix"> <div class="app-init-container"> <div class="nav-wrapper--app"></div> <div class="app__content page-showcase-dashboard" <?php if (empty($_SESSION['drp_diy_store'])) { ?>style=" 100%;"<?php } ?>> <div class="widget-app-board ui-box member_degree" style="border: none;"> <div class="widget-app-board-info"> <h3>温馨提示:</h3> <div> <p>如果您输入的字数超过规定的字数,提交到微信服务器审核,会报错或审核不通过,请到pc端设置公众号菜单。</p> </div> </div> </div> <div class="msg_menu"> <div class="msg_menu_bord"> <div class="msg_menu_left"> <div class="msg_menu_top"></div> <div class="msg_menu_two"> <?php foreach($menu as $k => $v){?> <div class="msg_menu_chlid" style=" <?php echo ((-(count($menu)>2?2:count($menu)) + 3) * 32.55)?>%;"> <div class="msg_menu_add_chlid" data-ac="add" data-pid="<?php echo $v['id']?>" data-pname="<?php echo $v['title']?>">+</div> <?php if(isset($menuChlid)) foreach($menuChlid as $kk =>$vv){?> <?php if($vv['pid'] == $v['id']){?> <div class="msg_menu_add_chlid"> <a href="javascript:;" data-id="<?php echo $vv['id'];?>" data-ac="edit" class="edit_menu"><?php echo $vv['title']?></a> </div> <?php }?> <?php }?> </div> <?php }?> </div> <div class="msg_menu_three"> <?php foreach($menu as $v){?> <div class="msg_menu_fa " style=" <?php echo ((-(count($menu)>2?2:count($menu)) + 3) * 32.9)?>%;" ><a href="javascript :;" data-id="<?php echo $v['id'];?>" data-ac="edit" class="edit_menu"><?php echo $v['title'];?></a></div> <?php }?> <?php if(count($menu) < 3){?> <div class="msg_menu_fa add-menu" <?php if(count($menu) == 0) echo 'style="99.7%"';?> data-ac="add" data-pid="0" data-pname="选择父级菜单">+</div> <?php }?> </div> </div> </div> <!-- 左面s --> <div class="msg_menu_bord"> <div class="msg_menu_bord_border"> </div> </div> <!-- end --> <div class="msg_menu_butt"> <a href="javascript:;" class="btn btn-add-wx-menu">一键生成菜单</a></div> </div> </div> </div> </div> </div> </div> <?php include display('public:footer');?> <div id="nprogress"><div class="bar" role="bar"><div class="peg"></div></div><div class="spinner" role="spinner"><div class="spinner-icon"></div></div></div> </body> </html> <script type="text/javascript" src="<?php echo STATIC_URL;?>js/layer/layer.min.js"></script> <script type="text/javascript" src="<?php echo TPL_URL;?>js/base.js"></script> <script type="text/javascript" src="<?php echo TPL_URL;?>js/wx_menu.js?r=<?php echo time();?>"></script> <script type="text/javascript"> $(function(){ $('.msg_menu_add_chlid,.add-menu,.edit_menu').click(function(){ var finfo = {} finfo.ac = $(this).data('ac'); if(finfo.ac == 'add'){ finfo.pid = $(this).data('pid'); finfo.pname = $(this).data('pname'); commWxMenu(12,finfo); }else if(finfo.ac == 'edit'){ $.post('?c=bonus&a=wx_find', {id: $(this).data('id')}, function(re) { re.msg.ac = finfo.ac; commWxMenu(re.f_menu,re.msg); },'json'); //console.log(finfo.pid); } }); $('.btn-add-wx-menu').click(function(){ if (confirm('你确定要生成吗!')) { $.post('?c=bonus&a=add_wx_menu', {admin:'yepai'},function(re){ console.log(re); if(re.error == 0){ alert(re.msg); }else{ alert(re.msg); } },'json'); } }); }); lk.auto(); function commWxMenu(f,finfo){ lk.wx_add_menu(f,finfo,function(data){ console.log(data); if(data.type == 1){ $.post('?c=bonus&a=wx_creat', data, function(re) { if(re.error == 0){ alert(re.msg); lk.rel(1000); }else{ alert(re.msg); } },'json'); }else if(data.type == 2){ if (confirm('你确定要删除吗!')) { $.post('?c=bonus&a=del_ment', data, function(re) { if(re.error == 0){ alert(re.msg); lk.rel(1000); }else{ alert(re.msg); } },'json'); } } }); } </script>
css
.msg_menu{ border: 1px solid #f4f5f9; 100%; height: 676px; background: #fff; } .msg_menu_butt{ border-top: 1px solid #f4f5f9; 100%; clear: both; height: 74.1px; line-height: 74.1px; text-align: center; } .btn-add-wx-menu{ /* margin-left: 50%; */ } .msg_menu_left{ clear: both; border: 1px solid #2f4050; 78%; height:525px; margin:10px auto; background: #dfdfdf; } .msg_menu_top{ 100%; height: 60px; background: url('/static/images/img_card_control.png?r=23'); background-size: 100% 60px; } .msg_menu_two{ border-bottom: 1px solid #2f4050; border-right: 1px solid #2f4050; 100%; height:407px; background: #fff; } .msg_menu_three{ 100%; min-height:57px; padding-right: 30px: } .msg_menu_chlid,.msg_menu_fa{ border-left:1px solid #2f4050; 32.88%; float: left; background:#fff; text-align: center; } .msg_menu_chlid{ border-right:1px solid #fff; border-left:1px solid #fff; min-height: 300px; margin-top: 105px; } .msg_menu_add_chlid{ border: 1px solid #000; 95%; margin: 0px auto; } .msg_menu_add_chlid,.msg_menu_fa{ min-height:57px; line-height: 57px; color: #000; } a { color:#000; } .msg_menu_bord{ border: 1px solid #f4f5f9; 49.70%; height: 560px; float: left; } .msg_menu_bord_border{ border: 1px solid #000; 90%; height: 500px; margin:40px auto 0px; clear: both; } .msg_menu_bord_border_tops{ 90%; height:60px; margin:10px auto; line-height: 60px; } .msg_menu_bord_border_top{ border-bottom: 1px solid #000; } .form-control, .single-line { margin-left: 32px; height: 32px; } .btn-wx{ 68px; height: 30px; line-height: 30px; margin-left: 56px; } .msg_menu_del{ float: right; }
js
var lk = { a:1, arrs:function(a){ var arrs = [a[0],a[1]]; return arrs; }, id:function(e){ return document.getElementById(e); }, die:function(e){ alert(e); }, isUrl:function(url){ var re =/^((https|http|ftp|rtsp|mms)?://)+[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/; if (re.test(url)) { return true; }else{ return false; } }, rel:function(e){ setTimeout(function(){ window.location.reload(); },e) }, auto:function(){ var menu_chlid = document.getElementsByClassName('msg_menu_chlid'); for (var i = 0; i< menu_chlid.length; i++) { var sum_height = 0; for(var j = 0; j <menu_chlid[i].children.length;j++ ){ sum_height += menu_chlid[i].children[j].offsetHeight; } var sum = menu_chlid[i].offsetHeight - sum_height; if(j < 6){ menu_chlid[i].children[0].style.marginTop = sum+"px"; }else if(j == 6){ menu_chlid[i].children[0].remove(); menu_chlid[i].children[0].style.marginTop = "4.2px"; } } } }; (function($,window){ $.wx_add_menu = function(f_menu,inputdata,backfun){ var CLASS_MSG = 'msg_menu_bord_border_tops'; var title = ''; var keyword = ''; var url = ''; var sort = ''; if(inputdata.ac == 'edit'){ title = inputdata.title; keyword = inputdata.keyword; url = inputdata.url; sort = inputdata.sort; } var msg_menu = document.getElementsByClassName('msg_menu_bord_border'); var html = '<div class="'+CLASS_MSG+' msg_menu_bord_border_top"><span style="font-size:18px">菜单信息</span>'; if(inputdata.ac == 'edit'){ html+='<span class="msg_menu_del"><a href="javascript:;" id="del" data-id="'+inputdata.id+'">X</a></span>' } html+='</div><div class="'+CLASS_MSG+'">菜单名称:<input type="text" id="title"value="'+title+'"class="form-control"placeholder="输入菜单名"/></div><div class="'+CLASS_MSG+'">*父级菜单:<select class="form-control"id="pid">'; if(inputdata.ac == 'add'){ html+='<option value="'+inputdata.pid+'">'+inputdata.pname+'</option>'; }else if(inputdata.ac == 'edit'){ var ps = inputdata.pid == 0?'selected = "selected"':''; html+='<option value="0" '+ps+'>选择父类菜单</option>'; for(var o in f_menu){ var p_ss = inputdata.pid == f_menu[o].id?'selected = "selected"':''; html+='<option value="'+f_menu[o].id+'" '+p_ss+'>'+f_menu[o].title+'</option>'; } } html+='</select></div><div class="'+CLASS_MSG+'">*菜单类型:<select class="form-control"id="menu_type">'; if(inputdata.ac == 'add'){ html+='<option value="0">关键词回复菜单</option><option value="1">url链接菜单</option>'; }else if(inputdata.ac == 'edit'){ var bs_u = inputdata.url?'selected = "selected"':''; var bs_k = inputdata.keyword?'selected = "selected"':''; html+='<option value="0" '+bs_k+'>关键词回复菜单</option><option value="1" '+bs_u+'>url链接菜单</option>'; } html+='</select></div><div class="'+CLASS_MSG+'" id="keyword">关联关键词:<input type="text"id="keywords"value="'+keyword+'"class="form-control"placeholder="请输入关键词"/></div>'; html+='<div class="'+CLASS_MSG+'"style="display: none;" id="url_css">*外链接url:<input type="text" id="url"value="'+url+'"class="form-control"placeholder="输入url"/></div><div class="'+CLASS_MSG+'">*菜单排序:<input type="text"id="sort"value="'+sort+'"class="form-control"placeholder="输入序号"/></div><div class="'+CLASS_MSG+'"><a href="javascript:;"class="btn btn-wx">取消</a><a href="javascript:;"class="btn btn-wx">确定</a></div>'; msg_menu[0].innerHTML = html; if(inputdata.ac == 'edit'){ if(inputdata.url){ $.id('url_css').style.display = 'block'; $.id('keyword').style.display = 'none'; }else{ $.id('url_css').style.display = 'none'; $.id('keyword').style.display = 'block'; } } var removePopupElement = function() { while(msg_menu[0].hasChildNodes()) //当div下还存在子节点时 循环继续 { msg_menu[0].removeChild(msg_menu[0].firstChild); } }; var popup = { close: function(index) { var data = {type:index}; if(index == 1){ data.title = $.id('title').value; if(data.title.length < 1){ $.die('菜单信息不能为空');return false; } data.pid = $.id('pid').value; data.menu_type = $.id('menu_type').value; if($.id('menu_type').value == 0){ data.keyword = $.id('keywords').value; data.url = ''; if(data.keyword.length < 1){ $.die('关联关键词不能为空');return false; } }else{ data.keyword = ''; data.url = $.id('url').value; console.log($.id('url').value); if(!$.isUrl(data.url)){ $.die('url格式不对');return false; } } data.sort = $.id('sort').value; if(inputdata.id){ data.id = inputdata.id; } } backfun(data); removePopupElement(); } }; var btn = document.getElementsByClassName('btn-wx'); var handleEvent = function(e){ popup.close($.arrs(btn).indexOf(e.target)); } var menuType = function(){ var a = this.value; var keyword = $.id('keyword'); var url = $.id('url_css'); if(a == 0 ){ keyword.style.display = 'block'; url.style.display = 'none'; }else{ keyword.style.display = 'none'; url.style.display = 'block'; } } var menuDel = function(){ var data = {type:2,id:inputdata.id}; backfun(data); removePopupElement(); } for (i=0; i<btn.length; i++) { var re_obj = btn[i].addEventListener('click', handleEvent); } $.id('menu_type').addEventListener('click', menuType); $.id('del').addEventListener('click', menuDel); } })(lk,window);
php
/** * 微信公众号菜单展示 */ public function wx_creat() { if (IS_POST) { $data = $this->clear_html($_POST); unset($data['type'],$data['menu_type']); if(!$data['id']){ $data['mid'] = $this->mid; $data['is_show'] = 1; //D('Aaep_wxmenu')->data($data)->add(); if(D('Aaep_wxmenu')->data($data)->add()){ $this->dexit(array('error'=>0,'msg'=>'添加成功')); }else{ $this->dexit(array('error'=>1,'msg'=>'添加失败')); } }else{ $menu = D('Aaep_wxmenu')->where(array('id'=>$data['id']))->find(); //检查子父类的修改 if($data['pid'] != $menu['pid']){ $count = D('Aaep_wxmenu')->where(array('pid'=>$data['pid'],'mid'=>$this->mid))->count('*'); if($count == 5 ){ $this->dexit(array('error'=>1,'msg'=>'子类菜单不能多于5个')); } if($data['pid'] == 0 && $count == 3){ $this->dexit(array('error'=>1,'msg'=>'父类菜单不能多于3个')); } } //检查父类 if($menu['pid'] == 0){ $counts = D('Aaep_wxmenu')->where(array('pid'=>$menu['id'],'mid'=>$this->mid))->count('*'); if($counts != 0){ $this->dexit(array('error'=>1,'msg'=>'还有子类不能修改!')); } } $id = $data['id'];unset($data['id']); if (D('Aaep_wxmenu')->data($data)->where(array('mid'=>$this->mid,'id'=>$id))->save()) { $this->dexit(array('error'=>0,'msg'=>'修改成功')); }else{ $this->dexit(array('error'=>1,'msg'=>'修改失败')); } } } $menu = D('Aaep_wxmenu')->field('id,title')->where(array('mid'=>$this->mid,'pid'=>0))->order('sort')->select(); $menuChlid = D('Aaep_wxmenu')->field('id,pid,title')->where(array('mid'=>$this->mid,'pid'=>array('<>',0)))->order('sort')->select(); //dump($menuChlid); $this->assign(array('menu'=>$menu,'menuChlid'=>$menuChlid)); $this->display(); } /** * 查询一个菜单 */ public function wx_find() { $id = $this->clear_html($_POST['id']); $menu = D('Aaep_wxmenu')->where(array('id'=>$id))->find(); $f_menu = D('Aaep_wxmenu')->field('id,title')->where(array('mid'=>$this->mid,'pid'=>0))->select(); $this->dexit(array('error'=>0,'msg'=>$menu,'f_menu'=>$f_menu)); } /** * 删除菜单 */ public function del_ment() { $id = $this->clear_html($_POST['id']); $menu = D('Aaep_wxmenu')->where(array('id'=>$id))->find(); if($menu['pid'] == 0){ $conut = D('Aaep_wxmenu')->where(array('pid'=>$id,'mid'=>$this->mid))->count('*'); if($conut != 0){ $this->dexit(array('error'=>1,'msg'=>'还有子类菜单不能删除!')); } } if(D('Aaep_wxmenu')->where(array('id'=>$id))->delete()){ $this->dexit(array('error'=>0,'msg'=>'删除成功')); }else{ $this->dexit(array('error'=>1,'msg'=>'删除失败')); } } /** * 一键生成微信菜单 * admin:方法条件 */ public function add_wx_menu() { if(IS_POST){ if ($this->clear_html($_POST['admin']) == 'yepai') { $re = $this->createmenu(); if($re == 1){ $this->dexit(array('error'=>0,'msg'=>'微信公众号菜单生成成功')); }else{ $this->dexit(array('error'=>1,'msg'=>'微信公众号菜单生成失败'.$re)); } } } } //创建菜单操作 public function createmenu() { $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$this->getAccessToken(); $where1 = array('pid'=>0,'mid'=>$this->mid); $wxmenu_model = D('Aaep_wxmenu'); $class = $wxmenu_model->where($where1)->order('sort')->limit('3')->select(); $kcount =$wxmenu_model->where($where1)->count('*'); $data = '{"button":['; foreach($class as $key=>$vo){ $data.='{"name":"'.$vo['title'].'",'; $where2 = array('pid'=>$vo['id'],'mid'=>$this->mid); $c = $wxmenu_model->where($where2)->order('sort')->select(); $count = $wxmenu_model->where($where2)->count('*'); if($c!=false){ $data.='"sub_button":['; }else{ if($vo['keyword']){ $data.='"type":"click","key":"'.$vo['keyword'].'"'; }else if($vo['url']){ $data.='"type":"view","url":"'.htmlspecialchars_decode($vo['url']).'"'; }else if($vo['wxsys']){ $data.='"type":"'.$this->_get_sys('send',$vo['wxsys']).'","key":"'.$vo['wxsys'].'"'; } } $i=1; foreach($c as $voo){ if($i==$count){ if($voo['keyword']){ $data.='{"type":"click","name":"'.$voo['title'].'","key":"'.$voo['keyword'].'"}'; }else if($voo['url']){ $data.='{"type":"view","name":"'.$voo['title'].'","url":"'.htmlspecialchars_decode($voo['url']).'"}'; }else if($voo['wxsys']){ $data.='{"type":"'.$this->_get_sys('send',$voo['wxsys']).'","name":"'.$voo['title'].'","key":"'.$voo['wxsys'].'"}'; } }else{ if($voo['keyword']){ $data.='{"type":"click","name":"'.$voo['title'].'","key":"'.$voo['keyword'].'"},'; }else if($voo['url']){ $data.='{"type":"view","name":"'.$voo['title'].'","url":"'.htmlspecialchars_decode($voo['url']).'"},'; }else if($voo['wxsys']){ $data.='{"type":"'.$this->_get_sys('send',$voo['wxsys']).'","name":"'.$voo['title'].'","key":"'.$voo['wxsys'].'"},'; } } $i++; } if($c!=false){ $data.=']'; } if($k==$kcount){ $data.='}'; }else{ $data.='},'; } $k++; } $data.=']}'; //========================================== //3.发送请求 $content = $this->request($url,true,'post',$data); //4.处理返回值 $content = json_decode($content); if($content->errmsg == 'ok'){ return 1; }else{ //echo '创建菜单不成功!'.'<br />'; return $content->errcode; } } private function _get_sys($type='',$key='') { $wxsys = array( '扫码带提示', '扫码推事件', '系统拍照发图', '拍照或者相册发图', '微信相册发图', '发送位置', ); if($type == 'send'){ $wxsys = array( '扫码带提示'=>'scancode_waitmsg', '扫码推事件'=>'scancode_push', '系统拍照发图'=>'pic_sysphoto', '拍照或者相册发图'=>'pic_photo_or_album', '微信相册发图'=>'pic_weixin', '发送位置'=>'location_select', ); return $wxsys[$key]; exit; } return $wxsys; } public function getAccessToken() { $filename = './cache/token/accesstoken'.$this->mid; if(!file_exists($filename) || (file_exists($filename) && (time()-filemtime($filename)) > 4000)){ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appsecret; $content = $this->request($url); $content = json_decode($content); $access_token = $content->access_token; file_put_contents($filename, $access_token); }else{ $access_token = file_get_contents($filename); } return $access_token; } public function request($url,$https=true,$method='get',$data=null) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($https === true){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if($method == 'post'){ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $str = curl_exec($ch); curl_close($ch); return $str; }
mysql
CREATE TABLE IF NOT EXISTS `ep_aaep_wxmenu` ( `id` int(11) NOT NULL, `mer_id` int(11) NOT NULL, `pid` int(11) NOT NULL, `title` varchar(30) NOT NULL, `keyword` varchar(30) NOT NULL, `is_show` tinyint(1) NOT NULL, `sort` tinyint(3) NOT NULL, `url` char(255) NOT NULL, `wxsys` int(11) NOT NULL, `mid` int(11) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4; ALTER TABLE `ep_aaep_wxmenu` ADD PRIMARY KEY (`id`); ALTER TABLE `ep_aaep_wxmenu` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=19;