where(array("openid"=>$openid))->one();
if($customer == null){
$customerinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN ';
$customerinfo_json = $this->curl_get($customerinfo_url);
$customerinfo = json_decode($customerinfo_json, true);
$customer = new Customer();
$customer->name = $customerinfo['nickname'];
$customer->headimg = $customerinfo['headimgurl'];
$customer->sex = $customerinfo['sex'];
$customer->openid = $customerinfo['openid'];
$customer->address = $customerinfo['country'].$customerinfo['province'].$customerinfo['city'];
$customer->created_at = time();
$customer->updated_at = time();
$customer->is_del = 0;
$customer->money = 0;
$customer->save();
}
setCookie("is_login", 1, time()+3600*24*30*12, "/");
setCookie("customer_id", $customer->id, time()+3600*24*30*12, "/");
$redirect_url = $hostname;
Header("Location: $redirect_url");
}
public function curl_get($url)
{
$host = parse_url($url, PHP_URL_HOST);
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)');
curl_setopt($ch,CURLOPT_REFERER, $host);
curl_setopt($ch,CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$str = curl_exec($ch);
curl_close($ch);
return $str;
}