Skip to content

Instantly share code, notes, and snippets.

@lgh06
Last active October 21, 2015 03:23
Show Gist options
  • Save lgh06/d698751314eadcb6d0d3 to your computer and use it in GitHub Desktop.
Save lgh06/d698751314eadcb6d0d3 to your computer and use it in GitHub Desktop.
微信手机端 网页 登陆(CI框架中的一个Model)
<?php
/**
* 手机网页端三方登陆Model
* by 刘各欢 20150730
* http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
***/
$id = config_item('wx_appid');
$sec = config_item('wx_appsecret');
define("APPID", $id);
define("APPSECRET",$sec);
class Login_model extends CI_Model{
public $_id = APPID;
public $_sec = APPSECRET;
function _construct(){
parent::__construct();
}
function getServer(){
$t1 = (strpos($_SERVER['SERVER_NAME'],"mtest.shuiqian.cc")===0);
$t2 = (strpos($_SERVER['HTTP_HOST'],"mtest.shuiqian.cc")===0);
$t3 = (strpos($_SERVER['SERVER_NAME'],"m.shuiqian.cc")===0);
$t4 = (strpos($_SERVER['HTTP_HOST'],"m.shuiqian.cc")===0);
if($t3||$t4){
return "http://www.shuiqian.cc/";
}else if($t1||$t2){
return "http://test.shuiqian.cc/";
}
}
/**
* 用获取的code去获取网页用户基本信息的access token
* @param $code
* @return mixed
*/
public function getWxJSLoginToken($code){
$actokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
.APPID
."&secret="
.APPSECRET
."&code="
.$code
."&grant_type=authorization_code";
$res = $this->lhttps_request($actokenUrl);//记录access_token
$jsonArray = json_decode($res,true);
/*$actoken = $jsonArray["access_token"];
$openid = $jsonArray["openid"];
$unionid = $jsonArray["unionid"];*/
return $jsonArray;
}
/**
* 用网页用户基本信息的access token和用户openid获取用户基本信息
* @param $actoken
* @param $openid
* @return mixed
*/
public function getWxUserInfo($actoken,$openid){
//获取用户信息
$userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$actoken."&openid=".$openid."&lang=zh_CN";
$userInfo = $this->lhttps_request($userInfoUrl);
$jsonArray = json_decode($userInfo,true);
return $jsonArray;
}
function returnAccessToken()
{
$r = $this->db->get_where("tmp_info","id = 1")->row();
$ac = "";
if($r&&$r->wx_access_token){//如果记录存在,则验证时间是否过期
$ac = $r->wx_access_token;
$ac_time = $r->wx_access_token_time;
if( (time()-$ac_time)>7100 ){//验证是否过期
$temp = $this->getAccessToken();
if($temp){
$this->db->set('wx_access_token', $temp);
$this->db->set('wx_access_token_time', time());
$this->db->where('id', 1);
$this->db->update('tmp_info');
$ac = $temp;
}
}
}else{ //如果记录不存在
$temp = $this->getAccessToken();
if($temp){
$this->db->set('wx_access_token', $temp);
$this->db->set('wx_access_token_time', time());
if(!$r){
$this->db->set('id', 1);
$this->db->insert('tmp_info');
}else{
$this->db->where('id', 1);
$this->db->update('tmp_info');
}
$ac = $temp;
}
}
return trim($ac);
}
function getAccessToken()
{
$info=$this->lhttp_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET);
$token=json_decode($info);
if($info){
return $token->{'access_token'};
}else{
return null;
}
}
function returnJsapiTicket()
{
$this->db->reset_query();
$r = $this->db->get_where("tmp_info","id = 1")->row();
$ti = "";
if($r&&$r->wx_jsapi_ticket){//如果记录存在,则验证时间是否过期
$ti = $r->wx_jsapi_ticket;
$ti_time = $r->wx_jsapi_ticket_time;
if( (time()-$ti_time)>7100 ){//验证是否过期
$temp = $this->getJsapiTicket();
if($temp){
$this->db->set('wx_jsapi_ticket', $temp);
$this->db->set('wx_jsapi_ticket_time', time());
$this->db->where('id', 1);
$this->db->update('tmp_info');
$ti = $temp;
}
}
}else{ //如果记录不存在
$temp = $this->getJsapiTicket();
if($temp){
$this->db->set('wx_jsapi_ticket', $temp);
$this->db->set('wx_jsapi_ticket_time', time());
$this->db->where('id', 1);
$this->db->update('tmp_info');
$ti = $temp;
}
}
return trim($ti);
}
function getJsapiTicket()
{
$info=$this->lhttps_request("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$this->returnAccessToken()."&type=jsapi");
$token=json_decode($info);
return $token->{'ticket'};
}
/**
* 生成随机字符串
* @param int $pw_length
* @return string
*/
function create_rand($pw_length = 6)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(97, 122));
}
return $randpwd;
}
function lhttp_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);//需要获取的URL地址,也可以在curl_init()函数中设置。
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);//禁用后cURL将终止从服务端进行验证。使用CURLOPT_CAINFO选项设置证书使用CURLOPT_CAPATH选项设置证书目录 如果CURLOPT_SSL_VERIFYPEER(默认值为2)被启用,CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);//默认即为GET请求
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//将exec执行结果以文件流形式返回不是直接输出到浏览器
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
function lhttps_request($url,$data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);//需要获取的URL地址,也可以在curl_init()函数中设置。
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);//禁用后cURL将终止从服务端进行验证。使用CURLOPT_CAINFO选项设置证书使用CURLOPT_CAPATH选项设置证书目录 如果CURLOPT_SSL_VERIFYPEER(默认值为2)被启用,CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);//默认即为GET请求
if (!empty($data))
{
curl_setopt($curl, CURLOPT_HTTPGET, FALSE);
curl_setopt($curl, CURLOPT_POST, TRUE);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//全部数据使用HTTP协议中的"POST"操作来发送
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//将exec执行结果以文件流形式返回不是直接输出到浏览器
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/**
* 把url指向的图片保存在本地
* @param $imgUrl
* @param $file
* @return mixed
*/
function saveImgToFile($imgUrl,$file){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $imgUrl);//需要获取的URL地址,也可以在curl_init()函数中设置。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE);//禁用后cURL将终止从服务端进行验证。使用CURLOPT_CAINFO选项设置证书使用CURLOPT_CAPATH选项设置证书目录 如果CURLOPT_SSL_VERIFYPEER(默认值为2)被启用,CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//将exec执行结果以文件流形式返回不是直接输出到浏览器
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);//默认即为GET请求
curl_exec($curl);
curl_close($curl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment