Add Some Functions
This commit is contained in:
parent
5d6de79738
commit
583a929c9b
56
control.go
56
control.go
@ -7,36 +7,60 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BindUser(m *tb.Message) bool {
|
//If Successfully return "",else return error information
|
||||||
fmt.Printf("%d Begin Bind", m.Chat.ID)
|
func BindUser(m *tb.Message) string {
|
||||||
|
fmt.Printf("%d Begin Bind\n", m.Chat.ID)
|
||||||
code := GetURLValue(m.Text, "code")
|
code := GetURLValue(m.Text, "code")
|
||||||
fmt.Println(code)
|
fmt.Println(code)
|
||||||
access, refresh := MSFirGetToken(code)
|
access, refresh := MSFirGetToken(code)
|
||||||
if refresh == "" {
|
if refresh == "" {
|
||||||
fmt.Printf("%d Bind error:Getinfo", m.Chat.ID)
|
fmt.Printf("%d Bind error:GetRefreshToken\n", m.Chat.ID)
|
||||||
return false
|
return "获取RefreshToken失败"
|
||||||
}
|
}
|
||||||
fmt.Printf("TGID:%d Refresh Token: %s", m.Chat.ID, refresh)
|
|
||||||
//
|
//token has gotten
|
||||||
bot.Send(m.Chat, "Token获取成功!")
|
bot.Send(m.Chat, "Token获取成功!")
|
||||||
info := MSGetUserInfo(access)
|
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 == "" {
|
if info == "" {
|
||||||
fmt.Printf("%d Bind error:Getinfo", m.Chat.ID)
|
fmt.Printf("%d Bind error:Getinfo\n", m.Chat.ID)
|
||||||
return false
|
return "获取用户信息错误"
|
||||||
}
|
}
|
||||||
|
|
||||||
var u MSData
|
var u MSData
|
||||||
u.tgId = m.Chat.ID
|
u.tgId = m.Chat.ID
|
||||||
u.refreshToken = refresh
|
u.refreshToken = refresh
|
||||||
u.msId = gjson.Get(info, "id").String()
|
u.msId = gjson.Get(info, "id").String()
|
||||||
u.uptime = time.Now()
|
u.uptime = time.Now()
|
||||||
u.other = ""
|
u.other = ""
|
||||||
//
|
|
||||||
bot.Send(m.Chat, "MS_ID:"+u.msId+"\nuserPrincipalName: "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName"+gjson.Get(info, "displayName").String())
|
//MS User Is Exist
|
||||||
if ok, err := AddData(db, u); !ok {
|
if MSUserIsExist(u.tgId, u.msId) {
|
||||||
fmt.Printf("%d Bind error: %s", m.Chat.ID, err)
|
fmt.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
|
||||||
return false
|
return "该ID对应的用户已经绑定过了"
|
||||||
}
|
}
|
||||||
fmt.Printf("%d Bind Successfully!", m.Chat.ID)
|
//MS information has gotten
|
||||||
return true
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user