125 lines
3.3 KiB
PHP
125 lines
3.3 KiB
PHP
![]() |
<?php
|
||
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
||
|
|
||
|
class Weixin_model extends CI_Model {
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
parent::__construct();
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
weixin appid
|
||
|
*/
|
||
|
public function getAppid()
|
||
|
{
|
||
|
return 'wx1a526f5ba2a543fc';
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
weixin appsecret
|
||
|
*/
|
||
|
private function getAppsecret()
|
||
|
{
|
||
|
return '2013b1e948bffc845f73a1337c1fe787';
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
weixin 取token url
|
||
|
*/
|
||
|
private function getTokenUrl($appid, $appsecret)
|
||
|
{
|
||
|
return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
weixin 取token
|
||
|
*/
|
||
|
function getToken()
|
||
|
{
|
||
|
date_default_timezone_set('prc');
|
||
|
//$data = $this->Db_model->getAllData("weixin");
|
||
|
$query = $this->db->query("SELECT * FROM weixin");
|
||
|
$row = $query->row();
|
||
|
|
||
|
$id = isset($row->id)?stripslashes($row->id):"";
|
||
|
$token = isset($row->token)?stripslashes($row->token):"";
|
||
|
$time = isset($row->updatetime)?stripslashes($row->updatetime):"";
|
||
|
|
||
|
$now = date("Y-m-d H:i:s");
|
||
|
|
||
|
if ($token == "0" || $token == "")
|
||
|
{
|
||
|
$jsoninfo = json_decode($this->https_request($this->getTokenUrl($this->getAppid(), $this->getAppsecret())), true);
|
||
|
$token = $jsoninfo["access_token"];
|
||
|
$this->db->query("UPDATE weixin SET token = '".$token."' ,updatetime = '".$now."' WHERE id = '".$id."'");
|
||
|
}else
|
||
|
{
|
||
|
$updatetime = $now;
|
||
|
$now = strtotime((string)$now);
|
||
|
$time = strtotime($time);
|
||
|
$t = $now - $time;
|
||
|
if ($t >= 7100)
|
||
|
{
|
||
|
$jsoninfo = json_decode($this->https_request($this->getTokenUrl($this->getAppid(), $this->getAppsecret())), true);
|
||
|
$token = $jsoninfo["access_token"];
|
||
|
$this->db->query("UPDATE weixin SET token = '".$token."' ,updatetime = '".$updatetime."' WHERE id = '".$id."'");
|
||
|
}
|
||
|
}
|
||
|
return $token;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
根据openid 获取用户信息
|
||
|
*/
|
||
|
public function getUserInfor($openid)
|
||
|
{
|
||
|
$_token = $this->getToken();
|
||
|
$_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$_token."&openid=".$openid;
|
||
|
$_result = json_decode($this->https_request($_url), true);
|
||
|
return $_result;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
oauth2 获取用户信息
|
||
|
*/
|
||
|
public function getOauthUserInfor($oauthtoken, $openid)
|
||
|
{
|
||
|
$_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $oauthtoken . "&openid=" . $openid;
|
||
|
$_result = json_decode($this->https_request($_url), true);
|
||
|
return $_result;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
使用code换取access_token
|
||
|
*/
|
||
|
public function getOauthToken($code)
|
||
|
{
|
||
|
$_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->getAppid() . "&secret=" . $this->getAppsecret() . "&code=" . $code . "&grant_type=authorization_code";
|
||
|
$_result = json_decode($this->https_request($_url), true);
|
||
|
return $_result;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
weixin 提交
|
||
|
*/
|
||
|
private function https_request($url, $data = null)
|
||
|
{
|
||
|
$curl = curl_init();
|
||
|
curl_setopt($curl, CURLOPT_URL, $url);
|
||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
|
||
|
if (!empty($data))
|
||
|
{
|
||
|
curl_setopt($curl, CURLOPT_POST, 1);
|
||
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||
|
}
|
||
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||
|
$output = curl_exec($curl);
|
||
|
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
|
||
|
curl_close($curl);
|
||
|
return $output;
|
||
|
}
|
||
|
|
||
|
}
|