zoukankan      html  css  js  c++  java
  • 小程序生成带推荐码(场景id)的二维码

    <?php
    namespace BusinessController;
    use ThinkHook;
    
    /**
     * 小程序码[带推荐码/场景id]
     * @author 戈丫汝<534208139@qq.com>
     */
    class XcxqrcodeController{
        protected $appid = '';
        protected $secret = '';
        protected $url = "";
        protected $access_tokens = "";
    
        public function __construct(){
            $mpid = $_GET['mpid'];
            $appid = M('test')->where(['mpid'=>$mpid,'name'=>'xcx_appid'])->getField('value');
            $appsecret = M('test')->where(['mpid'=>$mpid,'name'=>'xcx_appsecret'])->getField('value');
            $this->appid = $appid;
            $this->secret = $appsecret;
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret . "";
            $result = $this->httpRequest($url);
            $access_tokens = json_decode($result, true);
            $this->access_tokens = $access_tokens['access_token'];
        }
        //打印二维码显示  [[入口地址]]
        public function Follow(){
            $scene_str = $_GET['id'];//场景id/推荐码
            $mpid = $_GET['mpid'];
            $rs = $this->getTemporaryQrcode($mpid,$this->access_tokens, $scene_str);
            echo $rs;
        }
        //生成二维码
        public function getTemporaryQrcode($mpid,$access_tokens,$scene_str=''){
            //首页小程序码。无path
            $qcode ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_tokens. "";
            $param = json_encode(array("scene"=>$scene_str));
            $result = $this->httpRequest( $qcode, $param,"POST");
            $filename =  './Images/'.'xcx_'.$mpid.'-'.uniqid() . '.jpg'; //定义图片名字及格式 
            file_put_contents($filename,$result);  
            return $filename;
        }
        //把请求发送到微信服务器换取二维码
        function httpRequest($url, $data='', $method='GET'){
            $curl = curl_init();  
            curl_setopt($curl, CURLOPT_URL, $url);  
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);  
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);  
            curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1);  
            if($method=='POST')
            {
                curl_setopt($curl, CURLOPT_POST, 1); 
                if ($data != '')
                {
                    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
                }
            }
    
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);  
            curl_setopt($curl, CURLOPT_HEADER, 0);  
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
            $result = curl_exec($curl);  
            curl_close($curl);  
            return $result;
        } 
    }
    

     小程序端场景id接收,走接口绑定

    注意参数获取方式
    
    业务场景较多的小程序码,使用getwxacodeunlimit获取,其参数需要注意,可不是?id=2,
    扫码访问内容页面,在页面的onload方法中,解析小程序码的参数
    
    比如要传入一个用户id=12,scene参数就可以写成"12",多个参数按一定规则分开,如&符号,第二个参数是shop_id=3,
    则scene可以这样写"12&3"
    用户扫码进入后的逻辑
    
    我们可以在该页面的onload生命周期中处理参数
    onLoad:function(options){
    	if(options.scene){
    		let scene=decodeURIComponent(options.scene);
    		//&是我们定义的参数链接方式
    		let id=scene.split("&")[0];
    		let shop_id=scene.split('&')[1];
    		//其他逻辑处理。。。。。
    	}
    }
    

    场景id官方文档要求格式为:

    请求带场景id图片地址入口:  /index.php?s=/Xcxqrcode/mpid/22222/id/112

    生成图片示例:[生成的二维码默认logo就是小程序的logo,贼方便]



  • 相关阅读:
    奥数视频
    提车应该检查哪?4S店都怕你检查这4个“雷区”,别等后悔才知道
    乒乓球拍子和套胶选择
    2018天津英华国际学校初中报名指南
    水瓶座出生日期是几月几号到几月几号
    乒乓球 世锦赛
    鸡蛋羹要怎么蒸才会更嫩?秘诀在这里
    家庭理财方法:知道这7个定律可以帮你赚更多钱!
    要知道股市有“三底”,估值底、政策底、市场底!
    DbMigration使用方法
  • 原文地址:https://www.cnblogs.com/gyrgyr/p/13517665.html
Copyright © 2011-2022 走看看