handler.go 1.25 KB
package handler

import (
	"errors"
	"net/http"

	"client/bulletin/common"
	"client/bulletin/db"
	"client/bulletin/model"
	"client/bulletin/utils/render"
)

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

func GetBulletin(w http.ResponseWriter, r *http.Request) {
	gameID := r.URL.Query().Get("game_id")
	channelName := r.URL.Query().Get("channel")
	platformName := r.URL.Query().Get("platform")
	userID := r.URL.Query().Get("user_id")
	deviceID := r.URL.Query().Get("deviceId")
	forceShow := r.URL.Query().Get("force_show")

	if !(len(gameID) != 0 && len(channelName) != 0 && len(platformName) != 0) {
		render.BindError(w, r, errors.New("  获取参数错误"))
		return
	}
	if len(userID)+len(deviceID) == 0 {
		render.BindError(w, r, errors.New("  获取参数错误:用户及设备为空"))
		return
	}
	bulletins, err := model.GetBulletinByIDChannelAndPlatform(db.Mysql, gameID, userID, deviceID, channelName, platformName, forceShow)
	if err != nil {
		render.InternalError(w, r, err)
		return
	}

	if len(bulletins.Notices) == 0 {
		render.NullResultError(w, r, errors.New("  查询结果为空"))
		return
	}

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