gameConfig.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/config"
  7. "designs/global"
  8. "designs/model"
  9. "designs/utils"
  10. "github.com/gin-gonic/gin"
  11. "github.com/go-playground/validator/v10"
  12. "strconv"
  13. "time"
  14. )
  15. /* 添加游戏配置 */
  16. //http://127.0.0.1:8787/v1/user/AddGidConfig
  17. func AddGidConfig(c *gin.Context) {
  18. var data GameConfig
  19. form := request.Check(c, &data)
  20. // 在这种情况下,将自动选择合适的绑定
  21. if data.AppSecret != config.Get("app.app_secret") {
  22. response.Fail(c, 1003, "密钥参数错误")
  23. return
  24. }
  25. gameConfigData := make(map[string]interface{})
  26. gameConfigData["gid"] = form.Gid
  27. gameConfigData["gameName"] = form.GameName
  28. gameConfigData["wxAppid"] = form.WxAppid
  29. gameConfigData["wxSecret"] = form.WxSecret
  30. gameConfigData["ttAppid"] = form.TtAppid
  31. gameConfigData["ttSecret"] = form.TtSecret
  32. gameConfigData["ttTplId"] = form.TtTplId
  33. gameConfigData["wxTplId"] = form.WxTplId
  34. gidKey := config.Get("app.gid") + form.Gid
  35. err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
  36. if err != nil {
  37. response.Fail(c, 1003, "配置错误")
  38. return
  39. }
  40. response.Success(c, gin.H{
  41. "data": gameConfigData,
  42. })
  43. }
  44. /* 获取配置 */
  45. func GetGidConfig(c *gin.Context) {
  46. var data GetGameCfg
  47. form := request.Check(c, &data)
  48. if form.AppSecret != config.Get("app.app_secret") {
  49. response.Fail(c, 1003, "密钥不对")
  50. }
  51. gidKey := config.Get("app.gid") + "*"
  52. keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
  53. var gameData = []interface{}{}
  54. for _, val := range keys {
  55. res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
  56. gameData = append(gameData, res)
  57. }
  58. response.Success(c, gin.H{
  59. "data": gameData,
  60. })
  61. }
  62. type ActionOption struct {
  63. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  64. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  65. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  66. OptionStatus int `json:"optionStatus" form:"optionStatus" binding:""`
  67. }
  68. // 设置游戏的打点
  69. func SetGameAction(c *gin.Context) {
  70. form := request.Check(c, &struct {
  71. Gid string `form:"gid" json:"gid" binding:"required"`
  72. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  73. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  74. Remark string `form:"remark" json:"remark" binding:""`
  75. Status int `form:"status" json:"status" binding:""`
  76. Options []ActionOption `form:"options" json:"options" binding:""`
  77. }{})
  78. //验证一下option 内的参数
  79. validate := validator.New()
  80. for _, v := range form.Options {
  81. err := validate.Struct(&v)
  82. if err != nil {
  83. response.Fail(c, 1003, err.Error())
  84. return
  85. }
  86. }
  87. //存入数据库
  88. now := time.Now()
  89. err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
  90. gameAction := model.GameAction{
  91. Gid: form.Gid,
  92. ActionId: form.ActionId,
  93. ActionName: form.ActionName,
  94. Remark: form.Remark,
  95. Status: form.Status,
  96. CreatedAt: model.XTime{Time: now},
  97. UpdatedAt: model.XTime{Time: now},
  98. }
  99. err := tx.Table("game_action").Save(&gameAction).Error
  100. if err != nil {
  101. return err
  102. }
  103. for _, option := range form.Options {
  104. gameActionOption := model.GameActionOption{
  105. OptionName: option.OptionName,
  106. OptionId: option.OptionId,
  107. ActionId: gameAction.ID,
  108. OptionType: option.OptionType,
  109. Status: option.OptionStatus,
  110. CreatedAt: model.XTime{Time: now},
  111. UpdatedAt: model.XTime{Time: now},
  112. }
  113. err = tx.Table("game_action_option").Save(&gameActionOption).Error
  114. if err != nil {
  115. return err
  116. }
  117. }
  118. return nil
  119. })
  120. if err != nil {
  121. response.Fail(c, 1002, err.Error())
  122. return
  123. }
  124. response.Success(c, gin.H{})
  125. }
  126. // 更新游戏中的事件
  127. func UpdateGameAction(c *gin.Context) {
  128. form := request.Check(c, &struct {
  129. Gid string `form:"gid" json:"gid" binding:"required"`
  130. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  131. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  132. Remark string `form:"remark" json:"remark" binding:""`
  133. Status int `form:"status" json:"status" binding:""`
  134. }{})
  135. err := global.App.DB.Table("game_action").
  136. Where("gid", form.Gid).
  137. Where("actionId", form.ActionId).
  138. Updates(map[string]interface{}{
  139. "status": form.Status,
  140. "remark": form.Remark,
  141. "updatedAt": time.Now(),
  142. "actionName": form.ActionName,
  143. }).Error
  144. if err != nil {
  145. response.Fail(c, 1001, err.Error())
  146. return
  147. }
  148. response.Success(c, gin.H{})
  149. }
  150. // 更新事件中的选项
  151. func UpdateGameActionOption(c *gin.Context) {
  152. form := request.Check(c, &struct {
  153. Id int `form:"id" json:"id" binding:"required"`
  154. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  155. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  156. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  157. Status int `form:"status" json:"status" binding:""`
  158. }{})
  159. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Updates(map[string]interface{}{
  160. "optionName": form.OptionName,
  161. "optionId": form.OptionId,
  162. "optionType": form.OptionType,
  163. "status": form.Status,
  164. "updatedAt": time.Now(),
  165. }).Error
  166. if err != nil {
  167. response.Fail(c, 1003, err.Error())
  168. return
  169. }
  170. response.Success(c, gin.H{})
  171. }
  172. // 新增事件中的选项
  173. func AddGameActionOption(c *gin.Context) {
  174. form := request.Check(c, &struct {
  175. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  176. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  177. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  178. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  179. Status int `json:"status" form:"status" binding:""`
  180. }{})
  181. now := time.Now()
  182. gameActionOption := model.GameActionOption{
  183. OptionName: form.OptionName,
  184. OptionId: form.OptionId,
  185. ActionId: form.ActionId,
  186. OptionType: form.OptionType,
  187. Status: form.Status,
  188. CreatedAt: model.XTime{Time: now},
  189. UpdatedAt: model.XTime{Time: now},
  190. }
  191. err := global.App.DB.Table("game_action_option").Save(&gameActionOption).Error
  192. if err != nil {
  193. response.Fail(c, 1001, err.Error())
  194. return
  195. }
  196. response.Success(c, gin.H{})
  197. }
  198. // 删除事件中的选项
  199. func DeleteGameActionOption(c *gin.Context) {
  200. form := request.Check(c, &struct {
  201. Id int `form:"id" json:"id" binding:"required"`
  202. }{})
  203. var d interface{}
  204. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Delete(d).Error
  205. if err != nil {
  206. response.Fail(c, 1001, err.Error())
  207. return
  208. }
  209. response.Success(c, gin.H{})
  210. }
  211. // 列表 事件列表
  212. func GameActionList(c *gin.Context) {
  213. form := request.Check(c, &struct {
  214. Offset int `form:"offset" json:"offset" binding:""`
  215. Limit int `form:"limit" json:"limit" binding:"required"`
  216. Search string `form:"search" json:"search" binding:""`
  217. Status string `form:"status" json:"status" binding:""`
  218. Order string `form:"order" json:"order" binding:""`
  219. Gid string `form:"gid" json:"gid" binding:"required"`
  220. }{})
  221. query := global.App.DB.Table("game_action").Where("gid", form.Gid)
  222. if form.Search != "" {
  223. query = query.WhereRaw(global.App.DB.Where("actionId", "like", "%"+form.Search+"%").Or("actionName like ?", "%"+form.Search+"%").SubQuery())
  224. }
  225. if form.Status != "" {
  226. status, _ := strconv.Atoi(form.Status)
  227. query = query.Where("status", status)
  228. }
  229. if form.Order != "" {
  230. query = query.Order("id " + form.Order)
  231. }
  232. var count int64
  233. err := query.Count(&count).Error
  234. if err != nil {
  235. response.Fail(c, 1001, err.Error())
  236. return
  237. }
  238. var actionList []model.GameAction
  239. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&actionList).Error
  240. if err != nil {
  241. response.Fail(c, 1002, err.Error())
  242. return
  243. }
  244. response.Success(c, gin.H{
  245. "data": actionList,
  246. "count": count,
  247. })
  248. }
  249. // 事件详情
  250. func GameActionDetail(c *gin.Context) {
  251. form := request.Check(c, &struct {
  252. Id int `form:"id" json:"id" binding:"required"`
  253. }{})
  254. var action model.GameAction
  255. err := global.App.DB.Table("game_action").Where("id", form.Id).First(&action).Error
  256. if err != nil {
  257. response.Fail(c, 1001, err.Error())
  258. return
  259. }
  260. //var optionList []model.GameActionOption
  261. //err = global.App.DB.Table("game_action_option").Where("actionId", action.ActionId).Scan(&optionList).Error
  262. response.Success(c, gin.H{
  263. "data": action,
  264. })
  265. }
  266. // 事件 选项 列表
  267. func GameActionOptionList(c *gin.Context) {
  268. form := request.Check(c, &struct {
  269. Offset int `form:"offset" json:"offset" binding:""`
  270. Limit int `form:"limit" json:"limit" binding:"required"`
  271. Search string `form:"search" json:"search" binding:""`
  272. Status string `form:"status" json:"status" binding:""`
  273. Order string `form:"order" json:"order" binding:""`
  274. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  275. }{})
  276. query := global.App.DB.Table("game_action_option").Where("actionId", form.ActionId)
  277. if form.Search != "" {
  278. query = query.WhereRaw(global.App.DB.Where("optionId", "like", "%"+form.Search+"%").Where("optionName", "like", "%"+form.Search+"%").SubQuery())
  279. }
  280. if form.Status != "" {
  281. status, _ := strconv.Atoi(form.Status)
  282. query = query.Where("status", status)
  283. }
  284. if form.Order != "" {
  285. query = query.Order("id " + form.Order)
  286. }
  287. var count int64
  288. err := query.Count(&count).Error
  289. if err != nil {
  290. response.Fail(c, 1001, err.Error())
  291. return
  292. }
  293. var optionList []model.GameActionOption
  294. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&optionList).Error
  295. if err != nil {
  296. response.Fail(c, 1002, err.Error())
  297. return
  298. }
  299. response.Success(c, gin.H{
  300. "data": optionList,
  301. "count": count,
  302. })
  303. }