gameConfig.go 15 KB

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