From 583a929c9badb11cd1a98bf8f05fbb45fcd50b19 Mon Sep 17 00:00:00 2001 From: iyear Date: Fri, 27 Mar 2020 23:01:17 +0800 Subject: [PATCH] Add Some Functions --- control.go | 56 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/control.go b/control.go index 3832a92..1040896 100644 --- a/control.go +++ b/control.go @@ -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 }