friendSystem.go 5.21 KB
package apis

import (
	. "client/friendSystem/models"
	"log"
	"strconv"

	"github.com/gin-gonic/gin"
)

func AddFriendApi(c *gin.Context) {
	addType := c.Request.FormValue("add_type")
	userId := c.Request.FormValue("user_id")
	objectId := c.Request.FormValue("object_id")
	log.Printf("addType [%s] userId [%s] objectId [%s]", addType, userId, objectId)
	if addType == "" || userId == "" || objectId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	f := FriendRelationship{
		FriendRelationshipAId: userId,
		FriendRelationshipBId: objectId,
	}
	result, err := f.AddFriend(addType)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func DeleteFriendApi(c *gin.Context) {
	userId := c.Request.FormValue("user_id")
	objectId := c.Request.FormValue("object_id")
	if userId == "" || objectId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	f := FriendRelationship{
		FriendRelationshipAId: userId,
		FriendRelationshipBId: objectId,
	}
	result, err := f.DeleteFriend()
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func RespondFriendRequestApi(c *gin.Context) {
	userId := c.Request.FormValue("user_id")
	requestId := c.Request.FormValue("request_id")
	respondResult := c.Request.FormValue("respond_result")
	if userId == "" || requestId == "" || respondResult == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	f := FriendRelationship{
		FriendRelationshipAId: requestId,
		FriendRelationshipBId: userId,
	}
	result, err := f.RespondFriendRequest(respondResult)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func CheckFriendRequestApi(c *gin.Context) {
	userId := c.Request.FormValue("user_id")
	if userId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	f := FriendRelationship{
		FriendRelationshipBId: userId,
	}
	result, err := f.CheckFriendRequest()
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func GetQrcodeImgApi(c *gin.Context) {
	userId := c.Request.FormValue("user_id")
	if userId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}

	result, err := GetQrCodeImg(userId)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func GetRandomFriendApi(c *gin.Context) {
	randomType := c.Request.FormValue("random_type")
	randomLength := c.Request.FormValue("random_length")
	if randomLength == "" || randomType == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	randomLengthInt, err := strconv.Atoi(randomLength)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	result, err := GetRandomFriend(randomType, randomLengthInt)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func GetFriendRanklistApi(c *gin.Context) {
	gameId := c.Request.FormValue("game_id")
	userId := c.Request.FormValue("user_id")
	ranklistLength := c.Request.FormValue("ranklist_length")
	ranklistIndex := c.Request.FormValue("ranklist_index")
	ranklistName := c.Request.FormValue("ranklist_name")
	if gameId == "" || userId == "" || ranklistIndex == "" || ranklistLength == "" || ranklistName == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	ranklistLengthInt, err := strconv.Atoi(ranklistLength)
	ranklistIndexInt, err := strconv.Atoi(ranklistIndex)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	f := FriendRanklistRequest{
		RanklistGameId:     gameId,
		RanklistUserId:     userId,
		RanklistLength:     ranklistLengthInt,
		RanklistFieldIndex: ranklistIndexInt,
		RanklistName:       ranklistName,
	}
	result, err := f.GetFriendRanklist()
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func GetFriendNameListApi(c *gin.Context) {
	UserId := c.Request.FormValue("user_id")
	if UserId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	result, err := GetFriendNameList(UserId)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func CreateDBTableApi(c *gin.Context) {
	state := true
	gameId := c.Request.FormValue("game_id")
	if gameId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	sql := "CREATE TABLE ice_tablename_" + gameId + "(ranklist_id int not null auto_increment,ranklist_user_id varchar(50) not null,ranklist_user_name varchar(50) not null,ranklist_head_portrait_url varchar(100) not null"
	for i := 0; i <= 7; i++ {
		num := strconv.Itoa(i + 1)
		key := "field_name_" + num
		value := c.Request.FormValue(key)
		if value != "" {
			state = false
			sql = sql + "," + value + " float"
		}
	}
	if state {
		ReturnRespones(c, "1003", nil)
		return
	}
	sql = sql + ",PRIMARY KEY (ranklist_id))ENGINE = InnoDB DEFAULT CHARSET=utf8"
	result, err := CreateDBTable(sql)
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}

func CheckFriendResponesApi(c *gin.Context) {
	userId := c.Request.FormValue("user_id")
	if userId == "" {
		ReturnRespones(c, "1003", nil)
		return
	}
	f := FriendRelationship{
		FriendRelationshipAId: userId,
	}
	result, err := f.CheckFriendRespones()
	if err != nil {
		ReturnRespones(c, "1000", err)
		return
	}
	ReturnRespones(c, "0", result)
	return
}