user_api_controller.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "sws_xcx/enums"
  5. "sws_xcx/models"
  6. "sws_xcx/service"
  7. "github.com/medivhzhan/weapp/v3/phonenumber"
  8. )
  9. type UserApiController struct {
  10. BaseApiAuthController
  11. }
  12. // @Title GetUserInfo
  13. // @Description 获取个人中心信息
  14. // @Success 200 {object} models.UserInfoResp success
  15. // @Failure 500 error
  16. // @Security token
  17. // @router /getuserinfo [get]
  18. func (c *UserApiController) GetUserInfo() {
  19. resp := models.UserInfoResp{
  20. Id: c.CurrentUser.Id,
  21. Avatar: c.CurrentUser.Avatar,
  22. Email: c.CurrentUser.Email,
  23. NickName: c.CurrentUser.NickName,
  24. Phone: c.CurrentUser.Phone,
  25. PrivacyProtocolVersions: c.CurrentUser.PrivacyProtocolVersions,
  26. UnionId: c.CurrentUser.UnionId,
  27. OpenId: c.CurrentUser.OpenId,
  28. Status: c.CurrentUser.Status,
  29. Source: c.CurrentUser.Source,
  30. Ctime: c.CurrentUser.Ctime,
  31. Mtime: c.CurrentUser.Mtime,
  32. }
  33. c.ServeSuccessJSON(resp)
  34. }
  35. // @Title UpdatePhoneByCode
  36. // @Description 获取小程序绑定的手机号码并更新到用户信息
  37. // @Param body body models.WxXcxLoginReq true "小程序登录请求参数"
  38. // @Success 200 {object} models.XcxUser success
  39. // @Failure 500 error
  40. // @Security token
  41. // @router /updatephonebycode [post]
  42. func (c *UserApiController) UpdatePhoneByCode() {
  43. dataBody := models.WxXcxLoginReq{}
  44. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody); err != nil || dataBody.Code == "" {
  45. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  46. return
  47. }
  48. wxcli := service.GetWxSdk().NewPhonenumber()
  49. resp, err := wxcli.GetPhoneNumber(&phonenumber.GetPhoneNumberRequest{
  50. Code: dataBody.Code,
  51. })
  52. if err != nil {
  53. c.ServeDynamicFailJsonSend(err.Error())
  54. return
  55. }
  56. if resp.ErrCode != 0 {
  57. c.ServeDynamicFailJsonSend(resp.ErrMSG)
  58. return
  59. }
  60. err = c.updateCurrentUser(func(u *models.XcxUser) {
  61. u.Phone = resp.Data.PhoneNumber
  62. })
  63. if err != nil {
  64. c.ServeDynamicFailJsonSend(err.Error())
  65. return
  66. }
  67. c.ServeSuccessJSON(c.CurrentUser)
  68. }
  69. // @Title SaveUserInfo
  70. // @Description 个人中心保存用户信息和透析病友信息
  71. // @Param body body models.SaveUserInfoReq true "小程序登录请求参数"
  72. // @Success 200 success
  73. // @Failure 500 error
  74. // @Security token
  75. // @router /saveuserinfo [post]
  76. func (c *UserApiController) SaveUserInfo() {
  77. req := &models.SaveUserInfoReq{}
  78. if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
  79. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  80. return
  81. }
  82. err := c.updateCurrentUser(func(u *models.XcxUser) {
  83. u.Avatar = req.Avatar
  84. u.Email = req.Email
  85. u.NickName = req.NickName
  86. u.Phone = req.Phone
  87. })
  88. if err != nil {
  89. c.ServeDynamicFailJsonSend(err.Error())
  90. return
  91. }
  92. err = service.NewUserHealthProfileService().SavePatientInfo(
  93. c.CurrentUser.Id,
  94. req.RealName,
  95. req.IdCard,
  96. req.InpatientRegPhone)
  97. if err != nil {
  98. c.ServeDynamicFailJsonSend(err.Error())
  99. return
  100. }
  101. c.ServeSuccessJSON(new(interface{}))
  102. }
  103. // @Title SaveHealthProfile
  104. // @Description 保存健康档案
  105. // @Param body body models.SaveHealthProfileReq true "小程序登录请求参数"
  106. // @Success 200 success
  107. // @Failure 500 error
  108. // @Security token
  109. // @router /savehealthprofile [post]
  110. func (c *UserApiController) SaveHealthProfile() {
  111. req := &models.SaveHealthProfileReq{}
  112. if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
  113. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
  114. return
  115. }
  116. err := service.NewUserHealthProfileService().SaveHealthProfile(c.CurrentUser.Id, *req)
  117. if err != nil {
  118. c.ServeDynamicFailJsonSend(err.Error())
  119. return
  120. }
  121. c.ServeSuccessJSON(new(interface{}))
  122. }
  123. // @Title GetHealthProfile
  124. // @Description 获取健康档案
  125. // @Success 200 {object} models.HealthProfileResp success
  126. // @Failure 500 error
  127. // @Security token
  128. // @router /gethealthprofile [get]
  129. func (c *UserApiController) GetHealthProfile() {
  130. p, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(c.CurrentUser.Id)
  131. if err != nil {
  132. c.ServeDynamicFailJsonSend(err.Error())
  133. return
  134. }
  135. resp := models.HealthProfileResp{}
  136. if p != nil {
  137. resp.Birthday = p.Birthday
  138. resp.BloodType = p.BloodType
  139. resp.CreatineTime = p.CreatineTime
  140. resp.Creatinine = p.Creatinine
  141. resp.CreatinineUnit = p.CreatinineUnit
  142. resp.Ctime = p.Ctime
  143. resp.Gender = p.Gender
  144. resp.Height = p.Height
  145. resp.Id = p.Id
  146. resp.IllnessState = p.IllnessState
  147. resp.Mtime = p.Mtime
  148. resp.RenalFunctionStatus = p.RenalFunctionStatus
  149. resp.Status = p.Status
  150. resp.UrineProtein = p.UrineProtein
  151. resp.UrineProteinTime = p.UrineProteinTime
  152. resp.UrineProteinUnit = p.UrineProteinUnit
  153. resp.UrineProtein24h = p.UrineProtein24h
  154. resp.UrineProtein24hTime = p.UrineProtein24hTime
  155. resp.UrineProtein24hUnit = p.UrineProtein24hUnit
  156. resp.Weight = p.Weight
  157. }
  158. c.ServeSuccessJSON(resp)
  159. }