123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- package v1
- import (
- "context"
- "designs/app/common/request"
- "designs/app/common/response"
- "designs/config"
- "designs/global"
- "designs/model"
- "designs/utils"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "strconv"
- "time"
- )
- /* 添加游戏配置 */
- //http://127.0.0.1:8787/v1/user/AddGidConfig
- func AddGidConfig(c *gin.Context) {
- var data GameConfig
- form := request.Check(c, &data)
- // 在这种情况下,将自动选择合适的绑定
- if data.AppSecret != config.Get("app.app_secret") {
- response.Fail(c, 1003, "密钥参数错误")
- return
- }
- gameConfigData := make(map[string]interface{})
- gameConfigData["gid"] = form.Gid
- gameConfigData["gameName"] = form.GameName
- gameConfigData["wxAppid"] = form.WxAppid
- gameConfigData["wxSecret"] = form.WxSecret
- gameConfigData["ttAppid"] = form.TtAppid
- gameConfigData["ttSecret"] = form.TtSecret
- gidKey := config.Get("app.gid") + form.Gid
- err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
- if err != nil {
- response.Fail(c, 1003, "配置错误")
- return
- }
- response.Success(c, gin.H{
- "data": gameConfigData,
- })
- }
- /* 获取配置 */
- func GetGidConfig(c *gin.Context) {
- var data GetGameCfg
- form := request.Check(c, &data)
- if form.AppSecret != config.Get("app.app_secret") {
- response.Fail(c, 1003, "密钥不对")
- }
- gidKey := config.Get("app.gid") + "*"
- keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
- var gameData = []interface{}{}
- for _, val := range keys {
- res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
- gameData = append(gameData, res)
- }
- response.Success(c, gin.H{
- "data": gameData,
- })
- }
- type ActionOption struct {
- OptionName string `json:"optionName" form:"optionName" binding:"required"`
- OptionId string `json:"optionId" form:"optionId" binding:"required"`
- OptionType string `json:"optionType" form:"optionType" binding:"required"`
- OptionStatus int `json:"optionStatus" form:"optionStatus" binding:""`
- }
- // 设置游戏的打点
- func SetGameAction(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- ActionId string `form:"actionId" json:"actionId" binding:"required"`
- ActionName string `form:"actionName" json:"actionName" binding:"required"`
- Remark string `form:"remark" json:"remark" binding:""`
- Status int `form:"status" json:"status" binding:""`
- Options []ActionOption `form:"options" json:"options" binding:""`
- }{})
- //验证一下option 内的参数
- validate := validator.New()
- for _, v := range form.Options {
- err := validate.Struct(&v)
- if err != nil {
- response.Fail(c, 1003, err.Error())
- return
- }
- }
- //存入数据库
- now := time.Now()
- err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
- gameAction := model.GameAction{
- Gid: form.Gid,
- ActionId: form.ActionId,
- ActionName: form.ActionName,
- Remark: form.Remark,
- Status: form.Status,
- CreatedAt: model.XTime{Time: now},
- UpdatedAt: model.XTime{Time: now},
- }
- err := tx.Table("game_action").Save(&gameAction).Error
- if err != nil {
- return err
- }
- for _, option := range form.Options {
- gameActionOption := model.GameActionOption{
- OptionName: option.OptionName,
- OptionId: option.OptionId,
- ActionId: gameAction.ID,
- OptionType: option.OptionType,
- Status: option.OptionStatus,
- CreatedAt: model.XTime{Time: now},
- UpdatedAt: model.XTime{Time: now},
- }
- err = tx.Table("game_action_option").Save(&gameActionOption).Error
- if err != nil {
- return err
- }
- }
- return nil
- })
- if err != nil {
- response.Fail(c, 1002, err.Error())
- return
- }
- response.Success(c, gin.H{})
- }
- // 更新游戏中的事件
- func UpdateGameAction(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- ActionId string `form:"actionId" json:"actionId" binding:"required"`
- ActionName string `form:"actionName" json:"actionName" binding:"required"`
- Remark string `form:"remark" json:"remark" binding:""`
- Status int `form:"status" json:"status" binding:""`
- }{})
- err := global.App.DB.Table("game_action").
- Where("gid", form.Gid).
- Where("actionId", form.ActionId).
- Updates(map[string]interface{}{
- "status": form.Status,
- "remark": form.Remark,
- "updatedAt": time.Now(),
- "actionName": form.ActionName,
- }).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{})
- }
- // 更新事件中的选项
- func UpdateGameActionOption(c *gin.Context) {
- form := request.Check(c, &struct {
- Id int `form:"id" json:"id" binding:"required"`
- OptionName string `json:"optionName" form:"optionName" binding:"required"`
- OptionId string `json:"optionId" form:"optionId" binding:"required"`
- OptionType string `json:"optionType" form:"optionType" binding:"required"`
- Status int `form:"status" json:"status" binding:""`
- }{})
- err := global.App.DB.Table("game_action_option").Where("id", form.Id).Updates(map[string]interface{}{
- "optionName": form.OptionName,
- "optionId": form.OptionId,
- "optionType": form.OptionType,
- "status": form.Status,
- "updatedAt": time.Now(),
- }).Error
- if err != nil {
- response.Fail(c, 1003, err.Error())
- return
- }
- response.Success(c, gin.H{})
- }
- // 新增事件中的选项
- func AddGameActionOption(c *gin.Context) {
- form := request.Check(c, &struct {
- ActionId int `form:"actionId" json:"actionId" binding:"required"`
- OptionName string `json:"optionName" form:"optionName" binding:"required"`
- OptionId string `json:"optionId" form:"optionId" binding:"required"`
- OptionType string `json:"optionType" form:"optionType" binding:"required"`
- Status int `json:"status" form:"status" binding:""`
- }{})
- now := time.Now()
- gameActionOption := model.GameActionOption{
- OptionName: form.OptionName,
- OptionId: form.OptionId,
- ActionId: form.ActionId,
- OptionType: form.OptionType,
- Status: form.Status,
- CreatedAt: model.XTime{Time: now},
- UpdatedAt: model.XTime{Time: now},
- }
- err := global.App.DB.Table("game_action_option").Save(&gameActionOption).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{})
- }
- // 删除事件中的选项
- func DeleteGameActionOption(c *gin.Context) {
- form := request.Check(c, &struct {
- Id int `form:"id" json:"id" binding:"required"`
- }{})
- var d interface{}
- err := global.App.DB.Table("game_action_option").Where("id", form.Id).Delete(d).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{})
- }
- // 列表 事件列表
- func GameActionList(c *gin.Context) {
- form := request.Check(c, &struct {
- Offset int `form:"offset" json:"offset" binding:""`
- Limit int `form:"limit" json:"limit" binding:"required"`
- Search string `form:"search" json:"search" binding:""`
- Status string `form:"status" json:"status" binding:""`
- Order string `form:"order" json:"order" binding:""`
- Gid string `form:"gid" json:"gid" binding:"required"`
- }{})
- query := global.App.DB.Table("game_action").Where("gid", form.Gid)
- if form.Search != "" {
- query = query.WhereRaw(global.App.DB.Where("actionId", "like", "%"+form.Search+"%").Or("actionName like ?", "%"+form.Search+"%").SubQuery())
- }
- if form.Status != "" {
- status, _ := strconv.Atoi(form.Status)
- query = query.Where("status", status)
- }
- if form.Order != "" {
- query = query.Order("id " + form.Order)
- }
- var count int64
- err := query.Count(&count).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var actionList []model.GameAction
- err = query.Offset(form.Offset).Limit(form.Limit).Scan(&actionList).Error
- if err != nil {
- response.Fail(c, 1002, err.Error())
- return
- }
- response.Success(c, gin.H{
- "data": actionList,
- "count": count,
- })
- }
- // 事件详情
- func GameActionDetail(c *gin.Context) {
- form := request.Check(c, &struct {
- Id int `form:"id" json:"id" binding:"required"`
- }{})
- var action model.GameAction
- err := global.App.DB.Table("game_action").Where("id", form.Id).First(&action).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //var optionList []model.GameActionOption
- //err = global.App.DB.Table("game_action_option").Where("actionId", action.ActionId).Scan(&optionList).Error
- response.Success(c, gin.H{
- "data": action,
- })
- }
- // 事件 选项 列表
- func GameActionOptionList(c *gin.Context) {
- form := request.Check(c, &struct {
- Offset int `form:"offset" json:"offset" binding:""`
- Limit int `form:"limit" json:"limit" binding:"required"`
- Search string `form:"search" json:"search" binding:""`
- Status string `form:"status" json:"status" binding:""`
- Order string `form:"order" json:"order" binding:""`
- ActionId int `form:"actionId" json:"actionId" binding:"required"`
- }{})
- query := global.App.DB.Table("game_action_option").Where("actionId", form.ActionId)
- if form.Search != "" {
- query = query.WhereRaw(global.App.DB.Where("optionId", "like", "%"+form.Search+"%").Where("optionName", "like", "%"+form.Search+"%").SubQuery())
- }
- if form.Status != "" {
- status, _ := strconv.Atoi(form.Status)
- query = query.Where("status", status)
- }
- if form.Order != "" {
- query = query.Order("id " + form.Order)
- }
- var count int64
- err := query.Count(&count).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var optionList []model.GameActionOption
- err = query.Offset(form.Offset).Limit(form.Limit).Scan(&optionList).Error
- if err != nil {
- response.Fail(c, 1002, err.Error())
- return
- }
- response.Success(c, gin.H{
- "data": optionList,
- "count": count,
- })
- }
|