home_api_controller.go 24KB

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