pc.go 29 KB

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