博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CI框架微信开发-自定义菜单
阅读量:4955 次
发布时间:2019-06-12

本文共 7280 字,大约阅读时间需要 24 分钟。

在CI框架下面实现了自定义菜单功能.写了一个model,一个类库.顺便附带access_token的实现方式

dolist(); return $this->setmenu(); } private function dolist(){ $CI =& get_instance(); $CI -> load ->model("Menu_model","menu"); $plist = $CI->menu ->isplist(); foreach($plist as $pid){ $pidarr[] = $pid['pid']; } $list = $CI->menu ->maketree($CI->menu->getlist()); foreach($list as $btn){ if(in_array($btn['id'],$pidarr)){ //生成不带key和url的链接作为父级菜单 $btn_arr[$btn['id']] = array("type"=>$btn['menutype'], "name"=>$btn['content']); }elseif($btn['pid'] == 0){ //生成有操作的一级菜单 $btn_arr[$btn['id']] = array("type"=>$btn['menutype'], "name"=>$btn['content'], "key"=>$btn['clickkey'], "url"=>$btn['url']); }else{ //生成子菜单 $btn_arr[$btn['pid']]['sub_button'][] = array("type"=>$btn['menutype'], "name"=>$btn['content'], "key"=>$btn['clickkey'], "url"=>$btn['url']); } } $btnarr['button'] = array_values($btn_arr); $r = $this->menustr = json_encode($btnarr,JSON_UNESCAPED_UNICODE); return $r; } private function setmenu(){ $accesstoken = get_access_token(); $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={
$accesstoken}"; $ch = curl_init(); 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($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, $this->menustr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $info = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $info; }}

 

上面是library里面的内容,主要是根据数据表生成菜单的json串

load->database(); $this->table_name = "data_menu"; } public function query($sql){ return $this->db->query($sql); } public function getone($id){ $get_sql = "select * from {
$this->table_name} where id = {
$id}"; return $this->query($get_sql)->row(); } public function addone($data){ if(($data['pid'] == 0)&&($this->checksum()>=3)){ //一级菜单不超过3个 return "toomany1"; }elseif(($data['pid']!=0)&&($this->checksum($data['pid']))>=7){ //二级菜单不超过7个 return "toomany2"; } if(is_array($data)&&!empty($data)){ $keys = "`".implode("`,`",array_keys($data))."`"; $vals = "'".implode("','",array_values($data))."'"; $insert_sql = "insert into {
$this->table_name} ($keys) values ($vals)"; return $this->query($insert_sql); }else{ return false; } } public function del($id){ $infos = $this->getone($id); $del_sql = "delete from {
$this->table_name} where id = {
$id} and pid = {
$id}"; return $this->query($del_sql); } private function checksum($id = ''){ if($id == ''){ $get_sql = "select count(1) as total from {
$this->table_name} where pid =0"; }else{ $id = intval($id); $get_sql = "select count(1) as total from {
$this->table_name} where pid ={
$id}"; } $r = $this->db->query($get_sql)->row(); return $r->total; } public function getplist(){ //获取一级菜单 $get_sql = "select * from {
$this->table_name} where pid=0 order by menuorder asc"; return $this->db->query($get_sql)->result_array(); } public function isplist(){ $get_sql = "select pid from {
$this->table_name} where pid <> 0 group by pid"; return $this->db->query($get_sql)->result_array(); } public function getlist(){ $get_sql = "select * from {
$this->table_name} where 1 order by pid asc, menuorder asc"; return $this->db->query($get_sql)->result_array(); } public function maketree($data){ $pids = array(); foreach($data as $k=>$v){ if($v['pid'] == 0){ $pids[$v['id']][] = $v; }else{ $pids[$v['pid']][] = $v; } } list($t1,$t2,$t3) = array_values($pids); $r = array_merge_recursive(is_array($t1)?$t1:array(),is_array($t2)?$t2:array(),is_array($t3)?$t3:array()); return $r; } public function update($data){ if(is_array($data)&&!empty($data)){ $id = $data['id']; unset($data['id']); foreach($data as $k=>$v){ $update_arr[] = "`".$k."` = '".$v."'"; } $update_fs = implode(",",$update_arr); $update_sql = "update {
$this->table_name} set {
$update_fs} where id = {
$id}"; return $this->query($update_sql); }else{ return false; } }}

 

上面是model里面的各种方法.

数据库的表结构如下,附创建表的语句.

CREATE TABLE `menu` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `content` varchar(20) DEFAULT NULL,  `pid` int(11) DEFAULT '0',  `menutype` enum('click','view','scancode_push','scancode_waitmsg','pic_sysphoto','pic_photo_or_album','pic_weixin','location_select') DEFAULT 'view' COMMENT '消息类型',  `url` varchar(200) DEFAULT NULL COMMENT '链接地址',  `clickkey` varchar(20) DEFAULT NULL COMMENT '事件KEY',  `menuorder` int(11) DEFAULT NULL COMMENT '排序',  `submenu` tinyint(2) DEFAULT '0',  PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8

 

 

  Field Type Comment
主键 id int(11) NOT NULL <ID>
  content varchar(20) NULL <内容>
  pid int(11) NULL <父类ID>
  menutype enum('click','view','scancode_push','scancode_waitmsg','pic_sysphoto','pic_photo_or_album','pic_weixin','location_select') NULL 消息类型
  url varchar(200) NULL 链接地址
  clickkey varchar(20) NULL 事件KEY
  menuorder int(11) NULL 排序
  submenu tinyint(2) NULL <是否是子菜单>

下面是写在system/core/common.php下面的获取token的方法,其实要做一个加盐处理,要么会有恶心的人做恶心的事情.

function get_access_token(){        //从微信服务器获取access_token  并保留一个小时        $old_filename = APPPATH."cache/".md5(date("YmdH",time()-3600)).".php";        @unlink($old_filename);        $filename = APPPATH."cache/".md5(date("YmdH",time())).".php";        if(is_file($filename)){            $r = include($filename);        }else{            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;            $access_token =  file_get_contents($url);            $res = "

 

 

前面的菜单管理就不写了,就是管理那个表的数据,保证数据表里面的数据没问题即可.

在控制器里面只需要

$this->load->library("Makemenu");

然后调用  $this->makemenu->dolist();

就会推送到微信的服务器.   还需要注意在入口文件定义两个常量   APPID和APPSECRET    .

放出来给大家,希望有用,也给我自己备份个.

转载于:https://www.cnblogs.com/swordphp/p/4159190.html

你可能感兴趣的文章
关于linux下jdk的安装与环境配置(来自朋友Janie)
查看>>
Python数据分析之numpy学习
查看>>
maven的setting,仓库连接为阿里云
查看>>
40款非常棒的 jQuery 插件和制作教程(系列一)
查看>>
[leetcode]Divide and Conquer-169. Majority Element
查看>>
IntegrityError错误
查看>>
爱情语录
查看>>
sql server死锁神器
查看>>
虚函数和纯虚函数
查看>>
43. Multiply Strings 字符串相乘
查看>>
python入门学习2
查看>>
centos 安装php扩展的两种方法
查看>>
实验四 用信号量解决进程互斥与同步问题
查看>>
To execute Mr.LDA
查看>>
数据库特定SQL分页pdf
查看>>
About Face 3:交互设计精髓pdf
查看>>
jsday8
查看>>
DFS之城堡问题
查看>>
Implement Stack using Queues
查看>>
理解javascript中的回调函数(callback)【转】
查看>>