public.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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_research_banner"
  18. "hongze/hongze_yb/models/tables/yb_research_signup_statistics"
  19. "hongze/hongze_yb/models/tables/yb_resource"
  20. "hongze/hongze_yb/models/tables/yb_suncode_pars"
  21. "hongze/hongze_yb/services"
  22. "hongze/hongze_yb/services/alarm_msg"
  23. "hongze/hongze_yb/services/user"
  24. "hongze/hongze_yb/services/wx_app"
  25. "hongze/hongze_yb/utils"
  26. "net/url"
  27. "os"
  28. "path"
  29. "strconv"
  30. "time"
  31. )
  32. // GetApplyVarietyList
  33. // @Tags 公共模块
  34. // @Summary 获取所有可以申请的品种权限列表
  35. // @Description 获取所有可以申请的品种权限列表
  36. // @Security ApiKeyAuth
  37. // @securityDefinitions.basic BasicAuth
  38. // @Param Authorization header string true "微信登录后获取到的token"
  39. // @Accept json
  40. // @Product json
  41. // @Success 200 {object} []logic.ApplyVariety "获取成功"
  42. // @failure 400 {string} string "获取失败"
  43. // @Router /public/get_apply_variety_list [get]
  44. func GetApplyVarietyList(c *gin.Context) {
  45. list, err := logic.GetApplyVarietyList()
  46. if err != nil {
  47. response.FailData("获取品种失败", "获取品种失败,Err:"+err.Error(), c)
  48. return
  49. }
  50. response.OkData("获取成功", list, c)
  51. }
  52. // Upload
  53. // @Tags 公共模块
  54. // @Summary 文件上传
  55. // @Description 文件上传
  56. // @Security ApiKeyAuth
  57. // @securityDefinitions.basic BasicAuth
  58. // @Param Authorization header string true "微信登录后获取到的token"
  59. // @Param file formData file false "操作描述"
  60. // @Accept multipart/form-data
  61. // @Product json
  62. // @Success 200 {object} string "上传成功"
  63. // @failure 400 {string} string "上传失败,存储目录创建失败"
  64. // @Router /public/upload [post]
  65. func Upload(c *gin.Context) {
  66. // 单文件
  67. file, err := c.FormFile("file")
  68. fmt.Println("file", file)
  69. if err != nil {
  70. response.FailData("获取资源失败", "获取资源失败,Err:"+err.Error(), c)
  71. return
  72. }
  73. ext := path.Ext(file.Filename)
  74. dateDir := time.Now().Format("20060102")
  75. uploadDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  76. err = os.MkdirAll(uploadDir, 0766)
  77. if err != nil {
  78. response.FailData("存储目录创建失败", "存储目录创建失败,Err:"+err.Error(), c)
  79. return
  80. }
  81. randStr := utils.GetRandStringNoSpecialChar(28)
  82. fileName := randStr + ext
  83. fpath := uploadDir + "/" + fileName
  84. // 上传文件至指定目录
  85. err = c.SaveUploadedFile(file, fpath)
  86. if err != nil {
  87. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  88. return
  89. }
  90. defer func() {
  91. os.Remove(fpath)
  92. }()
  93. //上传到阿里云
  94. resourceUrl, err := services.UploadAliyun(fileName, fpath)
  95. if err != nil {
  96. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  97. return
  98. }
  99. response.OkData("文件上传成功", resourceUrl, c)
  100. }
  101. // GetSharePoster
  102. // @Tags 公共模块
  103. // @Summary 获取分享海报
  104. // @Description 获取分享海报
  105. // @Security ApiKeyAuth
  106. // @securityDefinitions.basic BasicAuth
  107. // @Param request body services.SharePosterReq true "type json string"
  108. // @Accept application/json
  109. // @Success 200 {object} string "获取成功"
  110. // @failure 400 {string} string "获取失败"
  111. // @Router /public/get_share_poster [post]
  112. func GetSharePoster(c *gin.Context) {
  113. // 是否为备用小程序
  114. copyYb := c.Request.Header.Get("CopyYb")
  115. var req services.SharePosterReq
  116. if c.ShouldBind(&req) != nil {
  117. response.Fail("参数异常", c)
  118. return
  119. }
  120. if req.Source == "" {
  121. response.Fail("来源有误", c)
  122. return
  123. }
  124. imgUrl, err := services.CreatePosterFromSourceV2(req.CodePage, req.CodeScene, req.Source, req.Version, req.Pars, copyYb)
  125. if err != nil {
  126. response.FailData("获取分享海报失败", "获取分享海报失败, Err: "+err.Error(), c)
  127. return
  128. }
  129. response.OkData("获取成功", imgUrl, c)
  130. }
  131. // GetSuncodeScene 获取小程序太阳码scene值
  132. // @Tags 公共模块
  133. // @Summary 获取小程序太阳码scene值
  134. // @Description 获取小程序太阳码scene值
  135. // @Security ApiKeyAuth
  136. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  137. // @Param scene_key query string true "scene_key值"
  138. // @Success 200 {string} string "获取成功"
  139. // @failure 400 {string} string "获取失败"
  140. // @Router /public/get_suncode_scene [get]
  141. func GetSuncodeScene(c *gin.Context) {
  142. reqKey := c.DefaultQuery("scene_key", "")
  143. if reqKey == "" {
  144. response.Fail("参数有误", c)
  145. }
  146. pars, err := yb_suncode_pars.GetSceneByKey(reqKey)
  147. if err != nil && err != utils.ErrNoRow {
  148. response.FailMsg("获取失败", "GetSuncodeScene获取失败, Err: "+err.Error(), c)
  149. return
  150. }
  151. scene := ""
  152. if pars != nil {
  153. scene = pars.Scene
  154. }
  155. response.OkData("获取成功", scene, c)
  156. }
  157. // GetVarietyTagTree 标签树
  158. // @Tags 公共模块
  159. // @Summary 标签树
  160. // @Description 标签树
  161. // @Security ApiKeyAuth
  162. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  163. // @Param scene_key query string true "scene_key值"
  164. // @Success 200 {string} string "获取成功"
  165. // @failure 400 {string} string "获取失败"
  166. // @Router /public/get_variety_tag_tree [get]
  167. func GetVarietyTagTree(c *gin.Context) {
  168. questionId, _ := strconv.Atoi(c.Query("community_question_id"))
  169. list, err := services.GetTagTree(questionId)
  170. if err != nil {
  171. response.FailMsg("获取标签树失败", "获取标签树失败, Err: "+err.Error(), c)
  172. return
  173. }
  174. response.OkData("获取成功", list, c)
  175. }
  176. // WechatWarning 小程序前端预警提示
  177. // @Tags 公共模块
  178. // @Description 小程序前端预警提示
  179. // @Param content query string true "预警信息"
  180. // @Success 200 {string} string "操作成功"
  181. // @failure 400 {string} string "操作失败"
  182. // @Router /public/wechat_warning [post]
  183. func WechatWarning(c *gin.Context) {
  184. var req request.WechatWarningReq
  185. if err := c.Bind(&req); err != nil {
  186. response.Fail("参数有误", c)
  187. return
  188. }
  189. if req.Content != "" {
  190. tips := fmt.Sprintf("研报小程序前端报错预警-ErrMsg: %s", req.Content)
  191. global.LOG.Warning(tips)
  192. go alarm_msg.SendAlarmMsg(tips, 2)
  193. }
  194. response.Ok("操作成功", c)
  195. }
  196. // UploadAudio 上传音频文件
  197. // @Tags 公共模块
  198. // @Description 上传音频文件
  199. // @Param file query string true "音频文件"
  200. // @Success 200 {string} string "上传成功"
  201. // @failure 400 {string} string "上传失败"
  202. // @Router /public/upload_audio [post]
  203. func UploadAudio(c *gin.Context) {
  204. file, err := c.FormFile("file")
  205. if err != nil {
  206. response.FailMsg("获取资源失败", "获取资源失败, Err:"+err.Error(), c)
  207. return
  208. }
  209. ext := path.Ext(file.Filename)
  210. if ext != ".mp3" {
  211. response.Fail("暂仅支持mp3格式", c)
  212. return
  213. }
  214. dateDir := time.Now().Format("20060102")
  215. localDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  216. if err := os.MkdirAll(localDir, 0766); err != nil {
  217. response.FailMsg("存储目录创建失败", "UploadAudio 存储目录创建失败, Err:"+err.Error(), c)
  218. return
  219. }
  220. randStr := utils.GetRandStringNoSpecialChar(28)
  221. filtName := randStr + ext
  222. fpath := localDir + "/" + filtName
  223. defer func() {
  224. _ = os.Remove(fpath)
  225. }()
  226. // 生成文件至指定目录
  227. if err := c.SaveUploadedFile(file, fpath); err != nil {
  228. response.FailMsg("文件生成失败", "UploadAudio 文件生成失败, Err:"+err.Error(), c)
  229. return
  230. }
  231. //// 获取音频文件时长
  232. //fByte, err := ioutil.ReadFile(fpath)
  233. //if err != nil {
  234. // response.FailMsg("读取本地文件失败", "UploadAudio 读取本地文件失败", c)
  235. // return
  236. //}
  237. //if len(fByte) <= 0 {
  238. // response.FailMsg("文件大小有误", "UploadAudio 文件大小有误", c)
  239. // return
  240. //}
  241. seconds, err := utils.GetVideoPlaySeconds(fpath) //services.GetMP3PlayDuration(fByte)
  242. if err != nil {
  243. response.FailMsg("读取文件时长失败", "UploadAudio 读取文件时长失败", c)
  244. return
  245. }
  246. // 音频大小MB
  247. fi, err := os.Stat(fpath)
  248. if err != nil {
  249. response.FailMsg("读取文件大小失败", "UploadAudio 读取文件大小失败", c)
  250. return
  251. }
  252. mb := utils.Bit2MB(fi.Size(), 2)
  253. // 上传文件至阿里云
  254. ossDir := "yb_wx/audio/"
  255. resourceUrl, err := services.UploadAliyunToDir(filtName, fpath, ossDir)
  256. if err != nil {
  257. response.FailMsg("文件上传失败", "UploadAudio 文件上传失败, Err:"+err.Error(), c)
  258. return
  259. }
  260. resp := &respond.CommunityQuestionAudioUpload{
  261. AudioURL: resourceUrl,
  262. AudioPlaySeconds: fmt.Sprint(seconds),
  263. AudioSize: fmt.Sprint(mb),
  264. }
  265. // 记录文件
  266. go func() {
  267. extendData := struct {
  268. FileName string
  269. AudioURL string
  270. AudioPlaySeconds string
  271. AudioSize string
  272. }{
  273. file.Filename,
  274. resourceUrl,
  275. fmt.Sprint(seconds),
  276. fmt.Sprint(mb),
  277. }
  278. dataByte, e := json.Marshal(extendData)
  279. if e != nil {
  280. return
  281. }
  282. re := new(yb_resource.YbResource)
  283. re.ResourceUrl = resourceUrl
  284. re.ResourceType = yb_resource.ResourceTypeAudio
  285. re.ExtendData = string(dataByte)
  286. re.CreateTime = time.Now().Local()
  287. if e = re.Create(); e != nil {
  288. return
  289. }
  290. }()
  291. response.OkData("上传成功", resp, c)
  292. }
  293. // UpdateViewLog
  294. // @Description 更新各模块访问/点击日志(有时间的话统一改版做一个入口,统一访问日志)
  295. // @Success 200 {string} string "更新成功"
  296. // @Router /public/view_log/update [post]
  297. func UpdateViewLog(c *gin.Context) {
  298. var req request.ViewLogUpdateReq
  299. if err := c.Bind(&req); err != nil {
  300. response.Fail("参数有误:"+err.Error(), c)
  301. return
  302. }
  303. if req.Id <= 0 {
  304. response.Fail("参数有误", c)
  305. return
  306. }
  307. if req.Source <= 0 {
  308. response.Fail("来源有误", c)
  309. return
  310. }
  311. userInfo := user.GetInfoByClaims(c)
  312. err := services.UpdateViewLogBySource(int(userInfo.UserID), req.Id, req.StopSeconds, req.Source)
  313. if err != nil {
  314. fmt.Println(err.Error())
  315. response.FailMsg("更新日志失败", err.Error(), c)
  316. return
  317. }
  318. response.Ok("更新成功", c)
  319. }
  320. // GetTelAreaList 获取手机号区号列表
  321. func GetTelAreaList(c *gin.Context) {
  322. type TelAreaList struct {
  323. Name string `json:"name"`
  324. Value string `json:"value"`
  325. }
  326. // 读取配置
  327. var cond string
  328. var pars []interface{}
  329. cond += `config_code = ?`
  330. pars = append(pars, yb_config.TelAreaList)
  331. confDao := new(yb_config.YbConfig)
  332. conf, e := confDao.Fetch(cond, pars)
  333. if e != nil {
  334. response.FailMsg("获取失败", "获取手机号区号配置失败, Err: "+e.Error(), c)
  335. return
  336. }
  337. if conf.ConfigID <= 0 || conf.ConfigValue == "" {
  338. response.FailMsg("获取失败", "获取手机号区号配置失败", c)
  339. return
  340. }
  341. respList := make([]*TelAreaList, 0)
  342. if e = json.Unmarshal([]byte(conf.ConfigValue), &respList); e != nil {
  343. response.FailMsg("获取失败", "手机号区号配置解析失败, Err: "+e.Error(), c)
  344. return
  345. }
  346. response.OkData("获取成功", respList, c)
  347. }
  348. // BannerMark banner图埋点
  349. // @Tags 公共模块
  350. // @Summary banner图埋点
  351. // @Description banner图埋点
  352. // @Security ApiKeyAuth
  353. // @securityDefinitions.basic BasicAuth
  354. // @Param email query string true "电子邮箱账号"
  355. // @Accept json
  356. // @Product json
  357. // @Success 200 {string} string 获取验证码成功
  358. // @Failure 400 {string} string 请输入邮箱地址
  359. // @Router /banner/mark [post]
  360. func BannerMark(c *gin.Context) {
  361. userInfo := user.GetInfoByClaims(c)
  362. var req request.BannerMarkReq
  363. if err := c.Bind(&req); err != nil {
  364. response.Fail("参数有误:"+err.Error(), c)
  365. return
  366. }
  367. //if req.BannerUrl == "" {
  368. // response.FailMsg("参数有误", "BannerUrl不能为空", c)
  369. // return
  370. //}
  371. if req.FirstSource <= 0 {
  372. response.FailMsg("参数有误", "FirstSource不能为空", c)
  373. return
  374. }
  375. if req.SecondSource <= 0 {
  376. response.FailMsg("参数有误", "SecondSource", c)
  377. }
  378. if req.Id <= 0 {
  379. response.FailMsg("参数有误", "Id错误", c)
  380. }
  381. item, err := banner.GetBannerById(req.Id)
  382. if err != nil {
  383. fmt.Println("GetByUserId:", err.Error())
  384. return
  385. }
  386. // 联系人信息
  387. strInt64 := strconv.FormatUint(userInfo.UserID, 10)
  388. id, _ := strconv.Atoi(strInt64)
  389. wxUserInfo, err := wx_user.GetByUserId(id)
  390. if err != nil {
  391. fmt.Println("GetByUserId:", err.Error())
  392. return
  393. }
  394. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  395. if tmpErr != nil {
  396. err = tmpErr
  397. if tmpErr == utils.ErrNoRow {
  398. err = errors.New("找不到该客户")
  399. return
  400. }
  401. return
  402. }
  403. //新增userViewHistory记录
  404. banner_view_history := &banner_view_history.BannerViewHistory{
  405. UserID: userInfo.UserID,
  406. Mobile: wxUserInfo.Mobile,
  407. Email: wxUserInfo.Email,
  408. RealName: wxUserInfo.RealName,
  409. CompanyName: companyInfo.CompanyName,
  410. CreatedTime: time.Now(),
  411. LastUpdatedTime: time.Now(),
  412. FirstSource: req.FirstSource,
  413. SecondSource: req.SecondSource,
  414. BannerUrl: item.ImageUrlMobile,
  415. StartDate: item.StartDate,
  416. EndDate: item.EndDate,
  417. Remark: item.Remark,
  418. }
  419. err = banner_view_history.AddBannerViewHistory()
  420. if err != nil {
  421. fmt.Println("AddUserViewHistory err", err.Error())
  422. }
  423. response.Ok("成功", c)
  424. }
  425. // BannerList banner图列表
  426. // @Tags 公共模块
  427. // @Summary banner图列表
  428. // @Description banner图列表
  429. // @Security ApiKeyAuth
  430. // @securityDefinitions.basic BasicAuth
  431. // @Accept json
  432. // @Product json
  433. // @Success 200 {string} string 获取验证码成功
  434. // @Failure 400 {string} string 请输入邮箱地址
  435. // @Router /banner/list [get]
  436. func BannerList(c *gin.Context) {
  437. isHomepage, _ := strconv.Atoi(c.Query("is_homepage"))
  438. page, _ := strconv.Atoi(c.Query("page"))
  439. limit, _ := strconv.Atoi(c.Query("limit"))
  440. banner_type, _ := strconv.Atoi(c.Query("banner_type"))
  441. cond := " enable = 1 "
  442. if isHomepage != 1 {
  443. cond += " AND remark <> '调研合集' "
  444. }
  445. cond += " AND banner_type = " + strconv.Itoa(banner_type)
  446. list, err := banner.GetBannerList(cond, page, limit)
  447. if err != nil {
  448. response.FailMsg("获取失败", "获取banner失败, Err: "+err.Error(), c)
  449. return
  450. }
  451. response.OkData("获取成功", list, c)
  452. }
  453. // BannerHistoryList banner历史图列表
  454. // @Tags 公共模块
  455. // @Summary banner图列表
  456. // @Description banner图列表
  457. // @Security ApiKeyAuth
  458. // @securityDefinitions.basic BasicAuth
  459. // @Accept json
  460. // @Product json
  461. // @Success 200 {string} string 获取验证码成功
  462. // @Failure 400 {string} string 请输入邮箱地址
  463. // @Router /banner_history/list [get]
  464. func BannerHistoryList(c *gin.Context) {
  465. page, _ := strconv.Atoi(c.Query("page"))
  466. limit, _ := strconv.Atoi(c.Query("limit"))
  467. banner_type, _ := strconv.Atoi(c.Query("banner_type"))
  468. cond := ""
  469. cond += " enable = 0 "
  470. cond += " AND banner_type = " + strconv.Itoa(banner_type)
  471. total, err := banner.GetBannerListCount(cond)
  472. if err != nil {
  473. response.FailMsg("获取失败", "获取banner总数失败, Err: "+err.Error(), c)
  474. return
  475. }
  476. list, err := banner.GetBannerList(cond, page, limit)
  477. if err != nil {
  478. response.FailMsg("获取失败", "获取banner失败, Err: "+err.Error(), c)
  479. return
  480. }
  481. var resp respond.BannerRespItem
  482. resp.Paging = respond.GetPaging(page, limit, int(total))
  483. resp.List = list
  484. response.OkData("获取成功", resp, c)
  485. }
  486. // BannerList banner图详情
  487. // @Tags 公共模块
  488. // @Summary banner图详情
  489. // @Description banner图详情
  490. // @Security ApiKeyAuth
  491. // @securityDefinitions.basic BasicAuth
  492. // @Accept json
  493. // @Product json
  494. // @Success 200 {string} string 获取验证码成功
  495. // @Failure 400 {string} string 请输入邮箱地址
  496. // @Router /banner/detail [get]
  497. func BannerDetail(c *gin.Context) {
  498. bannerId, _ := strconv.Atoi(c.Query("banner_id"))
  499. item, err := banner.GetBannerById(bannerId)
  500. if err != nil {
  501. response.FailMsg("获取失败", "获取banner失败, Err: "+err.Error(), c)
  502. return
  503. }
  504. response.OkData("获取成功", item, c)
  505. }
  506. // BannerGetQRCode banner历史图列表
  507. // @Tags 公共模块
  508. // @Summary banner图列表
  509. // @Description banner图列表
  510. // @Security ApiKeyAuth
  511. // @securityDefinitions.basic BasicAuth
  512. // @Accept json
  513. // @Product json
  514. // @Success 200 {string} string 获取验证码成功
  515. // @Failure 400 {string} string 请输入邮箱地址
  516. // @Router /banner/get_qrcode [get]
  517. func BannerGetQRCode(c *gin.Context) {
  518. userId, _ := strconv.Atoi(c.Query("UserId"))
  519. bannerId, _ := strconv.Atoi(c.Query("BannerId"))
  520. remark := c.Query("Remark")
  521. wxUserInfo, err := wx_user.GetByUserId(userId)
  522. if err != nil {
  523. response.Ok(err.Error(), c)
  524. return
  525. }
  526. bannerItem, err := banner.GetBannerById(bannerId)
  527. if err != nil {
  528. response.Ok(err.Error(), c)
  529. return
  530. }
  531. if bannerItem.IsSignUp == 0 {
  532. response.Ok(`无需扫码报名`, c)
  533. return
  534. }
  535. companyInfo, tmpErr := company.GetByCompanyId(wxUserInfo.CompanyID)
  536. if tmpErr != nil {
  537. err = tmpErr
  538. if tmpErr == utils.ErrNoRow {
  539. response.Fail(err.Error(), c)
  540. err = errors.New("找不到该客户")
  541. return
  542. }
  543. response.Fail(err.Error(), c)
  544. return
  545. }
  546. companyName := companyInfo.CompanyName
  547. companyNameCode := url.QueryEscape(companyName) // 进行URL编码
  548. remarkCode := url.QueryEscape(remark) // 进行URL编码
  549. url := "pages-report/signUpPage/signUpPage?RealName=%s&CompanyName=%s&Mobile=%s&BannerId=%d&Title=%s"
  550. url = fmt.Sprintf(url, wxUserInfo.RealName, companyNameCode, wxUserInfo.Mobile, bannerId, remarkCode)
  551. picByte, err := wx_app.GetSunCodeV2(url)
  552. if err != nil {
  553. return
  554. }
  555. // 生成图片
  556. localPath := "./static/img"
  557. fileName := utils.GetRandStringNoSpecialChar(28) + ".png"
  558. fpath := fmt.Sprint(localPath, "/", fileName)
  559. f, err := os.Create(fpath)
  560. if err != nil {
  561. return
  562. }
  563. if _, err = f.Write(picByte); err != nil {
  564. return
  565. }
  566. defer func() {
  567. f.Close()
  568. os.Remove(fpath)
  569. }()
  570. //上传到阿里云
  571. resourceUrl, err := services.UploadAliyun(fileName, fpath)
  572. if err != nil {
  573. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  574. return
  575. }
  576. response.OkData("获取成功", resourceUrl, c)
  577. }
  578. // ResearchSignUp 报名
  579. // @Tags 公共模块
  580. // @Summary banner图列表
  581. // @Description banner图列表
  582. // @Security ApiKeyAuth
  583. // @securityDefinitions.basic BasicAuth
  584. // @Accept json
  585. // @Product json
  586. // @Success 200 {string} string 获取验证码成功
  587. // @Failure 400 {string} string 请输入邮箱地址
  588. // @Router /banner/signup [get]
  589. func ResearchSignUp(c *gin.Context) {
  590. //inviteCode := c.Query("InviteCode")
  591. realName := c.Query("RealName")
  592. companyName := c.Query("CompanyName")
  593. mobile := c.Query("Mobile")
  594. bannerId, _ := strconv.Atoi(c.Query("BannerId"))
  595. customName := c.Query("CustomName")
  596. customMobile := c.Query("CustomMobile")
  597. customCompanyName := c.Query("CustomCompanyName")
  598. //userInfo := user.GetInfoByClaims(c)
  599. //userDetail, err, errMsg := userLogic.GetUserInfo(userInfo)
  600. //if err != nil {
  601. // if errMsg != "" {
  602. // errMsg = "获取失败"
  603. // }
  604. // response.Fail(errMsg, c)
  605. // return
  606. //}
  607. count, err := yb_research_signup_statistics.GetSignUpCountByMobileAndBannerId(customMobile, bannerId)
  608. if err != nil {
  609. response.FailData("查询失败", "查询失败,Err:"+err.Error(), c)
  610. return
  611. }
  612. if count > 0 {
  613. response.FailData("该手机号已报名!", "该手机号已报名", c)
  614. return
  615. }
  616. ob := &yb_research_signup_statistics.YbResearchSignupStatistics{
  617. Mobile: mobile,
  618. CustomCompanyName: customCompanyName,
  619. CompanyName: companyName,
  620. BannerId: bannerId,
  621. CustomName: customName,
  622. RealName: realName,
  623. CustomMobile: customMobile,
  624. CreateTime: time.Now(),
  625. }
  626. err = ob.Create()
  627. if err != nil {
  628. response.FailData("报名失败", "报名失败,Err:"+err.Error(), c)
  629. return
  630. }
  631. response.Ok("报名成功", c)
  632. }
  633. // BannerGetQRCode banner调研图下载
  634. // @Tags 公共模块
  635. // @Summary banner调研图下载
  636. // @Description banner调研图下载
  637. // @Security ApiKeyAuth
  638. // @securityDefinitions.basic BasicAuth
  639. // @Accept json
  640. // @Product json
  641. // @Success 200 {string} string 获取验证码成功
  642. // @Failure 400 {string} string 请输入邮箱地址
  643. // @Router /banner/download [get]
  644. func BannerDowload(c *gin.Context) {
  645. var req request.BannerDownloadReq
  646. if err := c.Bind(&req); err != nil {
  647. response.Fail("参数有误:"+err.Error(), c)
  648. return
  649. }
  650. obj := yb_research_banner.YbResearchBanner{}
  651. item, err := obj.Fetch(req.UserId, req.BannerId)
  652. if err != nil && err != utils.ErrNoRow {
  653. response.Fail(err.Error(), c)
  654. return
  655. }
  656. resourceUrl := ""
  657. if item.ImgURL == "" {
  658. randStr := utils.GetRandStringNoSpecialChar(28)
  659. jpegPath := `./static/` + randStr + ".jpeg"
  660. err := services.ReportToJpeg(req.BannerUrl, jpegPath)
  661. if err != nil {
  662. response.FailData("图片转jpeg失败", "图片转jpeg失败,Err:"+err.Error(), c)
  663. return
  664. }
  665. dateDir := time.Now().Format("20060102")
  666. uploadDir := global.CONFIG.Serve.StaticDir + "hongze/" + dateDir
  667. err = os.MkdirAll(uploadDir, 0766)
  668. if err != nil {
  669. response.FailData("存储目录创建失败", "存储目录创建失败,Err:"+err.Error(), c)
  670. return
  671. }
  672. defer func() {
  673. os.Remove(jpegPath)
  674. }()
  675. //上传到阿里云
  676. resourceUrl, err = services.UploadAliyun(randStr+".jpeg", jpegPath)
  677. if err != nil {
  678. response.FailData("文件上传失败", "文件上传失败,Err:"+err.Error(), c)
  679. return
  680. }
  681. obj.BannerID = req.BannerId
  682. obj.UserID = req.UserId
  683. obj.ImgURL = resourceUrl
  684. err = obj.Create()
  685. if err != nil {
  686. response.FailData("创建banner长图记录失败", "创建banner长图记录失败,Err:"+err.Error(), c)
  687. return
  688. }
  689. } else {
  690. resourceUrl = item.ImgURL
  691. }
  692. response.OkData("获取成功", resourceUrl, c)
  693. }