handler.go 2.78 KB
package handler

import (
	"net/http"

	"client/gift/common"
	"client/gift/db"
	"client/gift/model"
	"client/gift/utils"
	"client/gift/utils/render"
	"errors"
	"log"
)

func TestGetGiftFunc(w http.ResponseWriter, r *http.Request) {
	gameCode := r.URL.Query().Get("gift_code")
	userID := r.URL.Query().Get("user_id")
	gameID := r.URL.Query().Get("game_id")
	if len(gameID) == 0 || len(gameCode) == 0 || len(userID) == 0 {
		render.BindError(w, r, errors.New("获取参数错误"))
		return
	}
	giftResults, err := model.GetGiftPropid(db.Mysql, gameCode, gameID)
	if err != nil {
		render.InternalError(w, r, err)
		return
	}
	if len(giftResults.Gifts) == 0 {
		render.InternalError(w, r, errors.New("查询结果为空"))
		return
	}

	render.JSON(w, r, http.StatusOK, map[string]interface{}{
		"code": common.OK,
		"obj":  giftResults,
	})
	return
}

func PingHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("ok\n"))
	return
}

func GetGiftContent(w http.ResponseWriter, r *http.Request) {
	var giftReq GiftReq
	giftReq.GiftCode = r.PostFormValue("gift_code")
	giftReq.GameID = r.PostFormValue("game_id")
	giftReq.User = r.PostFormValue("user_id")
	giftReq.ChannelName = r.PostFormValue("channel")
	giftReq.PlatformName = r.PostFormValue("platform")
	log.Print("parameters:", giftReq)
	if !utils.CheckParam(giftReq.GiftCode) {
		render.BindError(w, r, errors.New("建议检查礼包码"))
		return
	}

	if giftReq.GiftCode == "" || giftReq.GameID == "" || giftReq.User == "" {
		render.BindError(w, r, errors.New("缺少"))
		return
	}

	//检查同一task是否领过。
	gained, err := model.CheckTaskRepeat(db.Mysql, giftReq.GiftCode, giftReq.User)
	if err != nil {
		render.InternalError(w, r, errors.New("检出task重复时出错:"+err.Error()))
		return
	}
	if gained {
		render.TaskRepeatError(w, r, nil)
		return
	}

	//查到propid 和 account,,,time,code as parameters
	giftResults, err := model.GetGiftPropid(db.Mysql, giftReq.GiftCode, giftReq.GameID)

	if err != nil {
		render.InternalError(w, r, err)
		return
	}

	log.Println("result:", giftResults.Gifts)

	//上边为空,不调用。
	if len(giftResults.Gifts) > 0 {
		_, err := model.SetCheckStatus(db.Mysql, giftReq.User, giftReq.GiftCode)
		if err != nil {
			render.InternalError(w, r, errors.New("  设置用户时出错"))
			return
		}
		render.JSON(w, r, http.StatusOK, map[string]interface{}{
			"code": common.OK,
			"obj":  giftResults,
		})
		return
	}

	if len(giftResults.Gifts) == 0 {
		checkErr := model.CheckNullReason(db.Mysql, giftReq.GiftCode)
		if checkErr != nil {
			checkErr = errors.New("  未获取到内容" + checkErr.Error())
			render.InternalNullError(w, r, checkErr)
			return
		}
		render.InternalNullError(w, r, errors.New("  建议检查gameID"))
		return
	}
	render.InternalNullError(w, r, errors.New("  "))
	return

}