蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

401 lines
12 KiB

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