pc.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. }
  311. if url, ok := firstNameMap[bannerResp.ClassifyNameFirst]; ok {
  312. bannerResp.ImgUrl = url
  313. } else if url, ok = authorMap[bannerResp.ReportAuthor]; ok {
  314. bannerResp.ImgUrl = url
  315. }
  316. response.OkData("查询成功", bannerResp, c)
  317. return
  318. }
  319. // Recommend 报告详情页更多推荐
  320. func Recommend(c *gin.Context) {
  321. reqReportId := c.DefaultQuery("reportId", "")
  322. classifyName := c.DefaultQuery("classify_name_first", "")
  323. if reqReportId == "" {
  324. response.Fail("请输入二级分类标识", c)
  325. return
  326. }
  327. reportId, err := strconv.Atoi(reqReportId)
  328. if err != nil {
  329. response.Fail("报告ID格式有误", c)
  330. return
  331. }
  332. var recommendResp []*pcModels.RecommendResp
  333. if classifyName == "周报" || classifyName == "晨报" {
  334. recommendList, err := report_chapter.GetWeekRecommendList(reportId, classifyName)
  335. if err != nil {
  336. response.Fail("获取报告详情失败"+err.Error(), c)
  337. return
  338. }
  339. for _, chapter := range recommendList {
  340. resp := &pcModels.RecommendResp{
  341. ReportId: chapter.ReportId,
  342. ReportChapterID: chapter.ReportChapterId,
  343. Title: chapter.Title,
  344. Stage: chapter.Stage,
  345. ClassifyNameFirst: chapter.ClassifyNameFirst,
  346. ClassifySecondFirst: "",
  347. }
  348. recommendResp = append(recommendResp, resp)
  349. }
  350. } else {
  351. reportInfo, err := report.GetByReportId(reportId)
  352. if err != nil {
  353. response.Fail("报告查询出错", c)
  354. return
  355. }
  356. if reportInfo.Id == 0 {
  357. response.Fail("报告不存在", c)
  358. return
  359. }
  360. if reportInfo.State != 2 {
  361. response.Fail("报告未发布", c)
  362. return
  363. }
  364. reportType := 0
  365. if reportInfo.ClassifyNameFirst == "权益研报" {
  366. reportType = 1
  367. } else {
  368. reportType = 2
  369. }
  370. recommendList, err := pcModels.GetRecommendList(reportId, reportType, reportInfo.ClassifyIdSecond)
  371. if err != nil {
  372. response.Fail("获取报告详情失败"+err.Error(), c)
  373. return
  374. }
  375. for _, chapter := range recommendList {
  376. resp := &pcModels.RecommendResp{
  377. ReportId: chapter.Id,
  378. ReportChapterID: 0,
  379. Title: chapter.Title,
  380. Stage: chapter.Stage,
  381. ClassifyNameFirst: chapter.ClassifyNameFirst,
  382. ClassifySecondFirst: chapter.ClassifyNameSecond,
  383. }
  384. recommendResp = append(recommendResp, resp)
  385. }
  386. }
  387. response.OkData("查询成功", recommendResp, c)
  388. return
  389. }
  390. // ClassifyFirstList Ficc 菜单
  391. func ClassifyFirstList(c *gin.Context) {
  392. userinfo := userService.GetInfoByClaims(c)
  393. classList, err := pc.GetClassifyFirstList(userinfo)
  394. if err != nil {
  395. response.Fail(err.Error(), c)
  396. return
  397. }
  398. response.OkData("查询成功", classList, c)
  399. return
  400. }
  401. // CustomerComment 客户评价
  402. func CustomerComment(c *gin.Context) {
  403. lists, err := customer_comment.GetCustomerComment()
  404. if err != nil {
  405. response.Fail(err.Error(), c)
  406. return
  407. }
  408. response.OkData("查询成功", lists, c)
  409. return
  410. }
  411. // GetSunCode 获取太阳码
  412. // @Tags 公共模块
  413. // @Summary 获取分享海报
  414. // @Description 获取分享海报
  415. // @Param request body services.SunCodeReq true "type json string"
  416. // @Success 200 {object} string "获取成功"
  417. // @failure 400 {string} string "获取失败"
  418. // @Router /pc/getSunCode [post]
  419. func GetSunCode(c *gin.Context) {
  420. var req services.SunCodeReq
  421. if c.ShouldBind(&req) != nil {
  422. response.Fail("参数异常", c)
  423. return
  424. }
  425. var sunCodeUrl string
  426. //先查,查不到再去生成上传
  427. item, err := yb_pc_suncode.GetYbPcSunCode(req.CodeScene, req.CodePage)
  428. if err != nil && err != utils.ErrNoRow {
  429. response.FailMsg("获取太阳码失败", err.Error(), c)
  430. return
  431. }
  432. if item != nil && item.SuncodeURL != "" {
  433. sunCodeUrl = item.SuncodeURL
  434. }
  435. if sunCodeUrl == "" {
  436. sunCodeUrl, err = services.PcCreateAndUploadSunCode(req.CodeScene, req.CodePage)
  437. if err != nil {
  438. response.FailMsg("生成太阳码失败", err.Error(), c)
  439. return
  440. }
  441. }
  442. response.OkData("查询成功", sunCodeUrl, c)
  443. return
  444. }
  445. // @Title pc端微信登录接口
  446. // @Description pc端微信登录接口
  447. // @Param Code query string true "微信唯一编码code"
  448. // @Success 200 {object} models.WxLoginResp
  449. // @router /pc/login [get]
  450. func WechatLogin(c *gin.Context) {
  451. var resp pcModels.LoginResp
  452. var token string
  453. var userId int
  454. var isBind bool
  455. code := c.DefaultQuery("Code", "")
  456. if code == "" {
  457. response.Fail("code参数错误", c)
  458. return
  459. }
  460. item, err := wechat.PcWxGetUserOpenIdByCode(code)
  461. if err != nil {
  462. response.Fail(err.Error(), c)
  463. return
  464. }
  465. fmt.Println("token:", item.AccessToken)
  466. fmt.Println("token:", item.Errmsg)
  467. fmt.Println("token:", item.Errcode)
  468. if item.Errcode != 0 {
  469. response.Fail(item.Errmsg, c)
  470. return
  471. }
  472. openId := item.Openid
  473. //wxUserInfo, err := wx_app.GetSession(code)
  474. //if err != nil {
  475. // response.Fail("获取失败,Err:"+err.Error(), c)
  476. // return
  477. //}
  478. //获取用户信息
  479. wxPcUserInfo, err := wechat.PcWxGetUserInfo(openId, item.AccessToken)
  480. if err != nil {
  481. response.Fail(err.Error(), c)
  482. return
  483. }
  484. if wxPcUserInfo.Errcode != 0 {
  485. response.Fail(err.Error(), c)
  486. return
  487. }
  488. unionId := item.Unionid
  489. //sessionKey := wxUserInfo.SessionKey
  490. //needUpdateSessionKey := true //是否更新sessionKey
  491. QUERY_WX_USER:
  492. wxUser, wxUserErr := userService.GetWxUserItemByOpenId(openId)
  493. if wxUserErr == userService.ERR_NO_USER_RECORD { //没有用户openid记录
  494. _, recordErr := userService.AddUserRecord(openId, unionId, "", "", "", "", "", "", "", 3, 0, 0)
  495. //如果插入失败,那么直接将错误信息返回
  496. if recordErr != nil {
  497. err = recordErr
  498. return
  499. }
  500. //needUpdateSessionKey = false //因为是新增用户微信信息,所以不需要更新sessionKey的数据了
  501. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  502. goto QUERY_WX_USER
  503. } else if wxUserErr == userService.ERR_USER_NOT_BIND {
  504. // 未绑定则去查询unionId是否已经绑定了用户(其他平台,不区分平台),有相应的手机号邮箱信息则自动绑定
  505. platformUser, platformErr := userService.GetFirstWxUserItemByUnionId(unionId)
  506. if platformErr == nil {
  507. // 当公众号用户存在时
  508. if platformUser.Mobile != "" || platformUser.Email != "" {
  509. // 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
  510. countryCode := 0
  511. if platformUser.CountryCode != "" {
  512. countryCode, _ = strconv.Atoi(platformUser.CountryCode)
  513. }
  514. tempToken, tempUser, tempErr, errMsg := userService.BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
  515. if tempErr != nil {
  516. err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error() + ", errMsg:" + errMsg)
  517. return
  518. }
  519. token = tempToken
  520. userId = int(tempUser.UserID)
  521. isBind = true
  522. }
  523. }
  524. } else if wxUserErr != nil {
  525. err = wxUserErr
  526. return
  527. }
  528. //更新微信用户的sessionKey
  529. //if needUpdateSessionKey {
  530. // _ = user_record.ModifySessionKeyByOpenid(openId, sessionKey)
  531. //}
  532. // 如果已经登录注册绑定的情况下/或者首次登录且为弘则研究公众号用户
  533. if wxUserErr == nil {
  534. userId = int(wxUser.UserID)
  535. // 如果账户有绑定了手机号或者邮箱,那么标记为已绑定
  536. if wxUser.Mobile != "" || wxUser.Email != "" {
  537. isBind = true
  538. }
  539. }
  540. //获取登录token
  541. tokenItem, tokenErr := session.GetTokenByOpenId(openId)
  542. if tokenErr != nil && tokenErr != utils.ErrNoRow {
  543. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  544. return
  545. }
  546. fmt.Println("AccessToken:", item.AccessToken)
  547. fmt.Println("tokenItem:", tokenItem.AccessToken)
  548. if tokenErr != nil && tokenErr == utils.ErrNoRow {
  549. timeUnix := time.Now().Unix()
  550. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  551. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  552. fmt.Println("token:", tokenItem.AccessToken)
  553. //新增session
  554. {
  555. sessionItem := &session.Session{
  556. OpenID: openId,
  557. UserID: int64(userId),
  558. CreatedTime: time.Now(),
  559. LastUpdatedTime: time.Now(),
  560. ExpireTime: time.Now().AddDate(0, 3, 0),
  561. AccessToken: token,
  562. }
  563. sessionErr := sessionItem.Create()
  564. if err != nil {
  565. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  566. return
  567. }
  568. }
  569. } else {
  570. token = tokenItem.AccessToken
  571. //如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
  572. //if userId > 0 && tokenItem.UserID != int64(userId) {
  573. // _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
  574. //}
  575. //if userId > 0 {
  576. //}
  577. _ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
  578. }
  579. //新增登录日志
  580. {
  581. loginLog := &wx_user_log.WxUserLog{
  582. UserID: userId,
  583. OpenID: openId,
  584. UnionID: unionId,
  585. CreateTime: time.Now(),
  586. Handle: "yb_login",
  587. Remark: token,
  588. }
  589. go loginLog.Create()
  590. }
  591. resp.IsBind = isBind
  592. resp.Token = token
  593. response.OkData("查询成功", resp, c)
  594. return
  595. }
  596. // @Title 登录
  597. // @Description 登录接口
  598. // @Param request body models.LoginReq true "type json string"
  599. // @Success 200 {object} models.LoginResp
  600. // @router /pc/pcLogin [post]
  601. func PcLogin(c *gin.Context) {
  602. var req pcModels.PcLoginReq
  603. if c.ShouldBind(&req) != nil {
  604. response.Fail("参数异常", c)
  605. return
  606. }
  607. userId := 0
  608. var isAdd bool
  609. if req.LoginType == 1 {
  610. if req.Mobile == "" {
  611. response.Fail("请输入手机号", c)
  612. return
  613. }
  614. if req.SmsCode == "" {
  615. response.Fail("请输入有效手机验证码", c)
  616. return
  617. }
  618. item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
  619. if err != nil {
  620. if err == utils.ErrNoRow {
  621. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  622. return
  623. } else {
  624. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  625. return
  626. }
  627. }
  628. if item == nil {
  629. response.Fail("手机验证码错误,请重新输入", c)
  630. return
  631. }
  632. wxUser, err := wx_user.GetByMobile(req.Mobile)
  633. if err != nil {
  634. if err == utils.ErrNoRow {
  635. isAdd = true
  636. } else {
  637. response.Fail("根据手机号获取用户信息失败,Err:" + err.Error(), c)
  638. return
  639. }
  640. }
  641. if wxUser == nil {
  642. isAdd = true
  643. } else {
  644. userId = int(wxUser.UserID)
  645. }
  646. //BindMobile(openId, mobile string, userId, loginType int) (err error) {
  647. //newUserId, err = models.BindMobile(openId, req.Mobile, userId, req.LoginType)
  648. } else if req.LoginType == 2 {
  649. if req.Email == "" {
  650. response.Fail("邮箱不能为空,请输入邮箱", c)
  651. return
  652. }
  653. if !utils.ValidateEmailFormatat(req.Email) {
  654. response.Fail("邮箱格式错误,请重新输入", c)
  655. return
  656. }
  657. if req.SmsCode == "" {
  658. response.Fail("请输入有效验证码", c)
  659. return
  660. }
  661. item, err := msg_code.GetMsgCode(req.Email, req.SmsCode)
  662. if err != nil {
  663. if err == utils.ErrNoRow {
  664. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  665. return
  666. } else {
  667. response.Fail("校验验证码失败,Err:" + err.Error(), c)
  668. return
  669. }
  670. }
  671. if item == nil {
  672. response.Fail("邮箱证码错误,请重新输入:", c)
  673. return
  674. }
  675. wxUser, err := wx_user.GetByEmail(req.Email)
  676. if err != nil {
  677. if err == utils.ErrNoRow {
  678. isAdd = true
  679. } else {
  680. response.Fail("根据邮箱获取用户信息失败,Err:" + err.Error(), c)
  681. return
  682. }
  683. }
  684. if wxUser == nil {
  685. isAdd = true
  686. } else {
  687. userId = int(wxUser.UserID)
  688. }
  689. } else {
  690. response.Fail("无效的登录方式,loginType:" + strconv.Itoa(req.LoginType), c)
  691. }
  692. if isAdd {
  693. user := new(wx_user.WxUser)
  694. user.CompanyID = 1
  695. user.CreatedTime = time.Now()
  696. user.FirstLogin = 1
  697. user.Enabled = 1
  698. user.Email = req.Email
  699. user.Mobile = req.Mobile
  700. if req.LoginType == 1 {
  701. user.BindAccount = user.Mobile
  702. } else {
  703. user.BindAccount = user.Email
  704. }
  705. user.RegisterTime = time.Now()
  706. user.LoginTime = time.Now()
  707. user.RegisterPlatform = 2
  708. if req.IsFreeLogin {
  709. user.IsFreeLogin = 1
  710. }else {
  711. user.IsFreeLogin = 0
  712. }
  713. lastId, err := user.Add()
  714. if err != nil {
  715. response.Fail("登录失败,新增客户信息失败,Err:" + err.Error(), c)
  716. return
  717. }
  718. fmt.Println("lastId:", lastId)
  719. userId = int(lastId)
  720. timeUnix := time.Now().Unix()
  721. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  722. token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  723. //新增session
  724. {
  725. session := new(session.Session)
  726. session.UserID = int64(userId)
  727. session.CreatedTime = time.Now()
  728. session.LastUpdatedTime = time.Now()
  729. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  730. session.AccessToken = token
  731. err = session.Create()
  732. if err != nil {
  733. response.Fail("登录失败,新增用户session信息失败:" + err.Error(), c)
  734. return
  735. }
  736. }
  737. } else {
  738. user := new(wx_user.WxUser)
  739. user.UserID = uint64(userId)
  740. if req.IsFreeLogin {
  741. user.IsFreeLogin = 1
  742. }else {
  743. user.IsFreeLogin = 0
  744. }
  745. err := user.Update([]string{"is_free_login"})
  746. if err != nil {
  747. response.Fail("登录失败,修改登录信息失败,Err:" + err.Error(), c)
  748. return
  749. }
  750. }
  751. if userId == 0 {
  752. response.Fail("登录失败,id为 0", c)
  753. return
  754. }
  755. err := wx_user.ModifyFirstLogin(uint64(userId))
  756. if err != nil {
  757. response.Fail("登录失败,判断权限失败:" + err.Error(), c)
  758. return
  759. }
  760. var token string
  761. tokenItem, err := session.GetTokenByUid(userId)
  762. if err != nil && err != utils.ErrNoRow {
  763. response.Fail("登录失败,获取token失败:" + err.Error(), c)
  764. return
  765. }
  766. if tokenItem == nil || (err != nil && err == utils.ErrNoRow) {
  767. timeUnix := time.Now().Unix()
  768. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  769. token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  770. //新增session
  771. {
  772. session := new(session.Session)
  773. session.UserID = int64(userId)
  774. session.CreatedTime = time.Now()
  775. session.LastUpdatedTime = time.Now()
  776. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  777. session.AccessToken = token
  778. err = session.Create()
  779. if err != nil {
  780. response.Fail("登录失败,新增用户session信息失败:" + err.Error(), c)
  781. return
  782. }
  783. }
  784. } else {
  785. token = tokenItem.AccessToken
  786. }
  787. //新增登录日志
  788. {
  789. loginLog := new(wx_user_log.WxUserLog)
  790. loginLog.UserID = userId
  791. loginLog.Mobile = req.Mobile
  792. loginLog.Email = req.Email
  793. loginLog.Handle = "pc_login"
  794. loginLog.CreateTime = time.Now()
  795. go loginLog.Create()
  796. }
  797. response.OkData("登录成功", token, c)
  798. }
  799. // GetSmsCode 获取短信验证码接口
  800. // @Tags 用户模块
  801. // @Summary 获取短信验证码
  802. // @Description 获取短信验证码接口
  803. // @Security ApiKeyAuth
  804. // @securityDefinitions.basic BasicAuth
  805. // @Param mobile query string true "手机号"
  806. // @Param area_num query string true "手机国际区号(中国大陆:86)"
  807. // @Accept json
  808. // @Product json
  809. // @Success 200 {string} string 获取验证码成功
  810. // @Failure 400 {string} string 手机号不能为空,请输入手机号
  811. // @Router /pc_code/get_sms_code [get]
  812. func GetSmsCode(c *gin.Context) {
  813. mobile := c.DefaultQuery("mobile", "")
  814. areaNum := c.DefaultQuery("area_num", "")
  815. err, errMsg := userLogic.PcSendSmsCode(mobile, areaNum)
  816. if err != nil {
  817. if errMsg != "" {
  818. errMsg = "获取验证码失败"
  819. }
  820. response.Fail(errMsg, c)
  821. return
  822. }
  823. response.Ok("获取验证码成功", c)
  824. }
  825. // GetEmailCode 获取邮箱验证码接口
  826. // @Tags 用户模块
  827. // @Summary 获取邮箱验证码
  828. // @Description 获取邮箱验证码
  829. // @Security ApiKeyAuth
  830. // @securityDefinitions.basic BasicAuth
  831. // @Param email query string true "电子邮箱账号"
  832. // @Accept json
  833. // @Product json
  834. // @Success 200 {string} string 获取验证码成功
  835. // @Failure 400 {string} string 请输入邮箱地址
  836. // @Router /pc_code/get_email_code [get]
  837. func GetEmailCode(c *gin.Context) {
  838. email := c.DefaultQuery("email", "")
  839. if email == "" {
  840. response.Fail("请输入邮箱地址", c)
  841. return
  842. }
  843. if !utils.ValidateEmailFormatat(email) {
  844. response.Fail("邮箱格式错误,请重新输入", c)
  845. return
  846. }
  847. err, errMsg := userLogic.PcSendEmailCode(email)
  848. if err != nil {
  849. if errMsg != "" {
  850. errMsg = "获取验证码失败"
  851. }
  852. response.Fail(errMsg, c)
  853. return
  854. }
  855. response.Ok("获取验证码成功", c)
  856. }