home_api_controller.go 24KB

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