国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. File name and location
2. Code
Background controller processing
WeChat event response
All sharing interface
Home PHP Framework ThinkPHP How ThinkPHP5 integrates JS-SDK to implement WeChat custom sharing function

How ThinkPHP5 integrates JS-SDK to implement WeChat custom sharing function

May 27, 2023 am 08:07 AM
thinkphp js-sdk

Jssdk class library

1. File name and location

Name: Jssdk.php
Location: extend\util\Jssdk.php

2. Code

<?php
namespace util;

class Jssdk {

    protected $appid = &#39;xxxx&#39;;
    protected $secret = &#39;xxxx&#39;;

    /**
     * 獲取access_token方法
     */
    public function getAccessToken(){
        //定義文件名稱
        $name = &#39;token_&#39; . md5($this->appid . $this->secret);
        //定義存儲(chǔ)文件路徑
        // $filename = __DIR__ . &#39;/cache/&#39; . $name . &#39;.php&#39;;
		$filename = &#39;../runtime/temp/&#39; . $name . &#39;.php&#39;;
        //判斷文件是否存在,如果存在,就取出文件中的數(shù)據(jù)值,如果不存在,就向微信端請(qǐng)求
        if (is_file($filename) && filemtime($filename) + 7100 > time()){
            $result = include $filename;
            //定義需要返回的內(nèi)容$data
            $data = $result[&#39;access_token&#39;];
        }else{
            // https請(qǐng)求方式: GET
			// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
            // 調(diào)用curl方法完成請(qǐng)求
            $url = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39;.$this->appid.&#39;&secret=&#39; . $this->secret;
            $result = $this->curl($url);
            //將返回得到的json數(shù)據(jù)轉(zhuǎn)成php數(shù)組
            $result = json_decode($result,true);
            //將內(nèi)容寫(xiě)入文件中
            file_put_contents($filename,"<?php\nreturn " . var_export($result,true) . ";\n?>");
            //定義需要返回的內(nèi)容
            $data = $result[&#39;access_token&#39;];
        }

        //將得到的access_token的值返回
        return $data;

    }

    /**
     *
     * 獲取臨時(shí)票據(jù)方法
     *
     * @return mixed
     */
    public function getJsapiTicket(){
        //存入文件中,定義文件的名稱和路徑
        $name = &#39;ticket_&#39; . md5($this->appid . $this->secret);
        //定義存儲(chǔ)文件路徑
        //$filename = __DIR__ . &#39;/cache/&#39; . $name . &#39;.php&#39;;
		$filename = &#39;../runtime/temp/&#39; . $name . &#39;.php&#39;;
        //判斷是否存在臨時(shí)票據(jù)的文件,如果存在,就直接取值,如果不存在,就發(fā)送請(qǐng)求獲取并保存
        if (is_file($filename) && filemtime($filename) + 7100 > time()){
            $result = include $filename;
        }else{
            //定義請(qǐng)求地址
            $url = &#39;https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=&#39;.$this
                    ->getAccessToken().&#39;&type=jsapi&#39;;
            //使用curl方法發(fā)送請(qǐng)求,獲取臨時(shí)票據(jù)
            $result = $this->curl($url);
            //轉(zhuǎn)換成php數(shù)組
            $result = json_decode($result,true);
            //將獲取到的值存入文件中
            file_put_contents($filename,"<?php\nreturn " . var_export($result,true) . ";\n?>");

        }
        //定義返回的數(shù)據(jù)
        $data = $result[&#39;ticket&#39;];
        //將得到的臨時(shí)票據(jù)結(jié)果返回
        return $data;
    }

    /**
     * 獲取簽名方法
     */
    public function sign(){
        //需要定義4個(gè)參數(shù),分別包括隨機(jī)數(shù),臨時(shí)票據(jù),時(shí)間戳和當(dāng)前url地址
        $nonceStr = $this->makeStr();
        $ticket = $this->getJsapiTicket();
        $time = time();
        //組合url
		//$url = $_SERVER[&#39;REQUEST_SCHEME&#39;] . &#39;://&#39; . $_SERVER[&#39;SERVER_NAME&#39;] . $_SERVER[&#39;REQUEST_URI&#39;];
        $url = &#39;http://&#39; . $_SERVER[&#39;SERVER_NAME&#39;] . $_SERVER[&#39;REQUEST_URI&#39;];
        //將4個(gè)參數(shù)放入一個(gè)數(shù)組中
        $arr = [
            &#39;noncestr=&#39; . $nonceStr,
            &#39;jsapi_ticket=&#39; . $ticket,
            &#39;timestamp=&#39; . $time,
            &#39;url=&#39; . $url
        ];
        //對(duì)數(shù)組進(jìn)行字段化排序
        sort($arr,SORT_STRING);
        //對(duì)數(shù)組進(jìn)行組合成字符串
        $string = implode(&#39;&&#39;,$arr);
        //將字符串加密生成簽名
        $sign = sha1($string);
        //由于調(diào)用簽名方法的時(shí)候不只需要簽名,還需要生成簽名的時(shí)候的隨機(jī)數(shù),時(shí)間戳,所以我們應(yīng)該返回由這些內(nèi)容組成的一個(gè)數(shù)組
        $reArr = [
            &#39;appId&#39; => $this->appid,
            &#39;timestamp&#39; => $time,
            &#39;nonceStr&#39; => $nonceStr,
            &#39;signature&#39; => $sign,
            &#39;url&#39; => $url
        ];
        //將數(shù)組返回
        return $reArr;
    }

    /**
     *
     * 生成隨機(jī)數(shù)
     *
     * @return string
     */
    protected function makeStr(){
        //定義字符串組成的種子
        $seed = &#39;www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net&#39;;
        //通過(guò)循環(huán)來(lái)組成一個(gè)16位的隨機(jī)字符串
        //定義一個(gè)空字符串 用來(lái)接收組合成的字符串內(nèi)容
        $str = &#39;&#39;;
        for ($i = 0;$i < 16; $i++){
            //定義一個(gè)隨機(jī)數(shù)
            $num = rand(0,strlen($seed) - 1);
            //循環(huán)連接隨機(jī)生成的字符串
            $str .= $seed[$num];
        }
        //將隨機(jī)數(shù)返回
        return $str;
    }


    /**
     *
     * 服務(wù)器之間請(qǐng)求的curl方法
     *
     * @param $url 請(qǐng)求地址
     * @param array $field post參數(shù)
     * @return string
     */
    public function curl($url,$field = []){
        //初始化curl
        $ch = curl_init();
        //設(shè)置請(qǐng)求的地址
        curl_setopt($ch,CURLOPT_URL,$url);
        //設(shè)置接收返回的數(shù)據(jù),不直接展示在頁(yè)面
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        //設(shè)置禁止證書(shū)校驗(yàn)
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        //判斷是否為post請(qǐng)求方式,如果傳遞了第二個(gè)參數(shù),就代表是post請(qǐng)求,如果么有傳遞,第二個(gè)參數(shù)為空,就是get請(qǐng)求
        if (!empty($field)){
            //設(shè)置請(qǐng)求超時(shí)時(shí)間
            curl_setopt($ch,CURLOPT_TIMEOUT,30);
            //設(shè)置開(kāi)啟post
            curl_setopt($ch,CURLOPT_POST,1);
            //傳遞post數(shù)據(jù)
            curl_setopt($ch,CURLOPT_POSTFIELDS,$field);
        }
        //定義一個(gè)空字符串,用來(lái)接收請(qǐng)求的結(jié)果
        $data = &#39;&#39;;
        if (curl_exec($ch)){
            $data = curl_multi_getcontent($ch);
        }
        //關(guān)閉curl
        curl_close($ch);
        //將得到的結(jié)果返回
        return $data;
    }

}
//測(cè)試獲取access_token值的方法
//$obj = new Wx();
//$data = $obj->getAccessToken();
//echo $data;

//測(cè)試獲取jsapiticket方法
//$obj = new Wx();
//$data = $obj->getJsapiTicket();
//echo $data;

//測(cè)試生成簽名方法
//$obj = new Wx();
//$data = $obj->sign();
//echo &#39;<pre class="brush:php;toolbar:false">&#39;;
//print_r($data);

?>

Background controller processing

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\admin\model\Menu;
use util\Jssdk;

class Index extends Controller {
    public function demo(){
        $id = input(&#39;id&#39;,0);//ID
        $catid = input(&#39;catid&#39;,0);//分類(lèi)ID

        $modelInfo = getModInfoById($catid);

        $info = Db::name($modelInfo[&#39;tablename&#39;])->where(&#39;id&#39;,$id)->find();
        $catinfo = getCatInfoById($catid);
        $p_catname = getCatInfoById($catinfo[&#39;parentid&#39;],&#39;catname&#39;);

		$obj = new Jssdk();
		$data = $obj->sign();

        $this->assign(&#39;infos&#39;,$info);
        $this->assign(&#39;catids&#39;,$catid);
        $this->assign(&#39;catnames&#39;,$catinfo[&#39;catname&#39;]);
        $this->assign(&#39;p_catnames&#39;,$p_catname);
		$this->assign(&#39;data&#39;,$data);

        return view(&#39;../application/index/view/default/index/&#39; . $modelInfo[&#39;show_template&#39;]);
    }
}
?>

WeChat event response

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
	// 通過(guò)config接口注入權(quán)限驗(yàn)證配置
	wx.config({
		debug: false, 
		appId: &#39;{$data.appId}&#39;,
		timestamp: &#39;{$data.timestamp}&#39;,
		nonceStr: &#39;{$data.nonceStr}&#39;, 
		signature: &#39;{$data.signature}&#39;,
		jsApiList: [
			&#39;onMenuShareTimeline&#39;,
			&#39;onMenuShareAppMessage&#39;
		]
	});
	// 通過(guò)ready接口處理成功驗(yàn)證
	wx.ready(function(){
		// 分享到朋友圈
		wx.onMenuShareTimeline({
			title: &#39;{$info.title}&#39;,
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			success: function () {
				// 用戶點(diǎn)擊了分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
		// 分享給朋友
		wx.onMenuShareAppMessage({
			title: &#39;{$info.title}&#39;, 
			desc: &#39;{$info.description}&#39;, 
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			type: &#39;link&#39;, // 分享類(lèi)型,music、video或link,不填默認(rèn)為link
			dataUrl: &#39;&#39;, // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空
			success: function () {
				// 用戶點(diǎn)擊了分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
	});
</script>

All sharing interface

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
	// 通過(guò)config接口注入權(quán)限驗(yàn)證配置
	wx.config({
		debug: true, 
		appId: &#39;{$data.appId}&#39;,
		timestamp: &#39;{$data.timestamp}&#39;,
		nonceStr: &#39;{$data.nonceStr}&#39;, 
		signature: &#39;{$data.signature}&#39;,
		jsApiList: [
			&#39;onMenuShareTimeline&#39;,
			&#39;onMenuShareAppMessage&#39;,
			&#39;onMenuShareQQ&#39;,
			&#39;onMenuShareWeibo&#39;,
			&#39;onMenuShareQZone&#39;
		]
	});
	// 通過(guò)ready接口處理成功驗(yàn)證
	wx.ready(function(){
		// 分享到朋友圈
		wx.onMenuShareTimeline({
			title: &#39;{$info.title}&#39;,
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			success: function () {
				// 用戶點(diǎn)擊了分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
		// 分享給朋友
		wx.onMenuShareAppMessage({
			title: &#39;{$info.title}&#39;, 
			desc: &#39;{$info.description}&#39;, 
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			type: &#39;link&#39;, // 分享類(lèi)型,music、video或link,不填默認(rèn)為link
			dataUrl: &#39;&#39;, // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空
			success: function () {
				// 用戶點(diǎn)擊了分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
		// 分享到QQ
		wx.onMenuShareQQ({
			title: &#39;{$info.title}&#39;, 
			desc: &#39;{$info.description}&#39;, 
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			success: function () {
				// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
			},
			cancel: function () {
				// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
		// 分享到騰訊微博
		wx.onMenuShareWeibo({
			title: &#39;{$info.title}&#39;,
			desc: &#39;{$info.description}&#39;, 
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			success: function () {
				// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
			},
			cancel: function () {
				// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
		// 分享到QQ空間
		wx.onMenuShareQZone({
			title: &#39;{$info.title}&#39;, 
			desc: &#39;{$info.description}&#39;, 
			link: &#39;{$data.url}&#39;, 
			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;, 
			success: function () {
				// 用戶確認(rèn)分享后執(zhí)行的回調(diào)函數(shù)
			},
			cancel: function () {
				// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
			}
		});
	});
</script>

The above is the detailed content of How ThinkPHP5 integrates JS-SDK to implement WeChat custom sharing function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

See all articles