Add Some Functions

This commit is contained in:
iyear 2020-03-27 23:01:17 +08:00
parent 5d6de79738
commit 583a929c9b

View File

@ -7,36 +7,60 @@ import (
"time"
)
func BindUser(m *tb.Message) bool {
fmt.Printf("%d Begin Bind", m.Chat.ID)
//If Successfully return "",else return error information
func BindUser(m *tb.Message) string {
fmt.Printf("%d Begin Bind\n", m.Chat.ID)
code := GetURLValue(m.Text, "code")
fmt.Println(code)
access, refresh := MSFirGetToken(code)
if refresh == "" {
fmt.Printf("%d Bind error:Getinfo", m.Chat.ID)
return false
fmt.Printf("%d Bind error:GetRefreshToken\n", m.Chat.ID)
return "获取RefreshToken失败"
}
fmt.Printf("TGID:%d Refresh Token: %s", m.Chat.ID, refresh)
//
//token has gotten
bot.Send(m.Chat, "Token获取成功!")
info := MSGetUserInfo(access)
fmt.Printf("TGID:%d Refresh Token: %s", m.Chat.ID, refresh)
fmt.Printf("TGID:%d Refresh Token: %s\n", m.Chat.ID, refresh)
if info == "" {
fmt.Printf("%d Bind error:Getinfo", m.Chat.ID)
return false
fmt.Printf("%d Bind error:Getinfo\n", m.Chat.ID)
return "获取用户信息错误"
}
var u MSData
u.tgId = m.Chat.ID
u.refreshToken = refresh
u.msId = gjson.Get(info, "id").String()
u.uptime = time.Now()
u.other = ""
//
bot.Send(m.Chat, "MS_ID:"+u.msId+"\nuserPrincipalName: "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName"+gjson.Get(info, "displayName").String())
if ok, err := AddData(db, u); !ok {
fmt.Printf("%d Bind error: %s", m.Chat.ID, err)
return false
//MS User Is Exist
if MSUserIsExist(u.tgId, u.msId) {
fmt.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
return "该ID对应的用户已经绑定过了"
}
fmt.Printf("%d Bind Successfully!", m.Chat.ID)
return true
//MS information has gotten
bot.Send(m.Chat, "MS_ID "+u.msId+"\nuserPrincipalName "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName "+gjson.Get(info, "displayName").String()+"\n")
if ok, err := AddData(db, u); !ok {
fmt.Printf("%d Bind error: %s\n", m.Chat.ID, err)
return "数据库写入错误"
}
fmt.Printf("%d Bind Successfully!\n", m.Chat.ID)
return ""
}
func GetBindNum(tgId int64) int {
data := QueryData(db, tgId)
return len(data)
}
//return true => exist
func MSUserIsExist(tgId int64, msId string) bool {
data := QueryData(db, tgId)
var res MSData
for _, res = range data {
if res.msId == msId {
return true
}
}
return false
}