互动
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.

61 lines
1.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package client
  2. import (
  3. "hudongzhuanjia/controllers"
  4. "hudongzhuanjia/models"
  5. "hudongzhuanjia/utils/code"
  6. "hudongzhuanjia/utils/define"
  7. )
  8. type CalorieCtl struct {
  9. controllers.AuthorCtl
  10. }
  11. // h5 1/s
  12. func (t *CalorieCtl) Shake() {
  13. calorieId := t.MustGetInt64("calorie_id")
  14. score := t.DefaultInt("score", 0)
  15. uid := t.MustGetUID()
  16. calorie := new(models.Calorie)
  17. exist, err := models.Get(calorie, calorieId)
  18. t.CheckErr(err)
  19. t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里不存在")
  20. activity := new(models.Activity)
  21. exist, err = models.Get(activity, calorie.ActivityId)
  22. t.CheckErr(err)
  23. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  24. t.CheckRunning(activity.Status)
  25. calorieUser := new(models.CalorieUser)
  26. exist, err = calorieUser.GetByCalorieIdAndUserId(calorieId, uid, activity.RehearsalId)
  27. t.CheckErr(err)
  28. t.Assert(exist, code.MSG_DATA_NOT_EXIST, "您尚未参与卡路里活动")
  29. // 增加score
  30. if calorie.Status == define.StatusRunning && score > 0 {
  31. _, err = calorieUser.IncrScore(score)
  32. t.CheckErr(err)
  33. calorieUser.Score += int64(score) // 增加
  34. }
  35. count, err := calorieUser.CountByCalorieId(calorie.Id, activity.RehearsalId)
  36. t.CheckErr(err)
  37. users, err := models.GetCalorieUsersByCalorieIdAndScore(calorie.Id, activity.RehearsalId, calorieUser.Score)
  38. t.CheckErr(err)
  39. var rank int
  40. for index, user := range users {
  41. if user.Id == calorieUser.Id {
  42. rank = int(count) - len(users) + index + 1
  43. break
  44. }
  45. }
  46. t.JSON(map[string]interface{}{
  47. "rank": rank,
  48. "score": calorieUser.Score, // 分数
  49. })
  50. }