home_api_controller.go 22KB

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