pc.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. package pc
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb/controller/response"
  7. userLogic "hongze/hongze_yb/logic/user"
  8. pcModels "hongze/hongze_yb/models/response/pc"
  9. "hongze/hongze_yb/models/tables/customer_comment"
  10. "hongze/hongze_yb/models/tables/rddp/classify"
  11. "hongze/hongze_yb/models/tables/rddp/msg_code"
  12. "hongze/hongze_yb/models/tables/rddp/report"
  13. "hongze/hongze_yb/models/tables/rddp/report_chapter"
  14. "hongze/hongze_yb/models/tables/rddp/session"
  15. "hongze/hongze_yb/models/tables/wx_user"
  16. "hongze/hongze_yb/models/tables/wx_user_log"
  17. "hongze/hongze_yb/models/tables/yb_activity"
  18. "hongze/hongze_yb/models/tables/yb_pc_suncode"
  19. "hongze/hongze_yb/services"
  20. "hongze/hongze_yb/services/pc"
  21. userService "hongze/hongze_yb/services/user"
  22. "hongze/hongze_yb/services/wechat"
  23. "hongze/hongze_yb/utils"
  24. "strconv"
  25. "time"
  26. )
  27. // LatestNews 研报首页最新资讯
  28. func LatestNews(c *gin.Context) {
  29. reqChartPermissionId := c.DefaultQuery("chart_permission_id", "")
  30. if reqChartPermissionId == "" {
  31. response.Fail("请输入权限ID", c)
  32. return
  33. }
  34. chartPermissionId, err := strconv.Atoi(reqChartPermissionId)
  35. if err != nil {
  36. response.Fail("权限ID格式错误", c)
  37. return
  38. }
  39. userinfo := userService.GetInfoByClaims(c)
  40. list, err := pc.GetLatestReportList(userinfo, chartPermissionId)
  41. if err != nil {
  42. response.Fail("获取报告失败", c)
  43. return
  44. }
  45. //recommendList, err := pcModels.GetLatestReportList()
  46. //if err != nil {
  47. // response.Fail("获取报告详情失败"+err.Error(), c)
  48. // return
  49. //}
  50. response.OkData("查询成功", list, c)
  51. return
  52. }
  53. // Banner 上新公告banner图
  54. func Banner(c *gin.Context) {
  55. var resp pcModels.LatestReleaseResp
  56. activityItem, err := yb_activity.GetLatestActivity()
  57. if err != nil {
  58. response.Fail(err.Error(), c)
  59. return
  60. }
  61. reportItem, err := report.GetLatestReport()
  62. if err != nil {
  63. response.Fail(err.Error(), c)
  64. return
  65. }
  66. imgMap := map[string]string{
  67. "晨报": utils.ALIYUN_YBIMG_HOST + "day.png",
  68. "周报": utils.ALIYUN_YBIMG_HOST + "week.png",
  69. "双周报": utils.ALIYUN_YBIMG_HOST + "two_week.png",
  70. "月报": utils.ALIYUN_YBIMG_HOST + "month.png",
  71. "草根调研": utils.ALIYUN_YBIMG_HOST + "cgdy.png",
  72. "需求报告": utils.ALIYUN_YBIMG_HOST + "xqbg.png",
  73. "宏观报告": utils.ALIYUN_YBIMG_HOST + "hgbg.png",
  74. "日度点评": utils.ALIYUN_YBIMG_HOST + "rddp.png",
  75. "数据点评": utils.ALIYUN_YBIMG_HOST + "sjdp.png",
  76. "碳市场价格周报": utils.ALIYUN_YBIMG_HOST + "coal.png",
  77. "行业调研": utils.ALIYUN_YBIMG_HOST + "hydy.png",
  78. "百家谈": utils.ALIYUN_YBIMG_HOST + "bjt.png",
  79. "会议纪要": utils.ALIYUN_YBIMG_HOST + "hyjy.png",
  80. "大事点评": utils.ALIYUN_YBIMG_HOST + "dsdp.png",
  81. "年报合集": utils.ALIYUN_YBIMG_HOST + "nbhj.png",
  82. }
  83. if activityItem.CreateTime.Before(reportItem.PublishTime) {
  84. resp.ActivityOrReport = 1
  85. resp.ReportId = reportItem.ReportId
  86. resp.ClassifyIdSecond = reportItem.ClassifyIdSecond
  87. resp.ImgUrl = imgMap[reportItem.ClassifyNameFirst]
  88. } else {
  89. resp.ActivityOrReport = 0
  90. resp.Activity = activityItem
  91. resp.ImgUrl = utils.ALIYUN_YBIMG_HOST + "activity.png"
  92. }
  93. response.OkData("查询成功", resp, c)
  94. return
  95. }
  96. // LatestRelease 上新公告
  97. func LatestRelease(c *gin.Context) {
  98. classifyIdString := c.DefaultQuery("ClassifyId", "")
  99. if classifyIdString == "" {
  100. response.Fail("请输入classifyId", c)
  101. return
  102. }
  103. classifyId, err := strconv.Atoi(classifyIdString)
  104. if err != nil {
  105. response.Fail("请输入正确classifyId", c)
  106. return
  107. }
  108. classifyInfo, err := classify.GetByClassifyId(classifyId)
  109. if err != nil {
  110. response.Fail("分类查询出错", c)
  111. return
  112. }
  113. response.OkData("查询成功", classifyInfo, c)
  114. return
  115. }
  116. // ClassifyDetail 专栏详情
  117. func ClassifyDetail(c *gin.Context) {
  118. reqClassifyIdSecond := c.DefaultQuery("classify_id_second", "")
  119. if reqClassifyIdSecond == "" {
  120. response.Fail("请输入二级分类标识", c)
  121. return
  122. }
  123. classifyIdSecond, err := strconv.Atoi(reqClassifyIdSecond)
  124. if err != nil {
  125. response.Fail("请输入正确的二级分类标识", c)
  126. return
  127. }
  128. userinfo := userService.GetInfoByClaims(c)
  129. detail, err := pc.GetClassifyDetail(userinfo, classifyIdSecond)
  130. if err != nil {
  131. response.Fail(err.Error(), c)
  132. return
  133. }
  134. response.OkData("查询成功", detail, c)
  135. return
  136. }
  137. // ClassifyDetailBanner 专栏详情banner图
  138. func ClassifyDetailBanner(c *gin.Context) {
  139. reqReportId := c.DefaultQuery("reportId", "")
  140. classifyName := c.DefaultQuery("classify_name_first", "")
  141. if reqReportId == "" {
  142. response.Fail("请输入二级分类标识", c)
  143. return
  144. }
  145. reportId, err := strconv.Atoi(reqReportId)
  146. if err != nil {
  147. response.Fail("报告ID格式有误", c)
  148. return
  149. }
  150. var bannerResp *pcModels.DetailBannerResp
  151. if classifyName == "周报" || classifyName == "晨报" {
  152. chapterItem, err := report_chapter.GetLatestChapterByClassifyName(classifyName)
  153. if err != nil {
  154. response.Fail("查询最新一期晨周报失败"+err.Error(), c)
  155. return
  156. }
  157. resp := &pcModels.DetailBannerResp{
  158. ReportId: chapterItem.ReportId,
  159. ClassifyNameFirst: chapterItem.ClassifyNameFirst,
  160. ClassifyIdFirst: chapterItem.ClassifyIdFirst,
  161. Stage: chapterItem.Stage,
  162. ImgUrl: "",
  163. Type: "报告合集",
  164. }
  165. bannerResp = resp
  166. } else {
  167. reportInfo, err := report.GetByReportId(reportId)
  168. if err != nil {
  169. response.Fail("报告查询出错", c)
  170. return
  171. }
  172. if reportInfo.Id == 0 {
  173. response.Fail("报告不存在", c)
  174. return
  175. }
  176. if reportInfo.State != 2 {
  177. response.Fail("报告未发布", c)
  178. return
  179. }
  180. reportItem, err := report.GetLatestReportByClassifyName(classifyName, reportInfo.ClassifyIdSecond)
  181. if err != nil {
  182. response.Fail("获取报告详情失败"+err.Error(), c)
  183. return
  184. }
  185. resp := &pcModels.DetailBannerResp{
  186. ReportId: reportItem.ReportId,
  187. Stage: reportItem.Stage,
  188. VipTitle: reportItem.VipTitle,
  189. Author: reportItem.Author,
  190. ReportAuthor: reportItem.ReportAuthor,
  191. ImgUrl: "",
  192. ClassifyNameFirst: reportItem.ClassifyNameFirst,
  193. ClassifyIdFirst: reportItem.ClassifyIdFirst,
  194. ClassifyNameSecond: reportItem.ClassifyNameSecond,
  195. ClassifyIdSecond: reportItem.ClassifyIdSecond,
  196. }
  197. if classifyName == "月报" || classifyName == "双周报" || classifyName == "会议纪要" || classifyName == "大事点评" || classifyName == "年报合集" {
  198. resp.Type = "报告合集"
  199. } else {
  200. resp.Type = "专栏详情"
  201. }
  202. bannerResp = resp
  203. }
  204. //有人的用人的底图,和上新公告图片不完全一样
  205. firstNameMap := map[string]string{
  206. "晨报": utils.ALIYUN_YBIMG_HOST + "day.png",
  207. "周报": utils.ALIYUN_YBIMG_HOST + "week.png",
  208. "双周报": utils.ALIYUN_YBIMG_HOST + "two_week.png",
  209. "月报": utils.ALIYUN_YBIMG_HOST + "month.png",
  210. "会议纪要": utils.ALIYUN_YBIMG_HOST + "hyjy.png",
  211. "大事点评": utils.ALIYUN_YBIMG_HOST + "dsdp.png",
  212. "年报合集": utils.ALIYUN_YBIMG_HOST + "nbhj.png",
  213. //"草根调研": utils.ALIYUN_YBIMG_HOST + "hy.png",
  214. //"需求报告": utils.ALIYUN_YBIMG_HOST + "lxt.png",
  215. //"碳市场价格周报": utils.ALIYUN_YBIMG_HOST + "sjx.png",
  216. //"行业调研": utils.ALIYUN_YBIMG_HOST + "qp.png",
  217. }
  218. //secondNameMap := map[string]string{
  219. // //宏观报告
  220. // "宏观周期观察": utils.ALIYUN_YBIMG_HOST + "gx.png",
  221. // "房地产市场跟踪": utils.ALIYUN_YBIMG_HOST + "gx.png",
  222. // "货币政策跟踪": utils.ALIYUN_YBIMG_HOST + "gx.png",
  223. // "全球宏观经济周度回顾": utils.ALIYUN_YBIMG_HOST + "ldb.png",
  224. // "宏观点评": utils.ALIYUN_YBIMG_HOST + "zzh.png",
  225. // //日度点评
  226. // "知白守黑日评": utils.ALIYUN_YBIMG_HOST + "wxy.png",
  227. // "有声有色日度闲": utils.ALIYUN_YBIMG_HOST + "qp.png",
  228. // "化里化外日评": utils.ALIYUN_YBIMG_HOST + "qmz.png",
  229. // "每日经济数据备忘录": utils.ALIYUN_YBIMG_HOST + "ldb.png",
  230. // "股债日评": utils.ALIYUN_YBIMG_HOST + "zzh.png",
  231. // "贵金属复盘": utils.ALIYUN_YBIMG_HOST + "hx.png",
  232. // //数据点评
  233. // "钢材周度数据点评": utils.ALIYUN_YBIMG_HOST + "wxy.png",
  234. // "甲醇开工数据点评": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  235. // "短纤数据点评": utils.ALIYUN_YBIMG_HOST + "zyy.png",
  236. // "玻璃数据点评": utils.ALIYUN_YBIMG_HOST + "gwy.png",
  237. // "聚烯烃数据点评": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  238. // "铜行业数据点评": utils.ALIYUN_YBIMG_HOST + "yf.png",
  239. // "国际钢材市场数据点评": utils.ALIYUN_YBIMG_HOST + "bx.png",
  240. // "废钢周度数据点评": utils.ALIYUN_YBIMG_HOST + "bx.png",
  241. // "PP数据点评": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  242. // "PVC数据点评": utils.ALIYUN_YBIMG_HOST + "lyx.png",
  243. // "BOPP数据点评": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  244. // "塑编数据点评": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  245. // "铁矿库存数据点评": utils.ALIYUN_YBIMG_HOST + "hk.png",
  246. // "宏观数据点评": utils.ALIYUN_YBIMG_HOST + "zzh.png",
  247. // "铝数据点评": utils.ALIYUN_YBIMG_HOST + "yf.png",
  248. // "甲醇库存数据点评": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  249. // "玻璃纯碱数据点评": utils.ALIYUN_YBIMG_HOST + "gwy.png",
  250. // "乙二醇数据点评": utils.ALIYUN_YBIMG_HOST + "zyy.png",
  251. // "纯碱数据点评": utils.ALIYUN_YBIMG_HOST + "gwy.png",
  252. // "COVID-19跟踪": utils.ALIYUN_YBIMG_HOST + "lxt.png",
  253. // "双焦数据点评": utils.ALIYUN_YBIMG_HOST + "zs.png",
  254. // "苯乙烯库存数据点评": utils.ALIYUN_YBIMG_HOST + "cy.png",
  255. // "苯乙烯数据点评": utils.ALIYUN_YBIMG_HOST + "cy.png",
  256. // "苯乙烯简评": utils.ALIYUN_YBIMG_HOST + "cy.png",
  257. // "EIA天然气库存点评": utils.ALIYUN_YBIMG_HOST + "wyy.png",
  258. // "美国油气钻机数据点评": utils.ALIYUN_YBIMG_HOST + "wyy.png",
  259. // "OPEC+产量点评": utils.ALIYUN_YBIMG_HOST + "hyy.png",
  260. // "原油船期数数据跟踪": utils.ALIYUN_YBIMG_HOST + "wyy.png",
  261. // "EIA原油库存点评": utils.ALIYUN_YBIMG_HOST + "hyy.png",
  262. // "苯乙烯开工数据点评": utils.ALIYUN_YBIMG_HOST + "cy.png",
  263. // "聚酯数据点评": utils.ALIYUN_YBIMG_HOST + "qc.png",
  264. // "铁矿航运数据点评": utils.ALIYUN_YBIMG_HOST + "hk.png",
  265. // //百家谈
  266. // "能化百家谈": utils.ALIYUN_YBIMG_HOST + "qmz.png",
  267. // "有色百家谈": utils.ALIYUN_YBIMG_HOST + "qp.png",
  268. // "黑色百家谈": utils.ALIYUN_YBIMG_HOST + "wxy.png",
  269. //}
  270. authorMap := map[string]string{
  271. "白昕": utils.ALIYUN_YBIMG_HOST + "bx.png",
  272. "曹阳": utils.ALIYUN_YBIMG_HOST + "cy.png",
  273. "曾硕": utils.ALIYUN_YBIMG_HOST + "zs.png",
  274. "曾滢月": utils.ALIYUN_YBIMG_HOST + "zyy.png",
  275. "程品": utils.ALIYUN_YBIMG_HOST + "cp.png",
  276. "高雯宇": utils.ALIYUN_YBIMG_HOST + "gwy.png",
  277. "高昕": utils.ALIYUN_YBIMG_HOST + "gx.png",
  278. "高吟": utils.ALIYUN_YBIMG_HOST + "gy.png",
  279. "陈聪聪": utils.ALIYUN_YBIMG_HOST + "ccc.png",
  280. "何凯": utils.ALIYUN_YBIMG_HOST + "hk.png",
  281. "黄鑫": utils.ALIYUN_YBIMG_HOST + "hx.png",
  282. "黄逸赟": utils.ALIYUN_YBIMG_HOST + "hyy.png",
  283. "黄溢": utils.ALIYUN_YBIMG_HOST + "hy.png",
  284. "李灵": utils.ALIYUN_YBIMG_HOST + "ll.png",
  285. "林秋馨": utils.ALIYUN_YBIMG_HOST + "lqx.png",
  286. "刘鼎邦": utils.ALIYUN_YBIMG_HOST + "ldb.png",
  287. "钱鹏": utils.ALIYUN_YBIMG_HOST + "qp.png",
  288. "刘艺羡": utils.ALIYUN_YBIMG_HOST + "lyx.png",
  289. "戚明之": utils.ALIYUN_YBIMG_HOST + "qmz.png",
  290. "秦钏": utils.ALIYUN_YBIMG_HOST + "qc.png",
  291. "施琪": utils.ALIYUN_YBIMG_HOST + "sq.png",
  292. "王存响": utils.ALIYUN_YBIMG_HOST + "wcx.png",
  293. "王沛": utils.ALIYUN_YBIMG_HOST + "wp.png",
  294. "苏畅": utils.ALIYUN_YBIMG_HOST + "sc.png",
  295. "史瑾璇": utils.ALIYUN_YBIMG_HOST + "sjx.png",
  296. "王亚丹": utils.ALIYUN_YBIMG_HOST + "wyd.png",
  297. "虞风": utils.ALIYUN_YBIMG_HOST + "yf.png",
  298. "魏忻悦": utils.ALIYUN_YBIMG_HOST + "wxy.png",
  299. "王艺滢": utils.ALIYUN_YBIMG_HOST + "wyy.png",
  300. "姚昕泽": utils.ALIYUN_YBIMG_HOST + "yxz.png",
  301. "章左昊": utils.ALIYUN_YBIMG_HOST + "zzh.png",
  302. "李晓瞳": utils.ALIYUN_YBIMG_HOST + "lxt.png",
  303. "弘则FICC无锡调研团队": utils.ALIYUN_YBIMG_HOST + "qp.png",
  304. "弘则能化组": utils.ALIYUN_YBIMG_HOST + "qmz.png",
  305. "弘则有色团队": utils.ALIYUN_YBIMG_HOST + "qp.png",
  306. "弘则黑色团队": utils.ALIYUN_YBIMG_HOST + "wp.png",
  307. "弘则宏观团队": utils.ALIYUN_YBIMG_HOST + "zzh.png",
  308. "张立舟": utils.ALIYUN_YBIMG_HOST + "zlz.png",
  309. "郑露": utils.ALIYUN_YBIMG_HOST + "zl.png",
  310. "酒子怡": utils.ALIYUN_YBIMG_HOST + "jzy.png",
  311. }
  312. if url, ok := firstNameMap[bannerResp.ClassifyNameFirst]; ok {
  313. bannerResp.ImgUrl = url
  314. } else if url, ok = authorMap[bannerResp.ReportAuthor]; ok {
  315. bannerResp.ImgUrl = url
  316. }
  317. response.OkData("查询成功", bannerResp, c)
  318. return
  319. }
  320. // Recommend 报告详情页更多推荐
  321. func Recommend(c *gin.Context) {
  322. reqReportId := c.DefaultQuery("reportId", "")
  323. classifyName := c.DefaultQuery("classify_name_first", "")
  324. if reqReportId == "" {
  325. response.Fail("请输入二级分类标识", c)
  326. return
  327. }
  328. reportId, err := strconv.Atoi(reqReportId)
  329. if err != nil {
  330. response.Fail("报告ID格式有误", c)
  331. return
  332. }
  333. var recommendResp []*pcModels.RecommendResp
  334. if classifyName == "周报" || classifyName == "晨报" {
  335. recommendList, err := report_chapter.GetWeekRecommendList(reportId, classifyName)
  336. if err != nil {
  337. response.Fail("获取报告详情失败"+err.Error(), c)
  338. return
  339. }
  340. for _, chapter := range recommendList {
  341. resp := &pcModels.RecommendResp{
  342. ReportId: chapter.ReportId,
  343. ReportChapterID: chapter.ReportChapterId,
  344. Title: chapter.Title,
  345. Stage: chapter.Stage,
  346. ClassifyNameFirst: chapter.ClassifyNameFirst,
  347. ClassifySecondFirst: "",
  348. }
  349. recommendResp = append(recommendResp, resp)
  350. }
  351. } else {
  352. reportInfo, err := report.GetByReportId(reportId)
  353. if err != nil {
  354. response.Fail("报告查询出错", c)
  355. return
  356. }
  357. if reportInfo.Id == 0 {
  358. response.Fail("报告不存在", c)
  359. return
  360. }
  361. if reportInfo.State != 2 {
  362. response.Fail("报告未发布", c)
  363. return
  364. }
  365. reportType := 0
  366. if reportInfo.ClassifyNameFirst == "权益研报" {
  367. reportType = 1
  368. } else {
  369. reportType = 2
  370. }
  371. recommendList, err := pcModels.GetRecommendList(reportId, reportType, reportInfo.ClassifyIdSecond)
  372. if err != nil {
  373. response.Fail("获取报告详情失败"+err.Error(), c)
  374. return
  375. }
  376. for _, chapter := range recommendList {
  377. resp := &pcModels.RecommendResp{
  378. ReportId: chapter.Id,
  379. ReportChapterID: 0,
  380. Title: chapter.Title,
  381. Stage: chapter.Stage,
  382. ClassifyNameFirst: chapter.ClassifyNameFirst,
  383. ClassifySecondFirst: chapter.ClassifyNameSecond,
  384. }
  385. recommendResp = append(recommendResp, resp)
  386. }
  387. }
  388. response.OkData("查询成功", recommendResp, c)
  389. return
  390. }
  391. // ClassifyFirstList Ficc 菜单
  392. func ClassifyFirstList(c *gin.Context) {
  393. userinfo := userService.GetInfoByClaims(c)
  394. classList, err := pc.GetClassifyFirstList(userinfo)
  395. if err != nil {
  396. response.Fail(err.Error(), c)
  397. return
  398. }
  399. response.OkData("查询成功", classList, c)
  400. return
  401. }
  402. // CustomerComment 客户评价
  403. func CustomerComment(c *gin.Context) {
  404. lists, err := customer_comment.GetCustomerComment()
  405. if err != nil {
  406. response.Fail(err.Error(), c)
  407. return
  408. }
  409. response.OkData("查询成功", lists, c)
  410. return
  411. }
  412. // GetSunCode 获取太阳码
  413. // @Tags 公共模块
  414. // @Summary 获取分享海报
  415. // @Description 获取分享海报
  416. // @Param request body services.SunCodeReq true "type json string"
  417. // @Success 200 {object} string "获取成功"
  418. // @failure 400 {string} string "获取失败"
  419. // @Router /pc/getSunCode [post]
  420. func GetSunCode(c *gin.Context) {
  421. var req services.SunCodeReq
  422. if c.ShouldBind(&req) != nil {
  423. response.Fail("参数异常", c)
  424. return
  425. }
  426. var sunCodeUrl string
  427. //先查,查不到再去生成上传
  428. item, err := yb_pc_suncode.GetYbPcSunCode(req.CodeScene, req.CodePage)
  429. if err != nil && err != utils.ErrNoRow {
  430. response.FailMsg("获取太阳码失败", err.Error(), c)
  431. return
  432. }
  433. if item != nil && item.SuncodeURL != "" {
  434. sunCodeUrl = item.SuncodeURL
  435. }
  436. if sunCodeUrl == "" {
  437. sunCodeUrl, err = services.PcCreateAndUploadSunCode(req.CodeScene, req.CodePage)
  438. if err != nil {
  439. response.FailMsg("生成太阳码失败", err.Error(), c)
  440. return
  441. }
  442. }
  443. response.OkData("查询成功", sunCodeUrl, c)
  444. return
  445. }
  446. // @Title pc端微信登录接口
  447. // @Description pc端微信登录接口
  448. // @Param Code query string true "微信唯一编码code"
  449. // @Success 200 {object} models.WxLoginResp
  450. // @router /pc/login [get]
  451. func WechatLogin(c *gin.Context) {
  452. var resp pcModels.LoginResp
  453. var token string
  454. var userId int
  455. var isBind bool
  456. code := c.DefaultQuery("Code", "")
  457. if code == "" {
  458. response.Fail("code参数错误", c)
  459. return
  460. }
  461. item, err := wechat.PcWxGetUserOpenIdByCode(code)
  462. if err != nil {
  463. response.Fail(err.Error(), c)
  464. return
  465. }
  466. fmt.Println("token:", item.AccessToken)
  467. fmt.Println("token:", item.Errmsg)
  468. fmt.Println("token:", item.Errcode)
  469. if item.Errcode != 0 {
  470. response.Fail(item.Errmsg, c)
  471. return
  472. }
  473. openId := item.Openid
  474. //wxUserInfo, err := wx_app.GetSession(code)
  475. //if err != nil {
  476. // response.Fail("获取失败,Err:"+err.Error(), c)
  477. // return
  478. //}
  479. //获取用户信息
  480. wxPcUserInfo, err := wechat.PcWxGetUserInfo(openId, item.AccessToken)
  481. if err != nil {
  482. response.Fail(err.Error(), c)
  483. return
  484. }
  485. if wxPcUserInfo.Errcode != 0 {
  486. response.Fail(err.Error(), c)
  487. return
  488. }
  489. unionId := item.Unionid
  490. //sessionKey := wxUserInfo.SessionKey
  491. //needUpdateSessionKey := true //是否更新sessionKey
  492. QUERY_WX_USER:
  493. wxUser, wxUserErr := userService.GetWxUserItemByOpenId(openId)
  494. if wxUserErr == userService.ERR_NO_USER_RECORD { //没有用户openid记录
  495. _, recordErr := userService.AddUserRecord(openId, unionId, "", "", "", "", "", "", "", 3, 0, 0)
  496. //如果插入失败,那么直接将错误信息返回
  497. if recordErr != nil {
  498. err = recordErr
  499. return
  500. }
  501. //needUpdateSessionKey = false //因为是新增用户微信信息,所以不需要更新sessionKey的数据了
  502. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  503. goto QUERY_WX_USER
  504. } else if wxUserErr == userService.ERR_USER_NOT_BIND {
  505. // 未绑定则去查询unionId是否已经绑定了用户(其他平台,不区分平台),有相应的手机号邮箱信息则自动绑定
  506. platformUser, platformErr := userService.GetFirstWxUserItemByUnionId(unionId)
  507. if platformErr == nil {
  508. // 当公众号用户存在时
  509. if platformUser.Mobile != "" || platformUser.Email != "" {
  510. // 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
  511. countryCode := 0
  512. if platformUser.CountryCode != "" {
  513. countryCode, _ = strconv.Atoi(platformUser.CountryCode)
  514. }
  515. tempToken, tempUser, tempErr, errMsg := userService.BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
  516. if tempErr != nil {
  517. err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error() + ", errMsg:" + errMsg)
  518. return
  519. }
  520. token = tempToken
  521. userId = int(tempUser.UserID)
  522. isBind = true
  523. }
  524. }
  525. } else if wxUserErr != nil {
  526. err = wxUserErr
  527. return
  528. }
  529. //更新微信用户的sessionKey
  530. //if needUpdateSessionKey {
  531. // _ = user_record.ModifySessionKeyByOpenid(openId, sessionKey)
  532. //}
  533. // 如果已经登录注册绑定的情况下/或者首次登录且为弘则研究公众号用户
  534. if wxUserErr == nil {
  535. userId = int(wxUser.UserID)
  536. // 如果账户有绑定了手机号或者邮箱,那么标记为已绑定
  537. if wxUser.Mobile != "" || wxUser.Email != "" {
  538. isBind = true
  539. }
  540. }
  541. //获取登录token
  542. tokenItem, tokenErr := session.GetTokenByOpenId(openId)
  543. if tokenErr != nil && tokenErr != utils.ErrNoRow {
  544. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  545. return
  546. }
  547. fmt.Println("AccessToken:", item.AccessToken)
  548. fmt.Println("tokenItem:", tokenItem.AccessToken)
  549. if tokenErr != nil && tokenErr == utils.ErrNoRow {
  550. timeUnix := time.Now().Unix()
  551. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  552. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  553. fmt.Println("token:", tokenItem.AccessToken)
  554. //新增session
  555. {
  556. sessionItem := &session.Session{
  557. OpenID: openId,
  558. UserID: int64(userId),
  559. CreatedTime: time.Now(),
  560. LastUpdatedTime: time.Now(),
  561. ExpireTime: time.Now().AddDate(0, 3, 0),
  562. AccessToken: token,
  563. }
  564. sessionErr := sessionItem.Create()
  565. if err != nil {
  566. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  567. return
  568. }
  569. }
  570. } else {
  571. token = tokenItem.AccessToken
  572. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  573. //if userId > 0 && tokenItem.UserID != int64(userId) {
  574. // _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
  575. //}
  576. //if userId > 0 {
  577. //}
  578. _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
  579. }
  580. //新增登录日志
  581. {
  582. loginLog := &wx_user_log.WxUserLog{
  583. UserID: userId,
  584. OpenID: openId,
  585. UnionID: unionId,
  586. CreateTime: time.Now(),
  587. Handle: "yb_login",
  588. Remark: token,
  589. }
  590. go loginLog.Create()
  591. }
  592. resp.IsBind = isBind
  593. resp.Token = token
  594. response.OkData("查询成功", resp, c)
  595. return
  596. }
  597. // @Title 登录
  598. // @Description 登录接口
  599. // @Param request body models.LoginReq true "type json string"
  600. // @Success 200 {object} models.LoginResp
  601. // @router /pc/pcLogin [post]
  602. func PcLogin(c *gin.Context) {
  603. var req pcModels.PcLoginReq
  604. if c.ShouldBind(&req) != nil {
  605. response.Fail("参数异常", c)
  606. return
  607. }
  608. userId := 0
  609. var isAdd bool
  610. if req.LoginType == 1 {
  611. if req.Mobile == "" {
  612. response.Fail("请输入手机号", c)
  613. return
  614. }
  615. if req.SmsCode == "" {
  616. response.Fail("请输入有效手机验证码", c)
  617. return
  618. }
  619. item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
  620. if err != nil {
  621. if err == utils.ErrNoRow {
  622. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  623. return
  624. } else {
  625. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  626. return
  627. }
  628. }
  629. if item == nil {
  630. response.Fail("手机验证码错误,请重新输入", c)
  631. return
  632. }
  633. wxUser, err := wx_user.GetByMobile(req.Mobile)
  634. if err != nil {
  635. if err == utils.ErrNoRow {
  636. isAdd = true
  637. } else {
  638. response.Fail("根据手机号获取用户信息失败,Err:" + err.Error(), c)
  639. return
  640. }
  641. }
  642. if wxUser == nil {
  643. isAdd = true
  644. } else {
  645. userId = int(wxUser.UserID)
  646. }
  647. //BindMobile(openId, mobile string, userId, loginType int) (err error) {
  648. //newUserId, err = models.BindMobile(openId, req.Mobile, userId, req.LoginType)
  649. } else if req.LoginType == 2 {
  650. if req.Email == "" {
  651. response.Fail("邮箱不能为空,请输入邮箱", c)
  652. return
  653. }
  654. if !utils.ValidateEmailFormatat(req.Email) {
  655. response.Fail("邮箱格式错误,请重新输入", c)
  656. return
  657. }
  658. if req.SmsCode == "" {
  659. response.Fail("请输入有效验证码", c)
  660. return
  661. }
  662. item, err := msg_code.GetMsgCode(req.Email, req.SmsCode)
  663. if err != nil {
  664. if err == utils.ErrNoRow {
  665. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  666. return
  667. } else {
  668. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  669. return
  670. }
  671. }
  672. if item == nil {
  673. response.Fail("邮箱证码错误,请重新输入:", c)
  674. return
  675. }
  676. wxUser, err := wx_user.GetByEmail(req.Email)
  677. if err != nil {
  678. if err == utils.ErrNoRow {
  679. isAdd = true
  680. } else {
  681. response.Fail("根据邮箱获取用户信息失败,Err:" + err.Error(), c)
  682. return
  683. }
  684. }
  685. if wxUser == nil {
  686. isAdd = true
  687. } else {
  688. userId = int(wxUser.UserID)
  689. }
  690. } else {
  691. response.Fail("无效的登录方式,loginType:" + strconv.Itoa(req.LoginType), c)
  692. }
  693. if isAdd {
  694. user := new(wx_user.WxUser)
  695. user.CompanyID = 1
  696. user.CreatedTime = time.Now()
  697. user.FirstLogin = 1
  698. user.Enabled = 1
  699. user.Email = req.Email
  700. user.Mobile = req.Mobile
  701. if req.LoginType == 1 {
  702. user.BindAccount = user.Mobile
  703. } else {
  704. user.BindAccount = user.Email
  705. }
  706. user.RegisterTime = time.Now()
  707. user.LoginTime = time.Now()
  708. user.RegisterPlatform = 2
  709. if req.IsFreeLogin {
  710. user.IsFreeLogin = 1
  711. }else {
  712. user.IsFreeLogin = 0
  713. }
  714. lastId, err := user.Add()
  715. if err != nil {
  716. response.Fail("登录失败,新增客户信息失败,Err:" + err.Error(), c)
  717. return
  718. }
  719. fmt.Println("lastId:", lastId)
  720. userId = int(lastId)
  721. timeUnix := time.Now().Unix()
  722. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  723. token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  724. //新增session
  725. {
  726. session := new(session.Session)
  727. session.UserID = int64(userId)
  728. session.CreatedTime = time.Now()
  729. session.LastUpdatedTime = time.Now()
  730. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  731. session.AccessToken = token
  732. err = session.Create()
  733. if err != nil {
  734. response.Fail("登录失败,新增用户session信息失败:" + err.Error(), c)
  735. return
  736. }
  737. }
  738. } else {
  739. user := new(wx_user.WxUser)
  740. user.UserID = uint64(userId)
  741. if req.IsFreeLogin {
  742. user.IsFreeLogin = 1
  743. }else {
  744. user.IsFreeLogin = 0
  745. }
  746. err := user.Update([]string{"is_free_login"})
  747. if err != nil {
  748. response.Fail("登录失败,修改登录信息失败,Err:" + err.Error(), c)
  749. return
  750. }
  751. }
  752. if userId == 0 {
  753. response.Fail("登录失败,id为 0", c)
  754. return
  755. }
  756. err := wx_user.ModifyFirstLogin(uint64(userId))
  757. if err != nil {
  758. response.Fail("登录失败,判断权限失败:" + err.Error(), c)
  759. return
  760. }
  761. var token string
  762. tokenItem, err := session.GetTokenByUid(userId)
  763. if err != nil && err != utils.ErrNoRow {
  764. response.Fail("登录失败,获取token失败:" + err.Error(), c)
  765. return
  766. }
  767. if tokenItem == nil || (err != nil && err == utils.ErrNoRow) {
  768. timeUnix := time.Now().Unix()
  769. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  770. token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  771. //新增session
  772. {
  773. session := new(session.Session)
  774. session.UserID = int64(userId)
  775. session.CreatedTime = time.Now()
  776. session.LastUpdatedTime = time.Now()
  777. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  778. session.AccessToken = token
  779. err = session.Create()
  780. if err != nil {
  781. response.Fail("登录失败,新增用户session信息失败:" + err.Error(), c)
  782. return
  783. }
  784. }
  785. } else {
  786. token = tokenItem.AccessToken
  787. }
  788. //新增登录日志
  789. {
  790. loginLog := new(wx_user_log.WxUserLog)
  791. loginLog.UserID = userId
  792. loginLog.Mobile = req.Mobile
  793. loginLog.Email = req.Email
  794. loginLog.Handle = "pc_login"
  795. loginLog.CreateTime = time.Now()
  796. go loginLog.Create()
  797. }
  798. response.OkData("登录成功", token, c)
  799. }
  800. // GetSmsCode 获取短信验证码接口
  801. // @Tags 用户模块
  802. // @Summary 获取短信验证码
  803. // @Description 获取短信验证码接口
  804. // @Security ApiKeyAuth
  805. // @securityDefinitions.basic BasicAuth
  806. // @Param mobile query string true "手机号"
  807. // @Param area_num query string true "手机国际区号(中国大陆:86)"
  808. // @Accept json
  809. // @Product json
  810. // @Success 200 {string} string 获取验证码成功
  811. // @Failure 400 {string} string 手机号不能为空,请输入手机号
  812. // @Router /pc_code/get_sms_code [get]
  813. func GetSmsCode(c *gin.Context) {
  814. mobile := c.DefaultQuery("mobile", "")
  815. areaNum := c.DefaultQuery("area_num", "")
  816. err, errMsg := userLogic.PcSendSmsCode(mobile, areaNum)
  817. if err != nil {
  818. if errMsg != "" {
  819. errMsg = "获取验证码失败"
  820. }
  821. response.Fail(errMsg, c)
  822. return
  823. }
  824. response.Ok("获取验证码成功", c)
  825. }
  826. // GetEmailCode 获取邮箱验证码接口
  827. // @Tags 用户模块
  828. // @Summary 获取邮箱验证码
  829. // @Description 获取邮箱验证码
  830. // @Security ApiKeyAuth
  831. // @securityDefinitions.basic BasicAuth
  832. // @Param email query string true "电子邮箱账号"
  833. // @Accept json
  834. // @Product json
  835. // @Success 200 {string} string 获取验证码成功
  836. // @Failure 400 {string} string 请输入邮箱地址
  837. // @Router /pc_code/get_email_code [get]
  838. func GetEmailCode(c *gin.Context) {
  839. email := c.DefaultQuery("email", "")
  840. if email == "" {
  841. response.Fail("请输入邮箱地址", c)
  842. return
  843. }
  844. if !utils.ValidateEmailFormatat(email) {
  845. response.Fail("邮箱格式错误,请重新输入", c)
  846. return
  847. }
  848. err, errMsg := userLogic.PcSendEmailCode(email)
  849. if err != nil {
  850. if errMsg != "" {
  851. errMsg = "获取验证码失败"
  852. }
  853. response.Fail(errMsg, c)
  854. return
  855. }
  856. response.Ok("获取验证码成功", c)
  857. }