pc.go 26 KB

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