<?php
namespace app\common;

use think\Request;
use Db;
class Wx
{
    public $appid = "";
    public $appsecret = "";
    /**
    * curl接口访问
    * @param string $port  访问的接口链接
    * @return array 返回的值
    */

    public function curl($port)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $port);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
        
        $output = curl_exec($ch);
    
        curl_close($ch);
        
        
        return json_decode($output, true);
    }
    
    
    
    /**
     *  获取微信用户的信息
     *  return array
     */

    public function userInfo()
    {
        $request = new Request;
        $code = $request->param("code");
            
            
        if (!$code) {
            $redirect_uri = urlencode($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
                
                
            header("Location:$url");
            die;
        } else {
            $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
     
                
            $access_token_info = $this->curl($access_token_url);
                
                
                
                
            $wxuser_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token_info['access_token']."&openid=".$access_token_info['openid']."&lang=zh_CN";
    
                
                
            $wxuser_data = $this->curl($wxuser_url);
    
                
            return $wxuser_data;
        }
    }
    
    /**
     *  获取微信基础access_token
     *  return array
     */

    public function access_token() 
    
    {
        
        $token = Db::name("set")->where('type', 'token')->find();
        $time = Db::name("set")->where('type', 'time')->find();
        if(!$time['value'] || $time['value'] < strtotime(date('Y-m-d H:00:00',time()))){
         $access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
        $access_token_info = $this->curl($access_token_url);
        Db::name("set")->where('type', 'token')->update(['value'=>$access_token_info['access_token']]);
        Db::name("set")->where('type', 'time')->update(['value'=>strtotime(date('Y-m-d H:00:00',time()))]);
        }else{
            $access_token_info['access_token']=$token['value'];
        }
        

        return $access_token_info;
    }

    /**
     *  作用:产生随机字符串,不长于32位
     */
    public function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ($i = 0; $i < $length; $i++) {
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
    /**
     *  作用:格式化参数,签名过程需要使用
     */
    public function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if ($urlencode) {
                $v = urlencode($v);
            }
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar;
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }
    public function share()
    {
        //微信分享
        $access_token = $this->access_token()['access_token'];
        $jsapi_ticket = $this->curl("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi")['ticket'];
        
        $wxdata['jsapi_ticket'] = $jsapi_ticket;
        $wxdata['timestamp'] = time();
        $wxdata['noncestr'] = $this->createNoncestr();
        $wxdata['url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        ksort($wxdata);
        $str = $this->formatBizQueryParaMap($wxdata, false);
        $wxdata['signature'] = sha1($str);
        $wxdata['appid'] = $this->appid;
        
        return $wxdata;
    }
}

点赞(0)

评论列表 共有 0 条评论

暂无评论
返回
顶部