1
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package service
|
||||
|
||||
type BaseClassService struct {
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"hepa-calc-api/api/dao"
|
||||
"hepa-calc-api/api/dto"
|
||||
"hepa-calc-api/extend/aliyun"
|
||||
"hepa-calc-api/global"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PublicService struct {
|
||||
}
|
||||
|
||||
// GetPhoneCode 获取手机验证码
|
||||
func (r *PublicService) GetPhoneCode(scene int, phone string) (bool, error) {
|
||||
var sendCodeCount int // // 获取验证码最大次数
|
||||
var code string // 验证码
|
||||
var templateCode string // 短信模版
|
||||
|
||||
// 登录获取验证码
|
||||
if scene == 1 {
|
||||
// 验证发送次数
|
||||
res, _ := global.Redis.Get(context.Background(), "login_code_count_"+phone).Result()
|
||||
if res != "" {
|
||||
sendCodeCount, err := strconv.Atoi(res)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if sendCodeCount > 3 {
|
||||
// 超出规定时间内最大获取次数
|
||||
return false, errors.New("手机号超出规定时间内最大获取次数,请您稍后再试")
|
||||
}
|
||||
}
|
||||
|
||||
// 生成随机数
|
||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
code = strconv.Itoa(rand.Intn(9000) + 1000)
|
||||
|
||||
// 模版
|
||||
templateCode = "SMS_243055263"
|
||||
|
||||
sendCodeCount = sendCodeCount + 1
|
||||
}
|
||||
|
||||
if code == "" || templateCode == "" {
|
||||
return false, errors.New("验证码发送失败,请您稍后再试")
|
||||
}
|
||||
|
||||
// 发送验证码
|
||||
templateParam := make(map[string]interface{})
|
||||
templateParam["code"] = code
|
||||
err := aliyun.SendSms(phone, templateCode, "获取验证码", templateParam)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 记录发送次数
|
||||
if sendCodeCount != 0 {
|
||||
_, err = global.Redis.Set(context.Background(), "login_code_count_"+phone, time.Now().Unix(), 60*5*time.Second).Result()
|
||||
if err != nil {
|
||||
return false, errors.New("验证码发送失败,请您稍后再试")
|
||||
}
|
||||
}
|
||||
|
||||
// 设置验证码有效期
|
||||
_, err = global.Redis.Set(context.Background(), "login_code_"+phone, code, 60*30*time.Second).Result()
|
||||
if err != nil {
|
||||
return false, errors.New("验证码发送失败,请您稍后再试")
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetIndex 获取首页数据
|
||||
func (r *PublicService) GetIndex(userId int64) (g *dto.IndexDto, err error) {
|
||||
// 获取疾病分类列表
|
||||
baseClassDao := dao.BaseClassDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["class_status"] = 1
|
||||
baseClasss, err := baseClassDao.GetBaseClassOrderList(maps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
questionService := &QuestionService{}
|
||||
|
||||
// 获取算一算热榜-人气数最高的9个
|
||||
hotQuestions, err := questionService.GetHotList()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取为你推荐-后台指定的推广
|
||||
recommendQuestions, err := questionService.GetRecommendList()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取猜你喜欢
|
||||
guessUserLikes, err := questionService.GetGuessUserLIkeList(userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g = &dto.IndexDto{}
|
||||
|
||||
g.LoadBaseClass(baseClasss)
|
||||
|
||||
g.LoadHotQuestionList(hotQuestions)
|
||||
|
||||
g.LoadRecommendQuestionList(recommendQuestions)
|
||||
|
||||
g.LoadGuessUserLikeList(guessUserLikes)
|
||||
|
||||
return g, nil
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"hepa-calc-api/api/dao"
|
||||
"hepa-calc-api/api/model"
|
||||
)
|
||||
|
||||
type QuestionService struct {
|
||||
}
|
||||
|
||||
// GetHotList 获取算一算热榜-人气数最高的9个
|
||||
func (r *QuestionService) GetHotList() (m []*model.Question, err error) {
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_status"] = 1
|
||||
maps["is_hide"] = 0
|
||||
questions, err := questionDao.GetQuestionOrderLimitList(maps, "click_count desc", 9)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return questions, nil
|
||||
}
|
||||
|
||||
// GetRecommendList 获取为你推荐-后台指定的推广
|
||||
func (r *QuestionService) GetRecommendList() (m []*model.Question, err error) {
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_status"] = 1
|
||||
maps["is_hide"] = 0
|
||||
maps["is_recommend"] = 1
|
||||
questions, err := questionDao.GetQuestionList(maps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return questions, nil
|
||||
}
|
||||
|
||||
// GetGuessUserLIkeList 获取猜你喜欢-暂用公众参与过的最新算一算,至多显示3个。若未参与,则指定或者随机显示3个
|
||||
func (r *QuestionService) GetGuessUserLIkeList(userId int64) (m []*model.Question, err error) {
|
||||
orderSingleDao := dao.OrderSingleDao{}
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
var questions []*model.Question
|
||||
|
||||
if userId != 0 {
|
||||
maps := make(map[string]interface{})
|
||||
maps["user_id"] = userId
|
||||
orderSingles, err := orderSingleDao.GetOrderSingleOrderList(maps, "created_at desc", 3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 参与过
|
||||
if len(orderSingles) > 0 {
|
||||
for i, single := range orderSingles {
|
||||
questions[i] = single.Question
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 未参与过/未指定用户
|
||||
if len(questions) == 0 {
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_status"] = 1
|
||||
maps["is_hide"] = 0
|
||||
questions, err = questionDao.GetQuestionListRand(maps, 3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return questions, nil
|
||||
}
|
||||
|
||||
// CheckUserCollectionQuestion 检测问题是否被用户收藏
|
||||
func (r *QuestionService) CheckUserCollectionQuestion(userId, questionId int64) (bool, error) {
|
||||
userCollectionDao := dao.UserCollectionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["user_id"] = userId
|
||||
maps["question_id"] = questionId
|
||||
userCollection, err := userCollectionDao.GetUserCollection(maps)
|
||||
if userCollection == nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// CheckUserBuyQuestion 检测用户是否购买过该问题
|
||||
func (r *QuestionService) CheckUserBuyQuestion(userId, questionId int64) bool {
|
||||
orderSingleDao := dao.OrderSingleDao{}
|
||||
orderSingle, _ := orderSingleDao.GetUserFirstTimeBuyOrderSingle(userId, questionId)
|
||||
if orderSingle == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GetUserFirstTimeBuyPrice 获取用户首次购买价格
|
||||
func (r *QuestionService) GetUserFirstTimeBuyPrice(userId, questionId int64) (f *float64, err error) {
|
||||
// 检测用户是否购买过该问题
|
||||
isFirstBuy := r.CheckUserBuyQuestion(userId, questionId)
|
||||
if isFirstBuy == false {
|
||||
// 未购买过
|
||||
systemSingleDao := dao.SystemSingleDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
systemSingle, err := systemSingleDao.GetSystemSingle(maps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &systemSingle.FirstTimePrice, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetQuestionBuyCount 获取问题被购买数量
|
||||
func (r *QuestionService) GetQuestionBuyCount(userId, questionId int64) (c int, err error) {
|
||||
// 未购买过
|
||||
systemSingleDao := dao.SystemSingleDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["user_id"] = userId
|
||||
maps["question_id"] = questionId
|
||||
maps["order_status"] = 2
|
||||
maps["refund_status"] = 0
|
||||
buyCount, err := systemSingleDao.GetSystemSingleCount(maps)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(buyCount), nil
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package service
|
||||
|
||||
type UserService struct {
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"hepa-calc-api/api/dao"
|
||||
"hepa-calc-api/api/model"
|
||||
"hepa-calc-api/global"
|
||||
)
|
||||
|
||||
type UserCollectionService struct {
|
||||
}
|
||||
|
||||
// CheckUserCollectionQuestion 检测问题是否被用户收藏
|
||||
func (r *UserCollectionService) CheckUserCollectionQuestion(userId, questionId int64) bool {
|
||||
userCollectionDao := dao.UserCollectionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["user_id"] = userId
|
||||
maps["question_id"] = questionId
|
||||
userCollection, _ := userCollectionDao.GetUserCollection(maps)
|
||||
if userCollection == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// PutUserCollection 收藏题目
|
||||
func (r *UserCollectionService) PutUserCollection(userId, questionId int64) (bool, error) {
|
||||
// 检测问题是否被用户收藏
|
||||
IsCollection := r.CheckUserCollectionQuestion(userId, questionId)
|
||||
if IsCollection == true {
|
||||
// 已收藏
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
userCollection := &model.UserCollection{
|
||||
UserId: userId,
|
||||
QuestionId: questionId,
|
||||
}
|
||||
|
||||
userCollectionDao := dao.UserCollectionDao{}
|
||||
userCollection, err := userCollectionDao.AddUserCollection(tx, userCollection)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("收藏失败")
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutUserCollectionCancel 取消收藏题目
|
||||
func (r *UserCollectionService) PutUserCollectionCancel(userId, questionId int64) (bool, error) {
|
||||
// 检测问题是否被用户收藏
|
||||
IsCollection := r.CheckUserCollectionQuestion(userId, questionId)
|
||||
if IsCollection == false {
|
||||
// 已收藏
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
userCollectionDao := dao.UserCollectionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["user_id"] = userId
|
||||
maps["question_id"] = questionId
|
||||
err := userCollectionDao.DeleteUserCollection(tx, maps)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("取消收藏失败")
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user