Skip to content

Instantly share code, notes, and snippets.

@wuliupo
Created May 5, 2017 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuliupo/a9f73150a94c7469855d344dff81d53a to your computer and use it in GitHub Desktop.
Save wuliupo/a9f73150a94c7469855d344dff81d53a to your computer and use it in GitHub Desktop.
WeixinController base in ThinkPHP
<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class WeixinController extends CommonController{
private $appid;
private $appsecret;
public function _initialize(){
//第1步 appid, appsecret
$this->checkConfig('appid');
$this->checkConfig('appsecret');
}
public function _empty(){
echo '';
}
public function index($openid = 0, $ajax = false){
if(empty($openid)){
$openid = cookie('openid');
}
if(!empty($openid)){ //本地浏览器调试,不需要提示微信
$data = $this->wxuser($openid);
if(!empty($data)){
echo json_encode($data);
exit(0);
}
}
//只是判断是否取用户数据,没有办法进行转发重定向
if($ajax){
echo json_encode(array('error' => 'no_login', 'openid' => $openid, 'appid' => ($this->appid)));
exit(0);
}
if(empty($openid)){
$this->all();
} else {
$this->openid($openid);
}
}
// 获取方法1,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid)
// https://mp.weixin.qq.com/advanced/wiki?t=home/index&id=mp1421140842&token=&lang=zh_CN
public function base(){
$this->oauth2('snsapi_base');
}
// 获取方法2,snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
public function all(){
$this->oauth2('snsapi_userinfo');
}
// 用于微信服务器回调
public function info($code, $state, $method2, $id2){
//第4步,根据code,获取用户信息
if(empty($code)) {
echo 'ERROR: no code';
exit(0);
}
if(empty($state)) {
$state = $id2;
}
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid = {$this->appid}&secret = {$this->appsecret}&code = $code&grant_type = authorization_code";
$info = $this->httpGet($url);
$info = json_decode($info,true);
$access_token = $info['access_token'];
$openid = $info['openid'];
// 已有用户,不需要获得用户信息。如果删除下面代码,稍后会更新用户信息到数据库
$data = $this->wxuser($openid);
if(!empty($data)){
$this->goApp($openid, $method2, $state);
}
//第5步,刷新 access_token
//$refresh_token = $info['refresh_token'];
//$url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid = $appid&grant_type = refresh_token&refresh_token = $refresh_token";
//第6步,获取用户信息
$this->getUser("https://api.weixin.qq.com/sns/userinfo?access_token = $access_token&openid = $openid&lang = zh_CN", $method2, $state);
}
// 获取方法3,通过openid获得公众号粉丝信息
//http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html
public function openid($openid){
//第2步,获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
$info = $this->httpGet($url);
$info = json_decode($info,true);
$access_token = $info['access_token'];
//第3步,获取用户信息
$this->getUser("https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid");
}
private function getUser($url, $method2, $state){
$userinfo = $this->httpGet($url);
$userinfo = json_decode($userinfo, true);
$openid = $userinfo['openid'];
$nickname = $userinfo['nickname'];
if(empty($openid) || empty($nickname)){
echo json_encode(array('error' => 'no openid or nickname'));
exit(0);
}
$file = 'upload/'.time().mt_rand().'.jpg';
@file_put_contents('../'.$file, @$this->httpGet($userinfo['headimgurl']));
$data = M('data');
$user = $this->wxuser($openid);
$user1 = array(
'openid' => $openid,
'title' => $nickname,
'img1' => $file,
'types' => 'wxuser',
'status' => 1
);
if(empty($user)){
$user1['createdate'] = time();
$data->add($user1);
}else{ // 更新用户信息到数据库
$user1['id'] = $user['id'];
$data->save($user1);
}
$this->goApp($openid, $method2, $state);
}
//http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
private function oauth2($scope){
//第3步,跳转到用户同意授权页面,获取code
$redirect_uri = C('DATA_URL')."data/?c = weixin&a = info";
$redirect_uri = urlencode($redirect_uri);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=0#wechat_redirect";
header("Location:$url");
exit(0);
}
private function wxuser($openid){
return M('data')->where(' types = "wxuser" AND openid = "'.$openid.'"')->find();
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function checkConfig($key){
$val = $this->getField($key);
if(empty($val)){
echo 'ERROR: no '.$key;
exit(0);
}
$this->$key = $val;
}
public function sign(){
// https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
// http://demo.open.weixin.qq.com/jssdk/sample.zip
require_once 'jssdk.php';
$jssdk = new \JSSDK($this->appid, $this->appsecret);
$signPackage = $jssdk->GetSignPackage();
echo json_encode($signPackage);
}
private function goApp($openid, $method, $id){
cookie('openid', $openid);
header('Location: ../'.((empty($method) || empty($id)) ? '' : "#/$method/$id"));
}
// /data/?c = weixin&a = requested
public function notice($type = ''){
require_once 'jssdk.php';
$jssdk = new \JSSDK($this->appid, $this->appsecret);
/*
$_POST = array(
'OPENID' => 'oFZdnt6VnwcrbrA4RwJ0yIwhEwSE',
'ITEMID' => 30615,
'USERNAME' => 'pauli',
'CATALOG' => '私家烘焙 > 卤味烧烤',
'CREATETIME' => '2016-06-06 06:06:06'
);
*/
$_POST['CREATETIME'] = gmdate('Y-m-d H:i:s', time() + 3600 * 8);
echo $jssdk->setMsg($type, $_POST);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment