Add Some Judgements

This commit is contained in:
iyear 2020-03-28 10:22:07 +08:00
parent 70618db06a
commit 252d39b70c

View File

@ -4,13 +4,21 @@ import (
"fmt" "fmt"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
"strings"
"time" "time"
) )
//If Successfully return "",else return error information //If Successfully return "",else return error information
func BindUser(m *tb.Message) string { func BindUser(m *tb.Message) string {
fmt.Printf("%d Begin Bind\n", m.Chat.ID) fmt.Printf("%d Begin Bind\n", m.Chat.ID)
code := GetURLValue(m.Text, "code") tmp := strings.Split(m.Text, " ")
fmt.Println("alias: " + tmp[1])
if len(tmp) != 2 {
fmt.Printf("%d Bind error:Wrong Bind Format\n", m.Chat.ID)
return "授权格式错误"
}
alias := tmp[1]
code := GetURLValue(tmp[0], "code")
fmt.Println(code) fmt.Println(code)
access, refresh := MSFirGetToken(code) access, refresh := MSFirGetToken(code)
if refresh == "" { if refresh == "" {
@ -32,8 +40,7 @@ func BindUser(m *tb.Message) string {
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 = SetJsonValue("{}", "alias", alias)
//MS User Is Exist //MS User Is Exist
if MSUserIsExist(u.tgId, u.msId) { if MSUserIsExist(u.tgId, u.msId) {
fmt.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID) fmt.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
@ -49,13 +56,13 @@ func BindUser(m *tb.Message) string {
return "" return ""
} }
func GetBindNum(tgId int64) int { func GetBindNum(tgId int64) int {
data := QueryData(db, tgId) data := QueryDataByTG(db, tgId)
return len(data) return len(data)
} }
//return true => exist //return true => exist
func MSUserIsExist(tgId int64, msId string) bool { func MSUserIsExist(tgId int64, msId string) bool {
data := QueryData(db, tgId) data := QueryDataByTG(db, tgId)
var res MSData var res MSData
for _, res = range data { for _, res = range data {
if res.msId == msId { if res.msId == msId {