微信小程序登录

问题

1、调用wx.login后code传给后端需要保存session_token,但是这个session_token无法设置缓存时间,有效时间和用户操作有关,只能通过前端wx.checksession判断有效性.
2、用户信息、手机号等授权接口解密都需要使用这个session_token。

解决方式

取消原先系统的登录验证方式,直接用checksession方法检查用户登录状态
前端直接用wx.checksession作为判断用户登录状态的方法,session无效再调用wx.login更新后端数据

流程

wx.checksession
->ok->判断用户已登录

->无效->执行->wx.login->用返回的code传给后端更新session_token

app.js

    wx.checkSession().then(
      ()=>{}, 
      ()=>{
        return utils.authLogin()
      }).finally(()=>{
        this.getUserInfo().then(res=>{
          this.globalData.userInfo = res.data;
          if(res.data.is_registered == 0){
            wx.navigateTo({
              url: '/pages/getUserProfile/getUserProfile'
            })
          } 
        })
      })

utils.js

module.exports = {
  formatTime,
  request: function(params){
    return new Promise((resolve, reject)=>{
      wx.request({
        ...params,
        dataType: 'json',
        header: {
          Authorization: 'Bearer ' + wx.getStorageSync('authToken')
        },
        success:res=>{
          if(res.data.code == 4003){
            wx.navigateTo({
              url: '/pages/index/forbidden'
            })
          }
          if(res.data.code == 403){
            this.authLogin()
          }
          resolve(res.data)
        },
        error:error=>{
          reject(error)
        }
      })
    })
  },
  login: function(){
    return new Promise((resolve, reject)=>{
      wx.login({
        success:res=>{
          resolve(res)
        },
        error:error=>{
          reject(error)
        }
      })
    })
  },
  authLogin: function(){
    this.login().then(res=>{
      this.request({
        url: 'http://www.pp.com/api/user/login',
        data: {
          code: res.code
        }
      }).then(res=>{
        wx.setStorageSync('authToken', res.data.token)
      }).catch(e=>{

      })

    })
  }
}

ESP8266 arduino wifi文档阅读

来源

https://arduino-esp8266.readthedocs.io/en/3.0.2/ideoptions.html
https://www.arduino.cc/en/Reference/WiFi

WIFI库

连接示例

#include <ESP8266WiFi.h>
void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin("network-name", "pass-to-network");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}
void loop(){}

包含的class

  • Station 连接wifi

  • SOFT-AP 作为软路由-提供其他设备联网能力

  • Scan 扫描范围内的SSID

  • Client

    WiFiClient()
    connected()
    connect()
    write()
    print()
    println()
    available()
    read()
    flush()
    stop()
  • WiFi Multi 记录多个ap并在断线时选择信号最好的wifi连接

    #include <ESP8266WiFiMulti.h>
    
    ESP8266WiFiMulti wifiMulti;
    // WiFi connect timeout per AP. Increase when connecting takes longer.
    const uint32_t connectTimeoutMs = 5000;
    
    void setup()
    {
      // Set in station mode
      WiFi.mode(WIFI_STA);
    
      // Register multi WiFi networks
      wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
      wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
      wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
    }
    
    void loop()
    {
      // Maintain WiFi connection
      if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {
          ...
      }
    }
  • Server
    例子

    #include <ESP8266WiFi.h>
    
    const char* ssid = "********";
    const char* password = "********";
    
    WiFiServer server(80);
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println();
    
      Serial.printf("Connecting to %s ", ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED)
      {
        delay(500);
        Serial.print(".");
      }
      Serial.println(" connected");
    
      server.begin();
      Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
    }
    
    // prepare a web page to be send to a client (web browser)
    String prepareHtmlPage()
    {
      String htmlPage;
      htmlPage.reserve(1024);               // prevent ram fragmentation
      htmlPage = F("HTTP/1.1 200 OK\r\n"
                   "Content-Type: text/html\r\n"
                   "Connection: close\r\n"  // the connection will be closed after completion of the response
                   "Refresh: 5\r\n"         // refresh the page automatically every 5 sec
                   "\r\n"
                   "<!DOCTYPE HTML>"
                   "<html>"
                   "Analog input:  ");
      htmlPage += analogRead(A0);
      htmlPage += F("</html>"
                    "\r\n");
      return htmlPage;
    }
    
    void loop()
    {
      WiFiClient client = server.available();
      // wait for a client (web browser) to connect
      if (client)
      {
        Serial.println("\n[Client connected]");
        while (client.connected())
        {
          // read line by line what the client (web browser) is requesting
          if (client.available())
          {
            String line = client.readStringUntil('\r');
            Serial.print(line);
            // wait for end of client's request, that is marked with an empty line
            if (line.length() == 1 && line[0] == '\n')
            {
              client.println(prepareHtmlPage());
              break;
            }
          }
        }
    
        while (client.available()) {
          // but first, let client finish its request
          // that's diplomatic compliance to protocols
          // (and otherwise some clients may complain, like curl)
          // (that is an example, prefer using a proper webserver library)
          client.read();
        }
    
        // close the connection:
        client.stop();
        Serial.println("[Client disconnected]");
      }
    }
  • UDP UDP通信

    begin()
    available()
    beginPacket()
    endPacket()
    write()
    parsePacket()
    peek()
    read()
    flush()
    stop()
    remoteIP()
    remotePort()

下载离线网站

wget --mirror --convert-links --adjust-extension --page-requisites 
--no-parent http://example.org
  • --mirror – Makes (among other things) the download recursive.
  • --convert-links – convert all the links (also to stuff like CSS stylesheets) to relative, so it will be suitable for offline viewing.
  • --adjust-extension – Adds suitable extensions to filenames (html or css) depending on their content-type.
  • --page-requisites – Download things like CSS style-sheets and images required to properly display the page offline.
  • --no-parent – When recursing do not ascend to the parent directory. It useful for restricting the download to only a portion of the site.

简写

wget -mkEpnp http://example.org

wget 准确率不高,HTTrack是更好用的工具

https://www.httrack.com/
HTTrack is a free (GPL, libre/free software) and easy-to-use offline browser utility.