package mobile_api_controllers import ( "encoding/json" "io/ioutil" "net/http" "net/url" "XT_New/enums" "XT_New/models" "XT_New/service" "XT_New/utils" "github.com/astaxie/beego" ) type LoginAPIController struct { MobileBaseAPIController } func (this *LoginAPIController) LoginByCs() { mobile := this.GetString("mobile") pwd := this.GetString("password") if len(mobile) == 0 || len(pwd) == 0 || utils.CellPhoneRegexp().MatchString(mobile) == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } ip := utils.GetIP(this.Ctx.Request) ssoDomain := beego.AppConfig.String("sso_domain") api := ssoDomain + "/m/login/pwd" values := make(url.Values) values.Set("mobile", mobile) values.Set("password", pwd) values.Set("app_type", "3") values.Set("ip", ip) resp, requestErr := http.PostForm(api, values) if requestErr != nil { utils.ErrorLog("请求SSO登录接口失败: %v", requestErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } defer resp.Body.Close() body, ioErr := ioutil.ReadAll(resp.Body) if ioErr != nil { utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } var respJSON map[string]interface{} utils.InfoLog(string(body)) if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil { utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if respJSON["state"].(float64) != 1 { msg := respJSON["msg"].(string) utils.ErrorLog("SSO登录接口请求失败: %v", msg) if int(respJSON["code"].(float64)) == 609 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong) return } this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else { utils.SuccessLog("SSO登录成功") // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢 userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{}) userJSONBytes, _ := json.Marshal(userJSON) var adminUser models.AdminUser if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil { utils.ErrorLog("解析管理员失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{}) orgJSONBytes, _ := json.Marshal(orgJSON) var org models.Org if err := json.Unmarshal(orgJSONBytes, &org); err != nil { utils.ErrorLog("解析机构失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{}) appJSONBytes, _ := json.Marshal(appJSON) var app models.OrgApp if err := json.Unmarshal(appJSONBytes, &app); err != nil { utils.ErrorLog("解析应用失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{}) appRoleJSONBytes, _ := json.Marshal(appRoleJSON) var appRole models.App_Role if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil { utils.ErrorLog("解析AppRole失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{}) subscibeJSONBytes, _ := json.Marshal(subscibeJSON) var subscibe models.ServeSubscibe if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil { utils.ErrorLog("解析Subscibe失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } service.GetOrgSubscibeState(&subscibe) templateInfo, _ := service.GetOrgInfoTemplate(org.Id) mobileAdminUserInfo := &MobileAdminUserInfo{ AdminUser: &adminUser, Org: &org, App: &app, AppRole: &appRole, Subscibe: &subscibe, TemplateInfo: &templateInfo, } this.SetSession("mobile_admin_user_info", mobileAdminUserInfo) // configList, _ := service.GetConfigList(org.Id) // var FiledList []*models.FiledConfig // FiledList, _ = service.FindFiledByOrgId(org.Id) // if len(FiledList) == 0 { // err := service.BatchInsertFiledConfig(org.Id) // if err == nil { // FiledList, _ = service.FindFiledByOrgId(org.Id) // } else { // utils.ErrorLog("字段批量插入失败:%v", err) // } // } this.ServeSuccessJSON(map[string]interface{}{ "user": map[string]interface{}{ "id": adminUser.Id, "mobile": adminUser.Mobile, "user_name": appRole.UserName, "avatar": appRole.Avatar, "intro": appRole.Intro, "user_type": appRole.UserType, "user_title": appRole.UserTitle, }, "org": map[string]interface{}{ "id": org.Id, "org_name": org.OrgName, "org_short_name": org.OrgShortName, "org_intro": org.OrgIntroduction, "org_logo": org.OrgLogo, "province": org.Province, "city": org.City, "district": org.District, "address": org.Address, }, "subscibe": map[string]interface{}{ "id": subscibe.ID, "period_start": subscibe.PeriodStart, "period_end": subscibe.PeriodEnd, "state": subscibe.State, }, "template_info": map[string]interface{}{ "id": templateInfo.ID, "org_id": templateInfo.OrgId, "template_id": templateInfo.TemplateId, }, // "config_list": configList, // "filed_list": FiledList, }) } } // /m/api/login/pwd [post] LoginByPwd // @param mobile:string // @param password:string func (this *LoginAPIController) LoginByPwd() { mobile := this.GetString("mobile") pwd := this.GetString("password") if len(mobile) == 0 || len(pwd) == 0 || utils.CellPhoneRegexp().MatchString(mobile) == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } ip := utils.GetIP(this.Ctx.Request) ssoDomain := beego.AppConfig.String("sso_domain") api := ssoDomain + "/m/login/pwd" values := make(url.Values) values.Set("mobile", mobile) values.Set("password", pwd) values.Set("app_type", "3") values.Set("ip", ip) resp, requestErr := http.PostForm(api, values) if requestErr != nil { utils.ErrorLog("请求SSO登录接口失败: %v", requestErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } defer resp.Body.Close() body, ioErr := ioutil.ReadAll(resp.Body) if ioErr != nil { utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } var respJSON map[string]interface{} utils.InfoLog(string(body)) if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil { utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if respJSON["state"].(float64) != 1 { msg := respJSON["msg"].(string) utils.ErrorLog("SSO登录接口请求失败: %v", msg) if int(respJSON["code"].(float64)) == 609 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong) return } this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else { utils.SuccessLog("SSO登录成功") // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢 userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{}) userJSONBytes, _ := json.Marshal(userJSON) var adminUser models.AdminUser if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil { utils.ErrorLog("解析管理员失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{}) orgJSONBytes, _ := json.Marshal(orgJSON) var org models.Org if err := json.Unmarshal(orgJSONBytes, &org); err != nil { utils.ErrorLog("解析机构失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{}) appJSONBytes, _ := json.Marshal(appJSON) var app models.OrgApp if err := json.Unmarshal(appJSONBytes, &app); err != nil { utils.ErrorLog("解析应用失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{}) appRoleJSONBytes, _ := json.Marshal(appRoleJSON) var appRole models.App_Role if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil { utils.ErrorLog("解析AppRole失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{}) subscibeJSONBytes, _ := json.Marshal(subscibeJSON) var subscibe models.ServeSubscibe if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil { utils.ErrorLog("解析Subscibe失败:%v", err) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } service.GetOrgSubscibeState(&subscibe) templateInfo, _ := service.GetOrgInfoTemplate(org.Id) mobileAdminUserInfo := &MobileAdminUserInfo{ AdminUser: &adminUser, Org: &org, App: &app, AppRole: &appRole, Subscibe: &subscibe, TemplateInfo: &templateInfo, } this.SetSession("mobile_admin_user_info", mobileAdminUserInfo) configList, _ := service.GetConfigList(org.Id) var FiledList []*models.FiledConfig FiledList, _ = service.FindFiledByOrgId(org.Id) if len(FiledList) == 0 { err := service.BatchInsertFiledConfig(org.Id) if err == nil { FiledList, _ = service.FindFiledByOrgId(org.Id) } else { utils.ErrorLog("字段批量插入失败:%v", err) } } this.ServeSuccessJSON(map[string]interface{}{ "user": map[string]interface{}{ "id": adminUser.Id, "mobile": adminUser.Mobile, "user_name": appRole.UserName, "avatar": appRole.Avatar, "intro": appRole.Intro, "user_type": appRole.UserType, "user_title": appRole.UserTitle, }, "org": map[string]interface{}{ "id": org.Id, "org_name": org.OrgName, "org_short_name": org.OrgShortName, "org_intro": org.OrgIntroduction, "org_logo": org.OrgLogo, "province": org.Province, "city": org.City, "district": org.District, "address": org.Address, }, "subscibe": map[string]interface{}{ "id": subscibe.ID, "period_start": subscibe.PeriodStart, "period_end": subscibe.PeriodEnd, "state": subscibe.State, }, "template_info": map[string]interface{}{ "id": templateInfo.ID, "org_id": templateInfo.OrgId, "template_id": templateInfo.TemplateId, }, "config_list": configList, "filed_list": FiledList, }) } } // /m/api/login/code [post] LoginByCode func (this *LoginAPIController) LoginByCode() { }