user_controller.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. RiskLevel string `json:"riskLevel"`
  302. Mobile string `json:"mobile"`
  303. RiskLevelStatus string `json:"riskLevelStatus"`
  304. }
  305. // Profile 获取用户信息
  306. // @Summary 获取用户信息
  307. // @Description 获取用户信息
  308. // @Success 200 {object} controllers.BaseResponse
  309. // @router /profile [get]
  310. func (u *UserController) Profile() {
  311. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  312. result = u.InitWrapData("获取用户信息详情失败")
  313. userInfo := u.Data["user"].(user.User)
  314. var userProfile user.UserProfile
  315. userProfile, err = user.GetUserProfile(userInfo.Id)
  316. u.SuccessResult("获取研究员详情成功", userProfile, result)
  317. return
  318. })
  319. }
  320. // FollowingAnalystList 获取关注动态列表
  321. // @Summary 获取关注动态列表
  322. // @Description 获取关注动态列表
  323. // @Success 200 {object} controllers.BaseResponse
  324. // @router /followingAnalystList [get]
  325. func (u *UserController) FollowingAnalystList() {
  326. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  327. result = u.InitWrapData("获取研究员详情失败")
  328. userInfo := u.Data["user"].(user.User)
  329. detail, err := user.GetFollowingAnalystList(userInfo.Id)
  330. if err != nil {
  331. u.FailedResult("获取关注研究员列表失败", result)
  332. return
  333. }
  334. u.SuccessResult("获取关注研究员列表成功", detail, result)
  335. return
  336. })
  337. }
  338. // UnReadMessageList 获取未读消息
  339. // @Summary 获取未读消息
  340. // @Description 获取未读消息
  341. // @Success 200 {object} controllers.BaseResponse
  342. // @router /message [get]
  343. func (u *UserController) UnReadMessageList() {
  344. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  345. result = u.InitWrapData("获取我的未读消息失败")
  346. userInfo := u.Data["user"].(user.User)
  347. messages, err := user.GetUnReadMessageList(userInfo.Id)
  348. if err != nil {
  349. u.FailedResult("获取我的未读消息失败", result)
  350. return
  351. }
  352. u.SuccessResult("获取我的未读消息成功", messages, result)
  353. return
  354. })
  355. }
  356. type ReadMessageReq struct {
  357. AnalystId int `json:"analystId"`
  358. MessageId int `json:"MessageId"`
  359. }
  360. // ReadMessage 获取未读消息
  361. // @Summary 获取未读消息
  362. // @Description 获取未读消息
  363. // @Success 200 {object} controllers.BaseResponse
  364. // @router /readMessage [post]
  365. func (u *UserController) ReadMessage() {
  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.MessageId <= 0 {
  371. logger.Error("消息Id非法")
  372. err = exception.New(exception.IllegalMessageId)
  373. u.FailedResult("已读消息失败", result)
  374. return
  375. }
  376. userInfo := u.Data["user"].(user.User)
  377. if user.ReadMessage(userInfo.Id, readMessageReq.MessageId) {
  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. // ReadMessages 获取未读消息
  388. // @Summary 获取未读消息
  389. // @Description 获取未读消息
  390. // @Success 200 {object} controllers.BaseResponse
  391. // @router /readMessages [post]
  392. func (u *UserController) ReadMessages() {
  393. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  394. result = u.InitWrapData("获取我的未读消息失败")
  395. readMessageReq := new(ReadMessageReq)
  396. u.GetPostParams(readMessageReq)
  397. if readMessageReq.AnalystId <= 0 {
  398. logger.Error("研究员Id非法")
  399. err = exception.New(exception.IllegalAnalystId)
  400. u.FailedResult("已读消息失败", result)
  401. return
  402. }
  403. userInfo := u.Data["user"].(user.User)
  404. if user.ReadMessages(userInfo.Id, readMessageReq.AnalystId) {
  405. u.SuccessResult("已读消息成功", nil, result)
  406. return
  407. } else {
  408. err = exception.New(exception.ReadMessageFailed)
  409. u.FailedResult("已读消息失败", result)
  410. return
  411. }
  412. })
  413. }
  414. // ReadMessages 绑定微信公众号
  415. // @Summary 绑定微信公众号
  416. // @Description 绑定微信公众号
  417. // @Success 200 {object} controllers.BaseResponse
  418. // @router /bind_gzh [post]
  419. func (u *UserController) BindGzh() {
  420. controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
  421. result = u.InitWrapData("绑定公众号失败")
  422. bindReq := new(BindGzhReq)
  423. u.GetPostParams(bindReq)
  424. code := bindReq.Code
  425. isBind, err := auth.BindWxGzh(code)
  426. if err != nil {
  427. logger.Error("绑定公众号失败:%v", err)
  428. u.FailedResult("绑定公众号失败", result)
  429. return
  430. }
  431. u.SuccessResult("绑定成功", IsBindGzhRes{
  432. IsBind: isBind,
  433. }, result)
  434. return
  435. })
  436. }
  437. type IsBindGzhRes struct {
  438. IsBind bool `json:"isBind"`
  439. }
  440. type BindGzhReq struct {
  441. Code string `json:"Code"`
  442. }
  443. // Notify 微信公众号回调
  444. // @Summary 微信公众号回调
  445. // @Description 微信公众号回调
  446. // @Success 200 {object} controllers.BaseResponse
  447. // @router /wx/notify [get,post]
  448. func (u *UserController) Notify() {
  449. echostr := u.GetString("echostr")
  450. method := u.Ctx.Input.Method()
  451. if method == "POST" {
  452. body := u.Ctx.Input.RequestBody
  453. item := new(Notify)
  454. err := xml.Unmarshal(body, &item)
  455. if err != nil {
  456. logger.Info("xml.Unmarshal:" + err.Error())
  457. }
  458. contactMsg := "你好,欢迎关注期海通行-投研点金!"
  459. var openId, returnResult string
  460. if item.MsgType != "" {
  461. openId = item.FromUserName
  462. }
  463. xmlTpl := `<xml>
  464. <ToUserName><![CDATA[%s]]></ToUserName>
  465. <FromUserName><![CDATA[%s]]></FromUserName>
  466. <CreateTime>%s</CreateTime>
  467. <MsgType><![CDATA[text]]></MsgType>
  468. <Content><![CDATA[%s]]></Content>
  469. </xml>`
  470. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  471. // WxId := "gh_5dc508325c6f" // 弘则投研公众号原始id
  472. xmlTpl = fmt.Sprintf(xmlTpl, openId, auth.HT_WX_ID, createTime, contactMsg)
  473. if item.MsgType == "event" {
  474. switch item.Event {
  475. case "order":
  476. fmt.Println("关注")
  477. go auth.BindWxGzhByOpenId(openId)
  478. case "unsubscribe":
  479. fmt.Println("取消关注")
  480. go auth.UnSubscribeWxGzhByOpenId(openId)
  481. case "CLICK":
  482. //returnResult = xmlTpl
  483. default:
  484. logger.Info("wechat notify event:" + item.Event)
  485. }
  486. u.Ctx.WriteString(xmlTpl)
  487. } else {
  488. returnResult = xmlTpl
  489. }
  490. u.Ctx.WriteString(returnResult)
  491. } else {
  492. u.Ctx.WriteString(echostr)
  493. }
  494. }
  495. type Notify struct {
  496. ToUserName string `xml:"ToUserName"`
  497. FromUserName string `xml:"FromUserName"`
  498. CreateTime int `xml:"CreateTime"`
  499. MsgType string `xml:"MsgType"`
  500. Event string `xml:"Event"`
  501. EventKey string `xml:"EventKey"`
  502. Content string `xml:"Content"`
  503. }
  504. type SyncCustomerRiskLevelReq struct {
  505. CustInfo CustInfo `json:"custInfo"`
  506. RiskInfo RiskInfo `json:"riskInfo"`
  507. }
  508. type CustInfo struct {
  509. MobileTel string `json:"mobile_tel"`
  510. ClientName string `json:"client_name"`
  511. IdKind string `json:"id_kind"`
  512. IdNo string `json:"id_no"`
  513. }
  514. type RiskInfo struct {
  515. CorpBeginDate string `json:"corp_begin_date"`
  516. CorpEndDate string `json:"corp_end_date"`
  517. UserInvestTerm string `json:"user_invest_term"`
  518. UserInvestKind string `json:"user_invest_kind"`
  519. CorpRiskLevel string `json:"corp_risk_level"`
  520. }
  521. type WebhookRequest struct {
  522. Data string `json:"data"`
  523. //EncryptKey string `json:"encryptKey"`
  524. }