12-網頁,網站,微信公眾號基礎入門(編寫后台PHP程序,實現Airkiss配網)


https://www.cnblogs.com/yangfengwu/p/11067590.html

 

首先說一下,這兩個地方需要配置一樣

 

 

 

 

網站根目錄建個文件夾 airkiss的文件夾

 

 

 里面放上

 

 

index.php

復制代碼
<?php
require_once "jssdk.php";
$jssdk = new JSSDK("yourAppID", "yourAppSecret");//這里改成自己的
$signPackage = $jssdk->GetSignPackage();
?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta charset="UTF-8">
    <title>微信JS-SDK測試</title>
  
    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>

    <script type="text/javascript">
 
        window.onload = function () {
            wx.config({
            beta:true,//開啟內測接口調用,注入wx.invoke方法
            debug:false,//關閉調試模式
            appId: '<?php echo $signPackage["appId"];?>',//AppID
            timestamp: <?php echo $signPackage["timestamp"];?>,//時間戳
            nonceStr: '<?php echo $signPackage["nonceStr"];?>',//隨機串
            signature: '<?php echo $signPackage["signature"];?>',//簽名
            jsApiList: [
              // 所有要調用的 API 都要加到這個列表中
              'configWXDeviceWiFi'
            ]
                    });

        };
 function jump(){
        wx.ready(function () {
          
            wx.checkJsApi({
                jsApiList: ['configWXDeviceWiFi'],
                success: function (res) {
  
                    wx.invoke('configWXDeviceWiFi', {},
                        function (res) {
                                    
                            var err_msg = res.err_msg;
                            
                            if (err_msg == 'configWXDeviceWiFi:ok') {

                                alert("配置 WIFI成功")
                                WeixinJSBridge.call('closeWindow');
                                return;
                                                  
                          } else {
                                alert("配置 WIFI失敗");
                            }
                        });

                }
            });
        });  
    }
     
         wx.error(function(res){
              alert("配置出錯");
          });
  
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
         <div style="width:100%;height:25rem;font-size:5rem;"></div>
        <p  style="text-align:center;font-size:40px;">1.確定手機已經連接wifi</p>
        <p  style="text-align:center;font-size:40px;">2.請長按模塊上的網絡配置按鈕</p>
        <p  style="text-align:center;font-size:40px;">3.請等待wifi配置指示燈閃爍后松開</p>           
        <div align="center">
        
          <input style="color:white;width:90%;height:8rem;font-size:4rem;" type="button" value="繼續配置" onclick=javascrtpt:jump()>
        </div>

    </div>
    </form>
</body>
</html>
復制代碼

 

 

jssdk.php

復制代碼
<?php
class JSSDK {
  private $appId;
  private $appSecret;

  public function __construct($appId, $appSecret) {
    $this->appId = $appId;
    $this->appSecret = $appSecret;
  }

  public function getSignPackage() {
    $jsapiTicket = $this->getJsApiTicket();

    // 注意 URL 一定要動態獲取,不能 hardcode.
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    $timestamp = time();
    $nonceStr = $this->createNonceStr();

    // 這里參數的順序要按照 key 值 ASCII 碼升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);

    $signPackage = array(
      "appId"     => $this->appId,
      "nonceStr"  => $nonceStr,
      "timestamp" => $timestamp,
      "url"       => $url,
      "signature" => $signature,
      "rawString" => $string
    );
    return $signPackage; 
  }

  private function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }

  private function getJsApiTicket() {
    // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例
    $data = json_decode($this->get_php_file("jsapi_ticket.php"));
    if ($data->expire_time < time()) {
      $accessToken = $this->getAccessToken();
      // 如果是企業號用以下 URL 獲取 ticket
      // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
      $res = json_decode($this->httpGet($url));
      $ticket = $res->ticket;
      if ($ticket) {
        $data->expire_time = time() + 7000;
        $data->jsapi_ticket = $ticket;
        $this->set_php_file("jsapi_ticket.php", json_encode($data));
      }
    } else {
      $ticket = $data->jsapi_ticket;
    }

    return $ticket;
  }

  private function getAccessToken() {
    // access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例
    $data = json_decode($this->get_php_file("access_token.php"));
    if ($data->expire_time < time()) {
      // 如果是企業號用以下URL獲取access_token
      // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
      $res = json_decode($this->httpGet($url));
      $access_token = $res->access_token;
      if ($access_token) {
        $data->expire_time = time() + 7000;
        $data->access_token = $access_token;
        $this->set_php_file("access_token.php", json_encode($data));
      }
    } else {
      $access_token = $data->access_token;
    }
    return $access_token;
  }

  private function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 為保證第三方服務器與微信服務器之間數據傳輸的安全性,所有微信接口采用https方式調用,必須使用下面2行代碼打開ssl安全校驗。
    // 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
  }

  private function get_php_file($filename) {
    return trim(substr(file_get_contents($filename), 15));
  }
  private function set_php_file($filename, $content) {
    $fp = fopen($filename, "w");
    fwrite($fp, "<?php exit();?>" . $content);
    fclose($fp);
  }
}
?>
復制代碼

 

注意:

 

 

 jsapi_ticket.php

<?php exit();?>{"jsapi_ticket":"LIKLckvwlJT9cWIhEQTwfI35ky7H5eEEOcj4jHRcMTUr_U_dMqmAtNBTNCcmxtMEovxX2nO71cs1RAJKdS6Zhg","expire_time":1565759510}

 

access_token.php

<?php exit();?>{"access_token":"24_w2RYneHczb89fnIljshdF4hPjLKrFmvpLLCQT5zFHqlE0kOPCmVt37jgdBHPxYmPQh27Ga4kpy1HwUu8rppnFZyaytsTSTEwwfUkwrv6Sqy3cN__3tkHspqi7EIGHVgAJAPQE","expire_time":1565759510}

 

然后

 

頁面增加了點自己的東西

 

 

整波動開關位置,STM32和Wi-Fi通信

  

 

短接STM32的PB2和Wi-Fi模塊的RST引腳(為了做項目穩定可靠,請使用單片機硬件復位Wi-Fi)

  

 

 

長按PB5大約4S,等待指示燈快閃,松開PB5,Wi-Fi模塊進入配網狀態

  

 

 

 這個是官方的

 

 

 

 

 

 

 

 

 

 

 

 

其它參考的地方

 

 

 

 

 

 

 

 

注:默認應該打開的文件是sample.php

我是把sample.php 改為了  index.php

 

 

 

 進入這個文件夾,默認訪問 index.php文件

當然也可以  "url": "http://mnifdv.cn/airkiss/sample.php"

網上比較多的是 "url": "http://mnifdv.cn/airkiss/AirKiss.php"  

 

https://www.cnblogs.com/yangfengwu/p/11192639.html

 


免責聲明!

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



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