日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

小程序生成带推荐码(场景id)的二维码

發(fā)布時(shí)間:2023/12/13 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 小程序生成带推荐码(场景id)的二维码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?php
namespace BusinessController;
use ThinkHook;

/**
 * 小程序碼[帶推薦碼/場(chǎng)景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'];//場(chǎng)景id/推薦碼
        $mpid = $_GET['mpid'];
        $rs = $this->getTemporaryQrcode($mpid,$this->access_tokens, $scene_str);
        echo $rs;
    }
    //生成二維碼
    public function getTemporaryQrcode($mpid,$access_tokens,$scene_str=''){
        //首頁(yè)小程序碼。無(wú)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;
    }
    //把請(qǐng)求發(fā)送到微信服務(wù)器換取二維碼
    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;
    } 
}

小程序端場(chǎng)景id接收,走接口綁定

注意參數(shù)獲取方式

業(yè)務(wù)場(chǎng)景較多的小程序碼,使用getwxacodeunlimit獲取,其參數(shù)需要注意,可不是?id=2,
掃碼訪(fǎng)問(wèn)內(nèi)容頁(yè)面,在頁(yè)面的onload方法中,解析小程序碼的參數(shù)

比如要傳入一個(gè)用戶(hù)id=12,scene參數(shù)就可以寫(xiě)成"12",多個(gè)參數(shù)按一定規(guī)則分開(kāi),如&符號(hào),第二個(gè)參數(shù)是shop_id=3,
則scene可以這樣寫(xiě)"12&3"
用戶(hù)掃碼進(jìn)入后的邏輯

我們可以在該頁(yè)面的onload生命周期中處理參數(shù)
onLoad:function(options){
	if(options.scene){
		let scene=decodeURIComponent(options.scene);
		//&是我們定義的參數(shù)鏈接方式
		let id=scene.split("&")[0];
		let shop_id=scene.split('&')[1];
		//其他邏輯處理。。。。。
	}
}

場(chǎng)景id官方文檔要求格式為:

請(qǐng)求帶場(chǎng)景id圖片地址入口: /index.php?s=/Xcxqrcode/mpid/22222/id/112

生成圖片示例:[生成的二維碼默認(rèn)logo就是小程序的logo,賊方便]



總結(jié)

以上是生活随笔為你收集整理的小程序生成带推荐码(场景id)的二维码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。