您的位置:首页 > 运维架构 > 网站架构

php实现微信网站获取用户信息的功能

2017-03-24 09:14 781 查看
php实现微信网站获取用户信息的功能,服务号获取信息经测试没得问题,订阅号这个未测试,希望测试过了的朋友能够回复指导一下。

<?php
use think\Controller;
use think\Db;

/**
* 作者 http://www.5atl.com *
*/
class Weilogin extends Controller
{

/**
* 参考资料详见 https://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html */

/**
* 订阅号
*/
// private $appid = 'wx149978cf463*****e';

// private $secret = '33f0d0314477c77fdd8d85***f';

/**
* 服务号
*/
private $appid = 'wx9916a5a3d3e*****c';

private $secret = 'e6d756b57d54d27cc03****17e63';

private $token = '';

private $openid = '';

private $reflash = '';

/**
* 授权接口
*
* @param unknown $appid
* @param unknown $jumpto
*/
public function index()
{
$weichatflat = true;'http://***********'; // 放获取微信信息的跳转链接
$bind = input('bind'); // 用作绑定还是登录
$url = '';

if ($weichatflat == true) { // 服务号使用
$jumpto = 'http://**********/weilogin/getinfo?bind=' . $bind;
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $this->appid . '&redirect_uri=' . $jumpto . '&response_type=code&scope=snsapi_userinfo&state=' . $bind . '#wechat_redirect';
} else { // 订阅号服务号都可以使用
$jumpto = 'http://**********/weilogin/getopenid?bind=' . $bind;
$url = $weichatflat . '/get-weixin-code.html?appid=' . $this->appid . '&redirect_uri=' . $jumpto . '&response_type=code&scope=snsapi_base&state=' . $bind . '#wechat_redirect';
}

return redirect($url);
}

/**
* 获取微信id
*/
public function getOpenid()
{
$bind = input('bind'); // 用作绑定还是登录

$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . input('code') . '&grant_type=authorization_code';

$json = file_get_contents($url);

$json = json_decode($json, true);

return $this->bindUserBase($json, $bind);
}

/**
* 订阅号登录或绑定
* @param unknown json带的值openid
* @param unknown 绑定还是登录
* @return \think\response\Redirect
*/
public function bindUserBase($json, $bind)
{
if ($bind == 0) {
$hasUser = Db::name('**************')->where('wxid', $json['openid'])->find();
session('oa_cid', $hasUser['cid']); // 公司id
session('oa_uid', $hasUser['mid']); // 用户ID
session('oa_username', $hasUser['username']); // 用户名
session('oa_portrait', $hasUser['face']); // 用户头像
session('oa_pmid', $hasUser['pmid']); // 上级id
return redirect(url('index/index'));
} else {
$mid = session('oa_uid');
$param['wxid'] = $json['openid'];
Db::name('************')->save($param, [
'mid' => $mid
]);
$this->success('绑定微信成功', url('index/index'));
}
}

/**
* 根据公众号appid获取用户的基本信息
*
* @param unknown $appid
* @return mixed
*/
public function getInfo()
{
$bind = input('bind'); // 用作绑定还是登录

// 1. 拿到token
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . input('code') . '&grant_type=authorization_code';
$reback = file_get_contents($url);
$json = json_decode($reback, true);

$this->token = $json['access_token'];
$this->openid = $json['openid'];
$this->reflash = $json['refresh_token'];

// 2. token是否过期
// $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $this->token . '&openid=' . $this->openid;
// $reback = file_get_contents($url);
// $json = json_decode($reback, true);

// 3. token过期后重新拿刷新,跟步骤1一样的结果
// if (array_key_exists('errcode', $json) && $json['errcode'] == 40001) {
// $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=' . $this->appid . '&grant_type=refresh_token&refresh_token=' . $this->reflash;
// $reback = file_get_contents($url);
// $json = json_decode($reback, true);

// // 新的token和openid
// $this->token = $json['access_token'];
// $this->openid = $json['openid'];
// $this->reflash = $json['refresh_token'];
// }

// 4. 拿详细信息了
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $this->token . '&openid=' . $this->openid . '&lang=zh_CN';
$reback = file_get_contents($url);
$json = json_decode($reback, true);
if (array_key_exists('errcode', $json)) {
$this->error($reback . '<br/>token=' . $this->token . '<br/>openid=' . $this->openid, null, null, 10);
}

// 5.绑定信息获登录
return $this->bindUserInfo($json, $reback, $bind);
}

/**
* 服务号登录还是绑定
* @param unknown $json openid,
* @param unknown $reback
* @param unknown $bind
* @return \think\response\Redirect
*/
public function bindUserInfo($json, $reback, $bind)
{
if ($bind == 0) {
$hasUser = Db::name('********')->where('wxid', $json['openid'])->find();

if ($hasUser == null) {
$this->error('对不起,您的账号还未绑定微信,请使用OA账号的鞥路', url('login/index'));
}

session('oa_cid', $hasUser['cid']); // 公司id
session('oa_uid', $hasUser['mid']); // 用户ID
session('oa_username', $hasUser['username']); // 用户名
session('oa_portrait', $hasUser['face']); // 用户头像
session('oa_pmid', $hasUser['pmid']); // 上级id
return redirect(url('index/index'));
} else {
$mid = session('oa_uid');
$param['face'] = $json['headimgurl'];
$param['wxid'] = $json['openid'];
$param['wxinfo'] = $reback;
Db::name('********')->where('mid', $mid)->update($param);
$this->success('绑定微信成功', url('index/index'));
}
}
}
?>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: