微信JS-SDK页面的注入配置信息


/**
 * Created by PhpStorm.
 * Date: 2018/8/20
 * Time: 10:47
 */
include_once "BaseLib.php";
include_once "IndexLogin.php";
include_once "SendMessageLib.php";
class IndexShare extends BaseLib
{
    private $ci;
    function __construct()
    {
        $this->configName = "cms";
        parent::__construct();
        $this->ci= &get_instance();
    }

    /**
     * Notes:获取微信JSSDK(H5专用)
     * Date: 2018/8/20
     * Time: 11:03
     * @param $url
     * @return array|bool
     */
    public function share_film_get_jssdk_conf($url,$conf = 'wechat')
    {
        if(empty($url)){return false;}
        $this->ci->load->config('services');
        //yjq 2018/9/5  电影节分享页面需要配置不同的公众号
        $config = $this->ci->config->item($conf);
        $time = time();

        $jsapi_ticket = $this->share_film_get_jsapi_ticket();
        $str = $this->share_film_get_char(18);

        $sign = "jsapi_ticket={$jsapi_ticket}&noncestr={$str}×tamp={$time}&url=" . $url;
        $sign = sha1($sign);

        $data = array(
            'appid'=>$config['APPID'],
            'timestamp'=>$time,
            'nonceStr'=>$str,
            'signature'=>$sign
        );

        return $data;
    }

    /**
     * Notes:获取jsapiticket
     * Date: 2018/8/20
     * Time: 15:09
     * @return string
     */
    protected function share_film_get_jsapi_ticket()
    {
        $this->ci->load->config('services');
        $config = $this->ci->config->item('wechat');
        $time = time();

        $this->ci->load->model('ClientJsticket');
        $where = "expiration_time > $time and appid='{$config['APPID']}'";
        $jsticket = $this->ci->ClientJsticket->getRow($where,'id,js_ticket');

        if(!empty($jsticket))
        {
            return $jsticket['js_ticket'];
        }
        $access_token = $this->share_film_get_access_token();
        if(empty($access_token)){return "";}

        $exp_time = $time+3600;
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$access_token}";
        $content = $this->urldispatchRequest($url);
        if(empty($content)){return "";}


        $set_data =array(
            'js_ticket'=>$content['ticket'],
            'create_time'=>time(),
            'expiration_time'=>$exp_time,
            'appid'=>$config['APPID']
        );
        $this->ci->ClientJsticket->add($set_data);

        return $content['ticket'];

    }

    /**
     * Notes:获取access_token
     * Date: 2018/8/20
     * Time: 15:20
     * @return string
     */
    protected  function share_film_get_access_token()
    {
        $this->ci->load->config('services');
        $config = $this->ci->config->item('wechat');
        $time = time();

        $this->ci->load->model('ClientAccessToken');
        $where = "expiration_time > $time and appid='{$config['APPID']}'";
        $result = $this->ci->ClientAccessToken->getRow($where,'id,access_token');
        if(!empty($result)){return $result['access_token'];}

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$config['APPID']}&secret={$config['APPSECRET']}";
        $content = $this->urldispatchRequest($url);
        if(empty($content['access_token'])){return "";}

        $exp_time = time()+3600;
        $set_data = array(
            'access_token'=>$content['access_token'],
            'create_time'=>time(),
            'expiration_time'=>$exp_time,
            'appid'=>$config['APPID']
        );
        $this->ci->ClientAccessToken->add($set_data);
        return $content['access_token'];
    }

    /**
     * Notes:获取随机字符串
     * Date: 2018/8/20
     * Time: 15:12
     * @param $length
     * @return string
     */
    protected function share_film_get_char($length)
    {
        $str = "";
        $strPol = "0123456789";
        $max = strlen($strPol) - 1;
        for ($i = 0; $i < $length; $i++) {
            $str .= $strPol[rand(0, $max)];
            //rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        return $str;
    }

    /**
     * Notes:微信登录获取code
     * Date: 2018/8/22
     * Time: 14:45
     * @param $url
     */
    public function share_film_wechat_get_code($url)
    {
        $url = urlencode($url);

        $config = $this->ci->config->item('wechat');
        $direct_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['APPID']}&redirect_uri=$url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        header('location:'.$direct_url);exit();
    }
    public function share_film_wechat_get_openid($code)
    {
        $config = $this->ci->config->item('wechat');
        $this->ci->load->library('Session');
        if(isset($_SESSION['user_data'])){
            return json_decode($_SESSION['user_data'],true);
        }

        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$config['APPID'].'&secret='.$config['APPSECRET'].'&code='.$code.'&grant_type=authorization_code';
        $result = $this->urldispatchRequest($url);


        if(!empty($result['openid']) && !empty($result['access_token'])){
            $userinfo=$this->share_film_wechat_get_userinfo($result['openid'],$result['access_token']);
            if(!empty($userinfo)){
                $this->ci->session->set_userdata('user_data',json_encode($userinfo));//保存session
                return $userinfo;
            }
            return false;
        }
        return false;
    }
    /**
     * Notes:微信获取用户信息
     * Date: 2018/8/22
     * Time: 14:49
     * @param $openid
     * @param $access_token
     * @return array
     */
    protected function share_film_wechat_get_userinfo($openid,$access_token)
    {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $this->requestMethod="GET";

        $arr = $this->urldispatchRequest($url);

        if(!empty($arr['nickname']))
        {
            $rel['headimgurl'] = $arr['headimgurl'];
            $rel['nickname'] = $arr['nickname'];
            $rel['openid'] = $openid;
            return $rel;
        }
        return array();
    }
}

你可能感兴趣的:(微信)