user_api_controller.go 6.1KB

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