123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace App\Servers\WeChat;
- use App\Servers\Common\RedisDataServer;
- class WeixinServer
- {
- /**
- * 单列对象
- * @var
- */
- private static $server = '';
- public $appid = '';
- public $appsecret = '';
- private function __construct(){
- $this->appid=env('WX_APPID','');
- $this->appsecret=env('WX_APPSECRET','');
- }
- /**
- * 创建对象
- * @return WeixinServer
- */
- static function creatServer()
- {
- if(empty(self::$server)){
- self::$server = new WeixinServer();
- }
- return self::$server;
- }
- /**
- * 网页授权
- * @return mixed
- */
- function authorize()
- {
- $code = request()->input('code', '');
- if (empty($code)) {
- $redirect_url = $this->getServerUrl();
- $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_url}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
- $this->redirect($url);
- return false;
- } else {
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
- $result = $this->sendRequest($url);
- $result = json_decode($result, true);
- if (isset($result['errcode'])) {
- return false;
- } else {
- // $openid = $result['openid'];
- // $access_token = $result['access_token'];
- // $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appid}&grant_type=refresh_token&refresh_token={$result['refresh_token']} ";
- // $result = $this->sendRequest($url);
- // $result = json_decode($result, true);
- // if (isset($result['access_token'])) {
- // $access_token = $result['access_token'];
- // $openid = $result['openid'];
- // }
- // $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
- // $result = $this->sendRequest($url);
- // $result = json_decode($result, true);
- // if (isset($result['errcode'])) {
- // return false;
- // }
- return $result;
- }
- }
- }
- /**
- * 获取当前连接
- * @return string
- */
- function getServerUrl()
- {
- return urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
- }
- function redirect($uri = '', $method = 'location', $http_response_code = 302)
- {
- if (!preg_match('#^https?://#i', $uri)) {
- $uri = site_url($uri);
- }
- switch ($method) {
- case 'refresh' :
- header("Refresh:0;url=" . $uri);
- break;
- default :
- header("Location: " . $uri, TRUE, $http_response_code);
- break;
- }
- exit();
- }
- /**
- * @param $url
- * @param string $type 请求方式
- * @param string $data 数据 数组格式
- * @return mixed
- */
- protected function sendRequest($url, $type = 'get', $data = '')
- {
- $ch = curl_init();
- if ($type == 'get' && $data) {
- $url = $url . '?' . http_build_query($data);
- }
- curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- $head = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
- curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
- if ($type == 'post') {
- curl_setopt($ch, CURLOPT_POST, TRUE);//post请求
- $data = json_encode($data, JSON_UNESCAPED_UNICODE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置传递的参数
- }
- $request = curl_exec($ch);
- curl_close($ch);
- return $request;
- }
- /**
- * 获取js授权信息
- * @return mixed
- */
- function getJsConfig($url = '')
- {
- $hash = '';
- $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz';
- $max = strlen($chars) - 1;
- for ($i = 0; $i < 16; $i++) {
- $hash .= $chars[mt_rand(0, $max)];
- }
- $data['noncestr'] = $hash;
- $data['jsapi_ticket'] = $this->getJsTicket();
- $data['timestamp'] = time();
- if (empty($url)) {
- $data['url'] = $this->getServerUrl();
- } else {
- $data['url'] = $url;
- }
- ksort($data);
- $str = '';
- foreach ($data as $key => $val) {
- $str .= '&';
- $str .= $key . '=' . $val;
- }
- $str = mb_substr($str, 1);
- $data['appid'] = $this->appid;
- $data['signature'] = sha1($str);
- unset($data['jsapi_ticket']);
- return $data;
- }
- /**
- * 获取 access_token
- * @return bool
- */
- protected function getAccessToken($type = 1)
- {
- $key = 'vouchername';
- $appId = $this->appid;
- $appsecret = $this->appsecret;
- $data = RedisDataServer::creatServer()->getData($key, 'json');
- if (empty($data) || $data['endtime'] < time() || true) {
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appsecret}";
- $result = $this->sendRequest($url);
- $result = json_decode($result, true);
- // $bey = Cache::set('vouchername', array('access_token' => $result['access_token'], 'endtime' => time() + 7000), 7000);
- if ( isset($result['access_token'])) {
- RedisDataServer::creatServer()->setData($key, array('access_token' => $result['access_token'], 'endtime' => time() + 1800), 'json',1800);
- return $result['access_token'];
- } else {
- return false;
- }
- } else {
- return $data['access_token'];
- }
- }
- /**
- * 获取Js调用凭证
- * @return bool
- */
- function getJsTicket()
- {
- // $data = Cache::get('js_ticket');
- $data = RedisDataServer::creatServer()->getData('js_ticket', 'json');
- if (empty($data) || $data['endtime'] < time()) {
- $access_token = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi";
- $result = $this->sendRequest($url);
- $result = json_decode($result, true);
- if ($result['errcode'] == 0) {
- //file_put_contents($this->js_ticket,serialize(array('access_token'=>$result['access_token'],'endtime'=>time()+7000)));
- // Cache::set('js_ticket', array('access_token' => $result['ticket'], 'endtime' => time() + 7000), 7000);
- RedisDataServer::creatServer()->setData('js_ticket', array('access_token' => $result['ticket'], 'endtime' => time() + 7000), 'json',7000);
- return $result['ticket'];
- }
- return false;
- } else {
- return $data['access_token'];
- }
- }
- }
|