home_api_controller.go 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. if org.Id > 0 {
  211. major, err := service.GetInspectionMajor(org.Id)
  212. if len(major) == 0 {
  213. QualityeList, err := service.FindQualityByOrgId(org.Id)
  214. if len(QualityeList) == 0 {
  215. err = service.BatchInsertQualityControl(org.Id)
  216. } else {
  217. utils.ErrorLog("字段批量插入失败:%v", err)
  218. }
  219. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  220. if len(InspectionList) == 0 {
  221. err = service.BatchInspectionConfiguration(org.Id)
  222. } else {
  223. utils.ErrorLog("字段批量插入失败:%v", err)
  224. }
  225. } else {
  226. utils.ErrorLog("字段批量插入失败:%v", err)
  227. }
  228. }
  229. }
  230. this.ServeSuccessJSON(map[string]interface{}{
  231. "admin": adminUser,
  232. "user": appRole,
  233. "org": org,
  234. "template_info": map[string]interface{}{
  235. "id": templateInfo.ID,
  236. "org_id": templateInfo.OrgId,
  237. "template_id": templateInfo.TemplateId,
  238. },
  239. "config_list": configList,
  240. "filed_list": FiledList,
  241. })
  242. }
  243. }
  244. func (this *HomeController) CreateOrg() {
  245. adminUserInfo := this.GetMobileAdminUserInfo()
  246. adminUser := adminUserInfo.AdminUser
  247. //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  248. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  249. // this.ServeJSON()
  250. // return
  251. //} else if didCreateOrg {
  252. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  253. // this.ServeJSON()
  254. // return
  255. //}
  256. name := this.GetString("org_name")
  257. shortName := name
  258. provinceName := this.GetString("provinces_name")
  259. cityName := this.GetString("city_name")
  260. districtName := this.GetString("district_name")
  261. address := this.GetString("address")
  262. org_type := this.GetString("org_type")
  263. contactName := this.GetString("contact_name")
  264. openXT := true
  265. openCDM := false
  266. openSCRM := false
  267. openMall := false
  268. 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 {
  269. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  270. this.ServeJSON()
  271. return
  272. }
  273. orgPhone := this.GetString("telephone")
  274. provinceID := 0
  275. cityID := 0
  276. districtID := 0
  277. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  278. if getProvinceErr != nil {
  279. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  280. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  281. this.ServeJSON()
  282. return
  283. } else if province != nil {
  284. provinceID = int(province.ID)
  285. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  286. if getCityErr != nil {
  287. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  288. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  289. this.ServeJSON()
  290. return
  291. } else if city != nil {
  292. cityID = int(city.ID)
  293. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  294. if getDistrictErr != nil {
  295. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  296. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  297. this.ServeJSON()
  298. return
  299. } else if district != nil {
  300. districtID = int(district.ID)
  301. }
  302. }
  303. }
  304. var orgs []*models.Org
  305. vmAdminUser, err := service.GetHomeData(adminUser.Id)
  306. if err != nil {
  307. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  308. return
  309. }
  310. for _, item := range vmAdminUser.Org {
  311. orgs = append(orgs, item)
  312. }
  313. for _, item := range vmAdminUser.VMApp_Role {
  314. for _, subItem := range item.Org {
  315. orgs = append(orgs, subItem)
  316. }
  317. }
  318. orgs = RemoveRepeatedOrgElement(orgs)
  319. orgType := service.GetOrgTypeByName(org_type)
  320. org := &models.Org{
  321. Creator: adminUser.Id,
  322. OrgName: name,
  323. OrgShortName: shortName,
  324. Province: int64(provinceID),
  325. City: int64(cityID),
  326. District: int64(districtID),
  327. Address: address,
  328. OrgType: orgType.ID,
  329. Telephone: orgPhone,
  330. ContactName: contactName,
  331. Claim: 1,
  332. Evaluate: 5,
  333. Status: 1,
  334. CreateTime: time.Now().Unix(),
  335. ModifyTime: time.Now().Unix(),
  336. }
  337. createErr := service.CreateOrg(org, adminUser.Name, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  338. if createErr != nil {
  339. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  340. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  341. this.ServeJSON()
  342. } else {
  343. //初始化病人和排班相关数据
  344. InitPatientAndSchedule(org)
  345. //初始化透析方案
  346. InitSystemPrescrption(org)
  347. //初始化医嘱模版
  348. //InitAdviceTemplate(org)
  349. //初始化角色和权限
  350. InitRoleAndPurviews(org)
  351. //初始化设备管理
  352. InitEquitMentInformation(org)
  353. //初始化显示配置和打印模版
  354. InitShowConfig(org)
  355. if len(orgs) == 0 {
  356. ip := utils.GetIP(this.Ctx.Request)
  357. ssoDomain := beego.AppConfig.String("sso_domain")
  358. api := ssoDomain + "/m/login/pwd"
  359. values := make(url.Values)
  360. values.Set("mobile", adminUser.Mobile)
  361. values.Set("password", adminUser.Password)
  362. values.Set("app_type", "3")
  363. values.Set("ip", ip)
  364. resp, requestErr := http.PostForm(api, values)
  365. if requestErr != nil {
  366. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  367. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  368. return
  369. }
  370. defer resp.Body.Close()
  371. body, ioErr := ioutil.ReadAll(resp.Body)
  372. if ioErr != nil {
  373. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  374. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  375. return
  376. }
  377. var respJSON map[string]interface{}
  378. utils.InfoLog(string(body))
  379. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  380. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  381. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  382. return
  383. }
  384. if respJSON["state"].(float64) != 1 {
  385. msg := respJSON["msg"].(string)
  386. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  387. if int(respJSON["code"].(float64)) == 609 {
  388. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  389. return
  390. }
  391. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  392. return
  393. } else {
  394. utils.SuccessLog("SSO登录成功")
  395. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  396. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  397. userJSONBytes, _ := json.Marshal(userJSON)
  398. var adminUser models.AdminUser
  399. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  400. utils.ErrorLog("解析管理员失败:%v", err)
  401. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  402. return
  403. }
  404. var org models.Org
  405. if respJSON["data"].(map[string]interface{})["org"] != nil {
  406. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  407. orgJSONBytes, _ := json.Marshal(orgJSON)
  408. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  409. utils.ErrorLog("解析机构失败:%v", err)
  410. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  411. return
  412. }
  413. }
  414. var app models.OrgApp
  415. if respJSON["data"].(map[string]interface{})["app"] != nil {
  416. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  417. appJSONBytes, _ := json.Marshal(appJSON)
  418. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  419. utils.ErrorLog("解析应用失败:%v", err)
  420. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  421. return
  422. }
  423. }
  424. var appRole models.App_Role
  425. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  426. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  427. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  428. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  429. utils.ErrorLog("解析AppRole失败:%v", err)
  430. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  431. return
  432. }
  433. }
  434. var subscibe models.ServeSubscibe
  435. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  436. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  437. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  438. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  439. utils.ErrorLog("解析Subscibe失败:%v", err)
  440. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  441. return
  442. }
  443. }
  444. //service.GetOrgSubscibeState(&subscibe)
  445. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  446. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  447. AdminUser: &adminUser,
  448. Org: &org,
  449. App: &app,
  450. AppRole: &appRole,
  451. Subscibe: &subscibe,
  452. TemplateInfo: &templateInfo,
  453. }
  454. this.Ctx.SetCookie("token_cookie", "")
  455. //设置seesion
  456. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  457. //设置cookie
  458. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  459. token := utils.GenerateLoginToken(mobile)
  460. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  461. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  462. var configList interface{}
  463. var FiledList []*models.FiledConfig
  464. if org.Id > 0 {
  465. configList, _ = service.GetConfigList(org.Id)
  466. FiledList, _ = service.FindFiledByOrgId(org.Id)
  467. }
  468. if len(FiledList) == 0 {
  469. var err error
  470. if org.Id > 0 {
  471. err = service.BatchInsertFiledConfig(org.Id)
  472. if err == nil {
  473. FiledList, _ = service.FindFiledByOrgId(org.Id)
  474. } else {
  475. utils.ErrorLog("字段批量插入失败:%v", err)
  476. }
  477. } else {
  478. FiledList = make([]*models.FiledConfig, 0)
  479. }
  480. }
  481. if org.Id > 0 {
  482. major, requestErr := service.GetInspectionMajor(org.Id)
  483. if len(major) == 0 {
  484. QualityeList, err := service.FindQualityByOrgId(org.Id)
  485. if len(QualityeList) == 0 {
  486. err = service.BatchInsertQualityControl(org.Id)
  487. } else {
  488. utils.ErrorLog("字段批量插入失败:%v", err)
  489. }
  490. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  491. if len(InspectionList) == 0 {
  492. err = service.BatchInspectionConfiguration(org.Id)
  493. } else {
  494. utils.ErrorLog("字段批量插入失败:%v", err)
  495. }
  496. } else {
  497. utils.ErrorLog("字段批量插入失败:%v", requestErr)
  498. }
  499. }
  500. this.ServeSuccessJSON(map[string]interface{}{
  501. "admin": adminUser,
  502. "user": appRole,
  503. "org": org,
  504. "template_info": map[string]interface{}{
  505. "id": templateInfo.ID,
  506. "org_id": templateInfo.OrgId,
  507. "template_id": templateInfo.TemplateId,
  508. },
  509. "config_list": configList,
  510. "filed_list": FiledList,
  511. "status": 1,
  512. })
  513. }
  514. } else {
  515. this.ServeSuccessJSON(map[string]interface{}{
  516. "org": org,
  517. "status": 2,
  518. })
  519. }
  520. }
  521. }
  522. func (this *HomeController) ModifyPsw() {
  523. mobile := this.GetString("mobile")
  524. code := this.GetString("code")
  525. password := this.GetString("password")
  526. checkErr := this.checkParam(mobile, code, password)
  527. if checkErr != nil {
  528. this.ServeFailJSONWithSGJErrorCode(checkErr.Code)
  529. return
  530. }
  531. adminUser, _ := service.GetValidAdminUserByMobileReturnErr(mobile)
  532. modifyErr := service.ModifyPassword(adminUser.Id, password)
  533. if modifyErr != nil {
  534. utils.ErrorLog("修改mobile=%v的用户的密码时失败: %v", mobile, modifyErr)
  535. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  536. return
  537. } else {
  538. // 修改成功后验证码就要使其失效
  539. redisClient := service.RedisClient()
  540. defer redisClient.Close()
  541. redisClient.Del("code_msg_" + mobile)
  542. this.ServeSuccessJSON(map[string]interface{}{
  543. "admin": adminUser,
  544. })
  545. return
  546. }
  547. }
  548. func (this *HomeController) checkParam(mobile string, code string, password string) *enums.SGJError {
  549. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  550. return &enums.SGJError{Code: enums.ErrorCodeMobileFormat}
  551. }
  552. if len(code) == 0 {
  553. return &enums.SGJError{Code: enums.ErrorCodeVerificationCodeWrong}
  554. }
  555. if len(password) == 0 {
  556. return &enums.SGJError{Code: enums.ErrorCodePasswordEmpty}
  557. }
  558. if service.IsMobileRegister(mobile) == false {
  559. return &enums.SGJError{Code: enums.ErrorCodeMobileNotExit}
  560. }
  561. redisClient := service.RedisClient()
  562. defer redisClient.Close()
  563. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  564. if cache_code != code {
  565. return &enums.SGJError{Code: enums.ErrorCodeVerificationCodeWrong}
  566. }
  567. return nil
  568. }
  569. func (this *HomeController) GetFuncPermission() {
  570. adminUserInfo := this.GetMobileAdminUserInfo()
  571. user_id := adminUserInfo.AdminUser.Id
  572. app_id := adminUserInfo.App.Id
  573. org_id := adminUserInfo.Org.Id
  574. create_url := this.GetString("create_url")
  575. modify_url := this.GetString("modify_url")
  576. modify_other_url := this.GetString("modify_other_url")
  577. del_url := this.GetString("del_url")
  578. del_other_url := this.GetString("del_other_url")
  579. exce_url := this.GetString("exce_url")
  580. check_url := this.GetString("check_url")
  581. modify_exce_url := this.GetString("modify_exce_url")
  582. module, _ := this.GetInt64("module", 0)
  583. app_role, _ := service.GetAppRole(org_id, app_id, user_id)
  584. var is_has_create bool
  585. var is_has_modify bool
  586. var is_has_modify_other bool
  587. var is_has_del bool
  588. var is_has_del_other bool
  589. var is_has_exce bool
  590. var is_has_check bool
  591. var is_has_modify_exce bool
  592. if adminUserInfo.AdminUser.Id != adminUserInfo.Org.Creator {
  593. if app_role != nil {
  594. if len(app_role.RoleIds) > 0 {
  595. roles := strings.Split(app_role.RoleIds, ",")
  596. var userRolePurviews string
  597. for _, item := range roles {
  598. role_id, _ := strconv.ParseInt(item, 10, 64)
  599. purviews, _ := service.GetRoleFuncPurviewIds(role_id)
  600. if len(userRolePurviews) == 0 {
  601. userRolePurviews = purviews
  602. } else {
  603. userRolePurviews = userRolePurviews + "," + purviews
  604. }
  605. }
  606. userRolePurviewsArr := RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
  607. funcPurviews, _ := service.FindAllFuncPurview(userRolePurviewsArr)
  608. for _, item := range funcPurviews {
  609. //for _, url := range strings.Split(item.Urlfor,","){
  610. if strings.Split(item.Urlfor, ",")[0] == create_url {
  611. is_has_create = true
  612. }
  613. if strings.Split(item.Urlfor, ",")[0] == modify_url {
  614. is_has_modify = true
  615. }
  616. if strings.Split(item.Urlfor, ",")[0] == modify_other_url {
  617. is_has_modify_other = true
  618. }
  619. if strings.Split(item.Urlfor, ",")[0] == del_url {
  620. is_has_del = true
  621. }
  622. if strings.Split(item.Urlfor, ",")[0] == del_other_url {
  623. is_has_del_other = true
  624. }
  625. if strings.Split(item.Urlfor, ",")[0] == exce_url {
  626. is_has_exce = true
  627. }
  628. if strings.Split(item.Urlfor, ",")[0] == check_url {
  629. is_has_check = true
  630. }
  631. if strings.Split(item.Urlfor, ",")[0] == modify_exce_url {
  632. is_has_modify_exce = true
  633. }
  634. }
  635. } else {
  636. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeRole)
  637. return
  638. }
  639. this.ServeSuccessJSON(map[string]interface{}{
  640. "is_has_create": is_has_create,
  641. "is_has_modify": is_has_modify,
  642. "is_has_modify_other": is_has_modify_other,
  643. "is_has_del": is_has_del,
  644. "is_has_del_other": is_has_del_other,
  645. "is_has_exce": is_has_exce,
  646. "is_has_check": is_has_check,
  647. "is_has_modify_exce": is_has_modify_exce,
  648. "module": module,
  649. })
  650. } else {
  651. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAdminUserIsExit)
  652. return
  653. }
  654. } else {
  655. this.ServeSuccessJSON(map[string]interface{}{
  656. "is_has_create": true,
  657. "is_has_modify": true,
  658. "is_has_modify_other": true,
  659. "is_has_del": true,
  660. "is_has_del_other": true,
  661. "is_has_exce": true,
  662. "is_has_check": true,
  663. "is_has_modify_exce": true,
  664. "module": true,
  665. })
  666. }
  667. }
  668. func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
  669. newArr = make([]string, 0)
  670. for i := 0; i < len(arr); i++ {
  671. repeat := false
  672. for j := i + 1; j < len(arr); j++ {
  673. if arr[i] == arr[j] {
  674. repeat = true
  675. break
  676. }
  677. }
  678. if !repeat {
  679. newArr = append(newArr, arr[i])
  680. }
  681. }
  682. return
  683. }
  684. func RemoveRepeatedOrgElementTwo(orgs []*models.SgjUserOrg) (newOrgs []*models.SgjUserOrg) {
  685. newOrgs = make([]*models.SgjUserOrg, 0)
  686. for i := 0; i < len(orgs); i++ {
  687. repeat := false
  688. for j := i + 1; j < len(orgs); j++ {
  689. if orgs[i].ID == orgs[j].ID {
  690. repeat = true
  691. break
  692. }
  693. }
  694. if !repeat {
  695. newOrgs = append(newOrgs, orgs[i])
  696. }
  697. }
  698. return
  699. }