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

 

posted on 2019-07-08 00:36  广源时代  阅读(2284)  评论(0编辑  收藏  举报

导航

支付宝 QQ群