蛋蛋星球-客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hdl_home_page.go 15 KiB

1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
2週間前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
2週間前
2週間前
2週間前
2週間前
1ヶ月前
2週間前
2週間前
1ヶ月前
2週間前
1ヶ月前
1ヶ月前
3週間前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
1ヶ月前
4週間前
1ヶ月前
4週間前
1ヶ月前
4週間前
1ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  12. "encoding/json"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/shopspring/decimal"
  16. "time"
  17. )
  18. // HomePage
  19. // @Summary 蛋蛋星球-主页-基础信息(获取)
  20. // @Tags 主页
  21. // @Description 基础信息(获取)
  22. // @Accept json
  23. // @Produce json
  24. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  25. // @Success 200 {object} md.HomePageResp "具体数据"
  26. // @Failure 400 {object} md.Response "具体错误"
  27. // @Router /api/v1/homePage/index [get]
  28. func HomePage(c *gin.Context) {
  29. now := time.Now()
  30. val, exists := c.Get("user")
  31. if !exists {
  32. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  33. return
  34. }
  35. user, ok := val.(*model.User)
  36. if !ok {
  37. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  38. return
  39. }
  40. //1、查找 `egg_energy_basic_setting` 基础设置
  41. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  42. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  43. "key": "is_open",
  44. "value": 1,
  45. })
  46. if err != nil {
  47. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  48. return
  49. }
  50. //2、计算今日涨幅
  51. err, rises, isRise, nowPrice, initialPrice := svc.CalcTodayEggEnergyPriceRises(db.Db, now)
  52. if err != nil {
  53. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  54. return
  55. }
  56. //3、获取当前用户 蛋蛋能量(可用+结算+今日签到预估蛋蛋能量)
  57. var isSign bool
  58. var signCountdown string
  59. var signPersonalEggEnergy = "0.00"
  60. var signTeamEggEnergy = "0.00"
  61. var totalEggEnergy float64
  62. signInDb := implement.NewEggSignInDb(db.Db)
  63. nowStr := now.Format("2006-01-02 15:04:05")
  64. has, signIn, err := signInDb.EggSignINGetOneByTimeAndUid("", nowStr, user.Id, 0)
  65. if err != nil {
  66. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  67. return
  68. }
  69. if has && utils.TimeParseStd(signIn.EndTime).After(now) {
  70. //3.2.1 获取预估每秒获得蛋蛋能量数
  71. estimatePerSecondPersonEggEnergyValue, err1 := decimal.NewFromString(signIn.EstimatePerSecondPersonEggEnergyValue)
  72. if err1 != nil {
  73. e.OutErr(c, e.ERR, err1.Error())
  74. return
  75. }
  76. estimatePerSecondTeamEggEnergyValue, err1 := decimal.NewFromString(signIn.EstimatePerSecondTeamEggEnergyValue)
  77. if err1 != nil {
  78. e.OutErr(c, e.ERR, err1.Error())
  79. return
  80. }
  81. signPersonalEggEnergy = estimatePerSecondPersonEggEnergyValue.String()
  82. signTeamEggEnergy = estimatePerSecondTeamEggEnergyValue.String()
  83. isSign = true
  84. signCountdown = signIn.EndTime
  85. }
  86. // 3.3 查询个人及团队蛋蛋能量
  87. amountDb := implement.NewUserVirtualAmountDb(db.Db)
  88. personEggEnergy, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.PersonEggEnergyCoinId)
  89. if err != nil {
  90. e.OutErr(c, e.ERR_DB_ORM, nil)
  91. return
  92. }
  93. teamEggEnergy, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.TeamEggEnergyCoinId)
  94. if err != nil {
  95. e.OutErr(c, e.ERR_DB_ORM, nil)
  96. return
  97. }
  98. signEggEnergyValue, _ := decimal.NewFromString(signPersonalEggEnergy)
  99. personEggEnergyValue, _ := decimal.NewFromString(personEggEnergy.Amount)
  100. teamEggEnergyValue, _ := decimal.NewFromString(teamEggEnergy.Amount)
  101. totalEggEnergy, _ = signEggEnergyValue.Add(personEggEnergyValue).Add(teamEggEnergyValue).Float64()
  102. //4、活跃积分(个人+团队)
  103. var totalActivePoints float64
  104. personActivePoints, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.PersonEggPointsCoinId)
  105. if err != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. teamActivePoints, err := amountDb.GetUserVirtualWalletBySession(user.Id, eggEnergyBasicSetting.TeamEggPointsCoinId)
  110. if err != nil {
  111. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  112. return
  113. }
  114. totalActivePoints = utils.StrToFloat64(personActivePoints.Amount) + utils.StrToFloat64(teamActivePoints.Amount)
  115. //5、计算当前基础速率、团队速率
  116. nowBasalRate := utils.StrToFloat64(signPersonalEggEnergy) * 60 * 60 //每小时基础速率
  117. NowTeamRate := utils.StrToFloat64(signTeamEggEnergy) * 60 * 60 // 每小时团队速率
  118. userDb := implement.NewUserDb(db.Db)
  119. userCount, err := userDb.UserCount()
  120. if err != nil {
  121. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  122. return
  123. }
  124. var residueTimes = utils.TimeParseStd(signCountdown).Unix() - time.Now().Unix()
  125. utils.FilePutContents("HomePage", utils.SerializeStr(map[string]interface{}{
  126. "user_phone": user.Phone,
  127. "user_uid": user.Id,
  128. "data": map[string]interface{}{
  129. "is_sign": isSign,
  130. "sign_end_date": signCountdown,
  131. "sign_end_timestamp": utils.TimeParseStd(signCountdown).Unix(),
  132. "residue_times": residueTimes,
  133. "total_egg_energy": totalEggEnergy,
  134. "total_active_points": totalActivePoints,
  135. "egg_energy_now_price": nowPrice,
  136. "initial_price": initialPrice,
  137. "is_rises": isRise,
  138. "rises": rises,
  139. "now_basal_rate": nowBasalRate,
  140. "now_team_rate": NowTeamRate,
  141. "user_count": userCount,
  142. "nickname": user.Nickname,
  143. },
  144. }))
  145. resp := md.HomePageResp{
  146. LeaveTimer: utils.Int64ToStr(residueTimes),
  147. IsSign: isSign,
  148. SignEndTime: signCountdown,
  149. TotalEggEnergy: utils.Float64ToStr(totalEggEnergy),
  150. TotalActivePoints: utils.Float64ToStr(totalActivePoints),
  151. InitialPrice: initialPrice,
  152. NowBasalRate: utils.Float64ToStr(nowBasalRate),
  153. NowTeamRate: utils.Float64ToStr(NowTeamRate),
  154. NickName: user.Nickname,
  155. }
  156. e.OutSuc(c, resp, nil)
  157. return
  158. }
  159. // HomePageWatchAdRule
  160. // @Summary 蛋蛋星球-主页-视频奖励规则(获取)
  161. // @Tags 主页
  162. // @Description 视频奖励规则(获取)
  163. // @Accept json
  164. // @Produce json
  165. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  166. // @Success 200 {object} md.HomePageWatchAdRuleResp "具体数据"
  167. // @Failure 400 {object} md.Response "具体错误"
  168. // @Router /api/v1/homePage/adRule [get]
  169. func HomePageWatchAdRule(c *gin.Context) {
  170. val, exists := c.Get("user")
  171. if !exists {
  172. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  173. return
  174. }
  175. user, ok := val.(*model.User)
  176. if !ok {
  177. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  178. return
  179. }
  180. // 1. 查找基础设置
  181. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  182. basicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOne()
  183. if err != nil {
  184. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  185. return
  186. }
  187. // 2. 计算当前还能看几个广告
  188. recordsDb := implement.NewEggEnergyUserWatchRecordsDb(db.Db)
  189. records, err := recordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  190. "key": "uid",
  191. "value": user.Id,
  192. })
  193. if err != nil {
  194. return
  195. }
  196. var videoRewardSystem md2.VideoRewardSystemStruct
  197. utils.Unserialize([]byte(basicSetting.VideoRewardSystem), &videoRewardSystem)
  198. redisKey := fmt.Sprintf(md2.UserNextWatchAdDate, user.Id)
  199. var residueWatchAdNum int
  200. if records == nil {
  201. residueWatchAdNum = utils.StrToInt(videoRewardSystem.RewardTotalNum)
  202. } else {
  203. now := time.Now()
  204. residueWatchAdNum = records.ResidueWatchAdNum
  205. if records.NextWatchAdDate.Before(now) {
  206. //TODO::下一轮时间已在当前时间之前
  207. residueWatchAdNum = utils.StrToInt(videoRewardSystem.RewardTotalNum)
  208. records.ResidueWatchAdNum = residueWatchAdNum
  209. _, err = recordsDb.EggEnergyUserWatchRecordsUpdate(records.Id, records, "residue_watch_ad_num")
  210. if err != nil {
  211. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  212. return
  213. }
  214. cache.SetEx(redisKey, "", 60*60*24)
  215. }
  216. }
  217. nextWatchAdDate, _ := cache.GetString(redisKey)
  218. nextWatchAdTimes := utils.TimeParseStd(nextWatchAdDate).Unix() - time.Now().Unix()
  219. if nextWatchAdTimes < 0 {
  220. nextWatchAdTimes = 0
  221. }
  222. var roundRemainingSecs int64
  223. now := time.Now()
  224. //开始时间 以当前时间 往前推多少
  225. signInDb := implement.NewEggSignInDb(db.Db)
  226. nowStr := now.Format("2006-01-02 15:04:05")
  227. has, signIn, err := signInDb.EggSignINGetOneByTimeAndUid("", nowStr, user.Id, 0)
  228. if err != nil {
  229. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  230. return
  231. }
  232. if has {
  233. roundRemainingSecs = utils.TimeParseStd(signIn.EndTime).Unix() - time.Now().Unix()
  234. if roundRemainingSecs < 0 {
  235. roundRemainingSecs = 0
  236. }
  237. }
  238. utils.FilePutContents("nextWatchAdDate", utils.SerializeStr(map[string]interface{}{
  239. "nextWatchAdDate": nextWatchAdDate,
  240. "next_watch_ad_times": nextWatchAdTimes,
  241. "uid": utils.Int64ToStr(user.Id),
  242. "time": time.Now().Format("2006-01-02 15:04:05.000"),
  243. }))
  244. resp := md.HomePageWatchAdRuleResp{
  245. RewardTotalNum: videoRewardSystem.RewardTotalNum,
  246. ResidueWatchAdNum: residueWatchAdNum,
  247. WatchAdRewardActivePoints: videoRewardSystem.RewardValue,
  248. NextWatchAdDate: nextWatchAdDate,
  249. NextWatchAdSecs: nextWatchAdTimes,
  250. EachRoundHour: videoRewardSystem.EachRoundHour,
  251. RoundRemainingSecs: roundRemainingSecs,
  252. }
  253. e.OutSuc(c, resp, nil)
  254. }
  255. const HomePageRealTimeRedisKey = "Home_Page_Real_Time_Cache_Key"
  256. // RealTimePrice
  257. // @Summary 蛋蛋星球-主页-实时数据(获取)
  258. // @Tags 主页
  259. // @Description 实时数据(获取)
  260. // @Accept json
  261. // @Produce json
  262. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  263. // @Success 200 {object} md.RealTimePriceResp "具体数据"
  264. // @Failure 400 {object} md.Response "具体错误"
  265. // @Router /api/v1/homePage/realTimePrice [get]
  266. func RealTimePrice(c *gin.Context) {
  267. var isRises, userCount, risesValue, nowPrice interface{}
  268. var rises float64
  269. redisKey := HomePageRealTimeRedisKey
  270. redisValue, err := cache.GetString(redisKey)
  271. if err != nil {
  272. if err.Error() == "redigo: nil returned" {
  273. now := time.Now()
  274. err, rises, isRises, nowPrice, _ = svc.CalcTodayEggEnergyPriceRises(db.Db, now)
  275. if err != nil {
  276. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  277. return
  278. }
  279. userDb := implement.NewUserDb(db.Db)
  280. userCount, err = userDb.UserCount()
  281. if err != nil {
  282. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  283. return
  284. }
  285. decimalRate := decimal.NewFromInt(100) //百分比
  286. risesValue = decimal.NewFromFloat(rises).Mul(decimalRate).StringFixed(16)
  287. // 缓存 5 s
  288. cache.SetEx(redisKey, utils.SerializeStr(map[string]interface{}{
  289. "user_count": userCount,
  290. "is_rises": isRises,
  291. "rises": risesValue,
  292. "egg_energy_now_price": nowPrice,
  293. }), 5)
  294. } else {
  295. e.OutErr(c, e.ERR, err.Error())
  296. return
  297. }
  298. } else {
  299. var result map[string]interface{}
  300. err1 := json.Unmarshal([]byte(redisValue), &result)
  301. if err1 != nil {
  302. e.OutErr(c, e.ERR, err1.Error())
  303. return
  304. }
  305. userCount = result["user_count"]
  306. isRises = result["is_rises"]
  307. risesValue = result["rises"]
  308. nowPrice = result["egg_energy_now_price"]
  309. }
  310. isRisesResp, ok := isRises.(bool)
  311. if !ok {
  312. e.OutErr(c, e.ERR, "断言失败")
  313. }
  314. resp := md.RealTimePriceResp{
  315. UserCount: utils.AnyToInt64(userCount),
  316. NowPrice: utils.AnyToString(nowPrice),
  317. IsRises: isRisesResp,
  318. Rises: utils.AnyToString(risesValue),
  319. }
  320. e.OutSuc(c, resp, nil)
  321. }
  322. // IsCanSignIn
  323. // @Summary 蛋蛋星球-主页-是否能签到(获取)
  324. // @Tags 主页
  325. // @Description 是否可以签到(获取)
  326. // @Accept json
  327. // @Produce json
  328. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  329. // @Success 200 {object} md.IsCanSignInResp "具体数据"
  330. // @Failure 400 {object} md.Response "具体错误"
  331. // @Router /api/v1/homePage/isCanSignIn [get]
  332. func IsCanSignIn(c *gin.Context) {
  333. val, exists := c.Get("user")
  334. if !exists {
  335. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  336. return
  337. }
  338. user, ok := val.(*model.User)
  339. if !ok {
  340. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  341. return
  342. }
  343. //1、查找 `OneCirclesPublicPlatoonBasicSetting` 基础设置
  344. energyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
  345. eggEnergyBasicSetting, err := energyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  346. "key": "is_open",
  347. "value": 1,
  348. })
  349. if err != nil {
  350. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  351. return
  352. }
  353. var videoRewardSystem *md2.VideoRewardSystemStruct
  354. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VideoRewardSystem), &videoRewardSystem)
  355. if err != nil {
  356. e.OutErr(c, e.ERR, err.Error())
  357. return
  358. }
  359. rewardTotalNum := utils.StrToInt(videoRewardSystem.RewardTotalNum)
  360. //2、查询当前用户观看视屏记录
  361. watchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(db.Db)
  362. userWatchRecords, err := watchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  363. "key": "uid",
  364. "value": user.Id,
  365. })
  366. if err != nil {
  367. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  368. return
  369. }
  370. var isCan bool
  371. now := time.Now()
  372. if userWatchRecords != nil && userWatchRecords.ResidueWatchAdNum == rewardTotalNum && userWatchRecords.NextWatchAdDate.After(now) {
  373. isCan = true
  374. }
  375. utils.FilePutContents("HomePageIsCanSignIn", utils.SerializeStr(map[string]interface{}{
  376. "user_phone": user.Phone,
  377. "user_uid": user.Id,
  378. "is_can": isCan,
  379. "time": now.Format("2006-01-02 15:04:05"),
  380. }))
  381. resp := md.IsCanSignInResp{
  382. IsCan: isCan,
  383. }
  384. e.OutSuc(c, resp, nil)
  385. return
  386. }
  387. // IsCanGetRedPackage
  388. // @Summary 蛋蛋星球-主页-是否领取红包(获取)
  389. // @Tags 主页
  390. // @Description 是否可以领取红包(获取)
  391. // @Accept json
  392. // @Produce json
  393. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  394. // @Success 200 {object} md.IsCanGetRedPackageResp "具体数据"
  395. // @Failure 400 {object} md.Response "具体错误"
  396. // @Router /api/v1/homePage/isCanGetRedPackage [get]
  397. func IsCanGetRedPackage(c *gin.Context) {
  398. val, exists := c.Get("user")
  399. if !exists {
  400. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  401. return
  402. }
  403. user, ok := val.(*model.User)
  404. if !ok {
  405. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  406. return
  407. }
  408. // 1. 红包活动是否开启
  409. redPackageDb := implement.NewNewUserRedPackageDb(db.Db)
  410. redPackage, err := redPackageDb.NewUserRedPackageGetOne()
  411. if err != nil {
  412. e.OutErr(c, e.ERR, err.Error())
  413. return
  414. }
  415. // 2. 用户已有红包活动记录
  416. userRecordsDb := implement.NewNewUserRedPackageWithUserRecordsDb(db.Db)
  417. records, err := userRecordsDb.NewUserRedPackageWithUserRecordsGetLastPendingByParams(map[string]interface{}{
  418. "key": "uid",
  419. "value": user.Id,
  420. })
  421. if err != nil {
  422. e.OutErr(c, e.ERR_DB_ORM, nil)
  423. return
  424. }
  425. isCan := false
  426. if redPackage != nil {
  427. if redPackage.IsOpen == 1 && records != nil {
  428. isCan = true
  429. }
  430. }
  431. resp := md.IsCanGetRedPackageResp{
  432. IsCan: isCan,
  433. }
  434. e.OutSuc(c, resp, nil)
  435. }