user.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/services"
  7. "hongze/hongze_cygx/utils"
  8. "rdluck_tools/paging"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. //用户
  14. type UserController struct {
  15. BaseAuthController
  16. }
  17. // @Title 登录
  18. // @Description 登录接口
  19. // @Param request body models.LoginReq true "type json string"
  20. // @Success 200 {object} models.LoginResp
  21. // @router /login [post]
  22. func (this *UserController) Login() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.LoginReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. user := this.User
  36. if user == nil {
  37. br.Msg = "请登录"
  38. br.ErrMsg = "请登录"
  39. br.Ret = 408
  40. return
  41. }
  42. unionId := this.User.UnionId
  43. userId := this.User.UserId
  44. if unionId == "" {
  45. br.Msg = "参数错误"
  46. br.ErrMsg = "参数错误,unionId 为空"
  47. return
  48. }
  49. newUserId := 0
  50. if req.LoginType == 1 {
  51. //BindMobile(openId, mobile string, userId, loginType int) (err error) {
  52. req.Mobile = strings.Trim(req.Mobile, " ")
  53. newUserId, err = models.PcBindMobile(unionId, req.Mobile, userId, req.LoginType)
  54. } else if req.LoginType == 2 {
  55. if req.Email == "" {
  56. br.ErrMsg = "邮箱不能为空,请输入邮箱"
  57. br.Msg = "邮箱不能为空,请输入邮箱"
  58. return
  59. }
  60. if !utils.ValidateEmailFormatat(req.Email) {
  61. br.ErrMsg = "邮箱格式错误,请重新输入"
  62. br.Msg = "邮箱格式错误,请重新输入"
  63. return
  64. }
  65. newUserId, err = models.PcBindMobile(unionId, req.Email, userId, req.LoginType)
  66. } else {
  67. br.Msg = "无效的登录方式"
  68. br.ErrMsg = "无效的登录方式,Err:" + err.Error()
  69. return
  70. }
  71. var token string
  72. tokenItem, err := models.GetTokenByUid(newUserId)
  73. if err != nil && err.Error() != utils.ErrNoRow() {
  74. br.Msg = "登录失败"
  75. br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  76. return
  77. }
  78. if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  79. timeUnix := time.Now().Unix()
  80. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  81. token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  82. //新增session
  83. {
  84. session := new(models.CygxSession)
  85. session.OpenId = unionId
  86. session.UnionId = unionId
  87. session.UserId = userId
  88. session.CreatedTime = time.Now()
  89. session.LastUpdatedTime = time.Now()
  90. session.ExpireTime = time.Now().AddDate(0, 1, 0)
  91. session.AccessToken = token
  92. err = models.AddSession(session)
  93. if err != nil {
  94. br.Msg = "登录失败"
  95. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  96. return
  97. }
  98. }
  99. } else {
  100. token = tokenItem.AccessToken
  101. }
  102. //新增登录日志
  103. {
  104. loginLog := new(models.WxUserLog)
  105. loginLog.UserId = userId
  106. loginLog.OpenId = unionId
  107. loginLog.Mobile = req.Mobile
  108. loginLog.Email = req.Email
  109. loginLog.CreateTime = time.Now()
  110. loginLog.Handle = "wechat_user_login"
  111. loginLog.Remark = token
  112. go models.AddWxUserLog(loginLog)
  113. }
  114. resp := new(models.LoginResp)
  115. resp.UserId = newUserId
  116. resp.Authorization = token
  117. br.Ret = 200
  118. br.Success = true
  119. br.Data = resp
  120. br.Msg = "登录成功"
  121. }
  122. // @Title 获取用户详情
  123. // @Description 获取用户详情接口
  124. // @Success 200 {object} models.UserDetail
  125. // @router /detail [get]
  126. func (this *UserController) Detail() {
  127. br := new(models.BaseResponse).Init()
  128. defer func() {
  129. this.Data["json"] = br
  130. this.ServeJSON()
  131. }()
  132. user := this.User
  133. if user == nil {
  134. br.Msg = "请登录"
  135. br.ErrMsg = "请登录,用户信息为空"
  136. br.Ret = 408
  137. return
  138. }
  139. item, err := models.GetUserDetailByUserId(user.UserId)
  140. if err != nil {
  141. br.Msg = "获取信息失败"
  142. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  143. return
  144. }
  145. companyItem, err := models.GetCompanyDetailById(user.CompanyId)
  146. if err != nil && err.Error() != utils.ErrNoRow() {
  147. br.Msg = "获取信息失败"
  148. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  149. return
  150. }
  151. if companyItem != nil {
  152. item.CompanyName = companyItem.CompanyName
  153. var hasPermission bool
  154. if companyItem.Status == "试用" || companyItem.Status == "永续" || companyItem.Status == "正式" {
  155. hasPermission = true
  156. permissionStr, err := models.GetCompanyPermission(companyItem.CompanyId)
  157. if err != nil {
  158. br.Msg = "获取信息失败"
  159. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  160. return
  161. }
  162. item.PermissionName = permissionStr
  163. }
  164. item.HasPermission = hasPermission
  165. }
  166. br.Ret = 200
  167. br.Success = true
  168. br.Msg = "获取成功"
  169. br.Data = item
  170. }
  171. // @Title 校验用户状态信息
  172. // @Description 校验用户状态信息
  173. // @Success 200 {object} models.CheckStatusResp
  174. // @router /check/status [get]
  175. func (this *UserController) CheckLogin() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. this.Data["json"] = br
  179. this.ServeJSON()
  180. }()
  181. if this.User == nil {
  182. br.Msg = "请登录"
  183. br.ErrMsg = "请登录"
  184. br.Ret = 408
  185. return
  186. }
  187. uid := this.User.UserId
  188. //判断token是否过期
  189. userItem, err := models.GetWxUserItemByUserId(uid)
  190. if err != nil {
  191. br.Msg = "获取用户信息失败"
  192. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  193. return
  194. }
  195. resp := new(models.CheckStatusResp)
  196. permissionStr, err := models.GetCompanyPermission(userItem.CompanyId)
  197. if err != nil {
  198. br.Msg = "获取信息失败"
  199. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  200. return
  201. }
  202. resp.PermissionName = permissionStr
  203. if userItem.Mobile == "" && userItem.Email == "" {
  204. resp.IsBind = true
  205. }
  206. if userItem.UnionId == "" {
  207. resp.IsAuth = true
  208. }
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. br.Data = resp
  212. br.Ret = 200
  213. }
  214. //
  215. //// @Title 绑定手机号或邮箱
  216. //// @Description 绑定手机号或邮箱
  217. //// @Param request body models.WxGetPhoneNumberReq true "type json string"
  218. //// @Success 200 {object} models.WxGetPhoneNumberResp
  219. //// @router /bind [post]
  220. //func (this *WechatController) Bind() {
  221. // br := new(models.BaseResponse).Init()
  222. // defer func() {
  223. // this.Data["json"] = br
  224. // this.ServeJSON()
  225. // }()
  226. // var req models.WxGetPhoneNumberReq
  227. // err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  228. // if err != nil {
  229. // br.Msg = "参数解析异常!"
  230. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  231. // return
  232. // }
  233. // if req.EncryptedData == "" || req.Iv == "" {
  234. // br.Msg = "参数错误"
  235. // return
  236. // }
  237. // user := this.User
  238. // if user == nil {
  239. // br.Msg = "请登陆"
  240. // br.Ret = 408
  241. // return
  242. // }
  243. // sessionKey := user.SessionKey
  244. // wxMobile, err := weapp.DecryptMobile(sessionKey, req.EncryptedData, req.Iv)
  245. // if err != nil {
  246. // br.Msg = "解析用户手机号信息失败"
  247. // br.ErrMsg = "解析用户手机号信息失败,Err:" + err.Error()
  248. // return
  249. // }
  250. // err = models.ModifyUsersMobile(user.UserId, wxMobile.PurePhoneNumber)
  251. // if err != nil {
  252. // br.Msg = "获取失败"
  253. // br.ErrMsg = "获取失败,Err:" + err.Error()
  254. // return
  255. // }
  256. // resp := new(models.WxGetPhoneNumberResp)
  257. // resp.Authorization = this.Token
  258. // resp.PhoneNumber = wxMobile.PhoneNumber
  259. // resp.PurePhoneNumber = wxMobile.PurePhoneNumber
  260. // resp.CountryCode = wxMobile.CountryCode
  261. // br.Msg = "获取成功!"
  262. // br.Ret = 200
  263. // br.Success = true
  264. // br.Data = resp
  265. //}
  266. // @Title 获取我的收藏
  267. // @Description 获取我的收藏列表
  268. // @Param PageSize query int true "PageSize"
  269. // @Param CurrentIndex query int true "CurrentIndex"
  270. // @Success 200 {object} models.ArticleCollectListResp
  271. // @router /collect/list [get]
  272. func (this *UserController) CollectList() {
  273. br := new(models.BaseResponse).Init()
  274. defer func() {
  275. this.Data["json"] = br
  276. this.ServeJSON()
  277. }()
  278. userId := this.User.UserId
  279. var pageSize, currentIndex, startSize int
  280. pageSize, _ = this.GetInt("PageSize")
  281. currentIndex, _ = this.GetInt("CurrentIndex")
  282. if pageSize <= 0 {
  283. pageSize = utils.PageSize20
  284. }
  285. if currentIndex <= 0 {
  286. currentIndex = 1
  287. }
  288. startSize = utils.StartIndex(currentIndex, pageSize)
  289. total, err := models.GetArticleUserCollectCount(userId)
  290. if err != nil {
  291. br.Msg = "获取数据失败"
  292. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  293. return
  294. }
  295. list, err := models.GetArticleUserCollectList(startSize, pageSize, userId)
  296. if err != nil {
  297. br.Msg = "获取数据失败"
  298. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  299. return
  300. }
  301. var articleIds []string
  302. for _, v := range list {
  303. articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
  304. }
  305. articleIdStr := strings.Join(articleIds, ",")
  306. articleMap := make(map[int]*models.ArticleDetail)
  307. if articleIdStr != "" {
  308. articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
  309. if err != nil {
  310. br.Msg = "获取数据失败"
  311. br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
  312. return
  313. }
  314. for _, v := range articleList {
  315. if _, ok := articleMap[v.ArticleId]; !ok {
  316. articleMap[v.ArticleId] = v
  317. }
  318. }
  319. }
  320. lenList := len(list)
  321. for i := 0; i < lenList; i++ {
  322. item := list[i]
  323. article := articleMap[item.ArticleId]
  324. list[i].Title = article.Title
  325. list[i].TitleEn = article.TitleEn
  326. list[i].UpdateFrequency = article.UpdateFrequency
  327. list[i].CreateDate = article.CreateDate
  328. list[i].PublishDate = article.PublishDate
  329. list[i].Body, _ = services.GetReportContentTextSub(article.Body)
  330. list[i].Abstract = article.Abstract
  331. list[i].CategoryName = article.CategoryName
  332. list[i].SubCategoryName = article.SubCategoryName
  333. }
  334. page := paging.GetPaging(currentIndex, pageSize, total)
  335. resp := new(models.ArticleCollectListResp)
  336. resp.List = list
  337. resp.Paging = page
  338. br.Msg = "获取成功!"
  339. br.Ret = 200
  340. br.Success = true
  341. br.Data = resp
  342. }
  343. // @Title 获取申请访谈列表
  344. // @Description 获取申请访谈列表
  345. // @Param PageSize query int true "PageSize"
  346. // @Param CurrentIndex query int true "CurrentIndex"
  347. // @Success 200 {object} models.ArticleInterviewApplyListResp
  348. // @router /interview/apply/list [get]
  349. func (this *UserController) InterviewApplyList() {
  350. br := new(models.BaseResponse).Init()
  351. defer func() {
  352. this.Data["json"] = br
  353. this.ServeJSON()
  354. }()
  355. userId := this.User.UserId
  356. var pageSize, currentIndex, startSize int
  357. pageSize, _ = this.GetInt("PageSize")
  358. currentIndex, _ = this.GetInt("CurrentIndex")
  359. if pageSize <= 0 {
  360. pageSize = utils.PageSize20
  361. }
  362. if currentIndex <= 0 {
  363. currentIndex = 1
  364. }
  365. startSize = utils.StartIndex(currentIndex, pageSize)
  366. total, err := models.GetArticleUserInterviewApplyCount(userId)
  367. if err != nil {
  368. br.Msg = "获取数据失败"
  369. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  370. return
  371. }
  372. list, err := models.GetArticleUserInterviewApplyList(startSize, pageSize, userId)
  373. if err != nil {
  374. br.Msg = "获取数据失败"
  375. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  376. return
  377. }
  378. var articleIds []string
  379. for _, v := range list {
  380. articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
  381. }
  382. articleIdStr := strings.Join(articleIds, ",")
  383. articleMap := make(map[int]*models.ArticleDetail)
  384. if articleIdStr != "" {
  385. articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
  386. if err != nil {
  387. br.Msg = "获取数据失败"
  388. br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
  389. return
  390. }
  391. for _, v := range articleList {
  392. if _, ok := articleMap[v.ArticleId]; !ok {
  393. articleMap[v.ArticleId] = v
  394. }
  395. }
  396. }
  397. lenList := len(list)
  398. for i := 0; i < lenList; i++ {
  399. item := list[i]
  400. article := articleMap[item.ArticleId]
  401. list[i].Title = article.Title
  402. list[i].TitleEn = article.TitleEn
  403. list[i].UpdateFrequency = article.UpdateFrequency
  404. list[i].CreateDate = article.CreateDate
  405. list[i].PublishDate = article.PublishDate
  406. list[i].Body, _ = services.GetReportContentTextSub(article.Body)
  407. list[i].Abstract = article.Abstract
  408. list[i].CategoryName = article.CategoryName
  409. list[i].SubCategoryName = article.SubCategoryName
  410. }
  411. page := paging.GetPaging(currentIndex, pageSize, total)
  412. resp := new(models.ArticleInterviewApplyListResp)
  413. resp.List = list
  414. resp.Paging = page
  415. br.Msg = "获取成功!"
  416. br.Ret = 200
  417. br.Success = true
  418. br.Data = resp
  419. }
  420. // @Title 获取浏览历史列表
  421. // @Description 获取浏览历史列表
  422. // @Param PageSize query int true "PageSize"
  423. // @Param CurrentIndex query int true "CurrentIndex"
  424. // @Success 200 {object} models.ArticleBrowseHistoryListResp
  425. // @router /browse/history/list [get]
  426. func (this *UserController) BrowseHistoryList() {
  427. br := new(models.BaseResponse).Init()
  428. defer func() {
  429. this.Data["json"] = br
  430. this.ServeJSON()
  431. }()
  432. userId := this.User.UserId
  433. var pageSize, currentIndex, startSize int
  434. pageSize, _ = this.GetInt("PageSize")
  435. currentIndex, _ = this.GetInt("CurrentIndex")
  436. if pageSize <= 0 {
  437. pageSize = utils.PageSize20
  438. }
  439. if currentIndex <= 0 {
  440. currentIndex = 1
  441. }
  442. startSize = utils.StartIndex(currentIndex, pageSize)
  443. endDate:=time.Now().AddDate(0,-1,0).Format(utils.FormatDate)
  444. total, err := models.GetArticleUserBrowseHistoryCount(userId,endDate)
  445. if err != nil {
  446. br.Msg = "获取数据失败"
  447. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  448. return
  449. }
  450. list, err := models.GetArticleUserBrowseHistoryList(startSize, pageSize, userId,endDate)
  451. if err != nil {
  452. br.Msg = "获取数据失败"
  453. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  454. return
  455. }
  456. var articleIds []string
  457. for _, v := range list {
  458. articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
  459. }
  460. articleIdStr := strings.Join(articleIds, ",")
  461. articleMap := make(map[int]*models.ArticleDetail)
  462. if articleIdStr != "" {
  463. articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
  464. if err != nil {
  465. br.Msg = "获取数据失败"
  466. br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
  467. return
  468. }
  469. for _, v := range articleList {
  470. if _, ok := articleMap[v.ArticleId]; !ok {
  471. articleMap[v.ArticleId] = v
  472. }
  473. }
  474. }
  475. lenList := len(list)
  476. for i := 0; i < lenList; i++ {
  477. item := list[i]
  478. article := articleMap[item.ArticleId]
  479. if article!=nil {
  480. list[i].Title = article.Title
  481. list[i].TitleEn = article.TitleEn
  482. list[i].UpdateFrequency = article.UpdateFrequency
  483. list[i].CreateDate = article.CreateDate
  484. list[i].PublishDate = article.PublishDate
  485. list[i].Body, _ = services.GetReportContentTextSub(article.Body)
  486. list[i].Abstract = article.Abstract
  487. list[i].CategoryName = article.CategoryName
  488. list[i].SubCategoryName = article.SubCategoryName
  489. }
  490. }
  491. page := paging.GetPaging(currentIndex, pageSize, total)
  492. resp := new(models.ArticleBrowseHistoryListResp)
  493. resp.List = list
  494. resp.Paging = page
  495. br.Msg = "获取成功!"
  496. br.Ret = 200
  497. br.Success = true
  498. br.Data = resp
  499. }