question.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. package community
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb/controller/response"
  7. "hongze/hongze_yb/global"
  8. userLogic "hongze/hongze_yb/logic/user"
  9. "hongze/hongze_yb/logic/yb_community_question"
  10. "hongze/hongze_yb/models/request"
  11. "hongze/hongze_yb/models/tables/yb_resource"
  12. "strings"
  13. respond "hongze/hongze_yb/models/response"
  14. responseModel "hongze/hongze_yb/models/response"
  15. "hongze/hongze_yb/services"
  16. "hongze/hongze_yb/services/community"
  17. "hongze/hongze_yb/services/user"
  18. userService "hongze/hongze_yb/services/user"
  19. "hongze/hongze_yb/services/wx_app"
  20. "hongze/hongze_yb/utils"
  21. "io/ioutil"
  22. "os"
  23. "path"
  24. "strconv"
  25. "time"
  26. )
  27. // QuestionList 问答列表
  28. // @Tags 问答社区模块
  29. // @Description 获取问答列表
  30. // @Param page_index query int false "页码"
  31. // @Param page_size query int false "每页数量"
  32. // @Param only_mine query int false "只看我的"
  33. // @Param variety_tag_id query int false "标签ID"
  34. // @Param reply_status query int false "回复状态 0-全部 2-未回答 3-已回答,4-待回答(研究员)"
  35. // @Success 200 {object} []respond.CommunityQuestionItem
  36. // @failure 400 {string} string "获取失败"
  37. // @Router /question/list [get]
  38. func QuestionList(c *gin.Context) {
  39. var req request.QuestionListReq
  40. if err := c.Bind(&req); err != nil {
  41. response.Fail("参数有误", c)
  42. return
  43. }
  44. if req.PageIndex == 0 {
  45. req.PageIndex = 1
  46. }
  47. if req.PageSize == 0 {
  48. req.PageSize = utils.PageSize20
  49. }
  50. userinfo := user.GetInfoByClaims(c)
  51. list, err := community.GetQuestionList(req.PageIndex, req.PageSize, req.OnlyMine, req.VarietyTagId, req.ReplyStatus, req.GroupId, userinfo)
  52. if err != nil {
  53. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  54. return
  55. }
  56. // 点赞/吐槽数据
  57. err = yb_community_question.HandleLikeOrTeaseByCommunityQuestionItemList(userinfo.UserID, list)
  58. if err != nil {
  59. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  60. return
  61. }
  62. // 评论数据
  63. err = yb_community_question.HandleCommentByCommunityQuestionItemList(userinfo.UserID, list)
  64. if err != nil {
  65. response.FailMsg("获取失败", "QuestionList ErrMsg:"+err.Error(), c)
  66. return
  67. }
  68. response.OkData("获取成功", list, c)
  69. }
  70. // QuestionDetail 问答详情
  71. // @Tags 问答社区模块
  72. // @Description 获取问答详情
  73. // @Param question_id query int true "问答ID"
  74. // @Success 200 {object} respond.CommunityQuestionItem
  75. // @failure 400 {string} string "获取失败"
  76. // @Router /question/detail [get]
  77. func QuestionDetail(c *gin.Context) {
  78. var req request.QuestionDetailReq
  79. if err := c.Bind(&req); err != nil {
  80. response.Fail("参数有误", c)
  81. return
  82. }
  83. if req.QuestionId == 0 {
  84. response.Fail("参数有误", c)
  85. return
  86. }
  87. userinfo := user.GetInfoByClaims(c)
  88. item, errMsg, err := community.GetQuestionDetail(req.QuestionId, userinfo)
  89. if err != nil {
  90. response.FailMsg(errMsg, "QuestionDetail ErrMsg:"+err.Error(), c)
  91. return
  92. }
  93. response.OkData("获取成功", item, c)
  94. }
  95. // QuestionAsk 发布提问
  96. // @Tags 问答社区模块
  97. // @Description 发布提问
  98. // @Param question_content query string true "问题内容"
  99. // @Success 200 {string} string "操作成功"
  100. // @failure 400 {string} string "操作失败"
  101. // @Router /question/ask [post]
  102. func QuestionAsk(c *gin.Context) {
  103. // 是否为备用小程序
  104. copyYb := c.Request.Header.Get("CopyYb")
  105. var req request.QuestionAskReq
  106. if err := c.ShouldBind(&req); err != nil {
  107. response.Fail("参数有误", c)
  108. return
  109. }
  110. if req.QuestionContent == "" {
  111. response.Fail("内容不可为空", c)
  112. return
  113. }
  114. contentRune := []rune(req.QuestionContent)
  115. if len(contentRune) > 50 {
  116. response.Fail("内容不可超过50个字符", c)
  117. return
  118. }
  119. if req.VarietyClassifyId <= 0 {
  120. response.Fail("请选择品种分类", c)
  121. return
  122. }
  123. if req.VarietyTagId <= 0 {
  124. response.Fail("请选择品种", c)
  125. return
  126. }
  127. // 敏感词校验, 只有小程序的用户才能走敏感词过滤接口
  128. userinfo := user.GetInfoByClaims(c)
  129. if userinfo.RecordInfo.OpenID != "" && (userinfo.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_YB || userinfo.RecordInfo.CreatePlatform == utils.USER_RECORD_PLATFORM_COPY_YB) {
  130. checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.QuestionContent, copyYb)
  131. if e == nil {
  132. if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
  133. errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)
  134. response.FailMsg("含有违禁词, 不允许发布", errMsg, c)
  135. return
  136. }
  137. }
  138. }
  139. if err := community.CreateQuestion(int(userinfo.UserID), req.VarietyClassifyId, req.VarietyTagId, userinfo.Mobile, userinfo.RealName, req.QuestionContent); err != nil {
  140. response.FailMsg("提交失败", "QuestionAsk ErrMsg:"+err.Error(), c)
  141. return
  142. }
  143. response.Ok("操作成功", c)
  144. }
  145. // QuestionReply 发布回复
  146. // @Tags 问答社区模块
  147. // @Description 发布回复
  148. // @Param question_id query int true "问答ID"
  149. // @Param audio_list query object true "音频列表"
  150. // @Success 200 {string} string "操作成功"
  151. // @failure 400 {string} string "操作失败"
  152. // @Router /question/reply [post]
  153. func QuestionReply(c *gin.Context) {
  154. var req request.QuestionReplyReq
  155. if err := c.ShouldBind(&req); err != nil {
  156. response.Fail("参数有误", c)
  157. return
  158. }
  159. if req.QuestionId == 0 {
  160. response.Fail("参数有误", c)
  161. return
  162. }
  163. if len(req.AudioList) == 0 {
  164. response.Fail("音频不可为空", c)
  165. return
  166. }
  167. for i := range req.AudioList {
  168. if req.AudioList[i].AudioUrl == "" || req.AudioList[i].AudioPlaySeconds == "" || req.AudioList[i].AudioSize == "" {
  169. response.Fail("音频信息有误, 请重新上传", c)
  170. return
  171. }
  172. if !strings.Contains(req.AudioList[i].AudioUrl, utils.ALIYUN_OSS_HOST) {
  173. response.Fail("音频地址有误, 请重新上传", c)
  174. return
  175. }
  176. }
  177. userinfo := user.GetInfoByClaims(c)
  178. msg, err := community.ReplyUserQuestion(int(userinfo.UserID), req.QuestionId, req.AudioList)
  179. if err != nil {
  180. response.FailMsg(msg, "QuestionReply ErrMsg:"+err.Error(), c)
  181. return
  182. }
  183. response.Ok("操作成功", c)
  184. }
  185. // QuestionReplyRead 已读回复
  186. // @Tags 问答社区模块
  187. // @Description 已读回复
  188. // @Param question_ids query string true "问答IDs"
  189. // @Success 200 {string} string "操作成功"
  190. // @failure 400 {string} string "操作失败"
  191. // @Router /question/reply/read [post]
  192. func QuestionReplyRead(c *gin.Context) {
  193. var req request.QuestionReadReq
  194. if err := c.ShouldBind(&req); err != nil {
  195. response.Fail("参数有误", c)
  196. return
  197. }
  198. if req.QuestionIds == "" {
  199. response.Fail("参数有误", c)
  200. return
  201. }
  202. userinfo := user.GetInfoByClaims(c)
  203. if err := community.ReadQuestionReply(req.QuestionIds, userinfo); err != nil {
  204. response.FailMsg("操作失败", "QuestionReplyRead ErrMsg:"+err.Error(), c)
  205. return
  206. }
  207. response.Ok("操作成功", c)
  208. }
  209. // QuestionListTotal 问答列表数量统计
  210. // @Tags 问答社区模块
  211. // @Description 问答列表数量统计
  212. // @Success 200 {object} respond.CommunityQuestionListTotal
  213. // @failure 400 {string} string "获取失败"
  214. // @Router /question/list/total [get]
  215. func QuestionListTotal(c *gin.Context) {
  216. userinfo := user.GetInfoByClaims(c)
  217. resp, err := community.GetQuestionListTotal(userinfo)
  218. if err != nil {
  219. response.FailMsg("获取失败", "QuestionReplyTotal ErrMsg:"+err.Error(), c)
  220. return
  221. }
  222. response.OkData("获取成功", resp, c)
  223. }
  224. // QuestionUploadAudio 上传回复音频
  225. // @Tags 问答社区模块
  226. // @Description 上传回复音频
  227. // @Param file query string true "音频文件"
  228. // @Success 200 {string} string "上传成功"
  229. // @failure 400 {string} string "上传失败"
  230. // @Router /question/reply/upload_audio [post]
  231. func QuestionUploadAudio(c *gin.Context) {
  232. file, err := c.FormFile("file")
  233. if err != nil {
  234. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  235. return
  236. }
  237. ext := path.Ext(file.Filename)
  238. if ext != ".mp3" {
  239. response.Fail("暂仅支持mp3格式", c)
  240. return
  241. }
  242. dateDir := time.Now().Format("20060102")
  243. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  244. if err := os.MkdirAll(localDir, 0766); err != nil {
  245. response.FailMsg("存储目录创建失败", "QuestionUploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  246. return
  247. }
  248. randStr := utils.GetRandStringNoSpecialChar(28)
  249. filtName := randStr + ext
  250. fpath := localDir + "/" + filtName
  251. defer func() {
  252. _ = os.Remove(fpath)
  253. }()
  254. // 生成文件至指定目录
  255. if err := c.SaveUploadedFile(file, fpath); err != nil {
  256. response.FailMsg("文件生成失败", "QuestionUploadAudio 文件生成失败, Err:"+err.Error(), c)
  257. return
  258. }
  259. // 获取音频文件时长
  260. fByte, err := ioutil.ReadFile(fpath)
  261. if err != nil {
  262. response.FailMsg("读取本地文件失败", "QuestionUploadAudio 读取本地文件失败", c)
  263. return
  264. }
  265. if len(fByte) <= 0 {
  266. response.FailMsg("文件大小有误", "QuestionUploadAudio 文件大小有误", c)
  267. return
  268. }
  269. seconds, err := services.GetMP3PlayDuration(fByte)
  270. if err != nil {
  271. response.FailMsg("读取文件时长失败", "QuestionUploadAudio 读取文件时长失败", c)
  272. return
  273. }
  274. // 音频大小MB
  275. fi, err := os.Stat(fpath)
  276. if err != nil {
  277. response.FailMsg("读取文件大小失败", "QuestionUploadAudio 读取文件大小失败", c)
  278. return
  279. }
  280. mb := utils.Bit2MB(fi.Size(), 2)
  281. // 上传文件至阿里云
  282. ossDir := "yb_wx/community_question_audio/"
  283. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  284. if err != nil {
  285. response.FailMsg("文件上传失败", "QuestionUploadAudio 文件上传失败, Err:"+err.Error(), c)
  286. return
  287. }
  288. resp := &respond.CommunityQuestionAudioUpload{
  289. AudioURL: resourceUrl,
  290. AudioPlaySeconds: fmt.Sprint(seconds),
  291. AudioSize: fmt.Sprint(mb),
  292. }
  293. // 记录文件
  294. userInfo := user.GetInfoByClaims(c)
  295. go func() {
  296. extendData := struct {
  297. FileName string
  298. AudioURL string
  299. AudioPlaySeconds string
  300. AudioSize string
  301. UserId int
  302. }{
  303. file.Filename,
  304. resourceUrl,
  305. fmt.Sprint(seconds),
  306. fmt.Sprint(mb),
  307. int(userInfo.UserID),
  308. }
  309. dataByte, e := json.Marshal(extendData)
  310. if e != nil {
  311. return
  312. }
  313. re := new(yb_resource.YbResource)
  314. re.ResourceUrl = resourceUrl
  315. re.ResourceType = yb_resource.ResourceTypeAudio
  316. re.ExtendData = string(dataByte)
  317. re.CreateTime = time.Now().Local()
  318. if e = re.Create(); e != nil {
  319. return
  320. }
  321. }()
  322. response.OkData("上传成功", resp, c)
  323. }
  324. // QuestionUnread 我的问答未读数
  325. // @Tags 问答社区模块
  326. // @Description 我的问答未读数
  327. // @Success 200 {int} int "获取成功"
  328. // @failure 400 {string} string "获取失败"
  329. // @Router /question/unread [get]
  330. func QuestionUnread(c *gin.Context) {
  331. userinfo := user.GetInfoByClaims(c)
  332. total, err := community.GetMyQuestionUnread(userinfo)
  333. if err != nil {
  334. fmt.Println(err.Error())
  335. response.FailMsg("获取失败", "QuestionUnread ErrMsg:"+err.Error(), c)
  336. return
  337. }
  338. response.OkData("获取成功", total, c)
  339. }
  340. // ResearchGroupList 研究方向分组列表
  341. // @Tags 问答社区模块
  342. // @Description 研究方向分组列表
  343. // @Success 200 {int} int "获取成功"
  344. // @failure 400 {string} string "获取失败"
  345. // @Router /question/research_group [get]
  346. func ResearchGroupList(c *gin.Context) {
  347. list, err := community.GetResearchGroupTree()
  348. if err != nil {
  349. response.FailMsg("获取失败", "ResearchGroupList ErrMsg:"+err.Error(), c)
  350. return
  351. }
  352. response.OkData("获取成功", list, c)
  353. }
  354. // AddAudioLog 添加用户点击音频日志
  355. // @Tags 问答社区模块
  356. // @Description 添加用户点击音频日志
  357. // @Param community_question_audio_id query int true "音频ID"
  358. // @Param source_agent query int true "操作来源,1:小程序,2:小程序 pc 3:弘则研究公众号,4:web pc"
  359. // @Success 200 {string} string "操作成功"
  360. // @failure 400 {string} string "操作失败"
  361. // @Router /AddAudioLog [post]
  362. func AddAudioLog(c *gin.Context) {
  363. var req request.CommunityAudioListenLogReq
  364. if err := c.ShouldBind(&req); err != nil {
  365. response.Fail("参数有误", c)
  366. return
  367. }
  368. if req.CommunityQuestionAudioID == 0 {
  369. response.Fail("请输入音频ID", c)
  370. return
  371. }
  372. if req.SourceAgent == 0 {
  373. response.Fail("请输入操作来源", c)
  374. return
  375. }
  376. userinfo := user.GetInfoByClaims(c)
  377. newId, err := community.AddAudioListenLog(userinfo, req.CommunityQuestionAudioID, req.SourceAgent)
  378. if err != nil {
  379. response.Fail("操作失败: "+err.Error(), c)
  380. return
  381. }
  382. response.OkData("操作成功", newId, c)
  383. }
  384. // SetLikeOrTease 问答点赞/吐槽设置
  385. func SetLikeOrTease(c *gin.Context) {
  386. var req request.ReqCommunityQuestionLikeTease
  387. if c.ShouldBind(&req) != nil {
  388. response.Fail("参数异常", c)
  389. return
  390. }
  391. userinfo := userService.GetInfoByClaims(c)
  392. // 不传来源默认为问答社区
  393. if req.Source == 0 {
  394. req.Source = 1
  395. }
  396. communityQuestionLikeTease, likeNum, teaseNum, err, errMsg := yb_community_question.SetLikeOrTease(userinfo.UserID, req.CommunityQuestionId, req.OpType, req.Enable, req.SourceAgent, req.Source)
  397. if err != nil {
  398. response.FailMsg(errMsg, err.Error(), c)
  399. return
  400. }
  401. response.OkData("操作成功", responseModel.RespCommunityQuestionLikeTease{
  402. LikeTotal: likeNum,
  403. TeaseTotal: teaseNum,
  404. OpType: communityQuestionLikeTease.OpType,
  405. Enabled: communityQuestionLikeTease.Enabled,
  406. }, c)
  407. return
  408. }
  409. // QuestionTransfer 转移问答
  410. // @Tags 问答社区模块
  411. // @Description 转移问答
  412. // @Param request body request.QuestionTransferReq true "type json string"
  413. // @Success 200 {string} string "操作成功"
  414. // @Failure 400 {string} string "操作失败"
  415. // @Router /question/transfer [post]
  416. func QuestionTransfer(c *gin.Context) {
  417. var req request.QuestionTransferReq
  418. if err := c.ShouldBind(&req); err != nil {
  419. response.Fail("参数有误", c)
  420. return
  421. }
  422. if req.CommunityQuestionID <= 0 {
  423. response.Fail("参数有误", c)
  424. return
  425. }
  426. if req.VarietyClassifyId <= 0 {
  427. response.Fail("请选择品种分类", c)
  428. return
  429. }
  430. if req.VarietyTagId <= 0 {
  431. response.Fail("请选择品种", c)
  432. return
  433. }
  434. if req.AdminId <= 0 {
  435. response.Fail("请选择研究员", c)
  436. return
  437. }
  438. userInfo := user.GetInfoByClaims(c)
  439. isInner, isResearcher, _, adminInfo, e := userLogic.GetUserIdentity(int(userInfo.CompanyID), userInfo.Mobile, userInfo.Email)
  440. if e != nil {
  441. response.FailMsg("操作失败", "QuestionTransfer ErrMsg:"+e.Error(), c)
  442. return
  443. }
  444. if isInner == 0 || isResearcher == 0 {
  445. response.Fail("无权操作", c)
  446. return
  447. }
  448. errMsg, e := community.TransferQuestion(req.CommunityQuestionID, int(userInfo.UserID), int(adminInfo.AdminID), req.VarietyClassifyId, req.VarietyTagId, req.AdminId, adminInfo.RealName)
  449. if e != nil {
  450. response.FailMsg(errMsg, "QuestionTransfer ErrMsg:"+e.Error(), c)
  451. return
  452. }
  453. response.Ok("操作成功", c)
  454. }
  455. // QuestionStop 终止问答
  456. // @Tags 问答社区模块
  457. // @Description 终止问答
  458. // @Param request body request.QuestionStopReq true "type json string"
  459. // @Success 200 {string} string "操作成功"
  460. // @failure 400 {string} string "操作失败"
  461. // @Router /question/stop [post]
  462. func QuestionStop(c *gin.Context) {
  463. // 是否为备用小程序
  464. copyYb := c.Request.Header.Get("CopyYb")
  465. var req request.QuestionStopReq
  466. if err := c.ShouldBind(&req); err != nil {
  467. response.Fail("参数有误", c)
  468. return
  469. }
  470. if req.CommunityQuestionID <= 0 {
  471. response.Fail("参数有误", c)
  472. return
  473. }
  474. if req.Reason == "" {
  475. response.Fail("请输入终止原因", c)
  476. return
  477. }
  478. // 敏感词校验, 只有小程序的用户才能走敏感词过滤接口
  479. userinfo := user.GetInfoByClaims(c)
  480. if userinfo.RecordInfo.OpenID != "" && userinfo.RecordInfo.CreatePlatform == 6 {
  481. checkResult, e := wx_app.MsgSecCheck(userinfo.RecordInfo.OpenID, req.Reason, copyYb)
  482. if e == nil {
  483. if checkResult.Result != nil && checkResult.Result.Suggest != "pass" {
  484. errMsg := "含有违禁词, 不允许发布: " + checkResult.Result.Suggest + ", 命中标签: " + strconv.Itoa(checkResult.Result.Label)
  485. response.FailMsg("含有违禁词, 不允许发布", errMsg, c)
  486. return
  487. }
  488. }
  489. }
  490. userInfo := user.GetInfoByClaims(c)
  491. isInner, isResearcher, _, _, e := userLogic.GetUserIdentity(int(userInfo.CompanyID), userInfo.Mobile, userInfo.Email)
  492. if e != nil {
  493. response.FailMsg("操作失败", "QuestionTransfer ErrMsg:"+e.Error(), c)
  494. return
  495. }
  496. if isInner == 0 || isResearcher == 0 {
  497. response.Fail("无权操作", c)
  498. return
  499. }
  500. errMsg, e := community.StopQuestion(req.CommunityQuestionID, int(userInfo.UserID), req.Reason)
  501. if e != nil {
  502. response.FailMsg(errMsg, "QuestionStop ErrMsg:"+e.Error(), c)
  503. return
  504. }
  505. response.Ok("操作成功", c)
  506. }