扫码登录 简单实现

简单原理是 服务器生成唯一的 key  附带到login 上
用户扫描 二维码 并且访问服务器 服务器反馈登录  状态
前端 页面 每隔一段时间扫描 服务器 当前的key是否扫描, 然后后续操作


代码:


package main

import (
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"time"

	. "github.com/soekchl/myUtils"
)

const file = `




  
  扫码



 

扫码登录

My Eth Address

` var ( keyMap = make(map[string]int) ) func HelloServer(w http.ResponseWriter, req *http.Request) { rand.Seed(time.Now().UnixNano()) r := rand.Int() keyMap[fmt.Sprint(r)] = 1 url := fmt.Sprint("http://10.0.82.88:8080/login?key=", r) Notice(url) io.WriteString(w, fmt.Sprintf(file, url)) } func main() { http.HandleFunc("/", HelloServer) http.HandleFunc("/login", login) http.HandleFunc("/checkLogin", checkLogin) Notice("Server Start!") err := http.ListenAndServe(":8080", nil) if err != nil { panic("ListenAndServe: " + err.Error()) } } func login(w http.ResponseWriter, req *http.Request) { key := req.FormValue("key") if keyMap[key] == 1 { Notice("login ok ", key) io.WriteString(w, fmt.Sprintf("", key)) Notice(len(key)) keyMap[key] = 2 } else { Notice("login ", key) io.WriteString(w, fmt.Sprintf("")) } } func checkLogin(w http.ResponseWriter, req *http.Request) { key := req.FormValue("key") if keyMap[key] == 2 { Debug("checkLogin ok ", key) io.WriteString(w, "1") // 登录成功 } else { io.WriteString(w, "0") } }


这个东东弊端是 key 随处可见,只是用于 学习,用在商务的话 需要慎重!!!

你可能感兴趣的:(技术,golang)