user_controller.go 15 KB

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