1
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/dto"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// AdminUserService 后台用户
|
||||
type AdminUserService struct {
|
||||
}
|
||||
|
||||
func (r *AdminUserService) Login(req requests.Login) (res *dto.AdminUserDto, err error) {
|
||||
// 获取用户信息
|
||||
AdminUserDao := dao.AdminUserDao{}
|
||||
adminUser, err := AdminUserDao.GetAdminUserFirstByAccess(req.Access)
|
||||
if err != nil || adminUser == nil {
|
||||
return nil, errors.New("用户名或密码错误")
|
||||
}
|
||||
|
||||
// 检测用户密码
|
||||
password := md5.Sum([]byte(req.Password + adminUser.Salt))
|
||||
// 将哈希值转换为16进制字符串
|
||||
passwordString := hex.EncodeToString(password[:])
|
||||
|
||||
if passwordString != adminUser.Password {
|
||||
return nil, errors.New("用户名或密码错误")
|
||||
}
|
||||
|
||||
// 检测用户状态
|
||||
if adminUser.IsDeleted == 1 {
|
||||
return nil, errors.New("非法用户")
|
||||
}
|
||||
|
||||
if adminUser.IsDisabled == 1 {
|
||||
return nil, errors.New("您的账号已被禁用,请联系管理员处理")
|
||||
}
|
||||
|
||||
// 检测用户状态
|
||||
if adminUser.Status != 1 {
|
||||
return nil, errors.New("您的账号已被禁用,请联系管理员处理")
|
||||
}
|
||||
|
||||
token := &utils.Token{
|
||||
UserId: strconv.FormatInt(adminUser.UserId, 10),
|
||||
}
|
||||
|
||||
// 生成jwt
|
||||
jwt, err := token.NewJWT()
|
||||
if err != nil {
|
||||
return nil, errors.New("登陆失败")
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g := dto.LoginDto(adminUser)
|
||||
|
||||
// 加载token
|
||||
g.LoadUserDoctor(jwt)
|
||||
|
||||
return g, nil
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
"knowledge/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type QuestionService struct {
|
||||
}
|
||||
|
||||
// AddQuestion 新增题目
|
||||
func (r *QuestionService) AddQuestion(req requests.AddQuestion) (bool, error) {
|
||||
// 验证一级标签
|
||||
firstLabelId, err := strconv.ParseInt(req.FirstLabelId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(firstLabelId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 验证二级标签
|
||||
var secondLabelId int64
|
||||
if req.SecondLabelId != "" {
|
||||
secondLabelId, err := strconv.ParseInt(req.SecondLabelId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, err = labelDao.GetLabelFirstById(secondLabelId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// 处理图片
|
||||
var questionImage string
|
||||
if len(req.QuestionImage) > 0 {
|
||||
result := make([]string, len(req.QuestionImage))
|
||||
for i, url := range req.QuestionImage {
|
||||
result[i] = utils.RemoveOssDomain(url)
|
||||
}
|
||||
|
||||
questionImage = strings.Join(result, ",")
|
||||
}
|
||||
|
||||
// 判断选项
|
||||
if req.QuestionType == 1 || req.QuestionType == 2 {
|
||||
if len(req.QuestionOption) == 0 {
|
||||
return false, errors.New("请填入选项")
|
||||
}
|
||||
}
|
||||
|
||||
// 验证重复
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_name"] = req.QuestionName
|
||||
question, _ := questionDao.GetQuestion(maps)
|
||||
if question != nil {
|
||||
return false, errors.New("题目名称重复")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 新增题目
|
||||
question = &model.Question{
|
||||
QuestionName: req.QuestionName,
|
||||
QuestionType: req.QuestionType,
|
||||
QuestionStatus: req.QuestionStatus,
|
||||
QuestionSource: req.QuestionSource,
|
||||
QuestionImage: questionImage,
|
||||
QuestionAnswer: req.QuestionAnswer,
|
||||
QuestionAnalysis: req.QuestionAnalysis,
|
||||
Difficulty: req.Difficulty,
|
||||
FirstLabelId: &firstLabelId,
|
||||
SecondLabelId: nil,
|
||||
}
|
||||
|
||||
if req.SecondLabelId != "" {
|
||||
question.SecondLabelId = &secondLabelId
|
||||
}
|
||||
|
||||
question, err = questionDao.AddQuestion(tx, question)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
// 新增选项
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
for _, s := range req.QuestionOption {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: s,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
}
|
||||
|
||||
//tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user