gameConfig.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. }
  74. err := global.App.DB.Table("game").Create(&gameNew).Error
  75. if err != nil {
  76. response.Fail(c, 1001, err.Error())
  77. return
  78. }
  79. } else {
  80. //更新
  81. err := global.App.DB.Table("game").Where("gid", form.Gid).Updates(&model.Game{
  82. Gid: form.Gid,
  83. Pid: form.Pid,
  84. GameName: form.GameName,
  85. WxAppid: form.WxAppid,
  86. WxSecret: form.WxSecret,
  87. TtAppid: form.TtAppid,
  88. TtSecret: form.TtSecret,
  89. TtTplId: form.TtTplId,
  90. WxTplId: form.WxTplId,
  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. gidKey := config.Get("app.gid") + form.Gid
  110. err := global.App.Redis.HMSet(context.Background(), gidKey, gameConfigData).Err()
  111. if err != nil {
  112. response.Fail(c, 1003, "配置错误")
  113. return
  114. }
  115. response.Success(c, gin.H{
  116. "data": gameConfigData,
  117. })
  118. }
  119. // 新增游戏gid
  120. func AddPid(c *gin.Context) {
  121. form := request.Check(c, &struct {
  122. Pid string `form:"pid" json:"pid" binding:"required"`
  123. PidName string `form:"pidName" json:"pidName" binding:"required"`
  124. }{})
  125. //查重
  126. var gid model.Game
  127. global.App.DB.Table("game_pid").
  128. Where("pid", form.Pid).
  129. Or("pidName = ? ", form.PidName).
  130. First(&gid)
  131. if gid.Gid != "" {
  132. response.Fail(c, 1001, "gid 已存在")
  133. return
  134. }
  135. err := global.App.DB.Table("game_pid").Create(&model.GamePid{
  136. Pid: form.Pid,
  137. PidName: form.PidName,
  138. }).Error
  139. if err != nil {
  140. response.Fail(c, 1001, "创建pid失败"+err.Error())
  141. return
  142. }
  143. response.Success(c, gin.H{})
  144. }
  145. func DelPid(c *gin.Context) {
  146. form := request.Check(c, &struct {
  147. Pid string `form:"pid" json:"pid" binding:"required"`
  148. }{})
  149. //有没有在使用
  150. var count int64
  151. global.App.DB.Table("game").Where("pid", form.Pid).Count(&count)
  152. if count > 0 {
  153. response.Fail(c, 1001, "gid已经使用,无法删除")
  154. return
  155. }
  156. err := global.App.DB.Table("game_pid").Where("pid", form.Pid).Delete(&model.GamePid{}).Error
  157. if err != nil {
  158. response.Fail(c, 1001, "删除失败"+err.Error())
  159. return
  160. }
  161. response.Success(c, gin.H{})
  162. }
  163. func PidList(c *gin.Context) {
  164. form := request.Check(c, &struct {
  165. Search string `form:"search" json:"search" binding:""`
  166. Offset int `form:"offset" json:"offset" binding:""`
  167. Limit int `form:"limit" json:"limit" binding:""`
  168. }{})
  169. query := global.App.DB.Table("game_pid")
  170. if form.Search != "" {
  171. query = query.Where("pid", "like", "%"+form.Search+"%").
  172. Or("pidName LIKE ?", "%"+form.Search+"%")
  173. }
  174. var count int64
  175. var res []model.GamePid
  176. err := query.Count(&count).Error
  177. if err != nil {
  178. response.Fail(c, 1001, "查询总数失败"+err.Error())
  179. return
  180. }
  181. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&res).Error
  182. if err != nil {
  183. response.Fail(c, 1002, err.Error())
  184. return
  185. }
  186. response.Success(c, gin.H{
  187. "data": res,
  188. "count": count,
  189. })
  190. }
  191. func PidToGidList(c *gin.Context) {
  192. form := request.Check(c, &struct {
  193. Search string `form:"search" binding:""`
  194. Active bool `form:"active" binding:"" `
  195. }{})
  196. type game struct {
  197. Id int `json:"id" gorm:"primary_key"`
  198. Pid string `json:"pid" gorm:"column:pid; "`
  199. Gid string `json:"gid" gorm:"column:gid; "`
  200. GameName string `json:"gameName" gorm:"column:gameName; "`
  201. PidName string `json:"pidName" gorm:"column:pidName; "`
  202. }
  203. var gameList []game
  204. query := global.App.DB.Table("game")
  205. if form.Search != "" {
  206. query = query.Where("gid", "like", "%"+form.Search+"%").
  207. Or("pid LIKE ?", "%"+form.Search+"%").
  208. Or("gameName LIKE ?", "%"+form.Search+"%")
  209. }
  210. //验证gid权限
  211. permission := c.GetString("permission")
  212. var PermissionSlice []string
  213. json.Unmarshal([]byte(permission), &PermissionSlice)
  214. if !utils.InArray("all", PermissionSlice) {
  215. fmt.Println(PermissionSlice)
  216. //需要验证gid权限
  217. query = query.WhereIn("gid", PermissionSlice)
  218. }
  219. if form.Active == true {
  220. //获取近七天有数据的gid
  221. ActiveGid := service.GetActiveGid()
  222. query = query.WhereIn("gid", ActiveGid)
  223. }
  224. err := query.
  225. LeftJoin("game_pid", "game_pid.pid = game.pid").
  226. Select("game.id", "game.gid", "game.gameName", "game.pid", "pidName").Where("id", ">", 0).Scan(&gameList).Error
  227. if err != nil {
  228. response.Fail(c, 1002, err.Error())
  229. return
  230. }
  231. type res struct {
  232. PidName string `json:"pidName" gorm:"column:pidName; "`
  233. Pid string `json:"pid" gorm:"column:pid; "`
  234. GidList []game `json:"gidList" gorm:"-"`
  235. }
  236. var Res []res
  237. var pidList []string
  238. for _, game := range gameList {
  239. if !utils.InArray(game.Pid, pidList) {
  240. pidList = append(pidList, game.Pid)
  241. Res = append(Res, res{
  242. PidName: game.PidName,
  243. Pid: game.Pid,
  244. })
  245. }
  246. }
  247. for _, game1 := range gameList {
  248. for k, value := range Res {
  249. if value.Pid == game1.Pid {
  250. Res[k].GidList = append(Res[k].GidList, game1)
  251. }
  252. }
  253. }
  254. response.Success(c, gin.H{
  255. "data": Res,
  256. })
  257. }
  258. /* 获取配置 */
  259. func GetGidConfig(c *gin.Context) {
  260. form := request.Check(c, &struct {
  261. Offset int `form:"offset" binding:"" json:"offset"`
  262. Limit int `form:"limit" binding:"" json:"limit"`
  263. Search string `form:"search" binding:"" json:"search"`
  264. }{})
  265. var gameList []model.Game
  266. var count int64
  267. query := global.App.DB.Table("game")
  268. if form.Search != "" {
  269. query = query.Where("gid", "like", "%"+form.Search+"%").
  270. Or("pid LIKE ?", "%"+form.Search+"%").
  271. Or("gameName LIKE ?", "%"+form.Search+"%")
  272. }
  273. err := query.Count(&count).Error
  274. if err != nil {
  275. response.Fail(c, 1001, err.Error())
  276. return
  277. }
  278. if form.Limit != 0 {
  279. query = query.Limit(form.Limit)
  280. }
  281. err = query.Offset(form.Offset).Scan(&gameList).Error
  282. if err != nil {
  283. response.Fail(c, 1002, err.Error())
  284. return
  285. }
  286. //gidKey := config.Get("app.gid") + "*"
  287. //keys, _ := global.App.Redis.Keys(context.Background(), gidKey).Result()
  288. //var gameData = []interface{}{}
  289. //for _, val := range keys {
  290. // res, _ := global.App.Redis.HGetAll(context.Background(), val).Result()
  291. // gameData = append(gameData, res)
  292. //}
  293. response.Success(c, gin.H{
  294. "data": gameList,
  295. "count": count,
  296. })
  297. }
  298. func GidList(c *gin.Context) {
  299. form := request.Check(c, &struct {
  300. Search string `form:"search" binding:""`
  301. Active bool `form:"active" binding:"" `
  302. }{})
  303. var gameList []struct {
  304. Id int `json:"id" gorm:"primary_key"`
  305. Pid string `json:"pid" gorm:"column:pid; "`
  306. Gid string `json:"gid" gorm:"column:gid; "`
  307. GameName string `json:"gameName" gorm:"column:gameName; "`
  308. }
  309. query := global.App.DB.Table("game")
  310. if form.Search != "" {
  311. query = query.Where("gid", "like", "%"+form.Search+"%").
  312. Or("pid LIKE ?", "%"+form.Search+"%").
  313. Or("gameName LIKE ?", "%"+form.Search+"%")
  314. }
  315. //验证gid权限
  316. permission := c.GetString("permission")
  317. var PermissionSlice []string
  318. json.Unmarshal([]byte(permission), &PermissionSlice)
  319. if !utils.InArray("all", PermissionSlice) {
  320. fmt.Println(PermissionSlice)
  321. //需要验证gid权限
  322. query = query.WhereIn("gid", PermissionSlice)
  323. }
  324. if form.Active == true {
  325. //获取近七天有数据的gid
  326. ActiveGid := service.GetActiveGid()
  327. query = query.WhereIn("gid", ActiveGid)
  328. }
  329. err := query.Select("id", "gid", "gameName").Where("id", ">", 0).Scan(&gameList).Error
  330. if err != nil {
  331. response.Fail(c, 1002, err.Error())
  332. return
  333. }
  334. response.Success(c, gin.H{
  335. "data": gameList,
  336. "count": len(gameList),
  337. })
  338. }
  339. type ActionOption struct {
  340. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  341. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  342. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  343. OptionStatus int `json:"optionStatus" form:"optionStatus" binding:""`
  344. }
  345. // 设置游戏的打点
  346. func SetGameAction(c *gin.Context) {
  347. form := request.Check(c, &struct {
  348. Gid string `form:"gid" json:"gid" binding:"required"`
  349. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  350. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  351. Remark string `form:"remark" json:"remark" binding:""`
  352. Status int `form:"status" json:"status" binding:""`
  353. Options []ActionOption `form:"options" json:"options" binding:""`
  354. }{})
  355. //验证一下option 内的参数
  356. validate := validator.New()
  357. for _, v := range form.Options {
  358. err := validate.Struct(&v)
  359. if err != nil {
  360. response.Fail(c, 1003, err.Error())
  361. return
  362. }
  363. }
  364. //存入数据库
  365. now := time.Now()
  366. var gameAction model.GameAction
  367. err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
  368. gameAction = model.GameAction{
  369. Gid: form.Gid,
  370. ActionId: form.ActionId,
  371. ActionName: form.ActionName,
  372. Remark: form.Remark,
  373. Status: form.Status,
  374. CreatedAt: model.XTime{Time: now},
  375. UpdatedAt: model.XTime{Time: now},
  376. }
  377. err := tx.Table("game_action").Save(&gameAction).Error
  378. if err != nil {
  379. return err
  380. }
  381. for _, option := range form.Options {
  382. gameActionOption := model.GameActionOption{
  383. OptionName: option.OptionName,
  384. OptionId: option.OptionId,
  385. ActionId: gameAction.ID,
  386. OptionType: option.OptionType,
  387. Status: 1,
  388. CreatedAt: model.XTime{Time: now},
  389. UpdatedAt: model.XTime{Time: now},
  390. }
  391. err = tx.Table("game_action_option").Save(&gameActionOption).Error
  392. if err != nil {
  393. return err
  394. }
  395. }
  396. return nil
  397. })
  398. if err != nil {
  399. response.Fail(c, 1002, err.Error())
  400. return
  401. }
  402. response.Success(c, gin.H{
  403. "data": map[string]interface{}{
  404. "id": gameAction.ID,
  405. },
  406. })
  407. }
  408. // 更新游戏中的事件
  409. func UpdateGameAction(c *gin.Context) {
  410. form := request.Check(c, &struct {
  411. Gid string `form:"gid" json:"gid" binding:"required"`
  412. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  413. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  414. Remark string `form:"remark" json:"remark" binding:""`
  415. Status int `form:"status" json:"status" binding:""`
  416. }{})
  417. err := global.App.DB.Table("game_action").
  418. Where("gid", form.Gid).
  419. Where("actionId", form.ActionId).
  420. Updates(map[string]interface{}{
  421. "status": form.Status,
  422. "remark": form.Remark,
  423. "updatedAt": time.Now(),
  424. "actionName": form.ActionName,
  425. }).Error
  426. if err != nil {
  427. response.Fail(c, 1001, err.Error())
  428. return
  429. }
  430. response.Success(c, gin.H{})
  431. }
  432. // 更新事件中的选项
  433. func UpdateGameActionOption(c *gin.Context) {
  434. form := request.Check(c, &struct {
  435. Id int `form:"id" json:"id" binding:"required"`
  436. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  437. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  438. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  439. Status int `form:"status" json:"status" binding:""`
  440. }{})
  441. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Updates(map[string]interface{}{
  442. "optionName": form.OptionName,
  443. "optionId": form.OptionId,
  444. "optionType": form.OptionType,
  445. "status": form.Status,
  446. "updatedAt": time.Now(),
  447. }).Error
  448. if err != nil {
  449. response.Fail(c, 1003, err.Error())
  450. return
  451. }
  452. response.Success(c, gin.H{})
  453. }
  454. // 新增事件中的选项
  455. func AddGameActionOption(c *gin.Context) {
  456. form := request.Check(c, &struct {
  457. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  458. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  459. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  460. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  461. Status int `json:"status" form:"status" binding:""`
  462. }{})
  463. now := time.Now()
  464. gameActionOption := model.GameActionOption{
  465. OptionName: form.OptionName,
  466. OptionId: form.OptionId,
  467. ActionId: form.ActionId,
  468. OptionType: form.OptionType,
  469. Status: form.Status,
  470. CreatedAt: model.XTime{Time: now},
  471. UpdatedAt: model.XTime{Time: now},
  472. }
  473. err := global.App.DB.Table("game_action_option").Save(&gameActionOption).Error
  474. if err != nil {
  475. response.Fail(c, 1001, err.Error())
  476. return
  477. }
  478. response.Success(c, gin.H{})
  479. }
  480. // 删除事件中的选项
  481. func DeleteGameActionOption(c *gin.Context) {
  482. form := request.Check(c, &struct {
  483. Id int `form:"id" json:"id" binding:"required"`
  484. }{})
  485. var d interface{}
  486. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Delete(d).Error
  487. if err != nil {
  488. response.Fail(c, 1001, err.Error())
  489. return
  490. }
  491. response.Success(c, gin.H{})
  492. }
  493. // 列表 事件列表
  494. func GameActionList(c *gin.Context) {
  495. form := request.Check(c, &struct {
  496. Offset int `form:"offset" json:"offset" binding:""`
  497. Limit int `form:"limit" json:"limit" binding:"required"`
  498. Search string `form:"search" json:"search" binding:""`
  499. Status string `form:"status" json:"status" binding:""`
  500. Order string `form:"order" json:"order" binding:""`
  501. Gid string `form:"gid" json:"gid" binding:"required"`
  502. }{})
  503. query := global.App.DB.Table("game_action").Where("gid", form.Gid)
  504. if form.Search != "" {
  505. query = query.WhereRaw(global.App.DB.Where("actionId", "like", "%"+form.Search+"%").Or("actionName like ?", "%"+form.Search+"%").SubQuery())
  506. }
  507. if form.Status != "" {
  508. status, _ := strconv.Atoi(form.Status)
  509. query = query.Where("status", status)
  510. }
  511. if form.Order != "" {
  512. query = query.Order("id " + form.Order)
  513. }
  514. var count int64
  515. err := query.Count(&count).Error
  516. if err != nil {
  517. response.Fail(c, 1001, err.Error())
  518. return
  519. }
  520. var actionList []model.GameAction
  521. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&actionList).Error
  522. if err != nil {
  523. response.Fail(c, 1002, err.Error())
  524. return
  525. }
  526. response.Success(c, gin.H{
  527. "data": actionList,
  528. "count": count,
  529. })
  530. }
  531. // 事件详情
  532. func GameActionDetail(c *gin.Context) {
  533. form := request.Check(c, &struct {
  534. Id int `form:"id" json:"id" binding:"required"`
  535. }{})
  536. var action model.GameAction
  537. err := global.App.DB.Table("game_action").Where("id", form.Id).First(&action).Error
  538. if err != nil {
  539. response.Fail(c, 1001, err.Error())
  540. return
  541. }
  542. //var optionList []model.GameActionOption
  543. //err = global.App.DB.Table("game_action_option").Where("actionId", action.ActionId).Scan(&optionList).Error
  544. response.Success(c, gin.H{
  545. "data": action,
  546. })
  547. }
  548. // 事件 选项 列表
  549. func GameActionOptionList(c *gin.Context) {
  550. form := request.Check(c, &struct {
  551. Offset int `form:"offset" json:"offset" binding:""`
  552. Limit int `form:"limit" json:"limit" binding:"required"`
  553. Search string `form:"search" json:"search" binding:""`
  554. Status string `form:"status" json:"status" binding:""`
  555. Order string `form:"order" json:"order" binding:""`
  556. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  557. }{})
  558. query := global.App.DB.Table("game_action_option").Where("actionId", form.ActionId)
  559. if form.Search != "" {
  560. query = query.WhereRaw(global.App.DB.Where("optionId", "like", "%"+form.Search+"%").Where("optionName", "like", "%"+form.Search+"%").SubQuery())
  561. }
  562. if form.Status != "" {
  563. status, _ := strconv.Atoi(form.Status)
  564. query = query.Where("status", status)
  565. }
  566. if form.Order != "" {
  567. query = query.Order("id " + form.Order)
  568. }
  569. var count int64
  570. err := query.Count(&count).Error
  571. if err != nil {
  572. response.Fail(c, 1001, err.Error())
  573. return
  574. }
  575. var optionList []model.GameActionOption
  576. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&optionList).Error
  577. if err != nil {
  578. response.Fail(c, 1002, err.Error())
  579. return
  580. }
  581. response.Success(c, gin.H{
  582. "data": optionList,
  583. "count": count,
  584. })
  585. }