123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- package mobile_api_controllers
-
- import (
- "encoding/json"
- "io/ioutil"
- "net/http"
- "net/url"
-
- "XT/enums"
- "XT/models"
- "XT/service"
- "XT/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() {
-
- }
|