activity.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package activity
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/models/tables/yb_activity"
  7. "hongze/hongze_yb/models/tables/yb_view_log"
  8. "hongze/hongze_yb/services/activity"
  9. "hongze/hongze_yb/services/user"
  10. "hongze/hongze_yb/services/view_log"
  11. "hongze/hongze_yb/utils"
  12. "io/ioutil"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // GetPageList 活动列表
  19. // @Tags 活动模块
  20. // @Summary 活动列表
  21. // @Description 获取活动列表
  22. // @Security ApiKeyAuth
  23. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  24. // @Accept json
  25. // @Product json
  26. // @Param title query string false "活动类别/标题"
  27. // @Param active_state query int false "活动状态 1-本周预告 2-进行中 3-已结束"
  28. // @Param activity_type query int false "活动类型 1-线上会议 3-线下沙龙"
  29. // @Param has_permission query int false "是否查看仅有权限的 0-否 1-是"
  30. // @Param page query int false "当前页码"
  31. // @Param limit query int false "每页数量"
  32. // @Success 200 {object} []activity.ActivityList
  33. // @failure 400 {string} string "活动获取失败"
  34. // @Router /activity/getPageList [get]
  35. func GetPageList(c *gin.Context) {
  36. page, _ := strconv.Atoi(c.Query("page"))
  37. limit, _ := strconv.Atoi(c.Query("limit"))
  38. userInfo := user.GetInfoByClaims(c)
  39. condition, pars, order := _handleListQuery(c, userInfo)
  40. listData, err := activity.PageList(condition, pars, page, limit, order, userInfo)
  41. if err != nil {
  42. response.Fail("活动获取失败", c)
  43. return
  44. }
  45. response.OkData("获取成功", listData, c)
  46. }
  47. func _handleListQuery(c *gin.Context, userInfo user.UserInfo) (string, []interface{}, string) {
  48. condition := "is_delete = 0 AND publish_status = 1"
  49. pars := make([]interface{}, 0)
  50. // 活动类别/标题
  51. reqTitle := c.DefaultQuery("title", "")
  52. if reqTitle != "" {
  53. condition += ` AND (activity_type_name LIKE ? OR activity_name LIKE ?)`
  54. pars = append(pars, "%" + reqTitle + "%")
  55. pars = append(pars, "%" + reqTitle + "%")
  56. }
  57. order := ""
  58. reqState := c.DefaultQuery("active_state", "0")
  59. if reqState != "0" {
  60. state, _ := strconv.Atoi(reqState)
  61. nowTime := time.Now().Format(utils.FormatDateTime)
  62. switch state {
  63. case 1:
  64. condition += ` AND start_time > ?`
  65. pars = append(pars, nowTime)
  66. case 2:
  67. condition += ` AND start_time <= ?`
  68. pars = append(pars, nowTime)
  69. condition += ` AND end_time > ?`
  70. pars = append(pars, nowTime)
  71. case 3:
  72. condition += ` AND end_time < ?`
  73. pars = append(pars, nowTime)
  74. order = "start_time desc"
  75. }
  76. }
  77. reqType := c.DefaultQuery("activity_type", "0")
  78. if reqType != "0" {
  79. activityTypeId, _ := strconv.Atoi(reqType)
  80. condition += ` AND first_activity_type_id = ?`
  81. pars = append(pars, activityTypeId)
  82. }
  83. // 是否仅查看有权限的
  84. reqHasPermission := c.DefaultQuery("has_permission", "0")
  85. if reqHasPermission != "0" {
  86. activityIds, _ := activity.GetUserAuthActivityIds(userInfo)
  87. condition += ` AND activity_id IN (?)`
  88. pars = append(pars, activityIds)
  89. }
  90. return condition, pars, order
  91. }
  92. // GetActivityDetail 获取活动详情
  93. // @Tags 活动模块
  94. // @Summary 获取活动详情
  95. // @Description 获取活动详情
  96. // @Security ApiKeyAuth
  97. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  98. // @Accept json
  99. // @Product json
  100. // @Param activity_id query int true "活动ID"
  101. // @Success 200 {object} yb_activity.ActivityDetail
  102. // @failure 400 {string} string "获取失败"
  103. // @Router /activity/getActivityDetail [get]
  104. func GetActivityDetail(c *gin.Context) {
  105. reqActivityId := c.DefaultQuery("activity_id", "0")
  106. if reqActivityId == "0" {
  107. response.Fail("参数异常", c)
  108. return
  109. }
  110. activityId, _ := strconv.Atoi(reqActivityId)
  111. userInfo := user.GetInfoByClaims(c)
  112. ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, activityId)
  113. if !ok {
  114. response.AuthError(permissionCheckInfo, "暂无权限", c)
  115. return
  116. }
  117. data, err := activity.GetActivityDetail(activityId, int(userInfo.UserID))
  118. if err != nil {
  119. response.Fail("未找到记录", c)
  120. return
  121. }
  122. response.OkData("获取成功", data, c)
  123. }
  124. // GetActivityVoices 获取活动录音
  125. // @Tags 活动模块
  126. // @Summary 获取活动录音
  127. // @Description 获取活动录音
  128. // @Security ApiKeyAuth
  129. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  130. // @Accept json
  131. // @Product json
  132. // @Param activity_id query int true "活动ID"
  133. // @Success 200 {object} []yb_activity_voice.YbActivityVoice
  134. // @failure 400 {string} string "获取失败"
  135. // @Router /activity/getActivityVoices [get]
  136. func GetActivityVoices(c *gin.Context) {
  137. reqActivityId := c.DefaultQuery("activity_id", "0")
  138. if reqActivityId == "0" {
  139. response.Fail("参数异常", c)
  140. return
  141. }
  142. activityId, _ := strconv.Atoi(reqActivityId)
  143. userInfo := user.GetInfoByClaims(c)
  144. ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, activityId)
  145. if !ok {
  146. response.AuthError(permissionCheckInfo, "暂无权限", c)
  147. return
  148. }
  149. listData, err := activity.GetVoicesByActivity(activityId)
  150. if err != nil {
  151. response.Fail("录音获取失败", c)
  152. return
  153. }
  154. response.OkData("获取成功", listData, c)
  155. }
  156. // GetActivityShareImg 生成活动分享图片
  157. // @Tags 活动模块
  158. // @Summary 生成活动分享图片
  159. // @Description 生成活动分享图片
  160. // @Security ApiKeyAuth
  161. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  162. // @Accept json
  163. // @Product json
  164. // @Param activity_id query int true "活动ID"
  165. // @Success 200 {string} string "获取成功"
  166. // @failure 400 {string} string "获取失败"
  167. // @Router /activity/getActivityShareImg [get]
  168. func GetActivityShareImg(c *gin.Context) {
  169. reqActivityId := c.DefaultQuery("activity_id", "0")
  170. activityId, _ := strconv.Atoi(reqActivityId)
  171. if activityId <= 0 {
  172. response.Fail("参数异常", c)
  173. return
  174. }
  175. activityInfo, err := yb_activity.GetDetailById(activityId)
  176. if err != nil {
  177. if err == utils.ErrNoRow {
  178. response.Fail("获取活动信息失败", c)
  179. }
  180. response.FailMsg("获取活动信息失败", "GetActivityShareImg: 获取活动信息失败" + err.Error(), c)
  181. return
  182. }
  183. if activityInfo == nil {
  184. response.FailMsg("获取活动信息失败", "GetActivityShareImg: 活动不存在", c)
  185. return
  186. }
  187. // 时间处理
  188. activityDate := activityInfo.StartTime.Format("2006-01-02")
  189. activityStart := activityInfo.StartTime.Format("15:04")
  190. activityEnd := activityInfo.EndTime.Format("15:04")
  191. activityWeek := activityInfo.StartTime.Weekday().String()
  192. var WeekDayMap = map[string]string{
  193. "Monday": "星期一",
  194. "Tuesday": "星期二",
  195. "Wednesday": "星期三",
  196. "Thursday": "星期四",
  197. "Friday": "星期五",
  198. "Saturday": "星期六",
  199. "Sunday": "星期日",
  200. }
  201. weekStr := WeekDayMap[activityWeek]
  202. dateStr := "会议时间:" + activityDate
  203. timeStr := activityStart + "-" + activityEnd + " " + weekStr
  204. // 获取原分享图
  205. originShareUrl := "static/img/report_share.png"
  206. if activityInfo.FirstActivityTypeID == 3 {
  207. originShareUrl = "static/img/report_share_sl.png"
  208. }
  209. fp, err := os.OpenFile(originShareUrl, os.O_CREATE|os.O_APPEND, 6)
  210. if err != nil {
  211. response.Fail("读取封面图失败", c)
  212. return
  213. }
  214. defer fp.Close()
  215. bytes, err := ioutil.ReadAll(fp)
  216. if err != nil {
  217. response.Fail("读取封面图失败", c)
  218. return
  219. }
  220. // 处理文字x轴y轴点
  221. drawText := activityInfo.ActivityTypeName
  222. fontSize := 42
  223. fontWidth := 50
  224. x, y, newSize, err := calcuDrawXandY(drawText, fontSize, 500, 400)
  225. if err != nil {
  226. response.Fail("生成新封面图失败", c)
  227. return
  228. }
  229. // 生成新分享图
  230. var drawInfo []*utils.DrawTextInfo
  231. text := &utils.DrawTextInfo{
  232. Text: drawText, X: x, Y: y - 50, FontSize: fontSize,
  233. }
  234. textDate := &utils.DrawTextInfo{
  235. Text: dateStr, X: 100, Y: y + 15, FontSize: 28,
  236. }
  237. textTime := &utils.DrawTextInfo{
  238. Text: timeStr, X: 120, Y: y + 75, FontSize: 28,
  239. }
  240. drawInfo = append(drawInfo, text, textDate, textTime)
  241. var colorRGBA = utils.FontRGBA{
  242. R: 255, G: 255, B: 255, A: 255,
  243. }
  244. picByte, err := utils.DrawStringRowsOnImage(bytes, drawInfo, colorRGBA, float64(newSize), fontWidth)
  245. if err != nil {
  246. response.Fail("生成新封面图失败", c)
  247. return
  248. }
  249. c.Header("Content-Type", "image/png")
  250. c.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", "图片地址"))
  251. c.Writer.WriteString(picByte.String())
  252. }
  253. // calcuDrawXandY 计算写入文字的x轴和y轴
  254. func calcuDrawXandY(drawText string, fontSize, canvasLength, canvasWidth int) (x, y, newSize int, err error) {
  255. // y轴取画布宽度的一半
  256. y = canvasWidth / 2
  257. // 拆分文字计算总宽度
  258. lenText := 0
  259. strArr := strings.Split(drawText, "")
  260. for _, str := range strArr {
  261. if len(str) == 1 {
  262. // 将字母符号算作2个长度
  263. lenText += 2
  264. } else {
  265. lenText += 3
  266. }
  267. }
  268. textUnit := float64(lenText) / 3
  269. textWidth := int(textUnit * float64(fontSize))
  270. // x轴上的起点位置
  271. newSize = fontSize
  272. doubleX := canvasLength - textWidth
  273. if doubleX < 50 {
  274. // 文字总宽太长则调整文字大小
  275. maxTextWidth := float64(canvasLength - 50)
  276. newSize = int(maxTextWidth / textUnit)
  277. x = 25
  278. } else {
  279. x = doubleX / 2
  280. }
  281. return
  282. }
  283. // AddStatistics
  284. // @Description 新增语音点击记录
  285. // @Success 200 {string} string "新增成功"
  286. // @Router /activity/voice_log/add [post]
  287. func AddActivityAudioLog(c *gin.Context) {
  288. var req yb_view_log.ActivityAudioLogAddReq
  289. if err := c.Bind(&req); err != nil {
  290. response.Fail("参数有误:"+err.Error(), c)
  291. return
  292. }
  293. if req.PrimaryId == 0 {
  294. response.Fail("请输入音频ID", c)
  295. return
  296. }
  297. if req.ExtendId == 0 {
  298. response.Fail("请输入活动ID", c)
  299. return
  300. }
  301. if req.Source == 0 {
  302. response.Fail("请输入操作来源", c)
  303. return
  304. }
  305. userinfo := user.GetInfoByClaims(c)
  306. data, err := view_log.AddActivityAudioLog(userinfo, req)
  307. if err != nil {
  308. response.FailMsg("添加日志失败", err.Error(), c)
  309. return
  310. }
  311. response.OkData("新增记录成功", data, c)
  312. }
  313. // AddStatistics
  314. // @Description 新增语音点击记录
  315. // @Success 200 {string} string "新增成功"
  316. // @Router /activity/voice_log/update [post]
  317. func UpdateActivityAudioLog(c *gin.Context) {
  318. var req yb_view_log.ActivityAudioLogStopReq
  319. if err := c.Bind(&req); err != nil {
  320. response.Fail("参数有误:"+err.Error(), c)
  321. return
  322. }
  323. if req.Id == 0 {
  324. response.Fail("请输入日志ID", c)
  325. return
  326. }
  327. userinfo := user.GetInfoByClaims(c)
  328. err := view_log.UpdateActivityAudioLog(userinfo, req)
  329. if err != nil {
  330. response.FailMsg("更新日志失败", err.Error(), c)
  331. return
  332. }
  333. response.Ok("更新日志成功", c)
  334. }