基本信息
源码名称:微信公共号开发基础接口 thinkphp实例
源码大小:0.05M
文件格式:.rar
开发语言:PHP
更新时间:2016-12-11
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


<?php
namespace Home\Controller;
use Think\Controller;
use Org\Net\Wechat;
class IndexController extends Controller {
protected $weObj;
private $apihost = "http://你的网址";
// private $redirect_uri = $this->apihost."/wx.php/Home/Index/callback";
public function _initialize(){
    $options = C('WX_CONFIG');
  $this->weObj = new Wechat($options);
}
    public function index(){
   
    $this->weObj->valid();
    $type = $this->weObj->getRev()->getRevType();
   switch($type) {
    case Wechat::MSGTYPE_TEXT:
    $this->weObj->text("hello, I'm wechat")->reply();
    exit;
    break;
    case Wechat::MSGTYPE_EVENT:
    $event = $this->weObj->getRev()->getRevEvent();
    $openid = $this->weObj->getRev()->getRevFrom();
    if($event['event'] == 'subscribe'){
   
    $id = $this->addUser($openid);
    $this->weObj->text("您是第".$id."个用户")->reply();
    }
    if($event['event'] =='CLICK' && $event['key'] == 'MENU_KEY_NEWS')
    {
    $this->weObj->text('<a href="'.$this->apihost.'/wx.php/Home/Index/userInfo/openid/'.$openid.'">点击查看用户资料</a>')->reply();
    }

    break;
    case Wechat::MSGTYPE_IMAGE:
    break;
    default:
    $this->weObj->text("help info")->reply();
   }
    }
    private function addUser($openid){
    $mod = D('User');
    $res = $mod->where(array('openid'=>$openid))->getField('id');
    if($res) return $res;
    $data = $this->weObj->getUserInfo($openid);
    $data['login_ip'] = get_client_ip();
    $data['add_time'] = time();
    $res = $mod->add($data);
    return $res; 
    // $mod->add
    }
    public function userInfo($openid){
    $openid = I('get.openid');
    if(!$openid) return false;
    $data = D('User')->where(array('openid'=>$openid))->find();
    foreach ($data as $k => $v) {
    echo "<p>".$k.'---'.$v."</p>";
    }
    }
    public function menu(){
     //获取菜单操作:
    $menu = $this->weObj->getMenu();
   //设置菜单
    $newmenu =  array(
    "button"=>
    array(
    array('type'=>'click','name'=>'查看用户消息','key'=>'MENU_KEY_NEWS'),
    array('type'=>'view','name'=>'登录查看消息','url'=>$this->apihost.'/wx.php/Home/Index/getUser'),
    )
    );
    $result = $this->weObj->createMenu($newmenu);

    }
    public function getUser($value='')
    {
    $options = C('WX_CONFIG');
    $auth = new \Org\Net\WechatAuth($options['appid'],$options['appsecret']);
    $url = $auth->getRequestCodeURL($redirect_uri=$this->apihost."/wx.php/Home/Index/callback", '','snsapi_base');
    redirect($url);
    }
    public function callback(){
    $options = C('WX_CONFIG');
    $auth = new \Org\Net\WechatAuth($options['appid'],$options['appsecret']);
    $code = I('get.code');
    $auth->getAccessToken('code', $code);
    $openid = $auth->openid;
    $data = $auth->getUserInfo($openid, $lang = 'zh_CN');
    foreach ($data as $k => $v) {
    if($k =='headimgurl') echo '<img src="'.$v.'" alt="">';
    echo "<div>".$k."---".$v."</div>";
    }
    }


}