123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- package new_mobile_api_controllers
-
- import (
- "XT_New/controllers/mobile_api_controllers"
- "XT_New/enums"
- "XT_New/models"
- "XT_New/service"
- "XT_New/utils"
- "encoding/json"
- "github.com/astaxie/beego"
- "io/ioutil"
- "net/http"
- "net/url"
- "strconv"
- "time"
- )
-
- type HomeController struct {
- NewMobileBaseAPIAuthController
- }
-
- func (this *HomeController) GetHomeData() {
- adminUserInfo := this.GetMobileAdminUserInfo()
- if adminUserInfo.Org != nil && adminUserInfo.Org.Id != 0 {
-
- var orgs []*models.Org
- adminUser, err := service.GetHomeData(adminUserInfo.AdminUser.Id)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
- for _, item := range adminUser.Org {
- orgs = append(orgs, item)
- }
- for _, item := range adminUser.VMApp_Role {
- for _, subItem := range item.Org {
- orgs = append(orgs, subItem)
- }
- }
- orgs = RemoveRepeatedOrgElement(orgs)
-
- apps, err := service.GetAllApp(adminUserInfo.Org.Id)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
-
- banners, err := service.GetSystemBanner()
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "orgs": orgs,
- "apps": apps,
- "banners": banners,
- "isCreateOrg": true,
- })
- } else {
- apps, err := service.GetAllApp(0)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
- banners, err := service.GetSystemBanner()
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "isCreateOrg": false,
- "apps": apps,
- "banners": banners,
- })
- }
-
- }
-
- func RemoveRepeatedOrgElement(orgs []*models.Org) (newOrgs []*models.Org) {
- newOrgs = make([]*models.Org, 0)
- for i := 0; i < len(orgs); i++ {
- repeat := false
- for j := i + 1; j < len(orgs); j++ {
- if orgs[i].Id == orgs[j].Id {
- repeat = true
- break
- }
- }
- if !repeat {
- newOrgs = append(newOrgs, orgs[i])
- }
- }
- return
- }
-
- func (this *HomeController) ChangeOrg() {
- org_id, _ := this.GetInt64("org_id")
- adminUserInfo := this.GetMobileAdminUserInfo()
-
- tempOrg, err := service.GetOrgById(org_id)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
-
- if tempOrg == nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeOrgNoExist)
- return
- }
-
- mobile := adminUserInfo.AdminUser.Mobile
-
- adminUser, getAdminErr := service.GetValidAdminUserByMobileReturnErr(mobile)
- if getAdminErr != nil {
- utils.ErrorLog("获取管理员失败:%v", getAdminErr)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- this.ServeJSON()
- return
- } else if adminUser == nil {
- utils.ErrorLog("查找不到 mobile = %v 的用户", mobile)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
- this.ServeJSON()
- return
- } else {
-
- var appRole *models.App_Role
- var org *models.Org
- var subscibe *models.ServeSubscibe
- var app *models.OrgApp
-
-
- tempApp, _ := service.GetOrgApp(tempOrg.Id, 3)
- tempRole, _ := service.GetAppRole(tempOrg.Id, tempApp.Id, adminUser.Id)
-
- tempSubscibe, getSubscibeErr := service.GetOrgServeSubscibe(tempOrg.Id)
- if getSubscibeErr != nil {
- utils.ErrorLog("获取机构订阅信息失败:%v", getSubscibeErr)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- this.ServeJSON()
- return
- }
-
- subscibe = tempSubscibe
- org = tempOrg
- appRole = tempRole
- app = tempApp
-
- templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
-
- mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
- AdminUser: adminUser,
- Org: org,
- App: app,
- AppRole: appRole,
- Subscibe: subscibe,
- TemplateInfo: &templateInfo,
- }
-
- if org != nil && appRole != nil {
-
- ip := this.GetString("ip")
- loginLog := &models.AdminUserLoginLog{
- AdminUserId: adminUser.Id,
- OrgId: org.Id,
- AppId: appRole.AppId,
- IP: ip,
- OperateType: 3,
- AppType: 3,
- CreateTime: time.Now().Unix(),
- }
- if insertErr := service.InsertLoginLog(loginLog); insertErr != nil {
- utils.ErrorLog("为手机号为%v的用户插入一条登录记录失败:%v", mobile, insertErr)
- }
- }
-
-
- this.DelSession("mobile_admin_user_info")
- this.Ctx.SetCookie("token_cookie", "")
-
-
- this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
-
- mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
- token := utils.GenerateLoginToken(mobile)
- expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
- this.Ctx.SetCookie("token_cookie", token, expiration, "/")
-
- var configList interface{}
- var FiledList []*models.FiledConfig
-
- if org.Id > 0 {
- configList, _ = service.GetConfigList(org.Id)
- FiledList, _ = service.FindFiledByOrgId(org.Id)
- }
- if len(FiledList) == 0 {
- var err error
- if org.Id > 0 {
- err = service.BatchInsertFiledConfig(org.Id)
- if err == nil {
- FiledList, _ = service.FindFiledByOrgId(org.Id)
- } else {
- utils.ErrorLog("字段批量插入失败:%v", err)
- }
- } else {
- FiledList = make([]*models.FiledConfig, 0)
- }
- }
- this.ServeSuccessJSON(map[string]interface{}{
- "admin": adminUser,
- "user": appRole,
- "org": org,
- "template_info": map[string]interface{}{
- "id": templateInfo.ID,
- "org_id": templateInfo.OrgId,
- "template_id": templateInfo.TemplateId,
- },
- "config_list": configList,
- "filed_list": FiledList,
- })
- }
-
- }
-
- func (this *HomeController) CreateOrg() {
-
- adminUserInfo := this.GetMobileAdminUserInfo()
- adminUser := adminUserInfo.AdminUser
-
-
-
-
-
-
-
-
-
-
-
- name := this.GetString("org_name")
- shortName := name
- provinceName := this.GetString("provinces_name")
- cityName := this.GetString("city_name")
- districtName := this.GetString("district_name")
- address := this.GetString("address")
- org_type := this.GetString("org_type")
- contactName := this.GetString("contact_name")
-
- openXT := true
- openCDM := false
- openSCRM := false
- openMall := false
-
- if len(name) == 0 || len(shortName) == 0 || len(contactName) == 0 || len(address) == 0 || len(provinceName) <= 0 || len(cityName) <= 0 || len(districtName) <= 0 || len(org_type) <= 0 {
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- this.ServeJSON()
- return
- }
- orgPhone := this.GetString("telephone")
-
- if len(orgPhone) > 0 {
- if utils.PhoneRegexp().MatchString(orgPhone) == false {
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- this.ServeJSON()
- return
- }
- }
-
- provinceID := 0
- cityID := 0
- districtID := 0
-
- province, getProvinceErr := service.GetProvinceWithName(provinceName)
- if getProvinceErr != nil {
- utils.ErrorLog("查询省名失败:%v", getProvinceErr)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- this.ServeJSON()
- return
- } else if province != nil {
- provinceID = int(province.ID)
- city, getCityErr := service.GetCityWithName(province.ID, cityName)
- if getCityErr != nil {
- utils.ErrorLog("查询城市名失败:%v", getCityErr)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- this.ServeJSON()
- return
- } else if city != nil {
- cityID = int(city.ID)
- district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
- if getDistrictErr != nil {
- utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- this.ServeJSON()
- return
- } else if district != nil {
- districtID = int(district.ID)
- }
- }
- }
-
- var orgs []*models.Org
- vmAdminUser, err := service.GetHomeData(adminUser.Id)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
- return
- }
- for _, item := range vmAdminUser.Org {
- orgs = append(orgs, item)
- }
- for _, item := range vmAdminUser.VMApp_Role {
- for _, subItem := range item.Org {
- orgs = append(orgs, subItem)
- }
- }
- orgs = RemoveRepeatedOrgElement(orgs)
-
- orgType := service.GetOrgTypeByName(org_type)
-
- org := &models.Org{
- Creator: adminUser.Id,
- OrgName: name,
- OrgShortName: shortName,
- Province: int64(provinceID),
- City: int64(cityID),
- District: int64(districtID),
- Address: address,
- OrgType: orgType.ID,
- Telephone: orgPhone,
- ContactName: contactName,
- Claim: 1,
- Evaluate: 5,
- Status: 1,
- CreateTime: time.Now().Unix(),
- ModifyTime: time.Now().Unix(),
- }
-
- createErr := service.CreateOrg(org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall)
-
- if createErr != nil {
- utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
- this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
- this.ServeJSON()
- } else {
-
-
- InitPatientAndSchedule(org)
-
- InitSystemPrescrption(org)
-
- InitAdviceTemplate(org)
-
-
-
-
-
-
- if len(orgs) == 0 {
- ip := utils.GetIP(this.Ctx.Request)
- ssoDomain := beego.AppConfig.String("sso_domain")
- api := ssoDomain + "/m/login/pwd"
- values := make(url.Values)
- values.Set("mobile", adminUser.Mobile)
- values.Set("password", adminUser.Password)
- 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登录成功")
-
- 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
- }
-
- var org models.Org
- if respJSON["data"].(map[string]interface{})["org"] != nil {
- orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
- orgJSONBytes, _ := json.Marshal(orgJSON)
- if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
- utils.ErrorLog("解析机构失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- }
-
- var app models.OrgApp
-
- if respJSON["data"].(map[string]interface{})["app"] != nil {
- appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
- appJSONBytes, _ := json.Marshal(appJSON)
- if err := json.Unmarshal(appJSONBytes, &app); err != nil {
- utils.ErrorLog("解析应用失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- }
-
- var appRole models.App_Role
-
- if respJSON["data"].(map[string]interface{})["app_role"] != nil {
- appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
- appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
- if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
- utils.ErrorLog("解析AppRole失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- }
-
- var subscibe models.ServeSubscibe
- if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
- subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
- subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
- if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
- utils.ErrorLog("解析Subscibe失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- }
-
-
- templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
-
- mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
- AdminUser: &adminUser,
- Org: &org,
- App: &app,
- AppRole: &appRole,
- Subscibe: &subscibe,
- TemplateInfo: &templateInfo,
- }
- this.Ctx.SetCookie("token_cookie", "")
-
-
- this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
-
-
- mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
- token := utils.GenerateLoginToken(mobile)
- expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
- this.Ctx.SetCookie("token_cookie", token, expiration, "/")
-
- var configList interface{}
- var FiledList []*models.FiledConfig
-
- if org.Id > 0 {
- configList, _ = service.GetConfigList(org.Id)
- FiledList, _ = service.FindFiledByOrgId(org.Id)
- }
- if len(FiledList) == 0 {
- var err error
- if org.Id > 0 {
- err = service.BatchInsertFiledConfig(org.Id)
- if err == nil {
- FiledList, _ = service.FindFiledByOrgId(org.Id)
- } else {
- utils.ErrorLog("字段批量插入失败:%v", err)
- }
- } else {
- FiledList = make([]*models.FiledConfig, 0)
- }
- }
-
- this.ServeSuccessJSON(map[string]interface{}{
- "admin": adminUser,
- "user": appRole,
- "org": org,
- "template_info": map[string]interface{}{
- "id": templateInfo.ID,
- "org_id": templateInfo.OrgId,
- "template_id": templateInfo.TemplateId,
- },
- "config_list": configList,
- "filed_list": FiledList,
- "status": 1,
- })
- }
-
- } else {
- this.ServeSuccessJSON(map[string]interface{}{
- "org": org,
- "status": 2,
- })
-
- }
-
- }
-
- }
|