home_api_controller.go 23KB

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