gameConfig.go 14 KB

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