今天呢,有一份开发需求,需要开发一个微信扫一扫识别到兑奖码,获取到之后锁定兑奖码为只读状态不可再进行编辑,最后点击进行兑换指定奖品的后续操作,
根据微信开发文档,再根据ThinkPHP3.2.3的代码规范,整理出以下可复制代入的代码,特此进行记录一下,方便后续开发直接拿来复用:
先在Thinkphp框架目录/Core/Extend/Vendor/WxJDK下面创建WxJDK文件夹,然后在创建文件jssdk.php.
jssdk.php创建位置如下截图所示:
jssdk.php内容为:
<?php
/**
* Class JSSDK
* auth: wdy
* 微信文档 :https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
* PHP框架:ThinkPHP3.2.3
*/
class JSSDK{
//公众号appid
public $appId;
//公众号appSecret
private $appSecret;
/**
* JSSDK constructor.
* @param $appId
* @param $appSecret
*/
public function __construct( $appId , $appSecret ) {
$this -> appId = $appId;
$this -> appSecret = $appSecret;
}
/**
* @return array
* 获取配置参数
*/
public function getWxConfig() {
//1.获取jsapi_ticket
$jsapiTicket = $this -> getJsApiTicket();
//2.获取调用页面的url
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//3.时间戳
$timestamp = time();
//4.获取随机字符串
$nonceStr = $this -> createNonceStr();
/**
* 参数的顺序一定要按照key值ASCII码升序排序
*/
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
//5.sha1获取签名
$signature = sha1($string);
//拼接页面所需传递的参数
$WxConfig = array(
"appId" => $this -> appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,