Exemplo n.º 1
0
    operation_fail("登陆失败");
}
$weixinAPI = new WeixinAPI();
$json = $weixinAPI->getOpenid($code);
session_start();
// 记录用户信息
$_SESSION['openid'] = $json['openid'];
$_SESSION['access_token'] = $json;
$openid = $json['openid'];
$home_url = '../index.php';
$account = Account::getAccount($json['openid']);
if ($account != null) {
    $_SESSION['account'] = $account;
    $extra = AccountExtra::getInfo($json['openid']);
    if ($extra != null) {
        $_SESSION['account_extra'] = $extra;
    }
} else {
    $weixinAPI = new WeixinAPI();
    $userInfo = $weixinAPI->getUserInfo($json['access_token'], $openid);
    Account::createAccount($openid, $userInfo['nickname']);
    $account = Account::getAccount($json['openid']);
    $_SESSION['account'] = $account;
}
//$home_url = '../h5/publish_task_page.php';
header('Location: ' . $home_url);
json_put("session", $_SESSION['access_token']);
json_output();
?>

 function push_info($msg, $wx_user, $token)
 {
     $push = M('push_info');
     $info = $push->where(array('openid' => $msg['FromUserName'], 'wechat_id' => $msg['ToUserName']))->find();
     //获取appid和appsecret
     if (empty($info) || $info && floor((time() - $info['update_time']) / 86400) >= 1) {
         require_once COMMON_PATH . '/WeixinAPI.php';
         $weixin = new WeixinAPI($wx_user['appid'], $wx_user['appsecret']);
         $user = $weixin->getUserInfo($msg['FromUserName']);
         if (empty($info)) {
             $data = array('openid' => $msg['FromUserName'], 'wechat_id' => $msg['ToUserName'], 'content' => json_encode($user), 'create_time' => time(), 'update_time' => time());
             $push->add($data);
             $this->push_user_info(json_encode($this->signature($user)), $token);
         } elseif (floor((time() - $info['update_time']) / 86400) >= 1) {
             $data = array('content' => json_encode($user), 'update_time' => time());
             $this->push_user_info(json_encode($this->signature($user)), $token);
             $push->where(array('openid' => $msg['FromUserName'], 'wechat_id' => $msg['ToUserName']))->save($data);
         }
     }
 }