小程序生成帶推薦碼(場景id)的二維碼


<?php
namespace Business\Controller;
use Think\Hook;

/**
 * 小程序碼[帶推薦碼/場景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,賊方便]

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM