From ebd887ff15bc5adc90e8052dab8004f9d175aa85 Mon Sep 17 00:00:00 2001 From: Penryn <15158052130@163.com> Date: Tue, 15 Oct 2024 15:05:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(admin):=E4=BF=AE=E6=94=B9=E8=B6=85=E7=BA=A7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E5=8F=AF=E4=BB=A5=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=AE=E5=8D=B7=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes internal/handler/admin/survey.go | 122 ++++++++++--------------------- internal/router/router.go | 3 +- 3 files changed, 39 insertions(+), 86 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f18ffc0ab94c80e31981a49786b81412547d9024 GIT binary patch literal 6148 zcmeHKOHRWu6dXgKr3l254N^`}i6y(VG!jU_f?k013nFc#78dNX=MG$f^Ra??wn2$o zwt&!kl0A++UKI0j(4kE3JY06>#PFlw<{6EZGkB{qD|KGDb==Xo(4Ksq_&MG z%#hcW<|t5Zk#~m?#>fKZbHI!^Yr)wk0q-r~ZS(#GXPvPg)wvtfZ@5c5Vpiv(L+(o@ z#|XE~ZixGUCtg%}P3AMl12b9>v$YOX`3+p)9H&Teg+9{y>_%LuvLkw2FvDfQ6Bnwy z2J_GCO5~{*b++}P%5LJ4?0c?xLQTH%yy=D6uXwS8Yp%D09(AepI&d0vrKaHWG=9Ya z?%5K}U58q$1L}Y}@a2GfA7U24#AE5uemYp$BLFdCw+*)KuRk%C4r1c5bjTitaZ;j_ z8h^wv{^Vesoa2e-mkymA#@?Cp_&XbaLNWf}VPZ>%NgQge4yXfl2R7a1f$ab3_vin* zNUziZb>Lq)V4_~X*JVn6Z(T`F_S%@`hDAbnr9&%W<+o$okgfQT#RkV>X%G{Sr9<{G P^hdzjpp`oCqYiuk+Uvdu literal 0 HcmV?d00001 diff --git a/internal/handler/admin/survey.go b/internal/handler/admin/survey.go index 773b605..b38475d 100644 --- a/internal/handler/admin/survey.go +++ b/internal/handler/admin/survey.go @@ -75,32 +75,32 @@ func CreateSurvey(c *gin.Context) { } questionMap := make(map[string]bool) for _, question := range data.Questions { - if question.Subject == "" { + if question.Subject == "" { c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "标题为空"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyIncomplete) return } if questionMap[question.Subject] { - c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.SerialNum)+"题目"+question.Subject+"重复"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "题目" + question.Subject + "重复"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyContentRepeat) return } questionMap[question.Subject] = true if question.QuestionType == 1 || question.QuestionType == 2 { if len(question.Options) < 1 { - c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.SerialNum)+"选项数量太少"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "选项数量太少"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyIncomplete) return } optionMap := make(map[string]bool) for _, option := range question.Options { if option.Content == "" { - c.Error(&gin.Error{Err: errors.New("选项"+strconv.Itoa(option.SerialNum)+"内容为空"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("选项" + strconv.Itoa(option.SerialNum) + "内容为空"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyIncomplete) return } if optionMap[option.Content] { - c.Error(&gin.Error{Err: errors.New("选项内容"+option.Content+"重复"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("选项内容" + option.Content + "重复"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyContentRepeat) return } @@ -149,7 +149,7 @@ func UpdateSurveyStatus(c *gin.Context) { } //判断权限 if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } @@ -179,12 +179,12 @@ func UpdateSurveyStatus(c *gin.Context) { questionMap := make(map[string]bool) for _, question := range questions { if question.Subject == "" { - c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.SerialNum)+"内容填写为空"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "内容填写为空"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyIncomplete) return } if questionMap[question.Subject] { - c.Error(&gin.Error{Err: errors.New("问题题目"+question.Subject+"重复"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("问题题目" + question.Subject + "重复"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyContentRepeat) return } @@ -197,19 +197,19 @@ func UpdateSurveyStatus(c *gin.Context) { return } if len(options) < 1 { - c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.ID)+"选项太少"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.ID) + "选项太少"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyIncomplete) return } optionMap := make(map[string]bool) for _, option := range options { if option.Content == "" { - c.Error(&gin.Error{Err: errors.New("选项"+strconv.Itoa(option.SerialNum)+"内容未填"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("选项" + strconv.Itoa(option.SerialNum) + "内容未填"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyIncomplete) return } if optionMap[option.Content] { - c.Error(&gin.Error{Err: errors.New("选项内容"+option.Content+"重复"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New("选项内容" + option.Content + "重复"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.SurveyContentRepeat) return } @@ -261,20 +261,28 @@ func UpdateSurvey(c *gin.Context) { } //判断权限 if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } - //判断问卷状态 - if survey.Status != 1 { - c.Error(&gin.Error{Err: errors.New("问卷状态不为未发布"), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.StatusRepeatError) - return - } - // 判断问卷的填写数量是否为零 - if survey.Num != 0 { - c.Error(&gin.Error{Err: errors.New("问卷已有填写数量"), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.SurveyNumError) + if user.AdminType == 2 { + // 超管可以直接修改问卷 + } else if user.AdminType == 1 && survey.UserID == user.ID { + //判断问卷状态 + if survey.Status != 1 { + c.Error(&gin.Error{Err: errors.New("问卷状态不为未发布"), Type: gin.ErrorTypeAny}) + utils.JsonErrorResponse(c, code.StatusRepeatError) + return + } + // 判断问卷的填写数量是否为零 + if survey.Num != 0 { + c.Error(&gin.Error{Err: errors.New("问卷已有填写数量"), Type: gin.ErrorTypeAny}) + utils.JsonErrorResponse(c, code.SurveyNumError) + return + } + } else { + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) + utils.JsonErrorResponse(c, code.NoPermission) return } //解析时间转换为中国时间(UTC+8) @@ -310,59 +318,6 @@ func UpdateSurvey(c *gin.Context) { utils.JsonSuccessResponse(c, nil) } -type UpdateSurveyPartData struct { - ID int `json:"id" binding:"required"` - Title string `json:"title"` - Desc string `json:"desc" ` - Img string `json:"img" ` - Time string `json:"time"` -} - -func UpdateSurveyPart(c *gin.Context) { - var data UpdateSurveyPartData - err := c.ShouldBindJSON(&data) - if err != nil { - c.Error(&gin.Error{Err: errors.New("获取参数失败原因: " + err.Error()), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.ParamError) - return - } - //鉴权 - user, err := service.GetUserSession(c) - if err != nil { - c.Error(&gin.Error{Err: errors.New("获取用户缓存信息失败原因: " + err.Error()), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.NotLogin) - return - } - // 获取问卷 - survey, err := service.GetSurveyByID(data.ID) - if err != nil { - c.Error(&gin.Error{Err: errors.New("获取问卷信息失败原因: " + err.Error()), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.ServerError) - return - } - //判断权限 - if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.NoPermission) - return - } - //解析时间转换为中国时间(UTC+8) - ddlTime, err := time.Parse(time.RFC3339, data.Time) - if err != nil { - c.Error(&gin.Error{Err: errors.New("时间解析失败原因: " + err.Error()), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.ServerError) - return - } - //修改问卷 - err = service.UpdateSurveyPart(data.ID, data.Title, data.Desc, data.Img, ddlTime) - if err != nil { - c.Error(&gin.Error{Err: errors.New("修改问卷失败原因: " + err.Error()), Type: gin.ErrorTypeAny}) - utils.JsonErrorResponse(c, code.ServerError) - return - } - utils.JsonSuccessResponse(c, nil) -} - // 删除问卷 type DeleteSurveyData struct { ID int `form:"id" binding:"required"` @@ -396,7 +351,7 @@ func DeleteSurvey(c *gin.Context) { } //判断权限 if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } @@ -414,7 +369,7 @@ func DeleteSurvey(c *gin.Context) { type GetSurveyAnswersData struct { ID int `form:"id" binding:"required"` Text string `form:"text"` - Unique bool `form:"unique"` + Unique bool `form:"unique"` PageNum int `form:"page_num" binding:"required"` PageSize int `form:"page_size" binding:"required"` } @@ -447,13 +402,13 @@ func GetSurveyAnswers(c *gin.Context) { } //判断权限 if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } //获取问卷收集数据 var num *int64 - answers, num, err := service.GetSurveyAnswers(data.ID, data.PageNum, data.PageSize, data.Text,data.Unique) + answers, num, err := service.GetSurveyAnswers(data.ID, data.PageNum, data.PageSize, data.Text, data.Unique) if err != nil { c.Error(&gin.Error{Err: errors.New("获取问卷收集数据失败原因: " + err.Error()), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.ServerError) @@ -568,7 +523,7 @@ func GetSurvey(c *gin.Context) { } //判断权限 if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } @@ -652,7 +607,7 @@ func DownloadFile(c *gin.Context) { } // 判断权限 if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } @@ -715,7 +670,7 @@ func GetSurveyStatistics(c *gin.Context) { } if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) { - c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny}) + c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny}) utils.JsonErrorResponse(c, code.NoPermission) return } @@ -835,7 +790,7 @@ func GetSurveyStatistics(c *gin.Context) { if start > end { start = end } - + // 按序号排序 sort.Slice(response, func(i, j int) bool { return response[i].SerialNum < response[j].SerialNum @@ -845,7 +800,6 @@ func GetSurveyStatistics(c *gin.Context) { resp := response[start:end] totalSumPage := math.Ceil(float64(len(response)) / float64(data.PageSize)) - utils.JsonSuccessResponse(c, gin.H{ "statistics": resp, "total": len(answersheets), diff --git a/internal/router/router.go b/internal/router/router.go index 151681a..d38744f 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -16,7 +16,7 @@ func Init(r *gin.Engine) { { api.POST("/admin/reg", a.Register) api.POST("/admin/login", a.Login) - user:= api.Group("/user") + user := api.Group("/user") { user.POST("/submit", u.SubmitSurvey) user.GET("/get", u.GetSurvey) @@ -30,7 +30,6 @@ func Init(r *gin.Engine) { admin.POST("/create", a.CreateSurvey) admin.PUT("/update/status", a.UpdateSurveyStatus) admin.PUT("/update/questions", a.UpdateSurvey) - admin.PUT("/update/part", a.UpdateSurveyPart) admin.GET("/list/answers", a.GetSurveyAnswers) admin.GET("/statics/answers", a.GetSurveyStatistics) admin.DELETE("/delete", a.DeleteSurvey)