home_api_controller.go 23KB

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