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