login_api_controller.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package mobile_api_controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/jinzhu/gorm"
  10. "io/ioutil"
  11. "net/http"
  12. "net/url"
  13. "strconv"
  14. "github.com/astaxie/beego"
  15. )
  16. type LoginAPIController struct {
  17. MobileBaseAPIController
  18. }
  19. // /m/api/login/pwd [post] LoginByPwd
  20. // @param mobile:string
  21. // @param password:string
  22. func (this *LoginAPIController) LoginByPwd() {
  23. mobile := this.GetString("mobile")
  24. pwd := this.GetString("password")
  25. //var mobile = "15089497668"
  26. //var pwd = "123456"
  27. if len(mobile) == 0 || len(pwd) == 0 || utils.CellPhoneRegexp().MatchString(mobile) == false {
  28. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  29. return
  30. }
  31. ip := utils.GetIP(this.Ctx.Request)
  32. ssoDomain := beego.AppConfig.String("sso_domain")
  33. //ssoDomain := "http://localhost:8091"
  34. api := ssoDomain + "/m/login/pwd"
  35. values := make(url.Values)
  36. values.Set("mobile", mobile)
  37. values.Set("password", pwd)
  38. values.Set("app_type", "3")
  39. values.Set("ip", ip)
  40. resp, requestErr := http.PostForm(api, values)
  41. if requestErr != nil {
  42. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  43. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  44. return
  45. }
  46. defer resp.Body.Close()
  47. body, ioErr := ioutil.ReadAll(resp.Body)
  48. if ioErr != nil {
  49. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  50. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  51. return
  52. }
  53. var respJSON map[string]interface{}
  54. utils.InfoLog(string(body))
  55. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  56. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  57. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  58. return
  59. }
  60. if respJSON["state"].(float64) != 1 {
  61. msg := respJSON["msg"].(string)
  62. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  63. if int(respJSON["code"].(float64)) == 609 {
  64. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  65. return
  66. }
  67. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  68. return
  69. } else {
  70. utils.SuccessLog("SSO登录成功")
  71. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  72. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  73. userJSONBytes, _ := json.Marshal(userJSON)
  74. var adminUser models.AdminUser
  75. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  76. utils.ErrorLog("解析管理员失败:%v", err)
  77. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  78. return
  79. }
  80. var org models.Org
  81. if respJSON["data"].(map[string]interface{})["org"] != nil {
  82. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  83. orgJSONBytes, _ := json.Marshal(orgJSON)
  84. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  85. utils.ErrorLog("解析机构失败:%v", err)
  86. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  87. return
  88. }
  89. }
  90. var app models.OrgApp
  91. if respJSON["data"].(map[string]interface{})["app"] != nil {
  92. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  93. appJSONBytes, _ := json.Marshal(appJSON)
  94. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  95. utils.ErrorLog("解析应用失败:%v", err)
  96. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  97. return
  98. }
  99. }
  100. var appRole models.App_Role
  101. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  102. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  103. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  104. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  105. utils.ErrorLog("解析AppRole失败:%v", err)
  106. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  107. return
  108. }
  109. }
  110. var subscibe models.ServeSubscibe
  111. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  112. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  113. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  114. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  115. utils.ErrorLog("解析Subscibe失败:%v", err)
  116. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  117. return
  118. }
  119. }
  120. //service.GetOrgSubscibeState(&subscibe)
  121. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  122. mobileAdminUserInfo := &MobileAdminUserInfo{
  123. AdminUser: &adminUser,
  124. Org: &org,
  125. App: &app,
  126. AppRole: &appRole,
  127. Subscibe: &subscibe,
  128. TemplateInfo: &templateInfo,
  129. }
  130. //设置seesion
  131. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  132. //设置cookie
  133. mobile = mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  134. token := utils.GenerateLoginToken(mobile)
  135. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  136. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  137. var configList interface{}
  138. var FiledList []*models.FiledConfig
  139. if org.Id > 0 {
  140. configList, _ = service.GetConfigList(org.Id)
  141. FiledList, _ = service.FindFiledByOrgId(org.Id)
  142. }
  143. if len(FiledList) == 0 {
  144. var err error
  145. if org.Id > 0 {
  146. err = service.BatchInsertFiledConfig(org.Id)
  147. if err == nil {
  148. FiledList, _ = service.FindFiledByOrgId(org.Id)
  149. } else {
  150. utils.ErrorLog("字段批量插入失败:%v", err)
  151. }
  152. } else {
  153. FiledList = make([]*models.FiledConfig, 0)
  154. }
  155. }
  156. //产寻该机构是否有收缩压和舒张压
  157. pressure, err := service.GetDefaultSystolicPressure(org.Id)
  158. fmt.Println(err)
  159. if len(pressure) == 0 {
  160. err = service.BathInsertQualityControlTwo(org.Id)
  161. } else {
  162. utils.ErrorLog("字段批量插入失败:%v", err)
  163. }
  164. //批量插入质控达标统计配置
  165. major, requestErr := service.GetInspectionMajor(org.Id)
  166. if len(major) == 0 {
  167. QualityeList, err := service.FindQualityByOrgId(org.Id)
  168. if len(QualityeList) == 0 {
  169. err = service.BatchInsertQualityControl(org.Id)
  170. } else {
  171. utils.ErrorLog("字段批量插入失败:%v", err)
  172. }
  173. //批量插入检验检查统计配置
  174. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  175. if len(InspectionList) == 0 {
  176. err = service.BatchInspectionConfiguration(org.Id)
  177. } else {
  178. utils.ErrorLog("字段批量插入失败:%v", err)
  179. }
  180. } else {
  181. utils.ErrorLog("字段批量插入失败:%v", requestErr)
  182. }
  183. //查询该机构是否存在医护排班
  184. _, errcode := service.GetDoctorScheduleByOrgId(org.Id)
  185. //如果没有就插入
  186. if errcode == gorm.ErrRecordNotFound {
  187. err = service.BatchInsertDoctorSchedule(org.Id)
  188. } else {
  189. utils.ErrorLog("医护排班默认数据插入失败:%v", err)
  190. }
  191. this.ServeSuccessJSON(map[string]interface{}{
  192. "admin": adminUser,
  193. "user": appRole,
  194. "org": org,
  195. "template_info": map[string]interface{}{
  196. "id": templateInfo.ID,
  197. "org_id": templateInfo.OrgId,
  198. "template_id": templateInfo.TemplateId,
  199. },
  200. "config_list": configList,
  201. "filed_list": FiledList,
  202. })
  203. }
  204. }
  205. // /m/api/login/code [post] LoginByCode
  206. func (this *LoginAPIController) LoginByCode() {
  207. }