home_api_controller.go 22KB

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