user_controller.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package user
  2. import (
  3. "encoding/xml"
  4. "eta/eta_mini_ht_api/common/component/cache"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/common/exception"
  7. authUtils "eta/eta_mini_ht_api/common/utils/auth"
  8. "eta/eta_mini_ht_api/controllers"
  9. userService "eta/eta_mini_ht_api/domian/user"
  10. "eta/eta_mini_ht_api/service/auth"
  11. "eta/eta_mini_ht_api/service/user"
  12. "fmt"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. // UserController Operations about Users
  18. type UserController struct {
  19. controllers.BaseController
  20. redis *cache.RedisCache
  21. }
  22. func (u *UserController) Prepare() {
  23. u.redis = cache.GetInstance()
  24. }
  25. type LoginCode struct {
  26. Code int `json:"code"`
  27. Msg string `json:"msg"`
  28. }
  29. // Get @Title GetAll
  30. // @Description get all Users
  31. // @Success 200 {object} models.User
  32. // @router / [get]
  33. func (u *UserController) Get() {
  34. logger.Warn("查询用户列表:%s")
  35. fmt.Print("查询用户列表")
  36. u.ServeJSON()
  37. }
  38. type FeedbackReq struct {
  39. Mobile string `json:"mobile"`
  40. Message string `json:"message"`
  41. }
  42. type FollowAnalystReq struct {
  43. AnalystId int `json:"analystId"`
  44. AnalystName string `json:"analystName"`
  45. FollowType string `json:"followType"`
  46. Mobile string `json:"mobile"`
  47. ByName bool `json:"byName"`
  48. }
  49. // Feedback 用户意见反馈
  50. // @Summary 用户意见反馈
  51. // @Description 用户意见反馈
  52. // @Success 200 {object} controllers.BaseResponse
  53. // @router /feedback [post]
  54. func (u *UserController) Feedback() {
  55. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  56. result = u.InitWrapData("提交反馈失败")
  57. feedback := new(FeedbackReq)
  58. u.GetPostParams(feedback)
  59. if !authUtils.IsValidMobile(feedback.Mobile) {
  60. u.FailedResult("手机号非法", result)
  61. err = exception.New(exception.IllegalPhoneNumber)
  62. return
  63. }
  64. var userInfo user.User
  65. userInfo = u.Data["user"].(user.User)
  66. if userInfo.Mobile != feedback.Mobile {
  67. u.FailedResult("非当前用户的手机号提交", result)
  68. err = exception.New(exception.NotCurrentUserError)
  69. return
  70. }
  71. if feedback.Message == "" {
  72. u.FailedResult("反馈信息不能为空", result)
  73. err = exception.New(exception.FeedBackMsgEmpty)
  74. return
  75. }
  76. err = user.FeedBack(userInfo.Id, feedback.Mobile, feedback.Message)
  77. if err != nil {
  78. err = exception.New(exception.FeedBackError)
  79. u.FailedResult("提交反馈失败", result)
  80. return
  81. }
  82. u.SuccessResult("提交反馈成功", nil, result)
  83. return
  84. })
  85. }
  86. type BatchFollowResp struct {
  87. FollowStatus string `json:"followStatus"`
  88. List []userService.FollowDTO `json:"list"`
  89. }
  90. type FollowResp struct {
  91. AnalystId int `json:"analystId"`
  92. FollowedType string `json:"FollowedType"`
  93. }
  94. type FollowAnalystsReq struct {
  95. AnalystNames string `json:"analystNames"`
  96. FollowType string `json:"followType"`
  97. Mobile string `json:"mobile"`
  98. }
  99. // FollowAnalysts 批量关注研究员
  100. // @Summary 批量关注研究员
  101. // @Description 批量关注研究员
  102. // @Success 200 {object} controllers.BaseResponse
  103. // @router /followAnalysts [post]
  104. func (u *UserController) FollowAnalysts() {
  105. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  106. result = u.InitWrapData("批量关注失败")
  107. followAnalyst := new(FollowAnalystsReq)
  108. u.GetPostParams(followAnalyst)
  109. if !authUtils.IsValidMobile(followAnalyst.Mobile) {
  110. u.FailedResult("手机号非法", result)
  111. err = exception.New(exception.IllegalPhoneNumber)
  112. return
  113. }
  114. var userInfo user.User
  115. userInfo = u.Data["user"].(user.User)
  116. if userInfo.Mobile != followAnalyst.Mobile {
  117. u.FailedResult("非当前用户的手机号提交", result)
  118. err = exception.New(exception.NotCurrentUserError)
  119. return
  120. }
  121. if !checkFollowType(followAnalyst.FollowType) {
  122. u.FailedResult("关注状态非法", result)
  123. err = exception.New(exception.IllegalFollowType)
  124. return
  125. }
  126. var msg string
  127. switch followAnalyst.FollowType {
  128. case "following":
  129. msg = "批量关注研究员"
  130. case "unfollowed":
  131. msg = "批量取消关注研究员"
  132. }
  133. var nameList []string
  134. names := followAnalyst.AnalystNames
  135. if strings.HasPrefix(names, ",") {
  136. names = names[1:]
  137. }
  138. if strings.HasSuffix(names, ",") {
  139. names = names[0 : len(names)-1]
  140. }
  141. if names == "" {
  142. nameList = []string{}
  143. } else {
  144. nameList = strings.Split(names, ",")
  145. }
  146. for i := 0; i < len(nameList); i++ {
  147. nameList[i] = strings.TrimSpace(nameList[i])
  148. if nameList[i] == "" {
  149. u.FailedResult("通过研究员姓名关注失败", result)
  150. err = exception.New(exception.AnalystNameEmptyError)
  151. return
  152. }
  153. }
  154. err = user.FollowAnalystsByName(userInfo.Id, nameList, followAnalyst.FollowType)
  155. if err != nil {
  156. u.FailedResult(msg+"失败", result)
  157. return
  158. }
  159. u.SuccessResult(msg+"成功", nil, result)
  160. return
  161. })
  162. }
  163. // CheckFollowStatus 获取关注状态
  164. // @Summary 获取关注状态
  165. // @Description 获取关注状态
  166. // @Success 200 {object} controllers.BaseResponse
  167. // @router /checkFollowStatus [get]
  168. func (u *UserController) CheckFollowStatus(names string) {
  169. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  170. result = u.InitWrapData("获取关注状态失败")
  171. var userInfo user.User
  172. userInfo = u.Data["user"].(user.User)
  173. var nameList []string
  174. if strings.HasPrefix(names, ",") {
  175. names = names[1:len(names)]
  176. }
  177. if strings.HasSuffix(names, ",") {
  178. names = names[0 : len(names)-1]
  179. }
  180. if names == "" {
  181. nameList = []string{}
  182. } else {
  183. nameList = strings.Split(names, ",")
  184. }
  185. for i := 0; i < len(nameList); i++ {
  186. nameList[i] = strings.TrimSpace(nameList[i])
  187. }
  188. list, err := user.CheckFollowStatusByNames(userInfo.Id, nameList)
  189. status := true
  190. data := BatchFollowResp{
  191. List: list,
  192. }
  193. for i := 0; i < len(list); i++ {
  194. if !checkFollowing(list[i].FollowType) {
  195. status = false
  196. break
  197. }
  198. }
  199. if status {
  200. data.FollowStatus = "following"
  201. } else {
  202. data.FollowStatus = "unfollowed"
  203. }
  204. if err != nil {
  205. u.FailedResult("获取关注状态失败", result)
  206. return
  207. }
  208. u.SuccessResult("获取关注状态成功", data, result)
  209. return
  210. })
  211. }
  212. func checkFollowing(followType string) bool {
  213. return followType == "following"
  214. }
  215. // FollowAnalyst 关注研究员
  216. // @Summary 关注研究员
  217. // @Description 关注研究员
  218. // @Success 200 {object} controllers.BaseResponse
  219. // @router /followAnalyst [post]
  220. func (u *UserController) FollowAnalyst() {
  221. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  222. result = u.InitWrapData("")
  223. followAnalyst := new(FollowAnalystReq)
  224. u.GetPostParams(followAnalyst)
  225. if !authUtils.IsValidMobile(followAnalyst.Mobile) {
  226. u.FailedResult("手机号非法", result)
  227. err = exception.New(exception.IllegalPhoneNumber)
  228. return
  229. }
  230. var userInfo user.User
  231. userInfo = u.Data["user"].(user.User)
  232. if userInfo.Mobile != followAnalyst.Mobile {
  233. u.FailedResult("非当前用户的手机号提交", result)
  234. err = exception.New(exception.NotCurrentUserError)
  235. return
  236. }
  237. if !checkFollowType(followAnalyst.FollowType) {
  238. u.FailedResult("关注状态非法", result)
  239. err = exception.New(exception.IllegalFollowType)
  240. return
  241. }
  242. var msg string
  243. switch followAnalyst.FollowType {
  244. case "following":
  245. msg = "关注研究员"
  246. case "unfollowed":
  247. msg = "取消关注研究员"
  248. }
  249. if followAnalyst.ByName {
  250. logger.Info("通过研究员姓名关注")
  251. if followAnalyst.AnalystName == "" {
  252. u.FailedResult("通过研究员姓名关注失败", result)
  253. err = exception.New(exception.AnalystNameEmptyError)
  254. return
  255. }
  256. err = user.FollowAnalystByName(userInfo.Id, followAnalyst.AnalystName, followAnalyst.FollowType)
  257. } else {
  258. if followAnalyst.AnalystId <= 0 {
  259. u.FailedResult("通过研究员姓名关注失败", result)
  260. err = exception.New(exception.IllegalAnalystIdError)
  261. return
  262. }
  263. err = user.FollowAnalyst(userInfo.Id, followAnalyst.AnalystId, followAnalyst.FollowType)
  264. }
  265. if err != nil {
  266. u.FailedResult(msg+"失败", result)
  267. return
  268. }
  269. resp := FollowResp{
  270. AnalystId: followAnalyst.AnalystId,
  271. FollowedType: followAnalyst.FollowType,
  272. }
  273. u.SuccessResult(msg+"成功", resp, result)
  274. return
  275. })
  276. }
  277. func checkFollowType(followType string) bool {
  278. return followType == "following" || followType == "unfollowed"
  279. }
  280. // FollowingAnalysts 关注研究员
  281. // @Summary 关注研究员
  282. // @Description 关注研究员
  283. // @Success 200 {object} controllers.BaseResponse
  284. // @router /followingAnalysts [get]
  285. func (u *UserController) FollowingAnalysts(analystId int) {
  286. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  287. result = u.InitWrapData("关注研究员列表失败")
  288. fmt.Println(analystId)
  289. userInfo := u.Data["user"].(user.User)
  290. detail, err := user.GetAnalystDetail(userInfo.Id, analystId)
  291. if err != nil {
  292. u.FailedResult("关注研究员失败", result)
  293. return
  294. }
  295. u.SuccessResult("关注研究员成功", detail, result)
  296. return
  297. })
  298. }
  299. type UserProfileReq struct {
  300. UserName string `json:"userName"`
  301. Mobile string `json:"mobile"`
  302. }
  303. // Profile 获取用户信息
  304. // @Summary 获取用户信息
  305. // @Description 获取用户信息
  306. // @Success 200 {object} controllers.BaseResponse
  307. // @router /profile [get]
  308. func (u *UserController) Profile() {
  309. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  310. result = u.InitWrapData("获取用户信息详情失败")
  311. userInfo := u.Data["user"].(user.User)
  312. u.SuccessResult("获取研究员详情成功", covertToUserProfile(userInfo), result)
  313. return
  314. })
  315. }
  316. // FollowingAnalystList 获取关注动态列表
  317. // @Summary 获取关注动态列表
  318. // @Description 获取关注动态列表
  319. // @Success 200 {object} controllers.BaseResponse
  320. // @router /followingAnalystList [get]
  321. func (u *UserController) FollowingAnalystList() {
  322. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  323. result = u.InitWrapData("获取研究员详情失败")
  324. userInfo := u.Data["user"].(user.User)
  325. detail, err := user.GetFollowingAnalystList(userInfo.Id)
  326. if err != nil {
  327. u.FailedResult("获取关注研究员列表失败", result)
  328. return
  329. }
  330. u.SuccessResult("获取关注研究员列表成功", detail, result)
  331. return
  332. })
  333. }
  334. // UnReadMessageList 获取未读消息
  335. // @Summary 获取未读消息
  336. // @Description 获取未读消息
  337. // @Success 200 {object} controllers.BaseResponse
  338. // @router /message [get]
  339. func (u *UserController) UnReadMessageList() {
  340. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  341. result = u.InitWrapData("获取我的未读消息失败")
  342. userInfo := u.Data["user"].(user.User)
  343. messages, err := user.GetUnReadMessageList(userInfo.Id)
  344. if err != nil {
  345. u.FailedResult("获取我的未读消息失败", result)
  346. return
  347. }
  348. u.SuccessResult("获取我的未读消息成功", messages, result)
  349. return
  350. })
  351. }
  352. type ReadMessageReq struct {
  353. AnalystId int `json:"analystId"`
  354. MessageId int `json:"MessageId"`
  355. }
  356. // ReadMessage 获取未读消息
  357. // @Summary 获取未读消息
  358. // @Description 获取未读消息
  359. // @Success 200 {object} controllers.BaseResponse
  360. // @router /readMessage [post]
  361. func (u *UserController) ReadMessage() {
  362. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  363. result = u.InitWrapData("获取我的未读消息失败")
  364. readMessageReq := new(ReadMessageReq)
  365. u.GetPostParams(readMessageReq)
  366. if readMessageReq.MessageId <= 0 {
  367. logger.Error("消息Id非法")
  368. err = exception.New(exception.IllegalMessageId)
  369. u.FailedResult("已读消息失败", result)
  370. return
  371. }
  372. userInfo := u.Data["user"].(user.User)
  373. if user.ReadMessage(userInfo.Id, readMessageReq.MessageId) {
  374. u.SuccessResult("已读消息成功", nil, result)
  375. return
  376. } else {
  377. err = exception.New(exception.ReadMessageFailed)
  378. u.FailedResult("已读消息失败", result)
  379. return
  380. }
  381. })
  382. }
  383. // ReadMessages 获取未读消息
  384. // @Summary 获取未读消息
  385. // @Description 获取未读消息
  386. // @Success 200 {object} controllers.BaseResponse
  387. // @router /readMessages [post]
  388. func (u *UserController) ReadMessages() {
  389. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  390. result = u.InitWrapData("获取我的未读消息失败")
  391. readMessageReq := new(ReadMessageReq)
  392. u.GetPostParams(readMessageReq)
  393. if readMessageReq.AnalystId <= 0 {
  394. logger.Error("研究员Id非法")
  395. err = exception.New(exception.IllegalAnalystId)
  396. u.FailedResult("已读消息失败", result)
  397. return
  398. }
  399. userInfo := u.Data["user"].(user.User)
  400. if user.ReadMessages(userInfo.Id, readMessageReq.AnalystId) {
  401. u.SuccessResult("已读消息成功", nil, result)
  402. return
  403. } else {
  404. err = exception.New(exception.ReadMessageFailed)
  405. u.FailedResult("已读消息失败", result)
  406. return
  407. }
  408. })
  409. }
  410. func covertToUserProfile(user user.User) UserProfileReq {
  411. return UserProfileReq{
  412. UserName: user.Username,
  413. Mobile: user.Mobile,
  414. }
  415. }
  416. // ReadMessages 绑定微信公众号
  417. // @Summary 绑定微信公众号
  418. // @Description 绑定微信公众号
  419. // @Success 200 {object} controllers.BaseResponse
  420. // @router /bind_gzh [post]
  421. func (u *UserController) BindGzh() {
  422. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  423. result = u.InitWrapData("绑定公众号失败")
  424. bindReq := new(BindGzhReq)
  425. u.GetPostParams(bindReq)
  426. code := bindReq.Code
  427. isBind, err := auth.BindWxGzh(code)
  428. if err != nil {
  429. logger.Error("绑定公众号失败:%v", err)
  430. u.FailedResult("绑定公众号失败", result)
  431. return
  432. }
  433. u.SuccessResult("绑定成功", IsBindGzhRes{
  434. IsBind: isBind,
  435. }, result)
  436. return
  437. })
  438. }
  439. type IsBindGzhRes struct {
  440. IsBind bool `json:"isBind"`
  441. }
  442. type BindGzhReq struct {
  443. Code string `json:"Code"`
  444. }
  445. // Notify 微信公众号回调
  446. // @Summary 微信公众号回调
  447. // @Description 微信公众号回调
  448. // @Success 200 {object} controllers.BaseResponse
  449. // @router /wx/notify [get,post]
  450. func (u *UserController) Notify() {
  451. echostr := u.GetString("echostr")
  452. method := u.Ctx.Input.Method()
  453. if method == "POST" {
  454. body := u.Ctx.Input.RequestBody
  455. item := new(Notify)
  456. err := xml.Unmarshal(body, &item)
  457. if err != nil {
  458. logger.Info("xml.Unmarshal:" + err.Error())
  459. }
  460. contactMsg := "你好,欢迎关注期海通行-投研点金!"
  461. var openId, returnResult string
  462. if item.MsgType != "" {
  463. openId = item.FromUserName
  464. }
  465. xmlTpl := `<xml>
  466. <ToUserName><![CDATA[%s]]></ToUserName>
  467. <FromUserName><![CDATA[%s]]></FromUserName>
  468. <CreateTime>%s</CreateTime>
  469. <MsgType><![CDATA[text]]></MsgType>
  470. <Content><![CDATA[%s]]></Content>
  471. </xml>`
  472. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  473. // WxId := "gh_5dc508325c6f" // 弘则投研公众号原始id
  474. xmlTpl = fmt.Sprintf(xmlTpl, openId, auth.HT_WX_ID, createTime, contactMsg)
  475. if item.MsgType == "event" {
  476. switch item.Event {
  477. case "subscribe":
  478. fmt.Println("关注")
  479. go auth.BindWxGzhByOpenId(openId)
  480. case "unsubscribe":
  481. fmt.Println("取消关注")
  482. go auth.UnSubscribeWxGzhByOpenId(openId)
  483. case "CLICK":
  484. //returnResult = xmlTpl
  485. default:
  486. logger.Info("wechat notify event:" + item.Event)
  487. }
  488. u.Ctx.WriteString(xmlTpl)
  489. } else {
  490. returnResult = xmlTpl
  491. }
  492. u.Ctx.WriteString(returnResult)
  493. } else {
  494. u.Ctx.WriteString(echostr)
  495. }
  496. }
  497. type Notify struct {
  498. ToUserName string `xml:"ToUserName"`
  499. FromUserName string `xml:"FromUserName"`
  500. CreateTime int `xml:"CreateTime"`
  501. MsgType string `xml:"MsgType"`
  502. Event string `xml:"Event"`
  503. EventKey string `xml:"EventKey"`
  504. Content string `xml:"Content"`
  505. }