public.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package controller
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "hongze/hongze_yb/controller/response"
  8. "hongze/hongze_yb/global"
  9. "hongze/hongze_yb/logic"
  10. "hongze/hongze_yb/models/request"
  11. respond "hongze/hongze_yb/models/response"
  12. "hongze/hongze_yb/models/tables/banner"
  13. "hongze/hongze_yb/models/tables/banner_view_history"
  14. "hongze/hongze_yb/models/tables/company"
  15. "hongze/hongze_yb/models/tables/wx_user"
  16. "hongze/hongze_yb/models/tables/yb_config"
  17. "hongze/hongze_yb/models/tables/yb_resource"
  18. "hongze/hongze_yb/models/tables/yb_suncode_pars"
  19. "hongze/hongze_yb/services"
  20. "hongze/hongze_yb/services/alarm_msg"
  21. "hongze/hongze_yb/services/user"
  22. "hongze/hongze_yb/utils"
  23. "io/ioutil"
  24. "os"
  25. "path"
  26. "strconv"
  27. "time"
  28. )
  29. // GetApplyVarietyList
  30. // @Tags 公共模块
  31. // @Summary 获取所有可以申请的品种权限列表
  32. // @Description 获取所有可以申请的品种权限列表
  33. // @Security ApiKeyAuth
  34. // @securityDefinitions.basic BasicAuth
  35. // @Param Authorization header string true "微信登录后获取到的token"
  36. // @Accept json
  37. // @Product json
  38. // @Success 200 {object} []logic.ApplyVariety "获取成功"
  39. // @failure 400 {string} string "获取失败"
  40. // @Router /public/get_apply_variety_list [get]
  41. func GetApplyVarietyList(c *gin.Context) {
  42. list, err := logic.GetApplyVarietyList()
  43. if err != nil {
  44. response.FailData("获取品种失败", "获取品种失败,Err:"+err.Error(), c)
  45. return
  46. }
  47. response.OkData("获取成功", list, c)
  48. }
  49. // Upload
  50. // @Tags 公共模块
  51. // @Summary 文件上传
  52. // @Description 文件上传
  53. // @Security ApiKeyAuth
  54. // @securityDefinitions.basic BasicAuth
  55. // @Param Authorization header string true "微信登录后获取到的token"
  56. // @Param file formData file false "操作描述"
  57. // @Accept multipart/form-data
  58. // @Product json
  59. // @Success 200 {object} string "上传成功"
  60. // @failure 400 {string} string "上传失败,存储目录创建失败"
  61. // @Router /public/upload [post]
  62. func Upload(c *gin.Context) {
  63. // 单文件
  64. file, err := c.FormFile("file")
  65. fmt.Println("file", file)
  66. if err != nil {
  67. response.FailData("获取资源失败", "获取资源失败,Err:"+err.Error(), c)
  68. return
  69. }
  70. ext := path.Ext(file.Filename)
  71. dateDir := time.Now().Format("20060102")
  72. uploadDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  73. err = os.MkdirAll(uploadDir, 0766)
  74. if err != nil {
  75. response.FailData("存储目录创建失败", "存储目录创建失败,Err:"+err.Error(), c)
  76. return
  77. }
  78. randStr := utils.GetRandStringNoSpecialChar(28)
  79. fileName := randStr + ext
  80. fpath := uploadDir + "/" + fileName
  81. // 上传文件至指定目录
  82. err = c.SaveUploadedFile(file, fpath)
  83. if err != nil {
  84. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  85. return
  86. }
  87. defer func() {
  88. os.Remove(fpath)
  89. }()
  90. //上传到阿里云
  91. resourceUrl, err := services.UploadAliyun(fileName, fpath)
  92. if err != nil {
  93. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  94. return
  95. }
  96. response.OkData("文件上传成功", resourceUrl, c)
  97. }
  98. // GetSharePoster
  99. // @Tags 公共模块
  100. // @Summary 获取分享海报
  101. // @Description 获取分享海报
  102. // @Security ApiKeyAuth
  103. // @securityDefinitions.basic BasicAuth
  104. // @Param request body services.SharePosterReq true "type json string"
  105. // @Accept application/json
  106. // @Success 200 {object} string "获取成功"
  107. // @failure 400 {string} string "获取失败"
  108. // @Router /public/get_share_poster [post]
  109. func GetSharePoster(c *gin.Context) {
  110. // 是否为备用小程序
  111. copyYb := c.Request.Header.Get("CopyYb")
  112. var req services.SharePosterReq
  113. if c.ShouldBind(&req) != nil {
  114. response.Fail("参数异常", c)
  115. return
  116. }
  117. if req.Source == "" {
  118. response.Fail("来源有误", c)
  119. return
  120. }
  121. imgUrl, err := services.CreatePosterFromSourceV2(req.CodePage, req.CodeScene, req.Source, req.Version, req.Pars, copyYb)
  122. if err != nil {
  123. response.FailData("获取分享海报失败", "获取分享海报失败, Err: "+err.Error(), c)
  124. return
  125. }
  126. response.OkData("获取成功", imgUrl, c)
  127. }
  128. // GetSuncodeScene 获取小程序太阳码scene值
  129. // @Tags 公共模块
  130. // @Summary 获取小程序太阳码scene值
  131. // @Description 获取小程序太阳码scene值
  132. // @Security ApiKeyAuth
  133. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  134. // @Param scene_key query string true "scene_key值"
  135. // @Success 200 {string} string "获取成功"
  136. // @failure 400 {string} string "获取失败"
  137. // @Router /public/get_suncode_scene [get]
  138. func GetSuncodeScene(c *gin.Context) {
  139. reqKey := c.DefaultQuery("scene_key", "")
  140. if reqKey == "" {
  141. response.Fail("参数有误", c)
  142. }
  143. pars, err := yb_suncode_pars.GetSceneByKey(reqKey)
  144. if err != nil && err != utils.ErrNoRow {
  145. response.FailMsg("获取失败", "GetSuncodeScene获取失败, Err: "+err.Error(), c)
  146. return
  147. }
  148. scene := ""
  149. if pars != nil {
  150. scene = pars.Scene
  151. }
  152. response.OkData("获取成功", scene, c)
  153. }
  154. // GetVarietyTagTree 标签树
  155. // @Tags 公共模块
  156. // @Summary 标签树
  157. // @Description 标签树
  158. // @Security ApiKeyAuth
  159. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  160. // @Param scene_key query string true "scene_key值"
  161. // @Success 200 {string} string "获取成功"
  162. // @failure 400 {string} string "获取失败"
  163. // @Router /public/get_variety_tag_tree [get]
  164. func GetVarietyTagTree(c *gin.Context) {
  165. questionId, _ := strconv.Atoi(c.Query("community_question_id"))
  166. list, err := services.GetTagTree(questionId)
  167. if err != nil {
  168. response.FailMsg("获取标签树失败", "获取标签树失败, Err: "+err.Error(), c)
  169. return
  170. }
  171. response.OkData("获取成功", list, c)
  172. }
  173. // WechatWarning 小程序前端预警提示
  174. // @Tags 公共模块
  175. // @Description 小程序前端预警提示
  176. // @Param content query string true "预警信息"
  177. // @Success 200 {string} string "操作成功"
  178. // @failure 400 {string} string "操作失败"
  179. // @Router /public/wechat_warning [post]
  180. func WechatWarning(c *gin.Context) {
  181. var req request.WechatWarningReq
  182. if err := c.Bind(&req); err != nil {
  183. response.Fail("参数有误", c)
  184. return
  185. }
  186. if req.Content != "" {
  187. tips := fmt.Sprintf("研报小程序前端报错预警-ErrMsg: %s", req.Content)
  188. global.LOG.Warning(tips)
  189. go alarm_msg.SendAlarmMsg(tips, 2)
  190. }
  191. response.Ok("操作成功", c)
  192. }
  193. // UploadAudio 上传音频文件
  194. // @Tags 公共模块
  195. // @Description 上传音频文件
  196. // @Param file query string true "音频文件"
  197. // @Success 200 {string} string "上传成功"
  198. // @failure 400 {string} string "上传失败"
  199. // @Router /public/upload_audio [post]
  200. func UploadAudio(c *gin.Context) {
  201. file, err := c.FormFile("file")
  202. if err != nil {
  203. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  204. return
  205. }
  206. ext := path.Ext(file.Filename)
  207. if ext != ".mp3" {
  208. response.Fail("暂仅支持mp3格式", c)
  209. return
  210. }
  211. dateDir := time.Now().Format("20060102")
  212. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  213. if err := os.MkdirAll(localDir, 0766); err != nil {
  214. response.FailMsg("存储目录创建失败", "UploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  215. return
  216. }
  217. randStr := utils.GetRandStringNoSpecialChar(28)
  218. filtName := randStr + ext
  219. fpath := localDir + "/" + filtName
  220. defer func() {
  221. _ = os.Remove(fpath)
  222. }()
  223. // 生成文件至指定目录
  224. if err := c.SaveUploadedFile(file, fpath); err != nil {
  225. response.FailMsg("文件生成失败", "UploadAudio 文件生成失败, Err:"+err.Error(), c)
  226. return
  227. }
  228. // 获取音频文件时长
  229. fByte, err := ioutil.ReadFile(fpath)
  230. if err != nil {
  231. response.FailMsg("读取本地文件失败", "UploadAudio 读取本地文件失败", c)
  232. return
  233. }
  234. if len(fByte) <= 0 {
  235. response.FailMsg("文件大小有误", "UploadAudio 文件大小有误", c)
  236. return
  237. }
  238. seconds, err := services.GetMP3PlayDuration(fByte)
  239. if err != nil {
  240. response.FailMsg("读取文件时长失败", "UploadAudio 读取文件时长失败", c)
  241. return
  242. }
  243. // 音频大小MB
  244. fi, err := os.Stat(fpath)
  245. if err != nil {
  246. response.FailMsg("读取文件大小失败", "UploadAudio 读取文件大小失败", c)
  247. return
  248. }
  249. mb := utils.Bit2MB(fi.Size(), 2)
  250. // 上传文件至阿里云
  251. ossDir := "yb_wx/audio/"
  252. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  253. if err != nil {
  254. response.FailMsg("文件上传失败", "UploadAudio 文件上传失败, Err:"+err.Error(), c)
  255. return
  256. }
  257. resp := &respond.CommunityQuestionAudioUpload{
  258. AudioURL: resourceUrl,
  259. AudioPlaySeconds: fmt.Sprint(seconds),
  260. AudioSize: fmt.Sprint(mb),
  261. }
  262. // 记录文件
  263. go func() {
  264. extendData := struct {
  265. FileName string
  266. AudioURL string
  267. AudioPlaySeconds string
  268. AudioSize string
  269. }{
  270. file.Filename,
  271. resourceUrl,
  272. fmt.Sprint(seconds),
  273. fmt.Sprint(mb),
  274. }
  275. dataByte, e := json.Marshal(extendData)
  276. if e != nil {
  277. return
  278. }
  279. re := new(yb_resource.YbResource)
  280. re.ResourceUrl = resourceUrl
  281. re.ResourceType = yb_resource.ResourceTypeAudio
  282. re.ExtendData = string(dataByte)
  283. re.CreateTime = time.Now().Local()
  284. if e = re.Create(); e != nil {
  285. return
  286. }
  287. }()
  288. response.OkData("上传成功", resp, c)
  289. }
  290. // UpdateViewLog
  291. // @Description 更新各模块访问/点击日志(有时间的话统一改版做一个入口,统一访问日志)
  292. // @Success 200 {string} string "更新成功"
  293. // @Router /public/view_log/update [post]
  294. func UpdateViewLog(c *gin.Context) {
  295. var req request.ViewLogUpdateReq
  296. if err := c.Bind(&req); err != nil {
  297. response.Fail("参数有误:"+err.Error(), c)
  298. return
  299. }
  300. if req.Id <= 0 {
  301. response.Fail("参数有误", c)
  302. return
  303. }
  304. if req.Source <= 0 {
  305. response.Fail("来源有误", c)
  306. return
  307. }
  308. userInfo := user.GetInfoByClaims(c)
  309. err := services.UpdateViewLogBySource(int(userInfo.UserID), req.Id, req.StopSeconds, req.Source)
  310. if err != nil {
  311. fmt.Println(err.Error())
  312. response.FailMsg("更新日志失败", err.Error(), c)
  313. return
  314. }
  315. response.Ok("更新成功", c)
  316. }
  317. // GetTelAreaList 获取手机号区号列表
  318. func GetTelAreaList(c *gin.Context) {
  319. type TelAreaList struct {
  320. Name string `json:"name"`
  321. Value string `json:"value"`
  322. }
  323. // 读取配置
  324. var cond string
  325. var pars []interface{}
  326. cond += `config_code = ?`
  327. pars = append(pars, yb_config.TelAreaList)
  328. confDao := new(yb_config.YbConfig)
  329. conf, e := confDao.Fetch(cond, pars)
  330. if e != nil {
  331. response.FailMsg("获取失败", "获取手机号区号配置失败, Err: "+e.Error(), c)
  332. return
  333. }
  334. if conf.ConfigID <= 0 || conf.ConfigValue == "" {
  335. response.FailMsg("获取失败", "获取手机号区号配置失败", c)
  336. return
  337. }
  338. respList := make([]*TelAreaList, 0)
  339. if e = json.Unmarshal([]byte(conf.ConfigValue), &respList); e != nil {
  340. response.FailMsg("获取失败", "手机号区号配置解析失败, Err: "+e.Error(), c)
  341. return
  342. }
  343. response.OkData("获取成功", respList, c)
  344. }
  345. // BannerMark banner图埋点
  346. // @Tags 公共模块
  347. // @Summary banner图埋点
  348. // @Description banner图埋点
  349. // @Security ApiKeyAuth
  350. // @securityDefinitions.basic BasicAuth
  351. // @Param email query string true "电子邮箱账号"
  352. // @Accept json
  353. // @Product json
  354. // @Success 200 {string} string 获取验证码成功
  355. // @Failure 400 {string} string 请输入邮箱地址
  356. // @Router /banner/mark [post]
  357. func BannerMark(c *gin.Context) {
  358. userInfo := user.GetInfoByClaims(c)
  359. var req request.BannerMarkReq
  360. if err := c.Bind(&req); err != nil {
  361. response.Fail("参数有误:"+err.Error(), c)
  362. return
  363. }
  364. //if req.BannerUrl == "" {
  365. // response.FailMsg("参数有误", "BannerUrl不能为空", c)
  366. // return
  367. //}
  368. if req.FirstSource <= 0 {
  369. response.FailMsg("参数有误", "FirstSource不能为空", c)
  370. return
  371. }
  372. if req.SecondSource <= 0 {
  373. response.FailMsg("参数有误", "SecondSource", c)
  374. }
  375. if req.Id <= 0 {
  376. response.FailMsg("参数有误", "Id错误", c)
  377. }
  378. item, err := banner.GetBannerById(req.Id)
  379. if err != nil {
  380. fmt.Println("GetByUserId:", err.Error())
  381. return
  382. }
  383. // 联系人信息
  384. strInt64 := strconv.FormatUint(userInfo.UserID, 10)
  385. id, _ := strconv.Atoi(strInt64)
  386. wxUserInfo, err := wx_user.GetByUserId(id)
  387. if err != nil {
  388. fmt.Println("GetByUserId:", err.Error())
  389. return
  390. }
  391. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  392. if tmpErr != nil {
  393. err = tmpErr
  394. if tmpErr == utils.ErrNoRow {
  395. err = errors.New("找不到该客户")
  396. return
  397. }
  398. return
  399. }
  400. //新增userViewHistory记录
  401. banner_view_history := &banner_view_history.BannerViewHistory{
  402. UserID: userInfo.UserID,
  403. Mobile: wxUserInfo.Mobile,
  404. Email: wxUserInfo.Email,
  405. RealName: wxUserInfo.RealName,
  406. CompanyName: companyInfo.CompanyName,
  407. CreatedTime: time.Now(),
  408. LastUpdatedTime: time.Now(),
  409. FirstSource: req.FirstSource,
  410. SecondSource: req.SecondSource,
  411. BannerUrl: item.ImageUrlMobile,
  412. StartDate: item.StartDate,
  413. EndDate: item.EndDate,
  414. Remark: item.Remark,
  415. }
  416. err = banner_view_history.AddBannerViewHistory()
  417. if err != nil {
  418. fmt.Println("AddUserViewHistory err", err.Error())
  419. }
  420. response.Ok("成功", c)
  421. }
  422. // BannerList banner图列表
  423. // @Tags 公共模块
  424. // @Summary banner图列表
  425. // @Description banner图列表
  426. // @Security ApiKeyAuth
  427. // @securityDefinitions.basic BasicAuth
  428. // @Accept json
  429. // @Product json
  430. // @Success 200 {string} string 获取验证码成功
  431. // @Failure 400 {string} string 请输入邮箱地址
  432. // @Router /banner/list [get]
  433. func BannerList(c *gin.Context) {
  434. isHomepage, _ := strconv.Atoi(c.Query("is_homepage"))
  435. page, _ := strconv.Atoi(c.Query("page"))
  436. limit, _ := strconv.Atoi(c.Query("limit"))
  437. cond := " enable = 1 "
  438. if isHomepage != 1 {
  439. cond += " AND id <> 9999"
  440. }
  441. list, err := banner.GetBannerList(cond, page, limit)
  442. if err != nil {
  443. response.FailMsg("获取失败", "获取banner失败, Err: "+err.Error(), c)
  444. return
  445. }
  446. response.OkData("获取成功", list, c)
  447. }
  448. // BannerHistoryList banner历史图列表
  449. // @Tags 公共模块
  450. // @Summary banner图列表
  451. // @Description banner图列表
  452. // @Security ApiKeyAuth
  453. // @securityDefinitions.basic BasicAuth
  454. // @Accept json
  455. // @Product json
  456. // @Success 200 {string} string 获取验证码成功
  457. // @Failure 400 {string} string 请输入邮箱地址
  458. // @Router /banner_history/list [get]
  459. func BannerHistoryList(c *gin.Context) {
  460. page, _ := strconv.Atoi(c.Query("page"))
  461. limit, _ := strconv.Atoi(c.Query("limit"))
  462. cond := ""
  463. cond += " enable = 0 "
  464. total, err := banner.GetBannerListCount(cond)
  465. if err != nil {
  466. response.FailMsg("获取失败", "获取banner总数失败, Err: "+err.Error(), c)
  467. return
  468. }
  469. list, err := banner.GetBannerList(cond, page, limit)
  470. if err != nil {
  471. response.FailMsg("获取失败", "获取banner失败, Err: "+err.Error(), c)
  472. return
  473. }
  474. var resp respond.BannerRespItem
  475. resp.Paging = respond.GetPaging(page, limit, int(total))
  476. resp.List = list
  477. response.OkData("获取成功", resp, c)
  478. }