思路:
1、后台后成链接二维码
2、微信扫码后,获取会员的openid
3、判断openid和会员是否已绑定
4、未绑定,则绑定成功
<?
header("content-type:text/html;charset=utf-8");
$appid = '';
$secret = '';
$code = $_REQUEST['code'];
$username = $_REQUEST['username'];
$id = $_REQUEST['id'];
if ($code) {
$result = getWxInfo($appid, $secret, $code, 1);
$_SESSION['openid'] = $result['openid'];
$_SESSION['nickname'] = $result['nickname'];
$openid = $result['openid'];
$nickname = $result['nickname'];
} else {
if($_SESSION['openid']){
$openid = $_SESSION['openid'];
$nickname = $_SESSION['nickname'];
}else{
getWxCode($appid, 1);
}
}
mysql_query('SET NAMES UTF8');
if($openid){
$query = mysql_query("SELECT * FROM user_wx WHERE openid='".$openid."'");
$row = mysql_fetch_row($query);
$query = mysql_query("SELECT * FROM user_wx WHERE uid='".$id."'");
$exist_u = mysql_fetch_row($query);
$query = mysql_query("SELECT * FROM users WHERE userid='".$id."' LIMIT 1");
$m = mysql_fetch_row($query);
if($m[1] != $username){
echo "<script>alert('识别失败!');setTimeout(function() {document.addEventListener('WeixinJSBridgeReady',function() {WeixinJSBridge.call('closeWindow');},false);WeixinJSBridge.call('closeWindow');}, 300);</script>";
exit();
}
if($row || $exist_u){
echo "<script>alert('已绑定过!');setTimeout(function() {document.addEventListener('WeixinJSBridgeReady',function() {WeixinJSBridge.call('closeWindow');},false);WeixinJSBridge.call('closeWindow');}, 300);</script>";
}else{
mysql_query("INSERT INTO user_wx(uid, openid, nickname) VALUES(".$id.", '".$openid."', '".str_replace("'", "’", $nickname)."')");
header("Content-type: text/html; charset=utf-8");
echo "<script>alert('绑定成功!');setTimeout(function() {document.addEventListener('WeixinJSBridgeReady',function() {WeixinJSBridge.call('closeWindow');},false);WeixinJSBridge.call('closeWindow');}, 300);</script>";
}
}
function getWxCode($appid, $type = false)
{
if ($type) {
$scope = 'snsapi_userinfo';
} else {
$scope = 'snsapi_base';
}
$url = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
$href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%d#wechat_redirect";
$href = sprintf($href, $appid, $url, $scope, 123);
header("Location:".$href);
exit;
}
function getWxInfo($appid, $secret, $code, $type=false){
$result = http_get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code');
$result = json_decode($result, 1);
if ($type) {
file_put_contents('token.txt', $result['access_token']);
if ($result['access_token']) {
$Userinfo = http_get('https://api.weixin.qq.com/sns/userinfo?access_token=' . $result['access_token'] . '&openid=' . $result['openid']);
$Userinfo = json_decode($Userinfo, 1);
return $Userinfo;
}
} else {
return $result;
}
return false;
}
function http_get($url){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
?>