上为背景,或许有人会说用open.wx.qq.com绑定然后统一USER表用户,但开放平台只能绑定10个公众号,对于一般的平台是够了,那如果是做投票或分销呢?显然是不够的,
我的思路是用户关注后不自动生成一个USER表用户,当用户点击菜单时在此做文章,
通过用auth去获取该链接的公众号配置,取出openid用 session('openid', $openid);将它存起来,再查询此openid表中绑定USER表中的名称是否存在,因为没有开启自动注册USER表第一次是不存在的,
接下来我们用一个默认的公众号去取当前用户的openid,(不要问我有什么用往下看),取得openid后我们就将它更新到数据库如:原存openid的字段为wxid,那我们新建一个字段,openid将刚刚获取的更新过来,同时USER生成一个新用户与微信绑定,哎!我也不知道在说什么了,自己看代码!测试通过的,主要用与多公众号时用户表数据统一,也是前台登陆时重要的一环!!!
class WechatController extends Controller{
/**
* 微信消息接口入口
* 所有发送到微信的消息都会推送到该操作
* 所以,微信公众平台后台填写的api地址则为该操作的访问地址
*/
public function index(){
$oid = $_GET['oid'];
if($oid!=''){
$wxid = M('wxch_oauth')->getFieldByOid($oid,'wxid');
$app = M('wxch_config')->where("id='$wxid'")->find();
//$subcurl = "?oid=$oid";
$wechat = "Wechat/wxch_oath/?oid=$oid";
$appid = $app['appid'];
$web_url = $app['url'];
}else{
$wechat = "Wechat/wxch_oath";
$app = M('wxch_config')->where('id=3')->find();
$appid = $app['appid'];
$web_url = $app['url'];
}
$cfg_ba
$url_sle ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$cfg_ba
//print($web_url);
//print_r($url_sle);
header('Expires: 0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cahe, must-revalidate');
header('Cache-Control: post-chedk=0, pre-check=0', false);
header('Pragma: no-cache');
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url_sle");
exit;
}
public function wxch_oath(){
$oid = $_GET['oid'];
$back = M('wxch_oauth')->where("oid='$oid'")->find();
$back_url = $back['contents'];
$name = $back['wxname'];
$app = M('wxch_config')->where('id='.$back['wxid'])->find();
$appid = $app['appid'];
$secret = $app['appsecret'];
$code = !empty($_GET['code']) ? $_GET['code'] : '';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
//echo $url;
$ret_json = $this->curl_get_contents($url);
//exit;
$ret = json_decode($ret_json);
//$openid = $ret->openid;
$openid = !empty($ret->openid) ? $ret->openid : '';
$access_token = $app['access_token'];
$user_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid";
$res_jsons = $this->curl_get_contents($user_url);
$w_user = json_decode($res_jsons,TRUE);
if($w_user['errcode'] == '40001' || $w_user['errcode'] == '42001')
{
$access_token = $this->new_access_token($back['wxid']);
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid";
$res_json = $this->curl_get_contents($url);
$w_user = json_decode($res_json,TRUE);
}
session('openid', $openid);
// print_r($id);
$uwxid = M('wxch_user')->where("wxid='$openid'")->find();
if($uwxid['openid']!=''){
$data['wxid'] = $w_user['openid'];
$data['uid'] = $id;
$data['nickname'] = $w_user['nickname'];
$data['sex'] = $w_user['sex'];
$data['wxid'] = $w_user['openid'];
$data['subscribe'] = $w_user['subscribe'];
$data['city'] = $w_user['city'];
$data['country'] = $w_user['country'];
$data['province'] = $w_user['province'];
$data['language'] = $w_user['language'];
$data['headimgurl'] = $w_user['headimgurl'];
M('wxch_user')->save($data);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $back_url");
exit;
}else{
$back_url = 'http://你的网址/Wechat/hb_index/?oid='.$oid;
//print_r($uid);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $back_url");
}
//print_r($uid);
}
public function hb_index(){
$oid = $_GET['oid'];
$wechat = 'Wechat/hb_oath/?oid='.$oid;
$app = M('wxch_config')->where('id=3')->find();
$appid = $app['appid'];
$web_url = $app['url'];
$cfg_ba
$url_sle ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$cfg_ba
header('Expires: 0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cahe, must-revalidate');
header('Cache-Control: post-chedk=0, pre-check=0', false);
header('Pragma: no-cache');
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url_sle");
exit;
}
public function hb_oath(){
$oid = $_GET['oid'];
$back = M('wxch_oauth')->where("oid='$oid'")->find();
$back_url = $back['contents'];
$app = M('wxch_config')->where("id='3'")->find();
$appid = $app['appid'];
$secret = $app['appsecret'];
$code = !empty($_GET['code']) ? $_GET['code'] : '';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$ret_json = $this->curl_get_contents($url);
$ret = json_decode($ret_json);
$openid = !empty($ret->openid) ? $ret->openid : '';
//$sql = M('wxch_user')->fetchSql(true)->where("wxid='$wxid'")->select();
//echo $sql;
$web = M('wxch_config')->where("gzhname='gh_9170571458c7'")->find();
$username = $this->md5get($web['namenum']);
$pwd = $this->md5get($web['pwdnum']);
$passwod = md5($pwd);
$uid = $_SESSION['openid'];
$data['openid'] = $openid;
$user = M('wxch_user')->where("openid='$openid'")->find();
$usname = $user['uname'];
if($usname!=''){
$users = M('user')->where("username='$usname'")->find();
$data['uname'] = $usname;
$datas['id'] = $users['id'];
$datas['wxid'] = $openid;
$datas['sex'] = $user['sex'];
session('home', array('id' => $datas['id']));
M('user')->save($datas);
}else{
$data['uname'] = $username;
$datas['username'] = $username;
$datas['password'] = $passwod;
$datas['wxid'] = $openid;
$datas['wxch_bd'] = 'ok';
$datas['password_tianxin'] = $pwd;
$uids = M('user')->add($datas);
session('home', array('id' => $uids));
}
print_r($uids);
M('wxch_user')->where("wxid='$uid'")->save($data);
//print($uid);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $back_url");
}
private function md5get($len)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
private function new_access_token($name)
{
$ret = M('wxch_config')->where("id='$back[wxid]'")->find();
$appid = $ret['appid'];
$appsecret = $ret['appsecret'];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$ret_json = $this->curl_get_contents($url);
$ret = json_decode($ret_json);
if($ret->access_token)
{
$data['access_token'] = $ret->access_token;
$data['dateline'] = time();
M('wxch_config')->where("id='$back[wxid]'")->save($data);
}
return $ret->access_token;
}
function curl_get_contents($url)
{
if(isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = $_SERVER['HTTP_USER_AGENT'];
} else {
$agent = '';
}
if(isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
} else {
$referer = '';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER,$referer);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
}
最佳答案