<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../js/jquery-1.11.1.min.js"></script>
<?php
isset($_COOKIE['union_id'])?$union_id=$_COOKIE['union_id']:$union_id="0";
require_once("../core/setting.php");
$appid = $setting['appid'];
$appsecret = $setting['appsecret'];
isset($_GET['code'])?$code = $_GET['code']:$code= null;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
$json = file_get_contents("$url");
$token = json_decode($json,true);
$token['expires_in'];
$token['refresh_token'];
$access_token = $token['access_token'];
$openid = $token['openid'];
$urlinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$infojson = file_get_contents("$urlinfo");
$info = json_decode($infojson,true);
//获取用户信息
$nicheng = $info['nickname'];
$openid = $info['openid'];//用户唯一标识
$sex = $info['sex'];//用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
$province = $info['province'];//省份
$city = $info['city'];//城市
$country = $info['country'];//国家，CN
$diqu = $country.".".$province.".".$city;
$headimgurl = $info['headimgurl'];//用户 头像
switch($sex){
    case "1":
    $sex = "男";
    break;
    case "2":
    $sex = "女";
    break;
}
require_once("../config.php");
$checkusersql = "select * from dunling_menber where openid='$openid'";
$checkuserery = mysql_query($checkusersql,$config);
$checkuser = mysql_num_rows($checkuserery);
if($checkuser=="0"){
    $insql = "insert into dunling_menber (openid,nicheng,touxiang,sex,diqu,moneypay,money,regtime,logintime,unions,kl) values ('$openid','$nicheng','$headimgurl','$sex','$diqu','0','0','$regtime','$regtime','$union_id','10')";
    mysql_query($insql,$config)or die(mysql_error());
}

$checkusersql = "select * from dunling_menber where openid='$openid'";
$checkuserery = mysql_query($checkusersql,$config);
$checkuser = mysql_fetch_array($checkuserery);
$username = $checkuser['username'];
$uid = $checkuser['id'];

if($username==null){
?>
<link rel="stylesheet" href="style/i.css" type="text/css" />
<script>
$(function(){
  $("#form").click(function(){
    var username = $("#username").val();
    $.post("username.php",{
        username:username,
        uid:'<?php echo $uid;?>'
    },function(aa,bb){
       if(aa=="ok"){
        location.href='../menber.php';
       }
    });
  });
    
});
</script>
<div id="top">请设置您的手机号码</div>
<div id="b"><input type="text" id="username" style="border:0;width:280px;height:40px;line-height:40px;text-align:center;font-size:20px;" /></div>
<div id="b"><input type="button" value="设置并进入会员中心" id="form" style="width:280px;height:40px;line-height:40px;" /></div>
<?php }else{
setcookie("uid",$uid,time()+3600*24,'/');
setcookie("username",$username,time()+3600*24,'/');
ini_set("date.timezone","PRC");
$time = time();
$uplogintime = "update dunling_menber set logintime = '$time' where username = '$username'";
mysql_query($uplogintime,$config);
mysql_close();
header("location:../menber.php");
}?>