home_api_controller.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. package new_mobile_api_controllers
  2. import (
  3. "XT_New/controllers/mobile_api_controllers"
  4. "XT_New/enums"
  5. "XT_New/models"
  6. "XT_New/service"
  7. "XT_New/utils"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/astaxie/beego"
  11. "github.com/jinzhu/gorm"
  12. "io/ioutil"
  13. "net/http"
  14. "net/url"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. //func InitGoodTypesConfig(org *models.Org){
  20. //
  21. //
  22. //}
  23. type HomeController struct {
  24. NewMobileBaseAPIAuthController
  25. }
  26. func (this *HomeController) GetHomeData() {
  27. adminUserInfo := this.GetMobileAdminUserInfo()
  28. if adminUserInfo.Org != nil && adminUserInfo.Org.Id != 0 {
  29. //获取该管理员所有机构列表
  30. var orgs []*models.Org
  31. adminUser, err := service.GetHomeData(adminUserInfo.AdminUser.Id)
  32. if err != nil {
  33. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  34. return
  35. }
  36. for _, item := range adminUser.Org {
  37. orgs = append(orgs, item)
  38. }
  39. for _, item := range adminUser.VMApp_Role {
  40. for _, subItem := range item.Org {
  41. orgs = append(orgs, subItem)
  42. }
  43. }
  44. orgs = RemoveRepeatedOrgElement(orgs)
  45. var isSubSuperAdmin bool = false
  46. if adminUserInfo.AppRole != nil && adminUserInfo.AppRole.Id > 0 {
  47. app_role, _ := service.GetAppRoleById(adminUserInfo.AppRole.Id)
  48. if len(app_role.RoleIds) > 0 {
  49. role_ids := strings.Split(app_role.RoleIds, ",")
  50. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  51. for _, item := range role_ids {
  52. id, _ := strconv.ParseInt(item, 10, 64)
  53. if id > 0 {
  54. role, _ := service.GetRoleByRoleID(id)
  55. if role != nil {
  56. if role.IsSystem == 1 && role.RoleName == "子管理员" {
  57. isSubSuperAdmin = true
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. apps, err := service.GetAllApp(adminUserInfo.Org.Id)
  66. if err != nil {
  67. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  68. return
  69. }
  70. banners, err := service.GetSystemBanner()
  71. if err != nil {
  72. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  73. return
  74. }
  75. cretor := adminUserInfo.AdminUser.Id
  76. //查询机构ID是否存在
  77. _, errcode := service.GetOrgFollowIsExist(adminUserInfo.Org.Id)
  78. if errcode == gorm.ErrRecordNotFound {
  79. information, _ := service.GetAdminUserRoleInformation(0)
  80. username, _ := service.GetMyOrgInformationFour(adminUserInfo.AdminUser.Id, adminUserInfo.Org.Id)
  81. this.ServeSuccessJSON(map[string]interface{}{
  82. "orgs": orgs,
  83. "apps": apps,
  84. "banners": banners,
  85. "isCreateOrg": true,
  86. "isSubSuperAdmin": isSubSuperAdmin,
  87. "cretor": cretor,
  88. "information": information,
  89. "username": username,
  90. })
  91. } else if errcode == nil {
  92. information, _ := service.GetAdminUserRoleInformation(adminUserInfo.Org.Id)
  93. username, _ := service.GetMyOrgInformationFour(adminUserInfo.AdminUser.Id, adminUserInfo.Org.Id)
  94. this.ServeSuccessJSON(map[string]interface{}{
  95. "orgs": orgs,
  96. "apps": apps,
  97. "banners": banners,
  98. "isCreateOrg": true,
  99. "isSubSuperAdmin": isSubSuperAdmin,
  100. "cretor": cretor,
  101. "information": information,
  102. "username": username,
  103. })
  104. }
  105. } else {
  106. apps, err := service.GetAllApp(0)
  107. if err != nil {
  108. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  109. return
  110. }
  111. banners, err := service.GetSystemBanner()
  112. if err != nil {
  113. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  114. return
  115. }
  116. _, errcode := service.GetOrgFollowIsExist(adminUserInfo.Org.Id)
  117. if errcode == gorm.ErrRecordNotFound {
  118. information, _ := service.GetAdminUserRoleInformation(0)
  119. this.ServeSuccessJSON(map[string]interface{}{
  120. "isCreateOrg": false,
  121. "apps": apps,
  122. "banners": banners,
  123. "isSubSuperAdmin": false,
  124. "information": information,
  125. })
  126. } else if errcode == nil {
  127. information, _ := service.GetAdminUserRoleInformation(adminUserInfo.Org.Id)
  128. this.ServeSuccessJSON(map[string]interface{}{
  129. "isCreateOrg": false,
  130. "apps": apps,
  131. "banners": banners,
  132. "isSubSuperAdmin": false,
  133. "information": information,
  134. })
  135. }
  136. }
  137. }
  138. func RemoveRepeatedOrgElement(orgs []*models.Org) (newOrgs []*models.Org) {
  139. newOrgs = make([]*models.Org, 0)
  140. for i := 0; i < len(orgs); i++ {
  141. repeat := false
  142. for j := i + 1; j < len(orgs); j++ {
  143. if orgs[i].Id == orgs[j].Id {
  144. repeat = true
  145. break
  146. }
  147. }
  148. if !repeat {
  149. newOrgs = append(newOrgs, orgs[i])
  150. }
  151. }
  152. return
  153. }
  154. func (this *HomeController) ChangeOrg() {
  155. org_id, _ := this.GetInt64("org_id")
  156. adminUserInfo := this.GetMobileAdminUserInfo()
  157. tempOrg, err := service.GetOrgById(org_id)
  158. if err != nil {
  159. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  160. return
  161. }
  162. if tempOrg == nil {
  163. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeOrgNoExist)
  164. return
  165. }
  166. mobile := adminUserInfo.AdminUser.Mobile
  167. // 只取最近被创建的 admin_role
  168. adminUser, getAdminErr := service.GetValidAdminUserByMobileReturnErr(mobile) //账号信息唯一值
  169. if getAdminErr != nil {
  170. utils.ErrorLog("获取管理员失败:%v", getAdminErr)
  171. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  172. this.ServeJSON()
  173. return
  174. } else if adminUser == nil {
  175. utils.ErrorLog("查找不到 mobile = %v 的用户", mobile)
  176. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  177. this.ServeJSON()
  178. return
  179. } else {
  180. var appRole *models.App_Role
  181. var org *models.Org
  182. var subscibe *models.ServeSubscibe
  183. var app *models.OrgApp
  184. //根据登录信息的机构和用户id,去获取对应用户信息和机构信息
  185. tempApp, _ := service.GetOrgApp(tempOrg.Id, 3)
  186. tempRole, _ := service.GetAppRole(tempOrg.Id, tempApp.Id, adminUser.Id)
  187. tempSubscibe, getSubscibeErr := service.GetOrgServeSubscibe(tempOrg.Id)
  188. if getSubscibeErr != nil {
  189. utils.ErrorLog("获取机构订阅信息失败:%v", getSubscibeErr)
  190. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  191. this.ServeJSON()
  192. return
  193. }
  194. subscibe = tempSubscibe
  195. org = tempOrg
  196. appRole = tempRole
  197. app = tempApp
  198. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  199. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  200. AdminUser: adminUser,
  201. Org: org,
  202. App: app,
  203. AppRole: appRole,
  204. Subscibe: subscibe,
  205. TemplateInfo: &templateInfo,
  206. }
  207. if org != nil && appRole != nil {
  208. // 插入一条登录记录
  209. ip := this.GetString("ip")
  210. loginLog := &models.AdminUserLoginLog{
  211. AdminUserId: adminUser.Id,
  212. OrgId: org.Id,
  213. AppId: appRole.AppId,
  214. IP: ip,
  215. OperateType: 3,
  216. AppType: 3,
  217. CreateTime: time.Now().Unix(),
  218. }
  219. if insertErr := service.InsertLoginLog(loginLog); insertErr != nil {
  220. utils.ErrorLog("为手机号为%v的用户插入一条登录记录失败:%v", mobile, insertErr)
  221. }
  222. }
  223. //删除session和cookie
  224. this.DelSession("mobile_admin_user_info")
  225. this.Ctx.SetCookie("token_cookie", "")
  226. //设置new seesion
  227. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  228. //设置new cookie
  229. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  230. token := utils.GenerateLoginToken(mobile)
  231. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  232. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  233. var configList interface{}
  234. var dict_config_list interface{}
  235. var FiledList []*models.FiledConfig
  236. if org.Id > 0 {
  237. dict_config_list, _ = service.GetDictConfigList(org.Id)
  238. configList, _ = service.GetConfigList(org.Id)
  239. FiledList, _ = service.FindFiledByOrgId(org.Id)
  240. }
  241. if len(FiledList) == 0 {
  242. var err error
  243. if org.Id > 0 {
  244. err = service.BatchInsertFiledConfig(org.Id)
  245. if err == nil {
  246. FiledList, _ = service.FindFiledByOrgId(org.Id)
  247. } else {
  248. utils.ErrorLog("字段批量插入失败:%v", err)
  249. }
  250. } else {
  251. FiledList = make([]*models.FiledConfig, 0)
  252. }
  253. if org.Id > 0 {
  254. //产寻该机构是否有收缩压和舒张压
  255. pressure, err := service.GetDefaultSystolicPressure(org.Id)
  256. fmt.Println(err)
  257. if len(pressure) == 0 {
  258. err = service.BathInsertQualityControlTwo(org.Id)
  259. } else {
  260. utils.ErrorLog("字段批量插入失败:%v", err)
  261. }
  262. major, err := service.GetInspectionMajor(org.Id)
  263. if len(major) == 0 {
  264. QualityeList, err := service.FindQualityByOrgId(org.Id)
  265. if len(QualityeList) == 0 {
  266. err = service.BatchInsertQualityControl(org.Id)
  267. } else {
  268. utils.ErrorLog("字段批量插入失败:%v", err)
  269. }
  270. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  271. if len(InspectionList) == 0 {
  272. err = service.BatchInspectionConfiguration(org.Id)
  273. } else {
  274. utils.ErrorLog("字段批量插入失败:%v", err)
  275. }
  276. } else {
  277. utils.ErrorLog("字段批量插入失败:%v", err)
  278. }
  279. }
  280. }
  281. this.ServeSuccessJSON(map[string]interface{}{
  282. "admin": adminUser,
  283. "user": appRole,
  284. "org": org,
  285. "template_info": map[string]interface{}{
  286. "id": templateInfo.ID,
  287. "org_id": templateInfo.OrgId,
  288. "template_id": templateInfo.TemplateId,
  289. },
  290. "config_list": configList,
  291. "dict_config_list": dict_config_list,
  292. "filed_list": FiledList,
  293. })
  294. }
  295. }
  296. func (this *HomeController) CreateOrg() {
  297. adminUserInfo := this.GetMobileAdminUserInfo()
  298. adminUser := adminUserInfo.AdminUser
  299. //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  300. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  301. // this.ServeJSON()
  302. // return
  303. //} else if didCreateOrg {
  304. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  305. // this.ServeJSON()
  306. // return
  307. //}
  308. name := this.GetString("org_name")
  309. shortName := name
  310. provinceName := this.GetString("provinces_name")
  311. cityName := this.GetString("city_name")
  312. districtName := this.GetString("district_name")
  313. address := this.GetString("address")
  314. org_type := this.GetString("org_type")
  315. contactName := this.GetString("contact_name")
  316. openXT := true
  317. openCDM := false
  318. openSCRM := false
  319. openMall := false
  320. 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 {
  321. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  322. this.ServeJSON()
  323. return
  324. }
  325. orgPhone := this.GetString("telephone")
  326. provinceID := 0
  327. cityID := 0
  328. districtID := 0
  329. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  330. if getProvinceErr != nil {
  331. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  332. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  333. this.ServeJSON()
  334. return
  335. } else if province != nil {
  336. provinceID = int(province.ID)
  337. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  338. if getCityErr != nil {
  339. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  340. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  341. this.ServeJSON()
  342. return
  343. } else if city != nil {
  344. cityID = int(city.ID)
  345. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  346. if getDistrictErr != nil {
  347. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  348. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  349. this.ServeJSON()
  350. return
  351. } else if district != nil {
  352. districtID = int(district.ID)
  353. }
  354. }
  355. }
  356. var orgs []*models.Org
  357. vmAdminUser, err := service.GetHomeData(adminUser.Id)
  358. if err != nil {
  359. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  360. return
  361. }
  362. for _, item := range vmAdminUser.Org {
  363. orgs = append(orgs, item)
  364. }
  365. for _, item := range vmAdminUser.VMApp_Role {
  366. for _, subItem := range item.Org {
  367. orgs = append(orgs, subItem)
  368. }
  369. }
  370. orgs = RemoveRepeatedOrgElement(orgs)
  371. orgType := service.GetOrgTypeByName(org_type)
  372. org := &models.Org{
  373. Creator: adminUser.Id,
  374. OrgName: name,
  375. OrgShortName: shortName,
  376. Province: int64(provinceID),
  377. City: int64(cityID),
  378. District: int64(districtID),
  379. Address: address,
  380. OrgType: orgType.ID,
  381. Telephone: orgPhone,
  382. ContactName: contactName,
  383. Claim: 1,
  384. Evaluate: 5,
  385. Status: 1,
  386. CreateTime: time.Now().Unix(),
  387. ModifyTime: time.Now().Unix(),
  388. }
  389. createErr := service.CreateOrg(org, adminUser.Name, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  390. if createErr != nil {
  391. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  392. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  393. this.ServeJSON()
  394. } else {
  395. //初始化病人和排班相关数据
  396. InitPatientAndSchedule(org)
  397. //初始化透析方案
  398. InitSystemPrescrption(org)
  399. //初始化医嘱模版
  400. //InitAdviceTemplate(org)
  401. //初始化角色和权限
  402. InitRoleAndPurviews(org)
  403. //初始化设备管理
  404. InitEquitMentInformation(org)
  405. //初始化显示配置和打印模版
  406. InitShowConfig(org)
  407. //初始化商品类型
  408. InitGoodTypesConfig(org)
  409. if len(orgs) == 0 {
  410. ip := utils.GetIP(this.Ctx.Request)
  411. ssoDomain := beego.AppConfig.String("sso_domain")
  412. api := ssoDomain + "/m/login/pwd"
  413. values := make(url.Values)
  414. values.Set("mobile", adminUser.Mobile)
  415. values.Set("password", adminUser.Password)
  416. values.Set("app_type", "3")
  417. values.Set("ip", ip)
  418. resp, requestErr := http.PostForm(api, values)
  419. if requestErr != nil {
  420. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  421. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  422. return
  423. }
  424. defer resp.Body.Close()
  425. body, ioErr := ioutil.ReadAll(resp.Body)
  426. if ioErr != nil {
  427. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  428. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  429. return
  430. }
  431. var respJSON map[string]interface{}
  432. utils.InfoLog(string(body))
  433. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  434. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  435. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  436. return
  437. }
  438. if respJSON["state"].(float64) != 1 {
  439. msg := respJSON["msg"].(string)
  440. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  441. if int(respJSON["code"].(float64)) == 609 {
  442. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  443. return
  444. }
  445. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  446. return
  447. } else {
  448. utils.SuccessLog("SSO登录成功")
  449. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  450. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  451. userJSONBytes, _ := json.Marshal(userJSON)
  452. var adminUser models.AdminUser
  453. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  454. utils.ErrorLog("解析管理员失败:%v", err)
  455. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  456. return
  457. }
  458. var org models.Org
  459. if respJSON["data"].(map[string]interface{})["org"] != nil {
  460. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  461. orgJSONBytes, _ := json.Marshal(orgJSON)
  462. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  463. utils.ErrorLog("解析机构失败:%v", err)
  464. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  465. return
  466. }
  467. }
  468. var app models.OrgApp
  469. if respJSON["data"].(map[string]interface{})["app"] != nil {
  470. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  471. appJSONBytes, _ := json.Marshal(appJSON)
  472. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  473. utils.ErrorLog("解析应用失败:%v", err)
  474. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  475. return
  476. }
  477. }
  478. var appRole models.App_Role
  479. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  480. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  481. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  482. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  483. utils.ErrorLog("解析AppRole失败:%v", err)
  484. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  485. return
  486. }
  487. }
  488. var subscibe models.ServeSubscibe
  489. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  490. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  491. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  492. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  493. utils.ErrorLog("解析Subscibe失败:%v", err)
  494. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  495. return
  496. }
  497. }
  498. //service.GetOrgSubscibeState(&subscibe)
  499. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  500. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  501. AdminUser: &adminUser,
  502. Org: &org,
  503. App: &app,
  504. AppRole: &appRole,
  505. Subscibe: &subscibe,
  506. TemplateInfo: &templateInfo,
  507. }
  508. this.Ctx.SetCookie("token_cookie", "")
  509. //设置seesion
  510. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  511. //设置cookie
  512. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  513. token := utils.GenerateLoginToken(mobile)
  514. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  515. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  516. var configList interface{}
  517. var dict_config_list interface{}
  518. var FiledList []*models.FiledConfig
  519. if org.Id > 0 {
  520. dict_config_list, _ = service.GetDictConfigList(org.Id)
  521. configList, _ = service.GetConfigList(org.Id)
  522. FiledList, _ = service.FindFiledByOrgId(org.Id)
  523. }
  524. if len(FiledList) == 0 {
  525. var err error
  526. if org.Id > 0 {
  527. err = service.BatchInsertFiledConfig(org.Id)
  528. if err == nil {
  529. FiledList, _ = service.FindFiledByOrgId(org.Id)
  530. } else {
  531. utils.ErrorLog("字段批量插入失败:%v", err)
  532. }
  533. } else {
  534. FiledList = make([]*models.FiledConfig, 0)
  535. }
  536. }
  537. if org.Id > 0 {
  538. major, requestErr := service.GetInspectionMajor(org.Id)
  539. if len(major) == 0 {
  540. QualityeList, err := service.FindQualityByOrgId(org.Id)
  541. if len(QualityeList) == 0 {
  542. err = service.BatchInsertQualityControl(org.Id)
  543. } else {
  544. utils.ErrorLog("字段批量插入失败:%v", err)
  545. }
  546. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  547. if len(InspectionList) == 0 {
  548. err = service.BatchInspectionConfiguration(org.Id)
  549. } else {
  550. utils.ErrorLog("字段批量插入失败:%v", err)
  551. }
  552. } else {
  553. utils.ErrorLog("字段批量插入失败:%v", requestErr)
  554. }
  555. }
  556. this.ServeSuccessJSON(map[string]interface{}{
  557. "admin": adminUser,
  558. "user": appRole,
  559. "org": org,
  560. "template_info": map[string]interface{}{
  561. "id": templateInfo.ID,
  562. "org_id": templateInfo.OrgId,
  563. "template_id": templateInfo.TemplateId,
  564. },
  565. "config_list": configList,
  566. "dict_config_list": dict_config_list,
  567. "filed_list": FiledList,
  568. "status": 1,
  569. })
  570. }
  571. } else {
  572. this.ServeSuccessJSON(map[string]interface{}{
  573. "org": org,
  574. "status": 2,
  575. })
  576. }
  577. }
  578. }
  579. func (this *HomeController) ModifyPsw() {
  580. mobile := this.GetString("mobile")
  581. code := this.GetString("code")
  582. password := this.GetString("password")
  583. checkErr := this.checkParam(mobile, code, password)
  584. if checkErr != nil {
  585. this.ServeFailJSONWithSGJErrorCode(checkErr.Code)
  586. return
  587. }
  588. adminUser, _ := service.GetValidAdminUserByMobileReturnErr(mobile)
  589. modifyErr := service.ModifyPassword(adminUser.Id, password)
  590. if modifyErr != nil {
  591. utils.ErrorLog("修改mobile=%v的用户的密码时失败: %v", mobile, modifyErr)
  592. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  593. return
  594. } else {
  595. // 修改成功后验证码就要使其失效
  596. redisClient := service.RedisClient()
  597. defer redisClient.Close()
  598. redisClient.Del("code_msg_" + mobile)
  599. this.ServeSuccessJSON(map[string]interface{}{
  600. "admin": adminUser,
  601. })
  602. return
  603. }
  604. }
  605. func (this *HomeController) checkParam(mobile string, code string, password string) *enums.SGJError {
  606. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  607. return &enums.SGJError{Code: enums.ErrorCodeMobileFormat}
  608. }
  609. if len(code) == 0 {
  610. return &enums.SGJError{Code: enums.ErrorCodeVerificationCodeWrong}
  611. }
  612. if len(password) == 0 {
  613. return &enums.SGJError{Code: enums.ErrorCodePasswordEmpty}
  614. }
  615. if service.IsMobileRegister(mobile) == false {
  616. return &enums.SGJError{Code: enums.ErrorCodeMobileNotExit}
  617. }
  618. redisClient := service.RedisClient()
  619. defer redisClient.Close()
  620. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  621. if cache_code != code {
  622. return &enums.SGJError{Code: enums.ErrorCodeVerificationCodeWrong}
  623. }
  624. return nil
  625. }
  626. func (this *HomeController) GetFuncPermission() {
  627. adminUserInfo := this.GetMobileAdminUserInfo()
  628. user_id := adminUserInfo.AdminUser.Id
  629. app_id := adminUserInfo.App.Id
  630. org_id := adminUserInfo.Org.Id
  631. create_url := this.GetString("create_url")
  632. modify_url := this.GetString("modify_url")
  633. modify_other_url := this.GetString("modify_other_url")
  634. del_url := this.GetString("del_url")
  635. del_other_url := this.GetString("del_other_url")
  636. exce_url := this.GetString("exce_url")
  637. check_url := this.GetString("check_url")
  638. modify_exce_url := this.GetString("modify_exce_url")
  639. module, _ := this.GetInt64("module", 0)
  640. app_role, _ := service.GetAppRole(org_id, app_id, user_id)
  641. var is_has_create bool
  642. var is_has_modify bool
  643. var is_has_modify_other bool
  644. var is_has_del bool
  645. var is_has_del_other bool
  646. var is_has_exce bool
  647. var is_has_check bool
  648. var is_has_modify_exce bool
  649. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  650. if app_role != nil {
  651. if len(app_role.RoleIds) > 0 {
  652. roles := strings.Split(app_role.RoleIds, ",")
  653. var userRolePurviews string
  654. for _, item := range roles {
  655. role_id, _ := strconv.ParseInt(item, 10, 64)
  656. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  657. if len(userRolePurviews) == 0 {
  658. userRolePurviews = purviews
  659. } else {
  660. userRolePurviews = userRolePurviews + "," + purviews
  661. }
  662. }
  663. userRolePurviewsArr := RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
  664. funcPurviews, _ := service.FindAllFuncPurview(userRolePurviewsArr)
  665. for _, item := range funcPurviews {
  666. //for _, url := range strings.Split(item.Urlfor,","){
  667. if strings.Split(item.Urlfor, ",")[0] == create_url {
  668. is_has_create = true
  669. }
  670. if strings.Split(item.Urlfor, ",")[0] == modify_url {
  671. is_has_modify = true
  672. }
  673. if strings.Split(item.Urlfor, ",")[0] == modify_other_url {
  674. is_has_modify_other = true
  675. }
  676. if strings.Split(item.Urlfor, ",")[0] == del_url {
  677. is_has_del = true
  678. }
  679. if strings.Split(item.Urlfor, ",")[0] == del_other_url {
  680. is_has_del_other = true
  681. }
  682. if strings.Split(item.Urlfor, ",")[0] == exce_url {
  683. is_has_exce = true
  684. }
  685. if strings.Split(item.Urlfor, ",")[0] == check_url {
  686. is_has_check = true
  687. }
  688. if strings.Split(item.Urlfor, ",")[0] == modify_exce_url {
  689. is_has_modify_exce = true
  690. }
  691. }
  692. } else {
  693. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRole)
  694. return
  695. }
  696. this.ServeSuccessJSON(map[string]interface{}{
  697. "is_has_create": is_has_create,
  698. "is_has_modify": is_has_modify,
  699. "is_has_modify_other": is_has_modify_other,
  700. "is_has_del": is_has_del,
  701. "is_has_del_other": is_has_del_other,
  702. "is_has_exce": is_has_exce,
  703. "is_has_check": is_has_check,
  704. "is_has_modify_exce": is_has_modify_exce,
  705. "module": module,
  706. })
  707. } else {
  708. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserIsExit)
  709. return
  710. }
  711. } else {
  712. this.ServeSuccessJSON(map[string]interface{}{
  713. "is_has_create": true,
  714. "is_has_modify": true,
  715. "is_has_modify_other": true,
  716. "is_has_del": true,
  717. "is_has_del_other": true,
  718. "is_has_exce": true,
  719. "is_has_check": true,
  720. "is_has_modify_exce": true,
  721. "module": true,
  722. })
  723. }
  724. }
  725. func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
  726. newArr = make([]string, 0)
  727. for i := 0; i < len(arr); i++ {
  728. repeat := false
  729. for j := i + 1; j < len(arr); j++ {
  730. if arr[i] == arr[j] {
  731. repeat = true
  732. break
  733. }
  734. }
  735. if !repeat {
  736. newArr = append(newArr, arr[i])
  737. }
  738. }
  739. return
  740. }
  741. func RemoveRepeatedOrgElementTwo(orgs []*models.SgjUserOrg) (newOrgs []*models.SgjUserOrg) {
  742. newOrgs = make([]*models.SgjUserOrg, 0)
  743. for i := 0; i < len(orgs); i++ {
  744. repeat := false
  745. for j := i + 1; j < len(orgs); j++ {
  746. if orgs[i].ID == orgs[j].ID {
  747. repeat = true
  748. break
  749. }
  750. }
  751. if !repeat {
  752. newOrgs = append(newOrgs, orgs[i])
  753. }
  754. }
  755. return
  756. }