home_api_controller.go 23KB

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