123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package controllers
-
- import (
- "encoding/json"
- "fmt"
- "sws_xcx/enums"
- "sws_xcx/models"
- "sws_xcx/service"
-
- "github.com/medivhzhan/weapp/v3/phonenumber"
- )
-
- type UserApiController struct {
- BaseApiAuthController
- }
-
- // @Title GetUserInfo
- // @Description 获取个人中心信息
- // @Success 200 {object} models.UserInfoResp success
- // @Failure 500 error
- // @Security token
- // @router /getuserinfo [get]
- func (c *UserApiController) GetUserInfo() {
-
- resp := models.UserInfoResp{
- Id: c.CurrentUser.Id,
- Avatar: c.CurrentUser.Avatar,
- Email: c.CurrentUser.Email,
- NickName: c.CurrentUser.NickName,
- Phone: c.CurrentUser.Phone,
- PrivacyProtocolVersions: c.CurrentUser.PrivacyProtocolVersions,
- UnionId: c.CurrentUser.UnionId,
- OpenId: c.CurrentUser.OpenId,
- Status: c.CurrentUser.Status,
- Source: c.CurrentUser.Source,
- Ctime: models.Time(c.CurrentUser.Ctime),
- Mtime: models.Time(c.CurrentUser.Mtime),
- UserOrgId: c.CurrentUser.UserOrgId,
- PatientId: c.CurrentUser.PatientId,
- }
-
- patient, _ := service.NewXcxUserService().GetPatientInfo(c.CurrentUser.PatientId)
-
- resp.DialysisNo = patient.DialysisNo
- resp.HealthCareNo = patient.HealthCareNo
-
- p, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(c.CurrentUser.Id)
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- }
- if p != nil {
-
- resp.RealName = p.RealName
- resp.IdCard = p.IdCard
- resp.InpatientRegPhone = p.InpatientRegPhone
- }
-
- c.ServeSuccessJSON(resp)
- }
-
- // @Title UpdatePhoneByCode
- // @Description 获取小程序绑定的手机号码并更新到用户信息
- // @Param body body models.WxXcxLoginReq true "小程序登录请求参数"
- // @Success 200 {object} models.XcxUser success
- // @Failure 500 error
- // @Security token
- // @router /updatephonebycode [post]
- func (c *UserApiController) UpdatePhoneByCode() {
- dataBody := models.WxXcxLoginReq{}
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody); err != nil || dataBody.Code == "" {
-
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
- return
- }
-
- wxcli := service.GetWxSdk().NewPhonenumber()
- resp, err := wxcli.GetPhoneNumber(&phonenumber.GetPhoneNumberRequest{
- Code: dataBody.Code,
- })
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- return
- }
- if resp.ErrCode != 0 {
- c.ServeDynamicFailJsonSend(resp.ErrMSG)
- return
- }
-
- err = c.updateCurrentUser(func(u *models.XcxUser) {
- u.Phone = resp.Data.PhoneNumber
- })
-
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- return
- }
- c.ServeSuccessJSON(c.CurrentUser)
-
- }
-
- // @Title SaveUserInfo
- // @Description 个人中心保存用户信息和透析病友信息
- // @Param body body models.SaveUserInfoReq true "小程序登录请求参数"
- // @Success 200 success
- // @Failure 500 error
- // @Security token
- // @router /saveuserinfo [post]
- func (c *UserApiController) SaveUserInfo() {
- req := &models.SaveUserInfoReq{}
-
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
- return
- }
-
- p, err1 := service.NewXcxUserService().GetMobilePatientInfo(req.InpatientRegPhone, req.RealName, req.IdCard)
-
- err := c.updateCurrentUser(func(u *models.XcxUser) {
- u.Avatar = req.Avatar
- u.Email = req.Email
- u.NickName = req.NickName
- u.Phone = req.Phone
- if p.ID > 0 {
- u.PatientId = p.ID
- }
- if p.UserOrgId > 0 {
- u.UserOrgId = p.UserOrgId
- }
-
- })
-
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- return
- }
-
- err = service.NewUserHealthProfileService().SavePatientInfo(
- c.CurrentUser.Id,
- req.RealName,
- req.IdCard,
- req.InpatientRegPhone)
-
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- return
- }
-
- if err1 != nil {
- c.ServeDynamicFailJsonSend(fmt.Sprintf("资料已保存,透析资料绑定失败:%v", err))
- return
- }
-
- if p.ID == 0 {
- c.ServeDynamicFailJsonSend("资料已保存,找不到绑定的病友资料")
- return
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "patient_id": p.ID,
- "user_org_id": p.UserOrgId,
- })
-
- }
-
- // @Title SaveHealthProfile
- // @Description 保存健康档案
- // @Param body body models.SaveHealthProfileReq true "小程序登录请求参数"
- // @Success 200 success
- // @Failure 500 error
- // @Security token
- // @router /savehealthprofile [post]
- func (c *UserApiController) SaveHealthProfile() {
-
- req := &models.SaveHealthProfileReq{}
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, req); err != nil {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)
- return
- }
- err := service.NewUserHealthProfileService().SaveHealthProfile(c.CurrentUser.Id, *req)
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- return
- }
- c.ServeSuccessJSON(new(interface{}))
-
- }
-
- // @Title GetHealthProfile
- // @Description 获取健康档案
- // @Success 200 {object} models.HealthProfileResp success
- // @Failure 500 error
- // @Security token
- // @router /gethealthprofile [get]
- func (c *UserApiController) GetHealthProfile() {
- p, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(c.CurrentUser.Id)
- if err != nil {
- c.ServeDynamicFailJsonSend(err.Error())
- return
- }
-
- resp := models.HealthProfileResp{}
- if p != nil {
-
- resp.Birthday = models.Time(p.Birthday)
- resp.BloodType = p.BloodType
- resp.CreatineTime = models.Time(p.CreatineTime)
- resp.Creatinine = p.Creatinine
- resp.CreatinineUnit = p.CreatinineUnit
- resp.Ctime = models.Time(p.Ctime)
- resp.Gender = p.Gender
- resp.Height = p.Height
- resp.Id = p.Id
- resp.IllnessState = p.IllnessState
- resp.Mtime = models.Time(p.Mtime)
- resp.RenalFunctionStatus = p.RenalFunctionStatus
- resp.Status = p.Status
- resp.UrineProtein = p.UrineProtein
- resp.UrineProteinTime = models.Time(p.UrineProteinTime)
- resp.UrineProteinUnit = p.UrineProteinUnit
- resp.UrineProtein24h = p.UrineProtein24h
- resp.UrineProtein24hTime = models.Time(p.UrineProtein24hTime)
- resp.UrineProtein24hUnit = p.UrineProtein24hUnit
-
- resp.Weight = p.Weight
-
- }
-
- c.ServeSuccessJSON(resp)
- }
|