增加U盘登录

This commit is contained in:
hoteas 2022-05-05 16:07:01 +08:00
parent 53f24c033c
commit 3b2a317d2b
10 changed files with 270 additions and 12 deletions

106
example/app/AES.go Normal file
View File

@ -0,0 +1,106 @@
package app
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
)
//高级加密标准Adevanced Encryption Standard ,AES
//16,24,32位字符串的话分别对应AES-128AES-192AES-256 加密方法
//key不能泄露
var PwdKey = []byte("mif022h3g9geAHUHY432,:da1adag389")
//PKCS7 填充模式
func pKCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
//Repeat()函数的功能是把切片[]byte{byte(padding)}复制padding个然后合并成新的字节切片返回
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
//填充的反向操作,删除填充字符串
func pKCS7UnPadding(origData []byte) ([]byte, error) {
//获取数据长度
length := len(origData)
if length == 0 {
return nil, errors.New("加密字符串错误!")
} else {
//获取填充字符串长度
unpadding := int(origData[length-1])
//截取切片,删除填充字节,并且返回明文
return origData[:(length - unpadding)], nil
}
}
//实现加密
func AesEcrypt(origData []byte, key []byte) ([]byte, error) {
//创建加密算法实例
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
//获取块的大小
blockSize := block.BlockSize()
//对数据进行填充,让数据长度满足需求
origData = pKCS7Padding(origData, blockSize)
//采用AES加密方法中CBC加密模式
blocMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
//执行加密
blocMode.CryptBlocks(crypted, origData)
return crypted, nil
}
//实现解密
func AesDeCrypt(cypted []byte, key []byte) ([]byte, error) {
//创建加密算法实例
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
//获取块大小
blockSize := block.BlockSize()
//创建加密客户端实例
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(cypted))
//这个函数也可以用来解密
blockMode.CryptBlocks(origData, cypted)
//去除填充字符串
origData, err = pKCS7UnPadding(origData)
if err != nil {
return nil, err
}
return origData, err
}
//加密base64
func EnPwdCode(pwd []byte) (string, error) {
result, err := AesEcrypt(pwd, PwdKey)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(result), err
}
//解密
func DePwdCode(pwd string) ([]byte, error) {
//解密base64字符串
pwdByte, err := base64.StdEncoding.DecodeString(pwd)
if err != nil {
return nil, err
}
//执行AES解密
return AesDeCrypt(pwdByte, PwdKey)
}
//func main() {
// str := []byte("12fff我是ww.topgoer.com的站长枯藤")
// pwd, _ := EnPwdCode(str)
// bytes, _ := DePwdCode(pwd)
// fmt.Println(string(bytes))
//}

View File

@ -388,15 +388,15 @@ var DeclareCtr = Ctr{
res = append(res, article)
}
minMoney := 0
maxMoney := 0
minMoney := float64(0)
maxMoney := float64(0)
for _, v := range res {
if v.GetMap("declare") != nil {
if v.GetMap("declare").GetInt("money_scope_min") < minMoney {
minMoney = v.GetMap("declare").GetInt("money_scope_min")
if v.GetMap("declare").GetFloat64("money_scope_min") < minMoney {
minMoney = v.GetMap("declare").GetFloat64("money_scope_min")
}
if v.GetMap("declare").GetInt("money_scope_max") > maxMoney {
maxMoney = v.GetMap("declare").GetInt("money_scope_max")
if v.GetMap("declare").GetFloat64("money_scope_max") > maxMoney {
maxMoney = v.GetMap("declare").GetFloat64("money_scope_max")
}
}
}
@ -410,11 +410,11 @@ var DeclareCtr = Ctr{
"del_flag": 0,
}
if maxMoney != minMoney {
seData["money_scope"] = ObjToStr(minMoney) + "-" + ObjToStr(maxMoney) + "万元"
seData["money_scope"] = ObjToStr(ObjToInt(minMoney)) + "-" + ObjToStr(ObjToInt(maxMoney)) + ""
} else if maxMoney == 0 {
seData["money_scope"] = ""
} else {
seData["money_scope"] = ObjToStr(maxMoney) + "万元"
seData["money_scope"] = ObjToStr(ObjToInt(maxMoney)) + ""
}
//匹配记录存储

View File

@ -5,6 +5,7 @@ import (
. "code.hoteas.com/golang/hotime/common"
"errors"
"time"
"unicode/utf8"
)
// Project 管理端项目
@ -22,6 +23,7 @@ var Project = Proj{
"search_record": SearchRecordCtr,
"sms": Sms,
"tag": TagCtr,
"upan": UpanCtr,
"user": UserCtr,
"vip_order": VipOrderCtr,
"websocket": WebsocketCtr,
@ -109,3 +111,15 @@ func auth(that *Context, phone, companyName string) error {
return nil
}
// FilterEmoji 过滤 emoji 表情
func FilterEmoji(content string) string {
new_content := ""
for _, value := range content {
_, size := utf8.DecodeRuneInString(string(value))
if size <= 3 {
new_content += string(value)
}
}
return new_content
}

84
example/app/upan.go Normal file
View File

@ -0,0 +1,84 @@
package app
import (
. "code.hoteas.com/golang/hotime"
"code.hoteas.com/golang/hotime/common"
"fmt"
"strings"
)
var UpanCtr = Ctr{
"login": func(that *Context) {
timestamp := that.Req.FormValue("timestamp")
sn := that.Req.FormValue("sn")
//str,_:=EnPwdCode([]byte(lus[len(lus)-1]+":"+ObjToStr(t)))//
re, e := DePwdCode(sn)
if e != nil {
that.Display(3, "数据异常")
return
}
reStr := string(re)
realSn := strings.Replace(reStr, ":"+timestamp, "", -1)
if len(realSn)+len(timestamp)+1 != len(reStr) {
that.Display(4, "数据验证失败")
return
}
fmt.Println("U盘校验", realSn)
user := that.Db.Get("user", "*", common.Map{"upankey": realSn})
if user == nil {
that.Display(5, "还没有绑定用户")
return
}
that.Session("user_id", user.GetCeilInt("id"))
that.Display(0, "登录成功")
},
"create": func(that *Context) {
timestamp := that.Req.FormValue("timestamp")
sn := that.Req.FormValue("sn")
//str,_:=EnPwdCode([]byte(lus[len(lus)-1]+":"+ObjToStr(t)))//
re, e := DePwdCode(sn)
if e != nil {
that.Display(3, "数据异常")
return
}
reStr := string(re)
realSn := strings.Replace(reStr, ":"+timestamp, "", -1)
if len(realSn)+len(timestamp)+1 != len(reStr) {
that.Display(4, "数据验证失败")
return
}
fmt.Println("U盘校验", realSn)
uuser := that.Db.Get("user", "id", common.Map{"upankey": realSn})
if uuser != nil {
that.Display(4, "已经绑定了其他企业")
return
}
phone := that.Req.FormValue("phone")
companyName := that.Req.FormValue("company_name")
//验证不成功则反馈
err := auth(that, phone, companyName)
if err != nil {
fmt.Println(err)
that.Display(3, err.Error())
return
}
num := that.Db.Update("user", common.Map{"upankey": realSn}, common.Map{"id": that.Session("user_id").Data})
if num == 0 {
that.Display(4, "更新失败")
return
}
that.Display(0, "绑定成功")
},
}

View File

@ -46,7 +46,7 @@ var VipOrderCtr = Ctr{
data := Map{
"sn": "SN" + time.Now().Format("20060102150405") + getSn(),
//"name":"1年VIP会员",
"amount": 72000, //720元
"amount": 36000, //720元
"user_id": user.GetCeilInt64("id"),
"company_id": user.GetCeilInt("company_id"),
"expiration_time": time.Now().Add(365 * 24 * time.Hour).Format("2006-01-02 15:04:05"),
@ -73,7 +73,7 @@ var VipOrderCtr = Ctr{
data["amount"] = 36000
//tp=tp
}
data["amount"] = 1
//data["amount"] = 1
if user.GetCeilInt("salesman_id") != 0 {
data["salesman_id"] = user.GetCeilInt("salesman_id")

View File

@ -29,7 +29,7 @@ var Wechath5 = Ctr{
"retoken": resToken.RefreshToken,
"appid": appid,
"unionid": userInfo.Unionid,
"nickname": userInfo.Nickname,
"nickname": FilterEmoji(userInfo.Nickname),
"avatar": userInfo.HeadImgURL,
"create_time[#]": "now()",
"modify_time[#]": "now()",

View File

@ -4,6 +4,7 @@ import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"time"
"unicode/utf8"
)
// Project 管理端项目
@ -33,3 +34,15 @@ func getCode() string {
//}
return res
}
// 过滤 emoji 表情
func FilterEmoji(content string) string {
new_content := ""
for _, value := range content {
_, size := utf8.DecodeRuneInString(string(value))
if size <= 3 {
new_content += string(value)
}
}
return new_content
}

View File

@ -22,6 +22,15 @@ var Salesman = Ctr{
that.Display(4, "找不到该业务员")
return
}
if salesman.GetString("nickname") == "" {
wechat := that.Db.Get("wechat", "*", Map{"salesman_id": salesman.GetCeilInt64("id")})
if wechat != nil {
salesman["nickname"] = wechat.GetString("nickname")
salesman["avatar"] = wechat.GetString("avatar")
that.Db.Update("salesman", Map{"nickname": wechat.GetString("nickname"), "avatar": wechat.GetString("avatar")}, Map{"id": salesman.GetCeilInt64("id")})
}
}
salesman["user"] = that.Db.Count("user", Map{"AND": Map{"salesman_id": that.Session("salesman_id").Data, "del_flag": 0}})
salesman["matters"] = that.Db.Count("matters", Map{"AND": Map{"salesman_id": that.Session("salesman_id").Data, "del_flag": 0}})

View File

@ -29,7 +29,7 @@ var Wechat = Ctr{
"retoken": resToken.RefreshToken,
"appid": appid,
"unionid": userInfo.Unionid,
"nickname": userInfo.Nickname,
"nickname": FilterEmoji(userInfo.Nickname),
"avatar": userInfo.HeadImgURL,
//"create_time[#]":"now()",
"modify_time[#]": "now()",

32
example/tpt/pc.html Normal file
View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>微信登录</title>
<script type="text/javascript" src="js/hotime.js"></script>
</head>
<body>
<script>
function run() {
var data={"timestamp": window.H.getParam("timestamp"),"sn":window.H.getParam("sn")}
// if(data.code==null){
// location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4d97696b9ecb49fc&redirect_uri='+location.href+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
// return
// }
window.H.post("app/upan/login", data, function (res) {
if (res.status != 0) {
alert(res.result.msg)
return
}
// let res = { "result": { "acttoken": "56_ogEfWa4mglG-Ilf2kD50FfekZWimJXUouZ4gMKmkVP2CwcLwv2lHO35LOn5NSLOQ-yEAZcIs3FvgIBhk2uF9CA", "appid": "wx4d97696b9ecb49fc", "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLNj0yKXe77H8C60ic2lUFIx5hkibf0FluNCRfTkiazrBfuqclqRhm4nDug9Hx3nsXQhtzNdfchAvJTQ/132", "nickname": "候鸟半夏", "openid": "oPoZT6juCkF6fvnMHrCFI6SK_vK8", "retoken": "56_tjuJPobvbLVvOPtqPRZjTzQHR7i3Vmx_aGXOy9j0WsNTFS_JRnb4ArmUGhWtq6e7eCpUnPNKulIQ44CPiBnYEA", "unionid": "ofKK36PEkbIt0xMMUgch4H-bVaFI" }, "status": 0 }
location.href="/#/home"
})
}
run()
</script>
</body>
</html>