Esempio n. 1
0
func GetSession(c *gin.Context) (session *Session, err error) {
	sid := c.Request.Header.Get("Sid")
	//fmt.Println("sid-->", sid)
	//fmt.Printf("Header = %#v\n", c.Request.Header)

	conn := db.GetConnection()
	var u Userinfo

	session = &Session{
		Sid: sid,
	}

	u.get_userinfo(conn, session.Sid)
	//fmt.Println("u===>", u)

	now := time.Now()
	utcnow := now.Add(-8 * time.Hour)
	d_time := utcnow.Add(-60 * time.Minute)
	l_time := d_time.Format("2006-01-02 15:04:05")

	//fmt.Println("u.LastActTime == >", u.LastActTime)
	if (u.LastActTime != "") && (l_time > u.LastActTime) {
		u.delete_sid(conn, u.Id)
		return nil, errors.New("session expired")
	}

	if !u.Online {
		u.set_useronline(conn, u.Id)
	}

	session.User = u

	return
}
Esempio n. 2
0
func NewMessage(c *gin.Context) {
	user, _ := c.Get("User")

	username := user.(middleware.Userinfo).Username
	sid, _ := c.Get("Sid")
	fmt.Println("sid--->", sid)

	var msg Message
	err := c.BindJSON(&msg)
	if err == nil {
		//fmt.Printf("%#v\n", msg)
		conn := db.GetConnection()
		//fmt.Println("--->", username)
		if msg.Body == "" {
			return
		}
		if username == "" {
			c.JSON(400, gin.H{"error": "session expired"})
			return
		}
		insert_message(conn, msg.Body, username)
		if msg.To != "" {
			//fmt.Println("go to:", msg.To)
			c.JSON(http.StatusOK, gin.H{"status": "have to", "body": "you say: " + msg.Body})
		} else {
			//fmt.Println("to all")
			c.JSON(http.StatusOK, gin.H{"status": "to all", "body": "you say: " + msg.Body})
		}
		return
	}
	fmt.Println("err===", err)
	c.JSON(400, gin.H{"error": err})
}
Esempio n. 3
0
func SignUpRequest(c *gin.Context) {
	s := &SignupStat{}
	err := c.BindJSON(s)
	if err == nil {
		conn := db.GetConnection()
		if s.Email == "" {
			c.JSON(200, gin.H{"error": "请填入注册邮件!"})
			log.Info("email already registered")
			return
		}

		if s.verify_email_reg(conn) {
			c.JSON(200, gin.H{"error": "此邮件已被注册!"})
			log.Info("email already registered")
			return
		}

		s.Authcode = RandseqDigit(6)
		s.Authcode_key = Randseq(64)
		fmt.Println("注册邮箱是: ", s.Email, s.Authcode)

		insert_authcode(conn, s.Authcode_key, s.Authcode, s.Email)

		SendMail(s.Email, s.Authcode)

		c.JSON(200, gin.H{"authcode_key": s.Authcode_key})
	} else {
		c.JSON(400, gin.H{"error": err.Error()})
	}
}
Esempio n. 4
0
func GetUserInfo(c *gin.Context) {
	conn := db.GetConnection()
	sid := c.Request.Header.Get("Sid")
	//fmt.Println("sid==>",sid)
	data := get_userinfo(conn, sid)
	c.JSON(200, gin.H{"id": data["id"], "email": data["email"], "username": data["username"], "last_msg_time": data["last_msg_time"]})

}
Esempio n. 5
0
// 查询新消息
func GetMessages(c *gin.Context) {
	//user, _ := c.Get("User")
	//sid, _ := c.Get("Sid")
	//fmt.Println("get user===>sid", user, sid)
	user, _ := c.Get("User")
	username := user.(middleware.Userinfo).Username

	var data_time []map[string]interface{}
	var online_users []string

	conn := db.GetConnection()

	if lasttime := c.Query("t"); len(lasttime) > 0 {
		lt_dt, _ := time.Parse(time.RFC3339Nano, lasttime)
		mysql_dt := lt_dt.Format("2006-01-02 15:04:05")

		now := time.Now()
		utcnow := now.Add(-8 * time.Hour)
		d_time := utcnow.Add(-10 * time.Minute)
		drop_time := d_time.Format("2006-01-02 15:04:05")
		//fmt.Println("drop_time", drop_time)

		for {
			data_time = select_message_time(conn, mysql_dt)
			//fmt.Println(data_time)
			if len(data_time) > 0 {
				break
			}
			time.Sleep(1 * time.Second)
		}

		online_users = get_useronline(conn, drop_time)

	} else {

		now := time.Now()
		utcnow := now.Add(-8 * time.Hour)
		d_time := utcnow.Add(-10 * time.Minute)
		drop_time := d_time.Format("2006-01-02 15:04:05")

		data_time = select_message_new(conn)
		online_users = get_useronline(conn, drop_time)

	}

	c.JSON(http.StatusOK, gin.H{"signinuser": username, "body": data_time, "onlineusers": online_users})
}
Esempio n. 6
0
func SignIn(c *gin.Context) {
	var l Login
	err := c.BindJSON(&l)
	if err == nil {
		if l.Username == "" {
			c.JSON(400, gin.H{"error": "请输入用户名!"})
			return
		}
		if l.Password == "" {
			c.JSON(400, gin.H{"error": "请输入密码!"})
			return
		}
		conn := db.GetConnection()
		mark_username := l.verify_user(conn)
		if !mark_username {
			c.JSON(400, gin.H{"error": "密码或用户名错误!"})
			fmt.Println("Singin no find username")
			return
		} else {
			mark_password, user_id := l.user_login(conn)
			if !mark_password {
				c.JSON(400, gin.H{"error": "密码或用户名错误!"})
				fmt.Println("Singin password error")
				return
			} else {
				if !select_session_user_id(conn, user_id) {
					l.Sid = Randseq(128)
					insert_sid(conn, l.Sid, user_id)
				} else {
					l.Sid = select_sid(conn, user_id)
					fmt.Println("此用户的 sid 已存在!")
				}
			}
		}
		c.JSON(200, gin.H{"sid": l.Sid})

	} else {
		fmt.Println("form err:", err)
		c.JSON(400, gin.H{"error": err.Error()})
	}

}
Esempio n. 7
0
func SignUp(c *gin.Context) {
	s := &SignupStat{}

	if c.BindJSON(s) == nil {
		conn := db.GetConnection()
		if !s.verify_authcode(conn) {
			c.JSON(200, gin.H{"error": "验证码错误!"})
			log.Error("verify_authcode return false")
			return
		}

		if s.verify_username_reg(conn) {
			c.JSON(200, gin.H{"error": "用户名已被注册!"})
			log.Error("username already registered")
			return
		}

		if (len(s.Password) < 6) || (len(s.Password)) > 20 {
			c.JSON(200, gin.H{"error": "请输入6-20位密码!"})
			log.Error("password to long or to short")
			return
		}

		password := EncryptPassword(s.Password)
		fmt.Printf("password===>, %x\n", password)
		s.Password = password

		if !insert_user(conn, s.Username, s.Password, s.Email) {
			log.Error("insert_user return false")
			return
		}
		c.JSON(200, gin.H{"authcode_key": s.Authcode_key, "authcode": s.Authcode, "username": s.Username, "password": s.Password})
	} else {
		fmt.Println("验证错误")
		c.JSON(400, gin.H{"error": "fail"})
		return
	}
}
Esempio n. 8
0
func Signout(c *gin.Context) {
	Sid, _ := c.Get("Sid")
	sid := Sid.(string)
	conn := db.GetConnection()
	signout_del_session(conn, sid)
}