gameConfig.go 9.6 KB

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