|
@@ -1,14 +1,33 @@
|
|
|
package community
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"hongze/hongze_yb/controller/response"
|
|
|
+ "hongze/hongze_yb/global"
|
|
|
"hongze/hongze_yb/models/request"
|
|
|
+ respond "hongze/hongze_yb/models/response"
|
|
|
+ "hongze/hongze_yb/services"
|
|
|
"hongze/hongze_yb/services/community"
|
|
|
"hongze/hongze_yb/services/user"
|
|
|
"hongze/hongze_yb/utils"
|
|
|
+ "os"
|
|
|
+ "path"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
+// QuestionList 问答列表
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 获取问答列表
|
|
|
+// @Param page_index query int false "页码"
|
|
|
+// @Param page_size query int false "每页数量"
|
|
|
+// @Param only_mine query int false "只看我的"
|
|
|
+// @Param chart_permission_id query int false "品种权限ID"
|
|
|
+// @Param reply_status query int false "回复状态 0-全部 2-待回答 3-已回答"
|
|
|
+// @Param replier_user_id query int false "回复人ID"
|
|
|
+// @Success 200 {object} respond.CommunityQuestionList
|
|
|
+// @failure 400 {string} string "获取失败"
|
|
|
+// @Router /question/list [get]
|
|
|
func QuestionList(c *gin.Context) {
|
|
|
var req request.QuestionListReq
|
|
|
if err := c.Bind(&req); err != nil {
|
|
@@ -30,6 +49,13 @@ func QuestionList(c *gin.Context) {
|
|
|
response.OkData("获取成功", list, c)
|
|
|
}
|
|
|
|
|
|
+// QuestionDetail 问答详情
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 获取问答详情
|
|
|
+// @Param question_id query int true "问答ID"
|
|
|
+// @Success 200 {object} respond.CommunityQuestionItem
|
|
|
+// @failure 400 {string} string "获取失败"
|
|
|
+// @Router /question/detail [get]
|
|
|
func QuestionDetail(c *gin.Context) {
|
|
|
var req request.QuestionDetailReq
|
|
|
if err := c.Bind(&req); err != nil {
|
|
@@ -48,7 +74,14 @@ func QuestionDetail(c *gin.Context) {
|
|
|
response.OkData("获取成功", item, c)
|
|
|
}
|
|
|
|
|
|
-func AskQuestion(c *gin.Context) {
|
|
|
+// QuestionAsk 发布提问
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 发布提问
|
|
|
+// @Param question_content query string true "问题内容"
|
|
|
+// @Success 200 {string} string "操作成功"
|
|
|
+// @failure 400 {string} string "操作失败"
|
|
|
+// @Router /question/ask [post]
|
|
|
+func QuestionAsk(c *gin.Context) {
|
|
|
var req request.QuestionAskReq
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
response.Fail("参数有误", c)
|
|
@@ -60,14 +93,22 @@ func AskQuestion(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
userinfo := user.GetInfoByClaims(c)
|
|
|
- if err := community.CreateQuestion(int(userinfo.UserID), userinfo.Mobile, userinfo.RealName, req.QuestionContent); err != nil {
|
|
|
- response.FailMsg("提交失败", "AskQuestion ErrMsg:"+err.Error(), c)
|
|
|
+ if err := community.CreateQuestion(int(userinfo.UserID), userinfo.Mobile, userinfo.OpenID, userinfo.RealName, req.QuestionContent); err != nil {
|
|
|
+ response.FailMsg("提交失败", "QuestionAsk ErrMsg:"+err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
response.Ok("操作成功", c)
|
|
|
}
|
|
|
|
|
|
-func ReplyQuestion(c *gin.Context) {
|
|
|
+// QuestionReply 发布回复
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 发布回复
|
|
|
+// @Param question_id query int true "问答ID"
|
|
|
+// @Param audio_list query object true "音频列表"
|
|
|
+// @Success 200 {string} string "操作成功"
|
|
|
+// @failure 400 {string} string "操作失败"
|
|
|
+// @Router /question/reply [post]
|
|
|
+func QuestionReply(c *gin.Context) {
|
|
|
var req request.QuestionReplyReq
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
response.Fail("参数有误", c)
|
|
@@ -83,13 +124,20 @@ func ReplyQuestion(c *gin.Context) {
|
|
|
}
|
|
|
userinfo := user.GetInfoByClaims(c)
|
|
|
if err := community.ReplyUserQuestion(int(userinfo.UserID), req.QuestionId, req.AudioList); err != nil {
|
|
|
- response.FailMsg("提交失败", "ReplyQuestion ErrMsg:"+err.Error(), c)
|
|
|
+ response.FailMsg("提交失败", "QuestionReply ErrMsg:"+err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
response.Ok("操作成功", c)
|
|
|
}
|
|
|
|
|
|
-func ReadReply(c *gin.Context) {
|
|
|
+// QuestionReplyRead 已读回复
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 已读回复
|
|
|
+// @Param question_id query int true "问答ID"
|
|
|
+// @Success 200 {string} string "操作成功"
|
|
|
+// @failure 400 {string} string "操作失败"
|
|
|
+// @Router /question/reply/read [post]
|
|
|
+func QuestionReplyRead(c *gin.Context) {
|
|
|
var req request.QuestionReadReq
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
response.Fail("参数有误", c)
|
|
@@ -101,13 +149,20 @@ func ReadReply(c *gin.Context) {
|
|
|
}
|
|
|
userinfo := user.GetInfoByClaims(c)
|
|
|
if err := community.ReadQuestionReply(int(userinfo.UserID), req.QuestionId); err != nil {
|
|
|
- response.FailMsg("操作失败", "ReadReply ErrMsg:"+err.Error(), c)
|
|
|
+ response.FailMsg("操作失败", "QuestionReplyRead ErrMsg:"+err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
response.Ok("操作成功", c)
|
|
|
}
|
|
|
|
|
|
-func ReplyListTotal(c *gin.Context) {
|
|
|
+// QuestionReplyTotal 问答列表数量统计
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 问答列表数量统计
|
|
|
+// @Param replier_user_id query int true "回复人ID"
|
|
|
+// @Success 200 {object} respond.CommunityReplyTotal
|
|
|
+// @failure 400 {string} string "获取失败"
|
|
|
+// @Router /question/reply/total [get]
|
|
|
+func QuestionReplyTotal(c *gin.Context) {
|
|
|
var req request.ReplyListTotalReq
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
response.Fail("参数有误", c)
|
|
@@ -119,8 +174,76 @@ func ReplyListTotal(c *gin.Context) {
|
|
|
}
|
|
|
resp, err := community.GetReplyListTotal(req.ReplierUserId)
|
|
|
if err != nil {
|
|
|
- response.FailMsg("获取失败", "ReplyListTotal ErrMsg:"+err.Error(), c)
|
|
|
+ response.FailMsg("获取失败", "QuestionReplyTotal ErrMsg:"+err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
response.OkData("获取成功", resp, c)
|
|
|
}
|
|
|
+
|
|
|
+// QuestionUploadAudio 上传回复音频
|
|
|
+// @Tags 问答社区模块
|
|
|
+// @Description 上传回复音频
|
|
|
+// @Param file query string true "音频文件"
|
|
|
+// @Success 200 {string} string "上传成功"
|
|
|
+// @failure 400 {string} string "上传失败"
|
|
|
+// @Router /question/reply/upload_audio [post]
|
|
|
+func QuestionUploadAudio(c *gin.Context) {
|
|
|
+ file, err := c.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ext := path.Ext(file.Filename)
|
|
|
+ if ext != ".mp3" {
|
|
|
+ response.Fail("暂仅支持mp3格式", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ dateDir := time.Now().Format("20060102")
|
|
|
+ localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
|
|
|
+ if err := os.MkdirAll(localDir, 0766); err != nil {
|
|
|
+ response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ randStr := utils.GetRandStringNoSpecialChar(28)
|
|
|
+ filtName := randStr + ext
|
|
|
+ fpath := localDir + "/" + filtName
|
|
|
+ defer func() {
|
|
|
+ _ = os.Remove(fpath)
|
|
|
+ }()
|
|
|
+ // 生成文件至指定目录
|
|
|
+ if err := c.SaveUploadedFile(file, fpath); err != nil {
|
|
|
+ response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 获取音频文件时长
|
|
|
+ fByte, err := os.ReadFile(fpath)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ seconds, err := services.GetMP3PlayDuration(fByte)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 音频大小MB
|
|
|
+ fi, err := os.Stat(fpath)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mb := utils.Bit2MB(fi.Size(), 2)
|
|
|
+ // 上传文件至阿里云
|
|
|
+ ossDir := "yb_wx/community_question_audio/"
|
|
|
+ resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
|
|
|
+ if err != nil {
|
|
|
+ response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp := &respond.CommunityQuestionAudioUpload{
|
|
|
+ AudioURL: resourceUrl,
|
|
|
+ AudioPlaySeconds: fmt.Sprint(seconds),
|
|
|
+ AudioSize: fmt.Sprint(mb),
|
|
|
+ }
|
|
|
+ response.OkData("上传成功", resp, c)
|
|
|
+}
|