user_controller.go 15 KB

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