gameConfig.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. "encoding/json"
  11. "github.com/gin-gonic/gin"
  12. "github.com/go-playground/validator/v10"
  13. "strconv"
  14. "time"
  15. )
  16. func InitGidConfig(c *gin.Context) {
  17. gidKey := config.Get("app.gid") + "*"
  18. keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
  19. var gameData = []interface{}{}
  20. for _, val := range keys {
  21. res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
  22. gameData = append(gameData, res)
  23. }
  24. now := model.XTime{
  25. Time: time.Now(),
  26. }
  27. for _, v := range gameData {
  28. var game model.Game
  29. data, _ := json.Marshal(v)
  30. json.Unmarshal(data, &game)
  31. game.CreatedAt = now
  32. game.UpdatedAt = now
  33. err := global.App.DB.Table("game").Create(&game).Error
  34. if err != nil {
  35. response.Fail(c, 111, "数据插入错误"+err.Error())
  36. return
  37. }
  38. }
  39. response.Success(c, gin.H{
  40. "res": "gid写入数据库完成",
  41. })
  42. }
  43. /* 添加游戏配置 */
  44. //http://127.0.0.1:8787/v1/user/AddGidConfig
  45. func AddGidConfig(c *gin.Context) {
  46. var data GameConfig
  47. form := request.Check(c, &data)
  48. // 在这种情况下,将自动选择合适的绑定
  49. if data.AppSecret != config.Get("app.app_secret") {
  50. response.Fail(c, 1003, "密钥参数错误")
  51. return
  52. }
  53. now := time.Now()
  54. //更新数据库
  55. var game model.Game
  56. global.App.DB.Table("game").Where("gid", form.Gid).First(&game)
  57. if game.Gid == "" {
  58. //新增
  59. gameNew := model.Game{
  60. Gid: form.Gid,
  61. Pid: form.Pid,
  62. GameName: form.GameName,
  63. WxAppid: form.WxAppid,
  64. WxSecret: form.WxSecret,
  65. TtAppid: form.TtAppid,
  66. TtSecret: form.TtSecret,
  67. TtTplId: form.TtTplId,
  68. WxTplId: form.WxTplId,
  69. CreatedAt: model.XTime{Time: now},
  70. UpdatedAt: model.XTime{Time: now},
  71. ConfigPath: form.ConfigPath,
  72. }
  73. err := global.App.DB.Table("game").Create(&gameNew).Error
  74. if err != nil {
  75. response.Fail(c, 1001, err.Error())
  76. return
  77. }
  78. } else {
  79. //更新
  80. err := global.App.DB.Table("game").Where("gid", form.Gid).Updates(&model.Game{
  81. Gid: form.Gid,
  82. Pid: form.Pid,
  83. GameName: form.GameName,
  84. WxAppid: form.WxAppid,
  85. WxSecret: form.WxSecret,
  86. TtAppid: form.TtAppid,
  87. TtSecret: form.TtSecret,
  88. TtTplId: form.TtTplId,
  89. WxTplId: form.WxTplId,
  90. ConfigPath: form.ConfigPath,
  91. CreatedAt: model.XTime{Time: now},
  92. UpdatedAt: model.XTime{Time: now},
  93. }).Error
  94. if err != nil {
  95. response.Fail(c, 1001, err.Error())
  96. return
  97. }
  98. }
  99. //更新redis
  100. gameConfigData := make(map[string]interface{})
  101. gameConfigData["gid"] = form.Gid
  102. gameConfigData["gameName"] = form.GameName
  103. gameConfigData["wxAppid"] = form.WxAppid
  104. gameConfigData["wxSecret"] = form.WxSecret
  105. gameConfigData["ttAppid"] = form.TtAppid
  106. gameConfigData["ttSecret"] = form.TtSecret
  107. gameConfigData["ttTplId"] = form.TtTplId
  108. gameConfigData["wxTplId"] = form.WxTplId
  109. gameConfigData["configPath"] = form.ConfigPath
  110. gidKey := config.Get("app.gid") + form.Gid
  111. err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
  112. if err != nil {
  113. response.Fail(c, 1003, "配置错误")
  114. return
  115. }
  116. response.Success(c, gin.H{
  117. "data": gameConfigData,
  118. })
  119. }
  120. /* 获取配置 */
  121. func GetGidConfig(c *gin.Context) {
  122. form := request.Check(c, &struct {
  123. Offset int `form:"offset" binding:"" json:"offset"`
  124. Limit int `form:"limit" binding:"" json:"limit"`
  125. Search string `form:"search" binding:"" json:"search"`
  126. }{})
  127. var gameList []model.Game
  128. var count int64
  129. query := global.App.DB.Table("game")
  130. if form.Search != "" {
  131. query = query.Where("gid", "like", "%"+form.Search+"%").
  132. Or("pid LIKE ?", "%"+form.Search+"%").
  133. Or("gameName LIKE ?", "%"+form.Search+"%")
  134. }
  135. err := query.Count(&count).Error
  136. if err != nil {
  137. response.Fail(c, 1001, err.Error())
  138. return
  139. }
  140. if form.Limit != 0 {
  141. query = query.Limit(form.Limit)
  142. }
  143. err = query.Offset(form.Offset).Scan(&gameList).Error
  144. if err != nil {
  145. response.Fail(c, 1002, err.Error())
  146. return
  147. }
  148. //gidKey := config.Get("app.gid") + "*"
  149. //keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
  150. //var gameData = []interface{}{}
  151. //for _, val := range keys {
  152. // res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
  153. // gameData = append(gameData, res)
  154. //}
  155. response.Success(c, gin.H{
  156. "data": gameList,
  157. "count": count,
  158. })
  159. }
  160. type ActionOption struct {
  161. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  162. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  163. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  164. OptionStatus int `json:"optionStatus" form:"optionStatus" binding:""`
  165. }
  166. // 设置游戏的打点
  167. func SetGameAction(c *gin.Context) {
  168. form := request.Check(c, &struct {
  169. Gid string `form:"gid" json:"gid" binding:"required"`
  170. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  171. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  172. Remark string `form:"remark" json:"remark" binding:""`
  173. Status int `form:"status" json:"status" binding:""`
  174. Options []ActionOption `form:"options" json:"options" binding:""`
  175. }{})
  176. //验证一下option 内的参数
  177. validate := validator.New()
  178. for _, v := range form.Options {
  179. err := validate.Struct(&v)
  180. if err != nil {
  181. response.Fail(c, 1003, err.Error())
  182. return
  183. }
  184. }
  185. //存入数据库
  186. now := time.Now()
  187. err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
  188. gameAction := model.GameAction{
  189. Gid: form.Gid,
  190. ActionId: form.ActionId,
  191. ActionName: form.ActionName,
  192. Remark: form.Remark,
  193. Status: form.Status,
  194. CreatedAt: model.XTime{Time: now},
  195. UpdatedAt: model.XTime{Time: now},
  196. }
  197. err := tx.Table("game_action").Save(&gameAction).Error
  198. if err != nil {
  199. return err
  200. }
  201. for _, option := range form.Options {
  202. gameActionOption := model.GameActionOption{
  203. OptionName: option.OptionName,
  204. OptionId: option.OptionId,
  205. ActionId: gameAction.ID,
  206. OptionType: option.OptionType,
  207. Status: option.OptionStatus,
  208. CreatedAt: model.XTime{Time: now},
  209. UpdatedAt: model.XTime{Time: now},
  210. }
  211. err = tx.Table("game_action_option").Save(&gameActionOption).Error
  212. if err != nil {
  213. return err
  214. }
  215. }
  216. return nil
  217. })
  218. if err != nil {
  219. response.Fail(c, 1002, err.Error())
  220. return
  221. }
  222. response.Success(c, gin.H{})
  223. }
  224. // 更新游戏中的事件
  225. func UpdateGameAction(c *gin.Context) {
  226. form := request.Check(c, &struct {
  227. Gid string `form:"gid" json:"gid" binding:"required"`
  228. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  229. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  230. Remark string `form:"remark" json:"remark" binding:""`
  231. Status int `form:"status" json:"status" binding:""`
  232. }{})
  233. err := global.App.DB.Table("game_action").
  234. Where("gid", form.Gid).
  235. Where("actionId", form.ActionId).
  236. Updates(map[string]interface{}{
  237. "status": form.Status,
  238. "remark": form.Remark,
  239. "updatedAt": time.Now(),
  240. "actionName": form.ActionName,
  241. }).Error
  242. if err != nil {
  243. response.Fail(c, 1001, err.Error())
  244. return
  245. }
  246. response.Success(c, gin.H{})
  247. }
  248. // 更新事件中的选项
  249. func UpdateGameActionOption(c *gin.Context) {
  250. form := request.Check(c, &struct {
  251. Id int `form:"id" json:"id" binding:"required"`
  252. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  253. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  254. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  255. Status int `form:"status" json:"status" binding:""`
  256. }{})
  257. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Updates(map[string]interface{}{
  258. "optionName": form.OptionName,
  259. "optionId": form.OptionId,
  260. "optionType": form.OptionType,
  261. "status": form.Status,
  262. "updatedAt": time.Now(),
  263. }).Error
  264. if err != nil {
  265. response.Fail(c, 1003, err.Error())
  266. return
  267. }
  268. response.Success(c, gin.H{})
  269. }
  270. // 新增事件中的选项
  271. func AddGameActionOption(c *gin.Context) {
  272. form := request.Check(c, &struct {
  273. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  274. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  275. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  276. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  277. Status int `json:"status" form:"status" binding:""`
  278. }{})
  279. now := time.Now()
  280. gameActionOption := model.GameActionOption{
  281. OptionName: form.OptionName,
  282. OptionId: form.OptionId,
  283. ActionId: form.ActionId,
  284. OptionType: form.OptionType,
  285. Status: form.Status,
  286. CreatedAt: model.XTime{Time: now},
  287. UpdatedAt: model.XTime{Time: now},
  288. }
  289. err := global.App.DB.Table("game_action_option").Save(&gameActionOption).Error
  290. if err != nil {
  291. response.Fail(c, 1001, err.Error())
  292. return
  293. }
  294. response.Success(c, gin.H{})
  295. }
  296. // 删除事件中的选项
  297. func DeleteGameActionOption(c *gin.Context) {
  298. form := request.Check(c, &struct {
  299. Id int `form:"id" json:"id" binding:"required"`
  300. }{})
  301. var d interface{}
  302. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Delete(d).Error
  303. if err != nil {
  304. response.Fail(c, 1001, err.Error())
  305. return
  306. }
  307. response.Success(c, gin.H{})
  308. }
  309. // 列表 事件列表
  310. func GameActionList(c *gin.Context) {
  311. form := request.Check(c, &struct {
  312. Offset int `form:"offset" json:"offset" binding:""`
  313. Limit int `form:"limit" json:"limit" binding:"required"`
  314. Search string `form:"search" json:"search" binding:""`
  315. Status string `form:"status" json:"status" binding:""`
  316. Order string `form:"order" json:"order" binding:""`
  317. Gid string `form:"gid" json:"gid" binding:"required"`
  318. }{})
  319. query := global.App.DB.Table("game_action").Where("gid", form.Gid)
  320. if form.Search != "" {
  321. query = query.WhereRaw(global.App.DB.Where("actionId", "like", "%"+form.Search+"%").Or("actionName like ?", "%"+form.Search+"%").SubQuery())
  322. }
  323. if form.Status != "" {
  324. status, _ := strconv.Atoi(form.Status)
  325. query = query.Where("status", status)
  326. }
  327. if form.Order != "" {
  328. query = query.Order("id " + form.Order)
  329. }
  330. var count int64
  331. err := query.Count(&count).Error
  332. if err != nil {
  333. response.Fail(c, 1001, err.Error())
  334. return
  335. }
  336. var actionList []model.GameAction
  337. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&actionList).Error
  338. if err != nil {
  339. response.Fail(c, 1002, err.Error())
  340. return
  341. }
  342. response.Success(c, gin.H{
  343. "data": actionList,
  344. "count": count,
  345. })
  346. }
  347. // 事件详情
  348. func GameActionDetail(c *gin.Context) {
  349. form := request.Check(c, &struct {
  350. Id int `form:"id" json:"id" binding:"required"`
  351. }{})
  352. var action model.GameAction
  353. err := global.App.DB.Table("game_action").Where("id", form.Id).First(&action).Error
  354. if err != nil {
  355. response.Fail(c, 1001, err.Error())
  356. return
  357. }
  358. //var optionList []model.GameActionOption
  359. //err = global.App.DB.Table("game_action_option").Where("actionId", action.ActionId).Scan(&optionList).Error
  360. response.Success(c, gin.H{
  361. "data": action,
  362. })
  363. }
  364. // 事件 选项 列表
  365. func GameActionOptionList(c *gin.Context) {
  366. form := request.Check(c, &struct {
  367. Offset int `form:"offset" json:"offset" binding:""`
  368. Limit int `form:"limit" json:"limit" binding:"required"`
  369. Search string `form:"search" json:"search" binding:""`
  370. Status string `form:"status" json:"status" binding:""`
  371. Order string `form:"order" json:"order" binding:""`
  372. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  373. }{})
  374. query := global.App.DB.Table("game_action_option").Where("actionId", form.ActionId)
  375. if form.Search != "" {
  376. query = query.WhereRaw(global.App.DB.Where("optionId", "like", "%"+form.Search+"%").Where("optionName", "like", "%"+form.Search+"%").SubQuery())
  377. }
  378. if form.Status != "" {
  379. status, _ := strconv.Atoi(form.Status)
  380. query = query.Where("status", status)
  381. }
  382. if form.Order != "" {
  383. query = query.Order("id " + form.Order)
  384. }
  385. var count int64
  386. err := query.Count(&count).Error
  387. if err != nil {
  388. response.Fail(c, 1001, err.Error())
  389. return
  390. }
  391. var optionList []model.GameActionOption
  392. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&optionList).Error
  393. if err != nil {
  394. response.Fail(c, 1002, err.Error())
  395. return
  396. }
  397. response.Success(c, gin.H{
  398. "data": optionList,
  399. "count": count,
  400. })
  401. }