关注   一半是海水qq

openant 封装微信sdk的用法示例,以获取微信公众号菜单为例说明

访问:72 回复:0 关注:2
板块:教程 由:一半是海水qq 发布于:1年前

1.到git仓库下载wechat sdk代码包

2.解压文件,把wechat文件夹复制到你的项目的libraries目录下

3.打开wechat/lib/Wechat_basic.php文件,找到235行如下代码,新建一张表来保存wechat的access_token,当然这不是必须的,你也可以把它的三个设置cache的方法改成存储在临时文件中(需要你自行修改)

setting表结构
     * 
     * -- ----------------------------
 --  Table structure for `setting`
 -- ----------------------------
 DROP TABLE IF EXISTS `setting`;
 CREATE TABLE `setting` (
   `setting_id` int(11) NOT NULL AUTO_INCREMENT,
   `store_id` int(11) NOT NULL DEFAULT '0',
   `code` varchar(32) NOT NULL,
   `key` varchar(64) NOT NULL,
   `value` text NOT NULL,
   `serialized` tinyint(1) NOT NULL,
   PRIMARY KEY (`setting_id`)
 ) ENGINE=MyISAM AUTO_INCREMENT=95 DEFAULT CHARSET=utf8;

4.在控制器新建一个php文件,这里我是把它放在了openant项目admin下的全局插件中,

./admin\controllers\extension_config\overall/wechat_menu.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Wechat_menu extends MY_Controller {
 
public function __construct() {
  parent::__construct();
  # 配置参数
  $config = array(
  'token'          => $this->config->get_config('encodingaeskey', 'wechat', '0'),
  'appid'          => $this->config->get_config('appID', 'wechat', '0'),
  'appsecret'      => $this->config->get_config('appsecret', 'wechat', '0'),
  'encodingaeskey' => $this->config->get_config('encodingaeskey', 'wechat', '0')
  );
  
  # 加载对应操作接口
  //文件夹名注意大写
  $this->load->library('Wechat/wechat_menu', $config);
  
  
  $this->load->language('wecome');
  if(!$this->user->hasPermission('access', 'admin/extension_config/overall/wechat_menu')){
   $this->session->set_flashdata('fali', '你没有访问权限!');
   redirect(site_url());
   exit;
  }
 }

 public function index()
 {
  $this->document->setTitle('微信公众号菜单设置');
  
  if($_SERVER['REQUEST_METHOD']=="POST" && $this->check_modify()){
   
   $menus=array();
   foreach($this->input->post()['menu']['button'] as $key=>$value){
    
   }
   var_dump($this->input->post()['menu']);
   //redirect(site_url('common/extension/overall'));
   exit;
  }
  
  $data['action']=array(
    'click'      =>'点击事件',
    'view'      =>'跳转URL',
    'scancode_push'    =>'扫码推事件',
    'scancode_waitmsg'   =>'扫码推事件且弹出“消息接收中”提示框',
    'pic_sysphoto'    =>'弹出系统拍照发图',
    'pic_photo_or_album'  =>'弹出拍照或者相册发图',
    'pic_weixin'    =>'弹出微信相册发图器',
    'location_select'   =>'弹出地理位置选择器',
    'media_id'     =>'下发消息',
    'view_limited'    =>'跳转图文消息URL'
  );
  
  $data['menus'] = $this->wechat_menu->getMenu();
  
  $data['header']=$this->header->index();
  $data['top']=$this->header->top();
  $data['footer']=$this->footer->index();
  
  $this->load->view('theme/default/template/extension_config/overall/wechat_menu',$data);
 }
 
 public function check_modify(){
  if (!$this->user->hasPermission('modify', 'admin/extension_config/overall/wechat_menu')) {
   $this->session->set_flashdata('danger', '你无权修改,请联系管理员!');
   redirect(site_url('common/extension/overall'));
   exit();
  }else {
   return true;
  }
 }
}
5.新建一个视图文件
./admin\views\theme\default\template\extension_config\overall/wecaht_menu.php

<?php echo $header;//装载header?>
<?php echo $top;//装载top?>
<!-- /widget-header -->
<form action="<?php echo $this->config->item('admin');?>/extension_config/overall/wechat_menu" method="post" enctype="multipart/form-data" id="form-module" class="form-horizontal"> <?php $row_top='0';?> <?php if(!empty($menus)):?> <?php foreach($menus['menu']['button'] as $key=>$value):?> <?php if(isset($value['sub_button']) && !empty($value['sub_button'])):?> <?php if(isset($value['key'])):?> <?php elseif(isset($value['url'])):?> <?php else:?> <?php endif;?> <?php $row_child='0';?> <?php foreach ($value['sub_button'] as $k=>$v):?> <?php if(isset($v['key'])):?> <?php elseif(isset($v['url'])):?> <?php else:?> <?php endif;?> <?php $row_child++;?> <?php endforeach;?> <?php else:?> <?php if(isset($value['key'])):?> <?php elseif(isset($value['url'])):?> <?php else:?> <?php endif;?> <?php endif;?> <?php $row_top++;?> <?php endforeach;?> <?php endif;?>
一级菜单 二级菜单 事件 关键词/链接 操作
<button type="button" class="btn btn-info btn-sm" onclick="add_top();">添加一级菜单</button>
</form> <!-- /area-chart -->
<!-- /widget-content -->
<!-- /widget -->
<!-- /span6 -->
<!-- /row --> [removed] var row_child = <?php echo $row_child + 1; ?>; function add_child(row_top){ if($('.menu-top-'+row_top).length > 4){ alert('最多添加5个子菜单'); }else{ add_child_html(row_top, row_child); } check_top(row_top); row_child++; } function check_top(row_top){ if($('tbody tr').hasClass('menu-top-' + row_top)){ $('tbody tr #menu-button-' + row_top + '-type').attr("disabled", "disabled"); $('tbody tr #menu-button-' + row_top + '-key-url').attr("disabled", "disabled"); }else{ $('tbody tr #menu-button-' + row_top + '-type').removeAttr("disabled"); $('tbody tr #menu-button-' + row_top + '-key-url').removeAttr("disabled"); } } function add_child_html(row_top, row_child){ html = ''; html += '<input type="text" class="form-control menu-button-' + row_top + '-name" placeholder="一级菜单名" value="" disabled="disabled">'; html += '<input type="text" class="form-control" id="menu-button-' + row_top + '-sub_button-' + row_child + '-name" name="menu[button][' + row_top + '][sub_button][' + row_child + '][name]" placeholder="二级菜单名" value="">'; html += ''; html += '<select class="form-control" id="menu-button-' + row_top + '-sub_button-' + row_child + '-type" name="menu[button][' + row_top + '][sub_button][' + row_child + '][type]" onchange="change_child_key(\\'' + row_top + ', \\'' + row_child + '\\');">'; html += ''; <?php foreach ($action as $ke=>$va):?> html += ''; <?php endforeach;?> html += '</select>'; html += ''; html += '<input type="text" class="form-control" id="menu-button-' + row_top + '-sub_button-' + row_child + '-key-url" name="menu[button][' + row_top + '][sub_button][' + row_child + '][key]" placeholder="关键词/链接" value="">'; html += '<button type="button" class="btn btn-danger btn-sm" onclick="$(\\'#menu-top-' + row_top + row_child + '\\').remove();check_top(' + row_top + ');">删除</button>'; html += ''; if($('tbody tr').hasClass('menu-top-' + row_top)){ $('.menu-top-' + row_top +':last').after(html); }else{ $('#menu-top-' + row_top).after(html); } $('.menu-button-' + row_top + '-name').val($('#menu-button-' + row_top + '-name').val()); } var row_top = <?php echo $row_top + 1;?>; function add_top(){ if($('tbody .menu-top').length > 2){ alert('最多添加3个一级菜单'); }else{ add_top_html(row_top); } row_top++; } function add_top_html(row_top){ html = ''; html += '<input type="text" onblur="change_topname(\\'' + row_top + '\\');" class="form-control" id="menu-button-' + row_top + '-name" name="menu[button][' + row_top + '][name]" placeholder="一级菜单名" value="">'; html += ''; html += '<select class="form-control" id="menu-button-' + row_top + '-type" name="menu[button][' + row_top + '][type]" onchange="change_top_key(\\'' + row_top + '\\');">'; html += ''; <?php foreach ($action as $k=>$v):?> html += ''; <?php endforeach;?> html += '</select>'; html += ''; html += '<input type="text" class="form-control" id="menu-button-' + row_top + '-key-url" name="menu[button][' + row_top + '][key]" placeholder="关键词/链接" value="">'; html += ''; html += '
'; html += '<button type="button" class="btn btn-info btn-sm" onclick="add_child(\\'' + row_top + '\\');">添加子菜单</button>'; html += '<button type="button" class="btn btn-danger btn-sm" onclick="$(\\'#menu-top-' + row_top + '\\').remove();$(\\'.menu-top-' + row_top + '\\').remove();">删除</button>'; html += '
'; html += ''; html += ''; if($('tbody tr').hasClass('menu-top')){ $('tbody tr:last').after(html); }else{ $('tbody').append(html); } } function change_topname(row_top){ $('.menu-button-' + row_top + '-name').val($('#menu-button-' + row_top + '-name').val()); } function change_top_key(row_top){ if($('#menu-button-' + row_top + '-type').val() == 'view'){ $('#menu-button-' + row_top + '-key-url').attr("name", "menu[button][" + row_top + "][url]"); }else{ $('#menu-button-' + row_top + '-key-url').attr("name", "menu[button][" + row_top + "][key]"); } } function change_child_key(row_top, row_child){ if($('#menu-button-' + row_top + '-sub_button-' + row_child + '-type').val() == 'view'){ $('#menu-button-' + row_top + '-sub_button-' + row_child + '-key-url').attr("name", "menu[button][" + row_top + "][sub_button][" + row_child + "][url]"); }else{ $('#menu-button-' + row_top + '-sub_button-' + row_child + '-key-url').attr("name", "menu[button][" + row_top + "][sub_button][" + row_child + "][key]"); } } [removed]
<!-- /container --> <?php echo $footer;//装载header?>

这就是全部,但是值得申明的是,这只是我在项目中使用的实例,wechat sdk类库封装可能适合你,但是上面这两段代码对你豪无用处,除非你使用的是openant, 否则你需要按你项目来设计业务逻辑

回复数量0
*请文明回帖,激烈讨论!
*单张图片上传最大200kb
*回帖内容2000字符内
*链接请直接输入"href"标签将会被过滤
 Ctrl+Enter