gameConfig.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "github.com/go-playground/validator/v10"
  14. "strconv"
  15. "strings"
  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 := 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. func GetActiveGid() []string {
  204. var activeGidList []string
  205. key := "activeGidList"
  206. activeGid, _ := global.App.Redis.Get(context.Background(), key).Result()
  207. activeGidList = strings.Split(activeGid, ",")
  208. if len(activeGidList) <= 2 {
  209. //if true {
  210. var gidList []string
  211. //
  212. ////重新读取数据
  213. //var dir string
  214. //if config.Get("app.local") == "local" {
  215. // //url = "mongodb://localhost:27017"
  216. // dir = "storage"
  217. //} else {
  218. // dir = "/www/wwwroot/chunhao_receive/storage"
  219. //}
  220. //
  221. //now := time.Now()
  222. //
  223. //for i := 0; i <= 7; i++ {
  224. // date := now.AddDate(0, 0, -i).Format("2006-01-02")
  225. // //读取对应的文件夹
  226. // dirPath := filepath.Join(dir, date)
  227. // dateDir, _ := os.ReadDir(dirPath)
  228. // //fmt.Println(dirPath, dateDir)
  229. //
  230. // for _, v := range dateDir {
  231. //
  232. // fileName := v.Name()
  233. //
  234. // fileNameSplit := strings.Split(fileName, "_")
  235. //
  236. // if len(fileNameSplit) < 2 {
  237. // continue
  238. // }
  239. // last := fileNameSplit[len(fileNameSplit)-1]
  240. // gid := strings.TrimRight(fileName, "_"+last)
  241. //
  242. // if !utils.InArray(gid, gidList) {
  243. // gidList = append(gidList, gid)
  244. // }
  245. // }
  246. //}
  247. //
  248. global.App.DB.Table("user").
  249. Where("createdAt", ">=", time.Now().AddDate(0, 0, -3)).
  250. Distinct("gid").Pluck("gid", &gidList)
  251. if len(gidList) > 0 {
  252. var gidString string
  253. for _, gid := range gidList {
  254. gidString = gid + ","
  255. }
  256. global.App.Redis.Set(context.Background(), key, gidString, time.Second*3600)
  257. activeGidList = gidList
  258. }
  259. }
  260. return activeGidList
  261. }
  262. type ActionOption struct {
  263. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  264. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  265. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  266. OptionStatus int `json:"optionStatus" form:"optionStatus" binding:""`
  267. }
  268. // 设置游戏的打点
  269. func SetGameAction(c *gin.Context) {
  270. form := request.Check(c, &struct {
  271. Gid string `form:"gid" json:"gid" binding:"required"`
  272. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  273. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  274. Remark string `form:"remark" json:"remark" binding:""`
  275. Status int `form:"status" json:"status" binding:""`
  276. Options []ActionOption `form:"options" json:"options" binding:""`
  277. }{})
  278. //验证一下option 内的参数
  279. validate := validator.New()
  280. for _, v := range form.Options {
  281. err := validate.Struct(&v)
  282. if err != nil {
  283. response.Fail(c, 1003, err.Error())
  284. return
  285. }
  286. }
  287. //存入数据库
  288. now := time.Now()
  289. var gameAction model.GameAction
  290. err := global.App.DB.Transaction(func(tx *utils.WtDB) error {
  291. gameAction = model.GameAction{
  292. Gid: form.Gid,
  293. ActionId: form.ActionId,
  294. ActionName: form.ActionName,
  295. Remark: form.Remark,
  296. Status: form.Status,
  297. CreatedAt: model.XTime{Time: now},
  298. UpdatedAt: model.XTime{Time: now},
  299. }
  300. err := tx.Table("game_action").Save(&gameAction).Error
  301. if err != nil {
  302. return err
  303. }
  304. for _, option := range form.Options {
  305. gameActionOption := model.GameActionOption{
  306. OptionName: option.OptionName,
  307. OptionId: option.OptionId,
  308. ActionId: gameAction.ID,
  309. OptionType: option.OptionType,
  310. Status: option.OptionStatus,
  311. CreatedAt: model.XTime{Time: now},
  312. UpdatedAt: model.XTime{Time: now},
  313. }
  314. err = tx.Table("game_action_option").Save(&gameActionOption).Error
  315. if err != nil {
  316. return err
  317. }
  318. }
  319. return nil
  320. })
  321. if err != nil {
  322. response.Fail(c, 1002, err.Error())
  323. return
  324. }
  325. response.Success(c, gin.H{
  326. "data": map[string]interface{}{
  327. "id": gameAction.ID,
  328. },
  329. })
  330. }
  331. // 更新游戏中的事件
  332. func UpdateGameAction(c *gin.Context) {
  333. form := request.Check(c, &struct {
  334. Gid string `form:"gid" json:"gid" binding:"required"`
  335. ActionId string `form:"actionId" json:"actionId" binding:"required"`
  336. ActionName string `form:"actionName" json:"actionName" binding:"required"`
  337. Remark string `form:"remark" json:"remark" binding:""`
  338. Status int `form:"status" json:"status" binding:""`
  339. }{})
  340. err := global.App.DB.Table("game_action").
  341. Where("gid", form.Gid).
  342. Where("actionId", form.ActionId).
  343. Updates(map[string]interface{}{
  344. "status": form.Status,
  345. "remark": form.Remark,
  346. "updatedAt": time.Now(),
  347. "actionName": form.ActionName,
  348. }).Error
  349. if err != nil {
  350. response.Fail(c, 1001, err.Error())
  351. return
  352. }
  353. response.Success(c, gin.H{})
  354. }
  355. // 更新事件中的选项
  356. func UpdateGameActionOption(c *gin.Context) {
  357. form := request.Check(c, &struct {
  358. Id int `form:"id" json:"id" binding:"required"`
  359. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  360. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  361. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  362. Status int `form:"status" json:"status" binding:""`
  363. }{})
  364. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Updates(map[string]interface{}{
  365. "optionName": form.OptionName,
  366. "optionId": form.OptionId,
  367. "optionType": form.OptionType,
  368. "status": form.Status,
  369. "updatedAt": time.Now(),
  370. }).Error
  371. if err != nil {
  372. response.Fail(c, 1003, err.Error())
  373. return
  374. }
  375. response.Success(c, gin.H{})
  376. }
  377. // 新增事件中的选项
  378. func AddGameActionOption(c *gin.Context) {
  379. form := request.Check(c, &struct {
  380. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  381. OptionName string `json:"optionName" form:"optionName" binding:"required"`
  382. OptionId string `json:"optionId" form:"optionId" binding:"required"`
  383. OptionType string `json:"optionType" form:"optionType" binding:"required"`
  384. Status int `json:"status" form:"status" binding:""`
  385. }{})
  386. now := time.Now()
  387. gameActionOption := model.GameActionOption{
  388. OptionName: form.OptionName,
  389. OptionId: form.OptionId,
  390. ActionId: form.ActionId,
  391. OptionType: form.OptionType,
  392. Status: form.Status,
  393. CreatedAt: model.XTime{Time: now},
  394. UpdatedAt: model.XTime{Time: now},
  395. }
  396. err := global.App.DB.Table("game_action_option").Save(&gameActionOption).Error
  397. if err != nil {
  398. response.Fail(c, 1001, err.Error())
  399. return
  400. }
  401. response.Success(c, gin.H{})
  402. }
  403. // 删除事件中的选项
  404. func DeleteGameActionOption(c *gin.Context) {
  405. form := request.Check(c, &struct {
  406. Id int `form:"id" json:"id" binding:"required"`
  407. }{})
  408. var d interface{}
  409. err := global.App.DB.Table("game_action_option").Where("id", form.Id).Delete(d).Error
  410. if err != nil {
  411. response.Fail(c, 1001, err.Error())
  412. return
  413. }
  414. response.Success(c, gin.H{})
  415. }
  416. // 列表 事件列表
  417. func GameActionList(c *gin.Context) {
  418. form := request.Check(c, &struct {
  419. Offset int `form:"offset" json:"offset" binding:""`
  420. Limit int `form:"limit" json:"limit" binding:"required"`
  421. Search string `form:"search" json:"search" binding:""`
  422. Status string `form:"status" json:"status" binding:""`
  423. Order string `form:"order" json:"order" binding:""`
  424. Gid string `form:"gid" json:"gid" binding:"required"`
  425. }{})
  426. query := global.App.DB.Table("game_action").Where("gid", form.Gid)
  427. if form.Search != "" {
  428. query = query.WhereRaw(global.App.DB.Where("actionId", "like", "%"+form.Search+"%").Or("actionName like ?", "%"+form.Search+"%").SubQuery())
  429. }
  430. if form.Status != "" {
  431. status, _ := strconv.Atoi(form.Status)
  432. query = query.Where("status", status)
  433. }
  434. if form.Order != "" {
  435. query = query.Order("id " + form.Order)
  436. }
  437. var count int64
  438. err := query.Count(&count).Error
  439. if err != nil {
  440. response.Fail(c, 1001, err.Error())
  441. return
  442. }
  443. var actionList []model.GameAction
  444. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&actionList).Error
  445. if err != nil {
  446. response.Fail(c, 1002, err.Error())
  447. return
  448. }
  449. response.Success(c, gin.H{
  450. "data": actionList,
  451. "count": count,
  452. })
  453. }
  454. // 事件详情
  455. func GameActionDetail(c *gin.Context) {
  456. form := request.Check(c, &struct {
  457. Id int `form:"id" json:"id" binding:"required"`
  458. }{})
  459. var action model.GameAction
  460. err := global.App.DB.Table("game_action").Where("id", form.Id).First(&action).Error
  461. if err != nil {
  462. response.Fail(c, 1001, err.Error())
  463. return
  464. }
  465. //var optionList []model.GameActionOption
  466. //err = global.App.DB.Table("game_action_option").Where("actionId", action.ActionId).Scan(&optionList).Error
  467. response.Success(c, gin.H{
  468. "data": action,
  469. })
  470. }
  471. // 事件 选项 列表
  472. func GameActionOptionList(c *gin.Context) {
  473. form := request.Check(c, &struct {
  474. Offset int `form:"offset" json:"offset" binding:""`
  475. Limit int `form:"limit" json:"limit" binding:"required"`
  476. Search string `form:"search" json:"search" binding:""`
  477. Status string `form:"status" json:"status" binding:""`
  478. Order string `form:"order" json:"order" binding:""`
  479. ActionId int `form:"actionId" json:"actionId" binding:"required"`
  480. }{})
  481. query := global.App.DB.Table("game_action_option").Where("actionId", form.ActionId)
  482. if form.Search != "" {
  483. query = query.WhereRaw(global.App.DB.Where("optionId", "like", "%"+form.Search+"%").Where("optionName", "like", "%"+form.Search+"%").SubQuery())
  484. }
  485. if form.Status != "" {
  486. status, _ := strconv.Atoi(form.Status)
  487. query = query.Where("status", status)
  488. }
  489. if form.Order != "" {
  490. query = query.Order("id " + form.Order)
  491. }
  492. var count int64
  493. err := query.Count(&count).Error
  494. if err != nil {
  495. response.Fail(c, 1001, err.Error())
  496. return
  497. }
  498. var optionList []model.GameActionOption
  499. err = query.Offset(form.Offset).Limit(form.Limit).Scan(&optionList).Error
  500. if err != nil {
  501. response.Fail(c, 1002, err.Error())
  502. return
  503. }
  504. response.Success(c, gin.H{
  505. "data": optionList,
  506. "count": count,
  507. })
  508. }