欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 人人商城 生成 临时微信二维码,并保存成海报图片 有效期一个月

程序员文章站 2022-05-17 23:53:01
public function getPoster(){ global $_W; global $_GPC; $mm = pdo_fetch('select nickname,codetime from ' . tablename('ewei_shop_member') . ' where open ......
php 人人商城  生成 临时微信二维码,并保存成海报图片 有效期一个月
    public function getposter(){
        global $_w;
        global $_gpc;

        $mm = pdo_fetch('select nickname,codetime from ' . tablename('ewei_shop_member') . ' where openid =:openid limit 1', array(':openid' => $_w['openid']));
        
        $path = ia_root . '/addons/ewei_shopv2/data/couponposter/' . $_w['uniacid'] . '/';
        if (!(is_dir($path))) 
        {
            load()->func('file');
            mkdirs($path);
        }
        

        $openid = $_w['openid'];

        if($mm['codetime'])$mm['codetime'] = date('ymd');
        $md5 = md5(json_encode(array('openid' => $openid,'version' => 3,'timezone' => $mm['codetime'])));
        $file = $md5 . '.jpg';

        
        if(time() > strtotime($mm['codetime']) + 60*60*24*29){
            $aa = unlink($path . $file);
        }
        // var_dump($path . $file);exit;
        if (!(is_file($path . $file))) 
        {
            set_time_limit(0);
            @ini_set('memory_limit', '256m');
            $font = ia_root . '/addons/ewei_shopv2/static/fonts/msyh.ttf';
            $target = imagecreatetruecolor(750, 1334);
            $bc = imagecolorallocate($target, 0, 3, 51);
            $cc = imagecolorallocate($target, 240, 102, 0);
            $wc = imagecolorallocate($target, 255, 255, 255);
            $yc = imagecolorallocate($target, 255, 255, 0);
            $bg = imagecreatefromjpeg(ia_root . '/addons/ewei_shopv2/data/couponposter/poster.jpg');
            imagecopy($target, $bg, 0, 0, 0, 0, 750, 1334);
            imagedestroy($bg);

//             $qrcode_file = tomedia($this->createmyshopqrcode($userinfo['id']));
            $qrcode_url = $this->getcode();
            $qrcode = $this->createimage($qrcode_url);

//             var_dump($qrcode);
            $w = imagesx($qrcode);
            $h = imagesy($qrcode);
            imagecopyresized($target, $qrcode, 215, 600, 0, 0, 320, 320, $w, $h);
            imagedestroy($qrcode);
            
            $str1 = $this->getwords($mm['nickname'],3);
            imagettftext($target, 22, 0, 180, 302, $bc, $font, $str1);
            $str2 = date("y-m-d",strtotime("+30 day"));
            imagettftext($target, 18, 0, 420, 398, $bc, $font, $str2);

            imagejpeg($target, $path . $file);
            imagedestroy($target);
        }
        $srcc = $_w['siteroot'] . 'addons/ewei_shopv2/data/couponposter/' . $_w['uniacid'] . '/' . $file;
        
        $arr = array('code' => 1, 'src' => $srcc);
        show_json(1, $arr);
        

    }
生成海报
php 人人商城  生成 临时微信二维码,并保存成海报图片 有效期一个月
    public function getcode(){
        global $_w;
        global $_gpc;
        $sec = m('common')->getsec();
        $sec = iunserializer($sec['sec']);

        $appid = $_w['account']['key'];
        $secret = $_w['account']['secret'];
    
        $data['openid'] = $_w['openid'];
          
            require_once('weixin.class.php');
          
            $wx = new weixin($appid,$secret,'');
            
            $mm = pdo_fetch('select id from ' . tablename('ewei_shop_member') . ' where openid=:openid and uniacid=:uniacid limit 1', array(':openid' => $_w['openid'], ':uniacid' => $_w['uniacid']));
            $sceneid=intval($mm['id']);
            if ($sceneid<=0) {
                echo "error!!";
            } else {
                $result = $wx->_getqrcode($sceneid);
                $time = date('ymd');
                pdo_update("ewei_shop_member", array("codetime"=>$time), array( "openid" => $_w['openid'] ));
                
                return $result;
            }
        
    }
生成微信二维码
php 人人商城  生成 临时微信二维码,并保存成海报图片 有效期一个月
<?php
// if (!defined('in_ia')) {
//     exit('access denied');
// }
/*
 * to change this license header, choose license headers in project properties.
 * to change this template file, choose tools | templates
 * and open the template in the editor.
 */

class weixin{
    private $appid;
    private $key;
    private $access_token;
    private $jsapi_ticket;
    private $get_access_token_url='https://api.weixin.qq.com/cgi-bin/token?';
    private $menu_url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token=';
    private $menu_delete_url='https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=';
    private $getcode_url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=';
    private $oauth2='https://api.weixin.qq.com/sns/oauth2/access_token?';
    private $jsapi_ticket_time;
    private $access_token_time;
    public $menu;
    public function __construct($appid,$key,$access_token) {
           //$this->extract=  sp_get_option('extract');
           $this->appid=$appid;
           $this->key=$key;
           $this->access_token=$this->get_access_token();//access_token_time

    }
    public function get_jsapi_ticket(){
        if($this->$jsapi_ticket_time<time()-7200){
            $url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this->access_token.'&type=jsapi';
            $result=$this->https($url);
            $result=json_decode($result,true);
            if($result['ticket']){
            $this->jsapi_ticket=$result['ticket'];
            $this->$jsapi_ticket_time=time();
            //$res = sp_set_option('extract', $this->extract);
            }
            else{
                print_r($result);
                exit();
            }
        }
        return $this->jsapi_ticket;
    }
    public function get_access_token(){
        if($this->access_token_time<time()-7200){
            $url=$this->get_access_token_url."grant_type=client_credential&appid=".$this->appid."&secret=".$this->key;
            $result=$this->https($url);
            $result=json_decode($result,true);
            if($result['access_token']){
                $this->access_token=$result['access_token'];
                $this->access_token_time=time();
                //$res = sp_set_option('extract', $this->extract);
            }
            else{
                print_r($result);
                exit();
            }
        }
        return $this->access_token;
    }
    public function get_oauth2($code){
            $url=$this->oauth2."appid=".$this->appid."&secret=".$this->key."&code=".$code."&grant_type=authorization_code";
            $result=$this->https($url);
            $result=json_decode($result,true);
            return $result;
            exit;
    }

    public function get_userinfo($openid){
            //需要关注的url
            $url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->access_token."&openid=".$openid."&lang=zh_cn";
            //无需关注
            //$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token."&openid=".$openid."&lang=zh_cn";
            $result=$this->https($url);
            $result=json_decode($result,true);
            return $result;
            exit;
    }
    public function get_qrcode($id){
            $url=$this->getcode_url.$this->access_token;
            $data='{"action_name": "qr_limit_scene", "action_info": {"scene": {"scene_id": '.$id.'}}}';
            $result=$this->https($url,$data,'json');
            $result=json_decode($result,true);
            return $result;
            exit;
    }
    public function updata_menu(){
        $url=$this->menu_url.$this->access_token;
        $this->menu=htmlspecialchars_decode($this->menu);
        $data=$this->menu;
        $result=$this->https($url,$data,'json');
        $result=json_decode($result,true);
        return $result;
        exit;
    }
    public function https($url,$data='',$method='get'){
        $ch = curl_init();
        curl_setopt ($ch, curlopt_url, $url);
        curl_setopt($ch, curlopt_ssl_verifypeer, false); // 对认证证书来源的检查  
        curl_setopt($ch, curlopt_ssl_verifyhost, false); // 从证书中检查ssl加密算法是否存在  
        curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (compatible; msie 5.01; windows nt 5.0)');  
        curl_setopt($ch, curlopt_followlocation, 1); // 使用自动跳转  
        curl_setopt($ch, curlopt_autoreferer, 1); // 自动设置referer
        if($method=='post'){  
        curl_setopt($ch, curlopt_post, 1); // 发送一个常规的post请求 
        curl_setopt($ch, curlopt_customrequest, "post");  
        if ($data != ''){  
            curl_setopt($ch, curlopt_postfields, $data); // post提交的数据包  
            }  
        }       
        curl_setopt($ch, curlopt_timeout, 30); // 设置超时限制防止死循环  
        if($method=='json'){
        curl_setopt($ch, curlopt_post, true);
        curl_setopt($ch, curlopt_customrequest, "post"); 
        curl_setopt($ch, curlopt_postfields, $data); // post提交的数据包  
        curl_setopt($ch, curlopt_httpheader, array(  
            'content-type: application/json; charset=utf-8',  
            'content-length: ' . strlen($data))  
        );  
        } 
        else{
            curl_setopt($ch, curlopt_header, 0); // 显示返回的header区域内容  
        }
        curl_setopt($ch, curlopt_returntransfer, 1); // 获取的信息以文件流的形式返回  
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }
    /** 
     * 首先创建二维码ticket 
     * @param string $sceneid 场景值id 
     * @param string $type 值为'temp'的时候生成临时二维码 
     * @param string $expire_seconds 二维码过期时间 
     * @return string 二维码ticket 
     */
    public function _getticket($sceneid,$type='temp',$expire_seconds=604800){ 
        if($type=='temp'){ 
          $data['expire_seconds']=604800;
          $data['action_name']='qr_scene';
          $data['action_info']['scene']['scene_id']=$sceneid;
        }else{ 
          $data['action_name']='qr_limit_scene';
          $data['action_info']['scene']['scene_id']=$sceneid;
        } 
     
        $args =json_encode($data);
        $curl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->access_token; 
        $content = $this->https($curl,$args,'post'); 
        $cont = json_decode($content); 
        
        return $cont->ticket; 
    }
    //然后凭借ticket到指定url换取二维码 
    public function _getqrcode($sceneid,$type='temp1',$expire_seconds=2592000){ 
         $ticket = $this->_getticket($sceneid,$type,$expire_seconds); 
         $curl = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket); 
         $content = $this->https($curl); 
         return $curl; 
    }
}
微信类

php 人人商城  生成 临时微信二维码,并保存成海报图片 有效期一个月