apiController.go 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. package controllers
  2. /*
  3. 核心控制器
  4. 所有api均通过该控制器进行业务分发
  5. 请求路由由router.go自动注册,通过在控制器中的方法注释注册为路由
  6. */
  7. import (
  8. "bufio"
  9. "crypto/md5"
  10. "encoding/hex"
  11. "encoding/json"
  12. "fmt"
  13. "log"
  14. "net"
  15. "os"
  16. "path/filepath"
  17. "regexp"
  18. "runtime"
  19. "scd_check_tools/conf"
  20. "scd_check_tools/global"
  21. "scd_check_tools/logger"
  22. "scd_check_tools/models/bo"
  23. "scd_check_tools/models/enum"
  24. "scd_check_tools/mqtt"
  25. "scd_check_tools/tools"
  26. "strconv"
  27. "strings"
  28. "time"
  29. "github.com/astaxie/beego/orm"
  30. //"github.com/shopspring/decimal"
  31. "github.com/patrickmn/go-cache"
  32. )
  33. //基础功能服务
  34. type ApiController struct {
  35. BaseController
  36. }
  37. func (this *ApiController) Prepare() {
  38. fmt.Println(this.Data["RouterPattern"].(string))
  39. }
  40. func init() {
  41. global.GoCahce = cache.New(5*time.Minute, 60*time.Second)
  42. //GlobalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":300}`)
  43. //go GlobalSessions.GC()
  44. }
  45. // @Title 语音播放
  46. // @Summary 语音播放
  47. // @Description 语音播放
  48. // @Tags 基础服务接口
  49. // @Accept x-www-form-urlencoded
  50. // @Produce json
  51. // @Param filename query string false "文件名称"
  52. // @Success 200 status 成功
  53. // @Failure 500 status 失败
  54. // @router /playWav [POST]
  55. func (c *ApiController) PlayWav() {
  56. filename := c.GetString("filename", "")
  57. buf := make([]byte, 4096)
  58. n, _ := c.Ctx.Request.Body.Read(buf)
  59. if filename == "" && n > 0 {
  60. jsondata := map[string]string{}
  61. json.Unmarshal(buf[0:n], &jsondata)
  62. filename = jsondata["filename"]
  63. }
  64. if filename != "" {
  65. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  66. fmt.Println(dir)
  67. }
  68. c.Data["json"] = map[string]interface{}{"result": "1", "success": true}
  69. c.ServeJSON()
  70. }
  71. // @Summary 视频播放
  72. // @router /net/video/play [post]
  73. func (c *ApiController) PlayNetVideo() {
  74. filename := c.GetString("url", "")
  75. title := c.GetString("title")
  76. clientname := c.GetString("clientname")
  77. videoinfo := map[string]string{"url": filename, "title": title}
  78. msg, _ := json.Marshal(videoinfo)
  79. if clientname == "" {
  80. ip := c.Ctx.Request.Header.Get("X-Real-IP")
  81. if net.ParseIP(ip) == nil {
  82. ips := c.Ctx.Request.Header.Get("X-Forward-For")
  83. for _, i := range strings.Split(ips, ",") {
  84. if net.ParseIP(i) != nil {
  85. ip = i
  86. break
  87. }
  88. }
  89. if ip == "" {
  90. ip, _, _ = net.SplitHostPort(c.Ctx.Request.RemoteAddr)
  91. }
  92. }
  93. clientname = ip
  94. }
  95. mqtt.PublishMessage("/jujutong/police/videoplayer/"+clientname, string(msg))
  96. c.Data["json"] = c.WarpOK("")
  97. c.ServeJSON()
  98. }
  99. // @Summary 获取当前程序版本
  100. // @NotAuth 不需要授权可访问
  101. // @Description 获取当前程序版本
  102. // @Tags 基础服务接口
  103. // @Accept x-www-form-urlencoded
  104. // @Produce json
  105. // @Success 200 {object} WarpOK 成功
  106. // @Failure 500 {object} WarpError 失败
  107. // @router /getVersion [get]
  108. func (c *ApiController) GetVersion() {
  109. exename := conf.GlobalConfig["appid"]
  110. if string(runtime.GOOS) == "windows" {
  111. exename = exename + ".exe"
  112. }
  113. f, err1 := os.Open(exename)
  114. if err1 != nil {
  115. fmt.Println(tools.NowTime()+":", err1.Error())
  116. c.Data["json"] = c.WarpError("获取版本号失败")
  117. c.ServeJSON()
  118. return
  119. }
  120. fi, err2 := f.Stat()
  121. if err2 != nil {
  122. fmt.Println(tools.NowTime()+":", err2.Error())
  123. c.Data["json"] = c.WarpError("获取版本号失败")
  124. c.ServeJSON()
  125. return
  126. }
  127. data := fi.ModTime().Format("20060102-15")
  128. c.Data["json"] = c.WarpOK(data)
  129. c.ServeJSON()
  130. }
  131. func (c *ApiController) CheckSeesion() bool {
  132. userid := c.Ctx.Input.Session("userid")
  133. r := c.GetSession(userid)
  134. if r == nil || r == "" {
  135. return false
  136. }
  137. return true
  138. }
  139. func (c *ApiController) GetUserToken() string {
  140. Authorization := c.Ctx.Request.Header.Get("Authorization")
  141. if Authorization == "" {
  142. return ""
  143. }
  144. return strings.TrimLeft(Authorization, "Bearer ")
  145. }
  146. // @Summary 客户端心跳
  147. // @NotAuth 不需要授权可访问
  148. // @Description 客户端心跳保持,由浏览器或客户端发起请求,以表示其还处于使用中
  149. // @Tags 基础服务接口
  150. // @Accept x-www-form-urlencoded
  151. // @Produce json
  152. // @Success 200 status 成功
  153. // @Failure 500 status 失败
  154. // @router /keep-alive [get]
  155. func (c *ApiController) KeepAlive() {
  156. loginip := c.GetClientIP()
  157. fmt.Println("keep-alive IP:" + loginip)
  158. if loginip != "" {
  159. curtoken := c.GetUserToken()
  160. if curtoken == "" {
  161. global.GoCahce.Delete(loginip)
  162. } else {
  163. global.GoCahce.Set(loginip, "", 62*time.Second)
  164. }
  165. }
  166. c.Data["json"] = "ok"
  167. c.ServeJSON()
  168. }
  169. // @Summary 登出
  170. // @NotAuth 不需要授权可访问
  171. // @Description 登出系统。系统会自动清理当前用户的相关信息,如缓存、token等
  172. // @Tags 基础服务接口
  173. // @Accept x-www-form-urlencoded
  174. // @Produce json
  175. // @Param sessionid query string true "用户token码"
  176. // @Success 200 {object} WarpOK 成功
  177. // @Failure 500 {object} WarpError 失败
  178. // @router /logout [post]
  179. func (c *ApiController) Logout() {
  180. loginip := c.GetClientIP()
  181. sessionid := c.GetString("sessionid", "")
  182. bo.RemoveSession(sessionid)
  183. //清除缓存的该token的ip地址数据
  184. global.GoCahce.Delete(sessionid + "-ip")
  185. if loginip != "" {
  186. global.GoCahce.Delete(loginip)
  187. }
  188. c.Data["json"] = "ok"
  189. c.ServeJSON()
  190. }
  191. // @Summary 登录
  192. // @Description 登录系统。系统会返回当前用户的相关信息,如基本信息、token等
  193. // @Tags 基础服务接口
  194. // @Accept x-www-form-urlencoded
  195. // @Produce json
  196. // @Param login_account query string true "用户帐号"
  197. // @Param pwd query string true "用户密码"
  198. // @Success 200 {object} WarpOK 成功
  199. // @Failure 500 {object} WarpError 失败
  200. // @router /login [post]
  201. func (c *ApiController) Login() {
  202. username := c.GetString("login_account", "")
  203. password := c.GetString("pwd", "")
  204. loginip := c.GetClientIP()
  205. userInfo := map[string]interface{}{}
  206. userInfo["ip"] = loginip
  207. userInfo["name"] = username
  208. if loginip != "" {
  209. if global.AllowAccessIps != "*" {
  210. //判断当前客户端是否在允许范围内
  211. ips := strings.Split(global.AllowAccessIps, ",")
  212. allowLogin := false
  213. for _, iplimt := range ips {
  214. if iplimt == loginip {
  215. allowLogin = true
  216. break
  217. }
  218. tmppos := strings.Index(iplimt, ".*")
  219. if tmppos > 1 {
  220. //ip段
  221. if len(loginip) < tmppos {
  222. continue
  223. }
  224. if loginip[:tmppos] == iplimt[:tmppos] {
  225. allowLogin = true
  226. break
  227. }
  228. }
  229. }
  230. if !allowLogin {
  231. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s在未授权终端(IP:%s)尝试访问系统。被拦截!", username, loginip), userInfo)
  232. c.Ctx.Redirect(430, "/static/430.html")
  233. return
  234. }
  235. }
  236. //判断当前客户端是否已存在登录用户
  237. _, ok := global.GoCahce.Get(loginip)
  238. if ok && global.UserLoginClientLimt == "o2o" {
  239. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("登录失败:帐号%s在多个终端登录", username), userInfo)
  240. c.Data["json"] = c.WarpError("系统限制:同时只能登录一个用户帐号!")
  241. c.ServeJSON()
  242. return
  243. }
  244. }
  245. if password == "" {
  246. c.Data["json"] = c.WarpError("密码不能为空")
  247. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("登录失败:帐号%s尝试使用空密码登录!", username), userInfo)
  248. } else if username == "" {
  249. c.Data["json"] = c.WarpError("帐号不能为空")
  250. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("登录失败:用户尝试使用空帐号登录!"), userInfo)
  251. } else {
  252. logininfo2 := map[string]interface{}{}
  253. if info, ok := bo.LoginInfo.Load(username); ok {
  254. logininfo2 = info.(map[string]interface{})
  255. count := logininfo2["count"].(int)
  256. lasttime := logininfo2["lasttime"].(int64)
  257. timeout := global.LoginFailLockTime
  258. if count >= 6 {
  259. timeout = timeout + 60
  260. } else if count >= 7 {
  261. timeout = timeout + 1800
  262. } else if count >= 8 {
  263. timeout = timeout + 3600
  264. }
  265. if count >= global.LoginFailMaxCount {
  266. subtime := lasttime + int64(timeout) - time.Now().Unix()
  267. if subtime > 0 {
  268. if subtime > 60 {
  269. min := subtime / 60
  270. sen := subtime % 60
  271. c.Data["json"] = c.WarpError(fmt.Sprintf("连续失败超过%d次,帐号已被锁定,请%d分%d秒后重试", global.LoginFailMaxCount, min, sen))
  272. } else {
  273. c.Data["json"] = c.WarpError(fmt.Sprintf("连续失败超过%d次,帐号已被锁定,请%d秒后重试", global.LoginFailMaxCount, subtime))
  274. }
  275. userInfo := map[string]interface{}{}
  276. userInfo["name"] = username
  277. userInfo["ip"] = c.GetClientIP()
  278. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s连续失败超过%d次,帐号已被锁定", username, global.LoginFailMaxCount), userInfo)
  279. c.ServeJSON()
  280. return
  281. }
  282. }
  283. }
  284. password = tools.OriginalCode(password)
  285. user := make(map[string]interface{})
  286. userInfo, err := bo.UserLogin(username, password)
  287. if err != nil {
  288. logger.Logger.Debug(password)
  289. logger.Logger.Error(err, "登录失败:"+err.Error())
  290. c.Data["json"] = c.WarpError(err.Error())
  291. userInfo = map[string]interface{}{}
  292. userInfo["name"] = username
  293. userInfo["ip"] = loginip
  294. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, "登录失败:帐号"+username+"登录失败:"+err.Error(), userInfo)
  295. //bo.SaveSyslog("用户帐号或密码不正确", "系统登录", false, username)
  296. } else {
  297. bindip := tools.IsEmpty(userInfo["bind_ips"])
  298. userInfo["ip"] = loginip
  299. if bindip != "" {
  300. //限制登录IP
  301. allowLogin := false
  302. ips := strings.Split(bindip, ",")
  303. for _, ipitem := range ips {
  304. if ipitem == loginip {
  305. allowLogin = true
  306. break
  307. }
  308. tmppos := strings.Index(ipitem, ".*")
  309. if tmppos > 1 {
  310. //ip段
  311. if len(loginip) < tmppos {
  312. continue
  313. }
  314. if loginip[:tmppos] == ipitem[:tmppos] {
  315. allowLogin = true
  316. break
  317. }
  318. }
  319. }
  320. if !allowLogin {
  321. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("未授权终端(IP:%s)尝试访问系统被拦截", loginip), userInfo)
  322. c.Ctx.Redirect(430, "/static/430.html")
  323. return
  324. }
  325. }
  326. //判断是否有日期限制
  327. datelimit_start := tools.IsEmpty(userInfo["datelimit_start"])
  328. if datelimit_start != "" {
  329. nowDay := time.Now().Format("2006-01-02")
  330. if nowDay < datelimit_start {
  331. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s在当前日期内禁止访问系统", username), userInfo)
  332. c.Ctx.Redirect(431, "/static/431.html")
  333. return
  334. }
  335. datelimit_end := tools.IsEmpty(userInfo["datelimit_end"])
  336. if datelimit_end != "" {
  337. if nowDay > datelimit_end {
  338. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s在当前日期内禁止访问系统", username), userInfo)
  339. c.Ctx.Redirect(431, "/static/431.html")
  340. return
  341. }
  342. }
  343. }
  344. user["userid"] = userInfo["userid"].(string)
  345. //生成sessionid
  346. h := md5.New()
  347. h.Write([]byte(tools.NowTime()))
  348. cipherStr := h.Sum(nil)
  349. sessionid := hex.EncodeToString(cipherStr) // 输出加密结果
  350. err = bo.SetSession(sessionid, userInfo["userid"].(string))
  351. if err != nil {
  352. logger.Logger.Error(err)
  353. c.Data["json"] = c.WarpError(err.Error())
  354. new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s登录系统失败:%s", username, err.Error()), userInfo)
  355. //bo.SaveSyslog(err.Error(), "系统登录", false, username)
  356. c.ServeJSON()
  357. return
  358. }
  359. //获取用户权限
  360. useridint, _ := strconv.Atoi(userInfo["userid"].(string))
  361. funcs, err2 := bo.GetUserFunc(useridint)
  362. if err2 != nil {
  363. tools.Log(err2)
  364. }
  365. userInfo["funcs"] = funcs
  366. //获取系统的帐号密码修改模式
  367. pwd_edit_mod, _ := bo.GetSysParamValue("user_pwd_edit_mod", "admin")
  368. userInfo["pwdeditmod"] = pwd_edit_mod
  369. user["userinfo"] = userInfo
  370. user["sessionid"] = sessionid
  371. //c.SetSession("sessionid", sessionid)
  372. c.Data["json"] = c.WarpOK(user)
  373. thrid_uer_roleid := ""
  374. thrid_uer_role, has := global.GoCahce.Get("global_code_pl_role_coderole_third_user")
  375. if has && thrid_uer_role != nil {
  376. thrid_uer_roleid = tools.IsEmpty(thrid_uer_role.(orm.Params)["id"])
  377. }
  378. if tools.IsEmpty(userInfo["role"]) != thrid_uer_roleid {
  379. new(bo.SystemLog).Success(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, "帐号"+username+"登录系统成功", userInfo)
  380. //bo.SaveSyslog("登录系统成功", "系统登录", true, userInfo["name"].(string))
  381. }
  382. global.GoCahce.Set(loginip, username, 62*time.Second)
  383. }
  384. }
  385. c.ServeJSON()
  386. }
  387. // @Summary 保存用户信息(已废弃)
  388. // @router /saveUser [post]
  389. func (c *ApiController) SaveUser() {
  390. name := c.GetString("name", "")
  391. reg := regexp.MustCompile(`(#|\?|%|,)`)
  392. if len(reg.FindAllString(name, -1)) > 0 {
  393. c.Data["json"] = c.WarpError("名称不能包含特殊字符:#?%,")
  394. c.ServeJSON()
  395. return
  396. }
  397. var err error
  398. id, _ := strconv.Atoi(c.GetString("id", "0"))
  399. memo := c.GetString("memo", "")
  400. account := c.GetString("account", "")
  401. pwd := c.GetString("pwd", "")
  402. obj := bo.T_data_user{Id: id, Name: name, Memo: memo, Account: account, Pwd: pwd}
  403. obj.Createtime = strings.Trim(tools.NowTime(), " ")
  404. userid, err2 := strconv.Atoi(c.GetUserToken())
  405. if err2 != nil {
  406. c.Ctx.ResponseWriter.WriteHeader(401) //token获取失败或无效时,直接返回401
  407. return
  408. }
  409. obj.Createuser = userid
  410. insertId, err := bo.SaveUser(obj, c.GetCurrentUserInfo())
  411. //tmp, _ := json.Marshal(obj)
  412. //fmt.Println(string(tmp))
  413. if err != nil {
  414. c.Data["json"] = c.WarpError(err.Error())
  415. } else {
  416. c.Data["json"] = c.WarpOK(insertId)
  417. }
  418. c.ServeJSON()
  419. }
  420. // @Summary 查询指定用户的详细信息
  421. // @Description 查询用户信息
  422. // @Tags 基础服务接口
  423. // @Accept x-www-form-urlencoded
  424. // @Produce json
  425. // @Param id query int true "用户ID"
  426. // @Success 200 {object} WarpOK 成功
  427. // @Failure 500 {object} WarpError 失败
  428. // @router /getUser [get]
  429. func (c *ApiController) GetUser() {
  430. var id = 0
  431. id, _ = strconv.Atoi(c.GetString("id", "0"))
  432. param := tools.ParseFormParam(c.Ctx.Request.Form)
  433. obj := bo.T_data_user{Id: id}
  434. datalist, err := bo.QueryUserList(param, obj, c.GetCurrentUserInfo())
  435. if err != nil {
  436. c.Data["json"] = c.WarpError(err.Error())
  437. } else {
  438. c.Data["json"] = c.WarpPageOk(datalist["meta"].(map[string]interface{}), datalist["data"])
  439. }
  440. c.ServeJSON()
  441. }
  442. // @Summary 创建用户
  443. // @Description 查询用户信息
  444. // @Tags 基础服务接口
  445. // @Accept x-www-form-urlencoded
  446. // @Produce json
  447. // @Param userid query int true "用户ID"
  448. // @Param role query int true "角色ID"
  449. // @Param username query string true "姓名"
  450. // @Param account query string true "登录帐号"
  451. // @Param password query string true "登录密码"
  452. // @Param mobilephone query string false "手机号"
  453. // @Param pwd_expire query string false "密码过期日期"
  454. // @Param bind_ips query string false "允许登录的IP"
  455. // @Param datelimit_start query string false "帐号有效期开始日期"
  456. // @Param datelimit_end query string false "帐号有效期结束日期"
  457. // @Param memo query string false "其他说明"
  458. // @Success 200 {object} WarpOK 成功
  459. // @Failure 500 {object} WarpError 失败
  460. // @router /createuser [post]
  461. func (c *ApiController) CreateUser() {
  462. parameter := map[string]interface{}{}
  463. parameter["userid"] = c.GetString("userid", "0")
  464. parameter["role"] = c.GetString("role", "")
  465. username := c.GetString("username", "")
  466. parameter["username"] = username
  467. reg := regexp.MustCompile(`(#|\?|%|,',\*,&)`)
  468. if len(reg.FindAllString(username, -1)) > 0 {
  469. c.Data["json"] = c.WarpError("名称不能包含特殊字符:#?%,'*&")
  470. c.ServeJSON()
  471. return
  472. }
  473. parameter["account"] = c.GetString("account", "")
  474. parameter["password"] = c.GetString("password", "")
  475. parameter["memo"] = c.GetString("memo", "")
  476. parameter["mobilephone"] = c.GetString("mobilephone", "")
  477. parameter["pwd_expire"] = c.GetString("pwd_expire") //密码过期日期
  478. parameter["bind_ips"] = c.GetString("bind_ips", "") //允许登录的IP
  479. parameter["datelimit_start"] = c.GetString("datelimit_start", "") //帐号有效期开始日期
  480. parameter["datelimit_end"] = c.GetString("datelimit_end", "") //帐号有效期结束日期
  481. status, err := bo.CreateUser(parameter, c.GetCurrentUserInfo())
  482. tools.CheckErr(err)
  483. if err != nil {
  484. c.Data["json"] = c.WarpError(err.Error())
  485. } else {
  486. c.Data["json"] = c.WarpOK(status)
  487. }
  488. c.ServeJSON()
  489. }
  490. //修改密码。只能修改自己的密码
  491. // @Summary 修改自己密码
  492. // @Description 修改密码。只能修改自己的密码
  493. // @Tags 基础服务接口
  494. // @Accept x-www-form-urlencoded
  495. // @Produce json
  496. // @Param newpwd query int true "原密码"
  497. // @Param oldpwd query int true "新密码"
  498. // @Success 200 {object} WarpOK 成功
  499. // @Failure 500 {object} WarpError 失败
  500. // @router /resetUserPwd [post]
  501. func (c *ApiController) ResetUserPwd() {
  502. newPwd := c.GetString("newpwd")
  503. oldpwd := c.GetString("oldpwd")
  504. if oldpwd == "" {
  505. c.Data["json"] = c.WarpError("原密码不能为空")
  506. c.ServeJSON()
  507. return
  508. }
  509. if newPwd == "" {
  510. c.Data["json"] = c.WarpError("密码不能为空")
  511. } else {
  512. editmod, _ := bo.GetSysParamValue("user_pwd_edit_mod", "admin")
  513. if editmod == "" || editmod == "admin" {
  514. c.Data["json"] = c.WarpError("当前不允许修改密码,请联系管理员!")
  515. c.ServeJSON()
  516. return
  517. }
  518. if len(newPwd) > 32 {
  519. newPwd = tools.OriginalCode(newPwd)
  520. }
  521. check, msg := bo.PwdRuleCheck(newPwd)
  522. if !check {
  523. c.Data["json"] = c.WarpError(msg)
  524. c.ServeJSON()
  525. return
  526. }
  527. //原密码
  528. if len(oldpwd) > 32 {
  529. oldpwd = tools.OriginalCode(oldpwd)
  530. }
  531. logger.Logger.Debug(fmt.Sprintf("用户修改密码:新密码:%s \n 原密码:%s", newPwd, oldpwd))
  532. has := md5.Sum([]byte(oldpwd))
  533. oldpwd = fmt.Sprintf("%x", has)
  534. userinfo := c.GetCurrentUserInfo()
  535. userid := tools.IsEmpty(userinfo["userid"])
  536. obj := bo.T_data_user{Id: 0}
  537. obj.Id, _ = strconv.Atoi(userid)
  538. userOneInfo := bo.OneUser(obj)
  539. if userOneInfo.Pwd != oldpwd {
  540. c.Data["json"] = c.WarpError("原密码不正确!")
  541. c.ServeJSON()
  542. return
  543. }
  544. obj.Pwd = newPwd
  545. _, err := bo.SavePwd(obj, userinfo)
  546. if err != nil {
  547. c.Data["json"] = c.WarpError(err.Error())
  548. } else {
  549. c.Data["json"] = c.WarpOK("")
  550. }
  551. }
  552. c.ServeJSON()
  553. }
  554. // @Summary 指定用户的功能列表
  555. // @Description 指定用户的功能列表。非RBAC模式下有效
  556. // @Tags 基础服务接口
  557. // @Accept x-www-form-urlencoded
  558. // @Produce json
  559. // @Param id query int true "用户ID"
  560. // @Success 200 {object} WarpOK 成功
  561. // @Failure 500 {object} WarpError 失败
  562. // @router /getUserPrivList [get]
  563. func (c *ApiController) GetUserPrivListUser() {
  564. var id = 0
  565. id, _ = strconv.Atoi(c.GetString("id", "0"))
  566. obj := bo.T_data_user_func{Userid: id}
  567. datalist, err := bo.QueryUserPrivList(obj, c.GetCurrentUserInfo())
  568. if err != nil {
  569. c.Data["json"] = c.WarpError(err.Error())
  570. } else {
  571. c.Data["json"] = c.WarpOK(datalist["data"])
  572. }
  573. c.ServeJSON()
  574. }
  575. // @Summary 保存指定用户的功能列表
  576. // @Description 保存指定用户的功能列表。非RBAC模式下有效
  577. // @Tags 基础服务接口
  578. // @Accept x-www-form-urlencoded
  579. // @Produce json
  580. // @Param id query int true "用户ID"
  581. // @Param privids query string true "功能ID列表,使用逗号分隔"
  582. // @Success 200 {object} WarpOK 成功
  583. // @Failure 500 {object} WarpError 失败
  584. // @router /saveUserPrivList [post]
  585. func (c *ApiController) SaveUserPrivList() {
  586. uid, _ := strconv.Atoi(c.GetString("id", "0"))
  587. privids := c.GetString("privids")
  588. datalist, err := bo.SaveUserPrivList(uid, privids, c.GetCurrentUserInfo())
  589. if err != nil {
  590. c.Data["json"] = c.WarpError(err.Error())
  591. } else {
  592. c.Data["json"] = c.WarpOK(datalist)
  593. }
  594. c.ServeJSON()
  595. }
  596. // @Summary 搜索查询用户
  597. // @Description 查询用户
  598. // @Tags 基础服务接口
  599. // @Accept x-www-form-urlencoded
  600. // @Produce json
  601. // @Param name query int false "姓名"
  602. // @Param pageindex query int false "分页页码,默认为1"
  603. // @Param pagesize query int false "每页最大记录数,默认为20"
  604. // @Param area_id query int false "区域ID"
  605. // @Param role_id query int false "角色ID"
  606. // @Success 200 {object} WarpOK 成功
  607. // @Failure 500 {object} WarpError 失败
  608. // @router /searchUserInfo [get]
  609. func (c *ApiController) SearchUserInfo() {
  610. pageIndex, _ := c.GetInt("pageindex", 1)
  611. pageSize, _ := c.GetInt("pagesize", 20)
  612. name := c.GetString("name")
  613. area_id := c.GetString("area_id")
  614. role_id := c.GetString("role_id")
  615. tableData, number, err := bo.SearchUserInfo(area_id, name, role_id, pageIndex, pageSize, c.GetCurrentUserInfo())
  616. if err != nil {
  617. c.Data["json"] = c.ResultError(err.Error())
  618. } else {
  619. c.Data["json"] = c.ResultOK(tableData, number)
  620. }
  621. c.ServeJSON()
  622. }
  623. // 获取指定用户所管理的区域列表
  624. // @Summary 获取指定用户所管理的区域列表
  625. // @Description 获取指定用户所管理的区域列表
  626. // @Tags 基础服务接口
  627. // @Accept x-www-form-urlencoded
  628. // @Produce json
  629. // @Param userid query int true "用户ID"
  630. // @Success 200 {object} ResultOK 成功
  631. // @Failure 500 {object} ResultError 失败
  632. // @router /getUserAreaList [get]
  633. func (c *ApiController) GetUserAreaList() {
  634. uid, _ := c.GetInt("userid", 0)
  635. if uid == 0 {
  636. c.Data["json"] = c.ResultError("无效的用户ID")
  637. c.ServeJSON()
  638. return
  639. }
  640. obj := new(bo.UserAreaRelationObject)
  641. obj.Model.Userid = uid
  642. tableData, err := obj.List()
  643. if err != nil {
  644. c.Data["json"] = c.ResultError(err.Error())
  645. } else {
  646. c.Data["json"] = c.ResultOK(tableData, len(tableData))
  647. }
  648. c.ServeJSON()
  649. }
  650. //保存用户所管理的区域列表
  651. // @Summary 保存指定用户所管理的区域列表
  652. // @Description 保存指定用户所管理的区域列表
  653. // @Tags 基础服务接口
  654. // @Accept x-www-form-urlencoded
  655. // @Produce json
  656. // @Param userid query int true "用户ID"
  657. // @Param areaid query string true "区域列表。JSON串,格式如:[{"id":"区域ID","allchecked":"all|空"}]"
  658. // @Success 200 {object} ResultOK 成功
  659. // @Failure 500 {object} ResultError 失败
  660. // @router /saveUserAreaList [post]
  661. func (c *ApiController) SaveUserAreaList() {
  662. uid, _ := c.GetInt("userid", 0)
  663. if uid == 0 {
  664. c.Data["json"] = c.ResultError("无效的用户ID")
  665. c.ServeJSON()
  666. return
  667. }
  668. areaids := c.GetString("areaid")
  669. obj := new(bo.UserAreaRelationObject)
  670. obj.SetUserInfo(c.GetCurrentUserInfo())
  671. obj.Model.Userid = uid
  672. err := obj.Delete() //清除原来的数据
  673. if err != nil {
  674. c.Data["json"] = c.ResultError(err.Error())
  675. c.ServeJSON()
  676. return
  677. }
  678. if areaids != "" {
  679. ids := []map[string]string{}
  680. json.Unmarshal([]byte(areaids), &ids)
  681. for _, id := range ids {
  682. obj.Model.Areaid, _ = strconv.Atoi(id["id"])
  683. //如果是该节点是全选的,需要将节点下的所有节点添加到关系表
  684. isall := id["allchecked"]
  685. if isall == "all" {
  686. err = obj.AppendNodeAll(uid, obj.Model.Areaid)
  687. } else {
  688. //添加单个节点
  689. err = obj.Save()
  690. }
  691. if err != nil {
  692. c.Data["json"] = c.ResultError(err.Error())
  693. c.ServeJSON()
  694. return
  695. }
  696. }
  697. }
  698. c.Data["json"] = c.ResultOK("", 0)
  699. c.ServeJSON()
  700. }
  701. // @Summary 获取指定角色的功能点列表
  702. // @Description 获取指定角色的功能点列表
  703. // @Tags 基础服务接口
  704. // @Accept x-www-form-urlencoded
  705. // @Produce json
  706. // @Param roleid query int true "角色ID"
  707. // @Success 200 {object} WarpOK 成功
  708. // @Failure 500 {object} WarpError 失败
  709. // @router /getRoleFuncList [get]
  710. func (c *ApiController) GetRoleFuncList() {
  711. roleid := c.GetString("roleid", "")
  712. if roleid == "" {
  713. c.Data["json"] = c.WarpError("角色ID不能为空")
  714. c.ServeJSON()
  715. return
  716. }
  717. roleidint, _ := strconv.Atoi(roleid)
  718. datalist, err := bo.GetRoleFunc(roleidint)
  719. if err != nil {
  720. c.Data["json"] = c.WarpError(err.Error())
  721. } else {
  722. c.Data["json"] = c.WarpOK(datalist)
  723. }
  724. c.ServeJSON()
  725. }
  726. // @Summary 保存指定角色的功能点列表
  727. // @Description 保存指定角色的功能点列表
  728. // @Tags 基础服务接口
  729. // @Accept x-www-form-urlencoded
  730. // @Produce json
  731. // @Param roleid query int true "角色ID"
  732. // @Param areaid query string true "区域列表。JSON串,格式如:[{"id":"区域ID","allchecked":"all|空"}]"
  733. // @Success 200 {object} WarpOK 成功
  734. // @Failure 500 {object} WarpError 失败
  735. // @router /saveRoleFunc [post]
  736. func (c *ApiController) SaveRoleFunc() {
  737. roleid := c.GetString("roleid", "")
  738. if roleid == "" {
  739. c.Data["json"] = c.WarpError("角色ID不能为空")
  740. c.ServeJSON()
  741. return
  742. }
  743. funcids := c.GetString("funcids", "")
  744. tmpAry := strings.Split(funcids, ",")
  745. roleidint, _ := strconv.Atoi(roleid)
  746. err := bo.SaveRoleFunc(roleidint, tmpAry, c.GetCurrentUserInfo())
  747. if err != nil {
  748. c.Data["json"] = c.WarpError(err.Error())
  749. } else {
  750. c.Data["json"] = c.WarpOK("")
  751. }
  752. c.ServeJSON()
  753. }
  754. // @Summary 初始化系统数据
  755. // @router /sys/init [post]
  756. func (c *ApiController) SysInit() {
  757. orgname := c.GetString("orgname")
  758. sn := c.GetString("sn")
  759. account := c.GetString("account")
  760. pwd := c.GetString("pwd")
  761. if orgname == "" || sn == "" {
  762. c.Data["json"] = c.WarpError("参数不正确")
  763. c.ServeJSON()
  764. return
  765. }
  766. pm := bo.Global_sys_param{Param_name: "orgname", Param_value: orgname, Param_memo: "单位"}
  767. _, err := bo.SaveSysParam(pm)
  768. if err != nil {
  769. c.Data["json"] = c.WarpError(err.Error())
  770. c.ServeJSON()
  771. return
  772. }
  773. if account != "" && pwd != "" {
  774. //清除原来的非超级管理员帐号
  775. bo.InitUser()
  776. bo.ClearAllSyslog()
  777. parameter := map[string]interface{}{}
  778. parameter["userid"] = ""
  779. parameter["role"] = c.GetString("role", "0")
  780. parameter["username"] = c.GetString("username", "")
  781. parameter["account"] = account
  782. parameter["password"] = pwd
  783. parameter["memo"] = c.GetString("memo", "")
  784. _, err = bo.CreateUser(parameter)
  785. if err != nil {
  786. c.Data["json"] = c.WarpError(err.Error())
  787. c.ServeJSON()
  788. return
  789. }
  790. }
  791. c.Data["json"] = c.WarpOK("")
  792. c.ServeJSON()
  793. }
  794. // @Summary 查询操作日志列表
  795. // @Description 查询操作日志列表
  796. // @Tags 基础服务接口
  797. // @Accept x-www-form-urlencoded
  798. // @Produce json
  799. // @Param pageindex query int true "当前查询页码。默认为1"
  800. // @Param pagesize query int true "每页显示数据条数。默认20"
  801. // @Param enddate query string false "查询条件:结束日期"
  802. // @Param startdate query string false "查询条件:开始日期"
  803. // @Param description query string false "查询条件:日志内容。模糊匹配"
  804. // @Param staff query string false "查询条件:操作人姓名。模糊匹配"
  805. // @Param ip query string false "查询条件:操作IP地址。模糊匹配"
  806. // @Param success query int false "查询条件:操作结果。关联系统字典log_optresult。0: 失败 1:成功"
  807. // @Param opttype query int false "查询条件:操作类型。关联系统字典log_opttype。"
  808. // @Param audittype query int false "查询条件:审计类型。关联系统字典log_audittype。"
  809. // @Param eventtype query int false "查询条件:事件类型。关联系统字典log_eventtype。"
  810. // @Param eventlevel query int false "查询条件:事件等级。关联系统字典log_eventlevel。"
  811. // @Success 200 {object} ResultOK 成功
  812. // @Failure 500 {object} ResultError 失败
  813. // @router /getSyslogList [get]
  814. func (c *ApiController) GetSysLog() {
  815. pageIndex, _ := c.GetInt("pageindex", 1)
  816. pageSize, _ := c.GetInt("pagesize", 20)
  817. con := map[string]string{}
  818. con["enddate"] = c.GetString("enddate", "")
  819. con["startdate"] = c.GetString("startdate", "")
  820. con["description"] = c.GetString("description", "")
  821. con["staff"] = c.GetString("staff", "")
  822. con["ip"] = c.GetString("ip", "")
  823. con["success"] = c.GetString("success", "")
  824. con["logtype"] = c.GetString("opttype", "")
  825. con["audittype"] = c.GetString("audittype", "")
  826. con["eventtype"] = c.GetString("eventtype", "")
  827. con["eventlevel"] = c.GetString("eventlevel", "")
  828. userinfo := c.GetCurrentUserInfo()
  829. syslog := new(bo.SystemLog)
  830. syslog.SetUserInfo(userinfo)
  831. tableData, number, err := syslog.SearchLogList(con, pageIndex, pageSize)
  832. if err != nil {
  833. c.Data["json"] = c.ResultError(err.Error())
  834. } else {
  835. c.Data["json"] = c.ResultOK(tableData, number)
  836. }
  837. c.ServeJSON()
  838. }
  839. // @Summary 操作日志分类(图形)统计
  840. // @Description 操作日志分类(图形)统计
  841. // @Tags 基础服务接口
  842. // @Accept x-www-form-urlencoded
  843. // @Produce json
  844. // @Param enddate query string false "统计条件:结束日期"
  845. // @Param startdate query string false "统计条件:开始日期"
  846. // @Param description query string false "统计条件:日志内容。模糊匹配"
  847. // @Param staff query string false "统计条件:操作人姓名。模糊匹配"
  848. // @Param ip query string false "统计条件:操作IP地址。模糊匹配"
  849. // @Param success query int false "统计条件:操作结果。关联系统字典log_optresult。0: 失败 1:成功"
  850. // @Param opttype query int false "统计条件:操作类型。关联系统字典log_opttype。"
  851. // @Param audittype query int false "统计条件:审计类型。关联系统字典log_audittype。"
  852. // @Param eventtype query int false "统计条件:事件类型。关联系统字典log_eventtype。"
  853. // @Param eventlevel query int false "统计条件:事件等级。关联系统字典log_eventlevel。"
  854. // @Param cycletype query int false "统计周期。值列表为:year,month,day,hour,week,默认为hour。"
  855. // @Success 200 {object} ResultOK 成功
  856. // @Failure 500 {object} ResultError 失败
  857. // @router /log/chartstat [get]
  858. func (c *ApiController) GetECharsSysLogStat() {
  859. con := map[string]string{}
  860. con["enddate"] = c.GetString("enddate", "")
  861. con["startdate"] = c.GetString("startdate", "")
  862. con["description"] = c.GetString("description", "")
  863. con["staff"] = c.GetString("staff", "")
  864. con["ip"] = c.GetString("ip", "")
  865. con["success"] = c.GetString("success", "")
  866. con["logtype"] = c.GetString("opttype", "")
  867. con["audittype"] = c.GetString("audittype", "")
  868. con["eventtype"] = c.GetString("eventtype", "")
  869. con["eventlevel"] = c.GetString("eventlevel", "")
  870. con["cycletype"] = c.GetString("cycletype", "")
  871. userinfo := c.GetCurrentUserInfo()
  872. syslog := new(bo.SystemLog)
  873. syslog.SetUserInfo(userinfo)
  874. tableData, err := syslog.EchartStat(con)
  875. if err != nil {
  876. c.Data["json"] = c.ResultError(err.Error())
  877. } else {
  878. c.Data["json"] = c.ResultOK(tableData, 0)
  879. }
  880. c.ServeJSON()
  881. }
  882. // @Summary 操作日志分类(数据列表)统计
  883. // @Description 操作日志分类(数据列表)统计
  884. // @Tags 基础服务接口
  885. // @Accept x-www-form-urlencoded
  886. // @Produce json
  887. // @Param enddate query int false "统计条件:结束日期"
  888. // @Param startdate query int false "统计条件:开始日期"
  889. // @Param description query int false "统计条件:日志内容。模糊匹配"
  890. // @Param staff query string false "统计条件:操作人姓名。模糊匹配"
  891. // @Param ip query string false "统计条件:操作IP地址。模糊匹配"
  892. // @Param success query int false "统计条件:操作结果。关联系统字典log_optresult。0: 失败 1:成功"
  893. // @Param opttype query int false "统计条件:操作类型。关联系统字典log_opttype。"
  894. // @Param audittype query int false "统计条件:审计类型。关联系统字典log_audittype。"
  895. // @Param eventtype query int false "统计条件:事件类型。关联系统字典log_eventtype。"
  896. // @Param eventlevel query int false "统计条件:事件等级。关联系统字典log_eventlevel。"
  897. // @Param stattype query int false "统计类型。值为:logtype或者空。"
  898. // @Success 200 {object} ResultOK 成功
  899. // @Failure 500 {object} ResultError 失败
  900. // @router /log/datastat [get]
  901. func (c *ApiController) GetDataSysLogStat() {
  902. con := map[string]string{}
  903. con["enddate"] = c.GetString("enddate", "")
  904. con["startdate"] = c.GetString("startdate", "")
  905. con["description"] = c.GetString("description", "")
  906. con["staff"] = c.GetString("staff", "")
  907. con["ip"] = c.GetString("ip", "")
  908. con["success"] = c.GetString("success", "")
  909. con["logtype"] = c.GetString("opttype", "")
  910. con["audittype"] = c.GetString("audittype", "")
  911. con["eventtype"] = c.GetString("eventtype", "")
  912. con["eventlevel"] = c.GetString("eventlevel", "")
  913. con["stattype"] = c.GetString("stattype", "") //统计字段
  914. userinfo := c.GetCurrentUserInfo()
  915. syslog := new(bo.SystemLog)
  916. syslog.SetUserInfo(userinfo)
  917. tableData, err := syslog.DataStat(con)
  918. if err != nil {
  919. c.Data["json"] = c.ResultError(err.Error())
  920. } else {
  921. c.Data["json"] = c.ResultOK(tableData, 0)
  922. }
  923. c.ServeJSON()
  924. }
  925. // @Summary 操作日志备份
  926. // @Description 操作日志备份
  927. // @Tags 基础服务接口
  928. // @Accept x-www-form-urlencoded
  929. // @Produce json
  930. // @Param enddate query string false "备份结束日期"
  931. // @Param startdate query string false "备份开始日期"
  932. // @Success 200 {object} ResultOK 成功
  933. // @Failure 500 {object} ResultError 失败
  934. // @router /log/backup [post]
  935. func (c *ApiController) BackupSysLog() {
  936. con := map[string]string{}
  937. con["enddate"] = c.GetString("enddate", "")
  938. con["startdate"] = c.GetString("startdate", "")
  939. userinfo := c.GetCurrentUserInfo()
  940. syslog := new(bo.SystemLog)
  941. syslog.SetUserInfo(userinfo)
  942. err := syslog.Backup(con)
  943. if err != nil {
  944. c.Data["json"] = c.ResultError(err.Error())
  945. } else {
  946. c.Data["json"] = c.ResultOK("", 0)
  947. }
  948. c.ServeJSON()
  949. }
  950. // @Summary 获取操作日志备份列表
  951. // @Description 获取操作日志备份列表
  952. // @Tags 基础服务接口
  953. // @Accept x-www-form-urlencoded
  954. // @Produce json
  955. // @Param pageindex query int false "当前查询的分页页码,默认为1"
  956. // @Param pagesize query int false "每页显示的数据条数,默认为20"
  957. // @Param enddate query string false "备份结束日期"
  958. // @Param startdate query string false "备份开始日期"
  959. // @Success 200 {object} ResultOK 成功
  960. // @Failure 500 {object} ResultError 失败
  961. // @router /log/backup/list [get]
  962. func (c *ApiController) BackupSysLogList() {
  963. pageIndex, _ := c.GetInt("pageindex", 1)
  964. pageSize, _ := c.GetInt("pagesize", 20)
  965. con := map[string]string{}
  966. con["enddate"] = c.GetString("enddate", "")
  967. con["startdate"] = c.GetString("startdate", "")
  968. userinfo := c.GetCurrentUserInfo()
  969. syslog := new(bo.SystemLog)
  970. syslog.SetUserInfo(userinfo)
  971. lst, total, err := syslog.BackupList(con, pageIndex, pageSize)
  972. if err != nil {
  973. c.Data["json"] = c.ResultError(err.Error())
  974. } else {
  975. c.Data["json"] = c.ResultOK(lst, total)
  976. }
  977. c.ServeJSON()
  978. }
  979. // @Summary 操作日志备份删除
  980. // @Description 操作日志备份删除
  981. // @Tags 基础服务接口
  982. // @Accept x-www-form-urlencoded
  983. // @Produce json
  984. // @Param id query int true "备份记录ID"
  985. // @Success 200 {object} ResultOK 成功
  986. // @Failure 500 {object} ResultError 失败
  987. // @router /log/backup/del [post]
  988. func (c *ApiController) DelBackupLog() {
  989. userinfo := c.GetCurrentUserInfo()
  990. sysdb := new(bo.SystemLog)
  991. sysdb.SetUserInfo(userinfo)
  992. id, _ := c.GetInt("id")
  993. err := sysdb.Delete(id)
  994. if err != nil {
  995. c.Data["json"] = c.ResultError(err.Error())
  996. } else {
  997. c.Data["json"] = c.ResultOK("", 0)
  998. }
  999. c.ServeJSON()
  1000. }
  1001. // @Summary 获取数据备份列表
  1002. // @Description 获取数据备份列表
  1003. // @Tags 基础服务接口
  1004. // @Accept x-www-form-urlencoded
  1005. // @Produce json
  1006. // @Param pageindex query int false "当前查询的分页页码,默认为1"
  1007. // @Param pagesize query int false "每页显示的数据条数,默认为20"
  1008. // @Param enddate query string false "备份结束日期"
  1009. // @Param startdate query string false "备份开始日期"
  1010. // @Success 200 {object} ResultOK 成功
  1011. // @Failure 500 {object} ResultError 失败
  1012. // @router /db/backup/list [get]
  1013. func (c *ApiController) BackupDBList() {
  1014. pageIndex, _ := c.GetInt("pageindex", 1)
  1015. pageSize, _ := c.GetInt("pagesize", 20)
  1016. con := map[string]string{}
  1017. con["enddate"] = c.GetString("enddate", "")
  1018. con["startdate"] = c.GetString("startdate", "")
  1019. userinfo := c.GetCurrentUserInfo()
  1020. sysdb := new(bo.GlobalBackupMgr)
  1021. sysdb.SetUserInfo(userinfo)
  1022. lst, total, err := sysdb.List(con, pageIndex, pageSize)
  1023. if err != nil {
  1024. c.Data["json"] = c.ResultError(err.Error())
  1025. } else {
  1026. c.Data["json"] = c.ResultOK(lst, total)
  1027. }
  1028. c.ServeJSON()
  1029. }
  1030. // @Summary 创建新数据备份
  1031. // @Description 创建新数据备份
  1032. // @Tags 基础服务接口
  1033. // @Accept x-www-form-urlencoded
  1034. // @Produce json
  1035. // @Success 200 {object} ResultOK 成功
  1036. // @Failure 500 {object} ResultError 失败
  1037. // @router /db/backup/new [post]
  1038. func (c *ApiController) NewBackupDB() {
  1039. userinfo := c.GetCurrentUserInfo()
  1040. sysdb := new(bo.GlobalBackupMgr)
  1041. sysdb.SetUserInfo(userinfo)
  1042. err := sysdb.Save()
  1043. if err != nil {
  1044. c.Data["json"] = c.ResultError(err.Error())
  1045. } else {
  1046. c.Data["json"] = c.ResultOK("", 0)
  1047. }
  1048. c.ServeJSON()
  1049. }
  1050. // @Summary 删除指定的数据备份
  1051. // @Description 删除指定的数据备份
  1052. // @Tags 基础服务接口
  1053. // @Accept x-www-form-urlencoded
  1054. // @Produce json
  1055. // @Param id query int false "备份记录ID"
  1056. // @Success 200 {object} ResultOK 成功
  1057. // @Failure 500 {object} ResultError 失败
  1058. // @router /db/backup/del [post]
  1059. func (c *ApiController) DelBackupDB() {
  1060. userinfo := c.GetCurrentUserInfo()
  1061. sysdb := new(bo.GlobalBackupMgr)
  1062. sysdb.SetUserInfo(userinfo)
  1063. sysdb.Model.Id, _ = c.GetInt("id")
  1064. err := sysdb.Delete()
  1065. if err != nil {
  1066. c.Data["json"] = c.ResultError(err.Error())
  1067. } else {
  1068. c.Data["json"] = c.ResultOK("", 0)
  1069. }
  1070. c.ServeJSON()
  1071. }
  1072. // @Summary 数据还原
  1073. // @Description 还原指定的数据备份
  1074. // @Tags 基础服务接口
  1075. // @Accept x-www-form-urlencoded
  1076. // @Produce json
  1077. // @Param id query int true "备份记录ID"
  1078. // @Success 200 {object} ResultOK 成功
  1079. // @Failure 500 {object} ResultError 失败
  1080. // @router /db/backup/recover [post]
  1081. func (c *ApiController) RecorverBackupDB() {
  1082. cid, _ := c.GetInt("id")
  1083. userinfo := c.GetCurrentUserInfo()
  1084. sysdb := new(bo.GlobalBackupMgr)
  1085. sysdb.SetUserInfo(userinfo)
  1086. err := sysdb.Recover(cid)
  1087. if err != nil {
  1088. c.Data["json"] = c.ResultError(err.Error())
  1089. } else {
  1090. c.Data["json"] = c.ResultOK("", 0)
  1091. }
  1092. c.ServeJSON()
  1093. }
  1094. // @Summary 获取当前的服务器运行日志
  1095. // @Description 获取当前的服务器运行日志。每次获取10Kb的日志内容,第一次获取时默认为0,继续获取时需要将上次的最大位置传给offset参数。
  1096. // @Tags 基础服务接口
  1097. // @Accept x-www-form-urlencoded
  1098. // @Produce json
  1099. // @Param offset query int true "获取的日志起始字节位置。"
  1100. // @Success 200 {object} ResultOK 成功
  1101. // @Failure 500 {object} ResultError 失败
  1102. // @router /log/runtime_log [get]
  1103. func (c *ApiController) GetSystemRuntimeLog() {
  1104. logfilename := "./" + time.Now().Format("20060102") + ".log"
  1105. fH, ferr := os.Stat(logfilename)
  1106. if ferr != nil {
  1107. c.Data["json"] = c.ResultError(ferr.Error())
  1108. c.ServeJSON()
  1109. return
  1110. }
  1111. logsize := fH.Size()
  1112. if logsize == 0 {
  1113. c.Data["json"] = c.ResultOK("", 0)
  1114. c.ServeJSON()
  1115. return
  1116. }
  1117. offsetkb, _ := c.GetInt64("offset", 0)
  1118. loghander, err2 := os.Open(logfilename)
  1119. if err2 != nil {
  1120. logger.Logger.Error(err2)
  1121. c.Data["json"] = c.ResultError(ferr.Error())
  1122. c.ServeJSON()
  1123. return
  1124. }
  1125. defer loghander.Close()
  1126. logreader := bufio.NewReader(loghander)
  1127. if offsetkb == 0 {
  1128. //初始加载,只获取最新的日志(10Kb)
  1129. if logsize > int64(1024*10) {
  1130. offsetkb = logsize - int64(1024*10)
  1131. }
  1132. }
  1133. _, ferr = loghander.Seek(offsetkb, 0)
  1134. if ferr != nil {
  1135. logger.Logger.Error(ferr)
  1136. c.Data["json"] = c.ResultError(ferr.Error())
  1137. c.ServeJSON()
  1138. return
  1139. }
  1140. var buf []byte
  1141. if (logsize - offsetkb) > int64(1024*10) {
  1142. buf = make([]byte, 1024*10)
  1143. } else {
  1144. buf = make([]byte, logsize-offsetkb)
  1145. }
  1146. n, ferr := logreader.Read(buf)
  1147. if ferr != nil {
  1148. logger.Logger.Error(ferr)
  1149. c.Data["json"] = c.ResultError(ferr.Error())
  1150. c.ServeJSON()
  1151. return
  1152. }
  1153. c.Data["json"] = c.ResultOK(string(buf), offsetkb+int64(n))
  1154. c.ServeJSON()
  1155. }
  1156. // @Summary 获取系统告警列表
  1157. // @Description 获取系统告警列表
  1158. // @Tags 基础服务接口
  1159. // @Accept x-www-form-urlencoded
  1160. // @Produce json
  1161. // @Param pageindex query int false "当前查询的分页页码,默认为1"
  1162. // @Param pagesize query int false "每页显示的数据条数,默认为20"
  1163. // @Param enddate query string false "查询条件:结束日期"
  1164. // @Param startdate query string false "查询条件:开始日期"
  1165. // @Param eventtype query int false "查询条件:事件类型。关联系统字典代码log_eventtype。"
  1166. // @Param alarmtype query string false "查询条件:告警类型。值为disk|cpu|mem之一。"
  1167. // @Param description query string false "查询条件:告警内容。模糊查询"
  1168. // @Success 200 {object} ResultOK 成功
  1169. // @Failure 500 {object} ResultError 失败
  1170. // @router /sys/alarm/list [get]
  1171. func (c *ApiController) GetSysAlarmList() {
  1172. pageIndex, _ := c.GetInt("pageindex", 1)
  1173. pageSize, _ := c.GetInt("pagesize", 20)
  1174. con := map[string]string{}
  1175. con["enddate"] = c.GetString("enddate", "")
  1176. con["startdate"] = c.GetString("startdate", "")
  1177. con["eventtype"] = c.GetString("eventtype", "")
  1178. con["alarmtype"] = c.GetString("alarmtype", "")
  1179. con["description"] = c.GetString("description")
  1180. userinfo := c.GetCurrentUserInfo()
  1181. syslog := new(bo.SystemAlarm)
  1182. syslog.SetUserInfo(userinfo)
  1183. lst, total, err := syslog.SearchList(con, pageIndex, pageSize)
  1184. if err != nil {
  1185. c.Data["json"] = c.ResultError(err.Error())
  1186. } else {
  1187. c.Data["json"] = c.ResultOK(lst, total)
  1188. }
  1189. c.ServeJSON()
  1190. }
  1191. // @Summary 获取全部或者指定的系统参数列表
  1192. // @NotAuth 不需要授权可访问
  1193. // @Description 获取全部或者指定的系统参数列表
  1194. // @Tags 基础服务接口
  1195. // @Accept x-www-form-urlencoded
  1196. // @Produce json
  1197. // @Param param_name query string false "参数名"
  1198. // @Success 200 {object} ResultOK 成功
  1199. // @Failure 500 {object} ResultError 失败
  1200. // @router /getSysParamList [get]
  1201. func (c *ApiController) GetSystemParam() {
  1202. param_name := c.GetString("param_name")
  1203. obj := bo.Global_sys_param{Param_name: param_name}
  1204. datalist, err := bo.GetSysParamList(obj, c.GetCurrentUserInfo())
  1205. if err != nil {
  1206. c.Data["json"] = c.ResultError(err.Error())
  1207. } else {
  1208. c.Data["json"] = c.ResultOK(datalist, len(datalist))
  1209. }
  1210. c.ServeJSON()
  1211. }
  1212. // 保存系统参数 godoc
  1213. // @Summary 保存系统参数
  1214. // @Description 保存系统参数
  1215. // @Tags 基础服务接口
  1216. // @Accept x-www-form-urlencoded
  1217. // @Produce json
  1218. // @Param param_name query string false "参数缟编码"
  1219. // @Param param_value query string false "参数值"
  1220. // @Param param_memo query string false "参数描述"
  1221. // @Success 200 {object} ResultOK 成功
  1222. // @Success 200 {object} ResultError 错误
  1223. // @Failure 500 status 失败
  1224. // @router /saveSysParam [post]
  1225. func (c *ApiController) SaveSystemParam() {
  1226. param_name := c.GetString("param_name")
  1227. param_value := c.GetString("param_value")
  1228. param_memo := c.GetString("param_memo")
  1229. obj := bo.Global_sys_param{Param_name: param_name, Param_memo: param_memo, Param_value: param_value}
  1230. _, err := bo.SaveSysParam(obj, c.GetCurrentUserInfo())
  1231. if err != nil {
  1232. c.Data["json"] = c.ResultError(err.Error())
  1233. } else {
  1234. c.Data["json"] = c.ResultOK("", 0)
  1235. }
  1236. c.ServeJSON()
  1237. }
  1238. // @Summary 获取指定系统字典的下级字典码定义
  1239. // @Description 获取指定系统字典的下级字典码定义
  1240. // @Tags 基础服务接口
  1241. // @Accept x-www-form-urlencoded
  1242. // @Produce json
  1243. // @Param code query string false "未指定code时,将返回根字典下的子字典定义列表。"
  1244. // @Success 200 {object} ResultOK 成功
  1245. // @Failure 500 {object} ResultError 失败
  1246. // @router /getGlobalChildrenCode [get]
  1247. func (c *ApiController) GetGlobalChildrenCode() {
  1248. param := tools.ParseFormParam(c.Ctx.Request.Form)
  1249. param["code"] = c.GetString("code", "")
  1250. obj := new(bo.Global)
  1251. tableData, err := obj.GetChildrenGlobalCode(tools.IsEmpty(param["code"]))
  1252. if err != nil {
  1253. c.Data["json"] = c.ResultError(err.Error())
  1254. } else {
  1255. c.Data["json"] = c.ResultOK(tableData, 0)
  1256. }
  1257. c.ServeJSON()
  1258. }
  1259. // @Summary 根据id获取字典定义
  1260. // @Description 根据id获取字典定义
  1261. // @Tags 基础服务接口
  1262. // @Accept x-www-form-urlencoded
  1263. // @Produce json
  1264. // @Param id query string false "系统字典码ID"
  1265. // @Success 200 {object} ResultOK 成功
  1266. // @Failure 500 {object} ResultError 失败
  1267. // @router /getGlobalCode/byid [get]
  1268. func (c *ApiController) GetGlobalCodeByID() {
  1269. param := tools.ParseFormParam(c.Ctx.Request.Form)
  1270. param["id"] = c.GetString("id", "")
  1271. param["pageindex"] = c.GetString("pageindex", "1")
  1272. param["pagesize"] = c.GetString("pagesize", "1")
  1273. obj := new(bo.Global)
  1274. tableData, number, err := obj.QueryGlobalCodeList(param)
  1275. if err != nil {
  1276. c.Data["json"] = c.ResultError(err.Error())
  1277. } else {
  1278. c.Data["json"] = c.ResultOK(tableData, number)
  1279. }
  1280. c.ServeJSON()
  1281. }
  1282. // @Summary 查询系统字典列表
  1283. // @Description 查询系统字典列表
  1284. // @Tags 基础服务接口
  1285. // @Accept x-www-form-urlencoded
  1286. // @Produce json
  1287. // @Param pageindex query int false "当前查询的页码。默认为1"
  1288. // @Param pagesize query int false "每页显示的数据记录数,默认为20"
  1289. // @Param pcode query string false "查询条件:指定的父级字典码"
  1290. // @Param code query string false "查询条件:指定的字典码"
  1291. // @Success 200 {object} ResultOK 成功
  1292. // @Failure 500 {object} ResultError 失败
  1293. // @router /getGlobalCode [get]
  1294. func (c *ApiController) GetGlobalCode() {
  1295. param := tools.ParseFormParam(c.Ctx.Request.Form)
  1296. param["pcode"] = c.GetString("pcode", "")
  1297. param["code"] = c.GetString("code", "")
  1298. param["pageindex"] = c.GetString("pageindex", "1")
  1299. param["pagesize"] = c.GetString("pagesize", "20")
  1300. obj := new(bo.Global)
  1301. tableData, number, err := obj.QueryGlobalCodeList(param)
  1302. if err != nil {
  1303. c.Data["json"] = c.ResultError(err.Error())
  1304. } else {
  1305. c.Data["json"] = c.ResultOK(tableData, number)
  1306. }
  1307. c.ServeJSON()
  1308. }
  1309. // @Summary 获取区域列表
  1310. // @Description 获取区域列表。以tree结构形式返回。
  1311. // @Tags 基础服务接口
  1312. // @Accept x-www-form-urlencoded
  1313. // @Produce json
  1314. // @Success 200 {object} ResultOK 成功
  1315. // @Failure 500 {object} ResultError 失败
  1316. // @router /area/tree [get]
  1317. func (c *ApiController) GetAdminAreaTree() {
  1318. obj := bo.BasicArea{}
  1319. obj.SetUserInfo(c.GetCurrentUserInfo())
  1320. gettype := c.GetString("type")
  1321. if gettype == "1" {
  1322. tableData, err := new(bo.Global).GetCategoryTree()
  1323. if err != nil {
  1324. c.Data["json"] = c.ResultError(err.Error())
  1325. } else {
  1326. c.Data["json"] = c.ResultOK(tableData, 0)
  1327. }
  1328. } else {
  1329. tableData, err := obj.GetAreaTree()
  1330. if err != nil {
  1331. c.Data["json"] = c.ResultError(err.Error())
  1332. } else {
  1333. c.Data["json"] = c.ResultOK(tableData, 0)
  1334. }
  1335. }
  1336. c.ServeJSON()
  1337. }
  1338. // @Summary 获取指定获取的下级区域列表
  1339. // @Description 获取指定获取的下级区域列表
  1340. // @Tags 基础服务接口
  1341. // @Accept x-www-form-urlencoded
  1342. // @Produce json
  1343. // @Param id query string true "查询条件:区域ID"
  1344. // @Param all query int false "查询条件:是否获取全部子区域。1表示是,反之表示否。默认为0"
  1345. // @Success 200 {object} ResultOK 成功
  1346. // @Failure 500 {object} ResultError 失败
  1347. // @router /area/children/list [get]
  1348. func (c *ApiController) GetAdminAreaChildren() {
  1349. obj := new(bo.BasicArea)
  1350. obj.SetUserInfo(c.GetCurrentUserInfo())
  1351. isall := c.GetString("all")
  1352. ext_datatype := c.GetString("ext_datatype") //扩展。需要追加节点的数据类型。支持电梯equ、摄像头camera、电子围栏ele、停车场park、门禁access_control等
  1353. var err error
  1354. var tableData []orm.Params
  1355. areaIdStr := c.GetString("id")
  1356. if isall == "1" {
  1357. tableData, err = obj.GetAreaAllChildrens(areaIdStr)
  1358. } else {
  1359. tableData, err = obj.GetAreaChildrens(areaIdStr)
  1360. }
  1361. if err != nil {
  1362. c.Data["json"] = c.ResultError(err.Error())
  1363. } else {
  1364. areaId, _ := c.GetInt("id", -1)
  1365. //是否需要追加业务数据节点
  1366. if areaId > -1 && ext_datatype != "" {
  1367. statCountLst := []orm.Params{}
  1368. //根据统计的下级区域子元素数量,设置区域节点的相关属性
  1369. if err != nil {
  1370. log.Println(err)
  1371. } else if len(statCountLst) > 0 {
  1372. for k1, row := range tableData {
  1373. //判断是否有子节点(isParent:true),已有子节点时不需要再次处理
  1374. if tools.IsEmpty(row["isParent"]) != "false" {
  1375. continue
  1376. }
  1377. tmpareaid := tools.IsEmpty(row["id"]) + tools.IsEmpty(row["ID"])
  1378. for _, row2 := range statCountLst {
  1379. area_id := tools.IsEmpty(row2["area_id"]) + tools.IsEmpty(row2["AREA_ID"])
  1380. if tmpareaid == area_id {
  1381. tableData[k1]["isParent"] = row2["isParent"]
  1382. tableData[k1]["count"] = row2["cnt"]
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. c.Data["json"] = c.ResultOK(tableData, 0)
  1389. }
  1390. c.ServeJSON()
  1391. }
  1392. // @router /code/tree [get]
  1393. func (c *ApiController) GetCategoryTree() {
  1394. obj := new(bo.Global)
  1395. tableData, err := obj.GetCategoryTree()
  1396. if err != nil {
  1397. c.Data["json"] = c.ResultError(err.Error())
  1398. } else {
  1399. c.Data["json"] = c.ResultOK(tableData, 0)
  1400. }
  1401. c.ServeJSON()
  1402. }
  1403. // @Summary 获取角色的已分配资源ID列表
  1404. // @Description 获取角色的已分配资源ID列表
  1405. // @Tags 基础服务接口
  1406. // @Accept x-www-form-urlencoded
  1407. // @Produce json
  1408. // @Param roleid query string true "角色ID"
  1409. // @Success 200 {object} ResultOK 成功
  1410. // @Failure 500 {object} ResultError 失败
  1411. // @router /get_menu_tree [get]
  1412. func (c *ApiController) GetMenuTree() {
  1413. Roleid, _ := c.GetInt("roleid", -1)
  1414. obj := new(bo.Global)
  1415. tableData, err := obj.GetMenuTree(Roleid)
  1416. if err != nil {
  1417. c.Data["json"] = c.ResultError(err.Error())
  1418. } else {
  1419. c.Data["json"] = c.ResultOK(tableData, 0)
  1420. }
  1421. c.ServeJSON()
  1422. }
  1423. // @Summary 保存系统字典
  1424. // @Description 保存系统字典
  1425. // @Tags 基础服务接口
  1426. // @Accept x-www-form-urlencoded
  1427. // @Produce json
  1428. // @Param name query string true "系统字典名称"
  1429. // @Param parentcode query string true "上级系统字典编码"
  1430. // @Param code query string true "系统字典编码"
  1431. // @Param id query string false "系统字典ID"
  1432. // @Success 200 {object} WarpOK 成功
  1433. // @Failure 500 {object} WarpError 失败
  1434. // @router /saveGlobalcode [post]
  1435. func (c *ApiController) SaveGlobalCode() {
  1436. name := c.GetString("name")
  1437. parentcode := c.GetString("parentcode")
  1438. code := c.GetString("code")
  1439. id, _ := strconv.Atoi(c.GetString("id", "0"))
  1440. obj := bo.Global_const_code{Id: id, Code: code, Parentcode: parentcode, Name: name}
  1441. user := c.GetCurrentUserInfo()
  1442. _, err := bo.SaveGlobalCode(obj, user)
  1443. if err != nil {
  1444. c.Data["json"] = c.WarpError(err.Error())
  1445. } else {
  1446. c.Data["json"] = c.WarpOK("")
  1447. }
  1448. c.ServeJSON()
  1449. }
  1450. // @Summary 删除指定系统字典定义
  1451. // @Description 根据字典编码或ID删除指定系统字典定义
  1452. // @Tags 基础服务接口
  1453. // @Accept x-www-form-urlencoded
  1454. // @Produce json
  1455. // @Param code query string false "系统字典编码"
  1456. // @Param id query string false "系统字典ID"
  1457. // @Success 200 {object} WarpOK 成功
  1458. // @Failure 500 {object} WarpError 失败
  1459. // @router /deleteGlobalcode [post]
  1460. func (c *ApiController) DelGlobalCode() {
  1461. code := c.GetString("code")
  1462. id, _ := strconv.Atoi(c.GetString("id", "0"))
  1463. obj := bo.Global_const_code{Code: code, Id: id}
  1464. user := c.GetCurrentUserInfo()
  1465. datalist, err := bo.DeleteGlobalCode(obj, user)
  1466. if err != nil {
  1467. c.Data["json"] = c.WarpError(err.Error())
  1468. } else {
  1469. c.Data["json"] = c.WarpOK(datalist)
  1470. }
  1471. c.ServeJSON()
  1472. }
  1473. // @Summary 删除指定模型的某条数据记录
  1474. // @Description 删除指定模型的某条数据记录
  1475. // @Tags 基础服务接口
  1476. // @Accept x-www-form-urlencoded
  1477. // @Produce json
  1478. // @Param tablename query string true "数据模型名称"
  1479. // @Param id query string true "数据记录ID"
  1480. // @Param primaryfield query string false "数据记录ID列名。默认为id"
  1481. // @Success 200 {object} WarpOK 成功
  1482. // @Failure 500 {object} WarpError 失败
  1483. // @router /record/delete [post]
  1484. func (c *ApiController) DeleteRecord() {
  1485. primaryField := c.GetString("primaryfield", "id")
  1486. Id := c.GetString("id", "")
  1487. tableName := c.GetString("tablename", "")
  1488. if Id == "" {
  1489. c.Data["json"] = c.ResultError("id参数不允许为空!")
  1490. } else if tableName == "" {
  1491. c.Data["json"] = c.ResultError("tablename参数不允许为空!")
  1492. } else {
  1493. err := tools.DeleteTableReorce(tableName, primaryField, Id)
  1494. if err != nil {
  1495. c.Data["json"] = c.ResultError(err.Error())
  1496. } else {
  1497. c.Data["json"] = c.ResultOK("", 0)
  1498. }
  1499. }
  1500. c.ServeJSON()
  1501. }
  1502. // @Summary 获取指定模型的某条数据记录
  1503. // @Description 获取指定模型的某条数据记录
  1504. // @Tags 基础服务接口
  1505. // @Accept x-www-form-urlencoded
  1506. // @Produce json
  1507. // @Param tablename query string true "数据模型名称"
  1508. // @Param id query string true "数据记录ID"
  1509. // @Param primaryfield query string false "数据记录ID列名。默认为id"
  1510. // @Success 200 {object} WarpOK 成功
  1511. // @Failure 500 {object} WarpError 失败
  1512. // @router /record/get [get]
  1513. func (c *ApiController) GetSingleRecord() {
  1514. primaryField := c.GetString("primaryfield", "id")
  1515. Id := c.GetString("id", "")
  1516. tableName := c.GetString("tablename", "")
  1517. if Id == "" {
  1518. c.Data["json"] = c.ResultError("id参数不允许为空!")
  1519. } else if tableName == "" {
  1520. c.Data["json"] = c.ResultError("tablename参数不允许为空!")
  1521. } else {
  1522. tableData, err := tools.GetSingleRecord(tableName, primaryField, Id)
  1523. if err != nil {
  1524. c.Data["json"] = c.ResultError(err.Error())
  1525. } else {
  1526. c.Data["json"] = c.ResultOK(tableData, 0)
  1527. }
  1528. }
  1529. c.ServeJSON()
  1530. }
  1531. // @Summary 根据查询条件搜索区域(变电站)列表
  1532. // @Description 根据查询条件搜索区域(变电站)列表
  1533. // @Tags 基础服务接口
  1534. // @Accept x-www-form-urlencoded
  1535. // @Produce json
  1536. // @Param pageindex query int false "当前查询的分页页码,默认为1"
  1537. // @Param pagesize query int false "每页显示的数据条数,默认为20"
  1538. // @Param name query string false "查询条件:区域名称。模糊匹配"
  1539. // @Param id query int false "查询条件:区域ID"
  1540. // @Param pid query int false "查询条件:父级区域ID"
  1541. // @Success 200 {object} ResultOK 成功
  1542. // @Failure 500 {object} ResultError 失败
  1543. // @router /basic_area/list [get]
  1544. func (c *ApiController) BasicAreaList() {
  1545. enableOrgCode, _ := bo.GetSysParamValue("xrootcode", "")
  1546. areaObject := new(bo.BasicArea)
  1547. areaObject.SetUserInfo(c.GetCurrentUserInfo())
  1548. pageIndex, _ := c.GetInt("pageindex", 1)
  1549. pageSize, _ := c.GetInt("pagesize", 1000)
  1550. areaObject.Model.Pid, _ = c.GetInt("pid", -1)
  1551. if areaObject.Model.Pid > -1 {
  1552. enableOrgCode = ""
  1553. }
  1554. areaObject.Model.AreaName = c.GetString("name", "")
  1555. areaObject.Model.AreaKind = c.GetString("area_kind", "")
  1556. areaObject.Model.AreaType = c.GetString("area_type", enableOrgCode)
  1557. areaObject.Model.AreaLevel, _ = c.GetInt("area_level", 0)
  1558. areaObject.Model.Id, _ = c.GetInt("id", 0)
  1559. tableData, number, err := areaObject.SearchArea(pageIndex, pageSize)
  1560. if err != nil {
  1561. c.Data["json"] = c.ResultError(err.Error())
  1562. } else {
  1563. c.Data["json"] = c.ResultOK(tableData, number)
  1564. }
  1565. c.ServeJSON()
  1566. }
  1567. // @Summary 查询指定区域的下级区域数量
  1568. // @Description 查询指定区域的下级区域数量
  1569. // @Tags 基础服务接口
  1570. // @Accept x-www-form-urlencoded
  1571. // @Produce json
  1572. // @Param pid query int true "查询条件:父级区域ID"
  1573. // @Success 200 {object} ResultOK 成功
  1574. // @Failure 500 {object} ResultError 失败
  1575. // @router /basic_area/getchildrencount [get]
  1576. func (c *ApiController) BasicAreaChildrenCount() {
  1577. areaObject := new(bo.BasicArea)
  1578. pid, _ := c.GetInt("pid", -1)
  1579. if pid == -1 {
  1580. c.Data["json"] = c.ResultError("pid不能为空")
  1581. c.ServeJSON()
  1582. return
  1583. }
  1584. number, err := areaObject.GetAreaTotal(pid)
  1585. if err != nil {
  1586. c.Data["json"] = c.ResultError(err.Error())
  1587. } else {
  1588. c.Data["json"] = c.ResultOK("", number)
  1589. }
  1590. c.ServeJSON()
  1591. }
  1592. // @Summary 保存区域信息
  1593. // @Description 保存区域信息
  1594. // @Tags 基础服务接口
  1595. // @Accept x-www-form-urlencoded
  1596. // @Produce json
  1597. // @Param id query int true "区域ID。编辑时必填。"
  1598. // @Param pid query int true "父级区域ID"
  1599. // @Param area_type query int true "区域类型"
  1600. // @Param area_name query string true "区域名称"
  1601. // @Param area_addr query string false "区域地址"
  1602. // @Success 200 {object} ResultOK 成功
  1603. // @Failure 500 {object} ResultError 失败
  1604. // @router /basic_area/save [post]
  1605. func (c *ApiController) BasicAreaSave() {
  1606. object := new(bo.BasicArea)
  1607. object.Model.Id, _ = c.GetInt("id", 0)
  1608. object.Model.Pid, _ = c.GetInt("pid", 0)
  1609. object.Model.AreaType = c.GetString("area_type", "")
  1610. object.Model.AreaName = c.GetString("area_name", "")
  1611. object.Model.AreaCode = ""
  1612. object.Model.AreaKind = c.GetString("area_kind", "")
  1613. object.Model.Jd, _ = strconv.ParseFloat(c.GetString("jd", "0"), 32)
  1614. object.Model.Wd, _ = strconv.ParseFloat(c.GetString("wd", "0"), 32)
  1615. object.Model.LiveNum, _ = c.GetInt("live_number", 0)
  1616. object.Model.AreaLevel, _ = c.GetInt("area_level", 0)
  1617. object.Model.IsLeaf, _ = c.GetInt("leaf", 0)
  1618. object.Model.AreaSort, _ = c.GetInt("area_sort", 0)
  1619. object.Model.AreaAddr = c.GetString("area_addr", "")
  1620. object.Model.Apmac = c.GetString("apmac", "")
  1621. RoomColor := c.GetString("room_color", "")
  1622. var unit = c.GetString("unit", "")
  1623. object.SetUserInfo(c.GetCurrentUserInfo())
  1624. AreaId, err := object.SaveArea(unit, RoomColor)
  1625. if err != nil {
  1626. if strings.Contains(err.Error(), "") {
  1627. c.Data["json"] = c.ResultError(object.Model.AreaName + "已存在")
  1628. } else {
  1629. c.Data["json"] = c.ResultError(err.Error())
  1630. }
  1631. } else {
  1632. c.Data["json"] = c.ResultOK(AreaId, 0)
  1633. }
  1634. c.ServeJSON()
  1635. }
  1636. // @Summary 删除指定区域信息
  1637. // @Description 删除指定区域信息
  1638. // @Tags 基础服务接口
  1639. // @Accept x-www-form-urlencoded
  1640. // @Produce json
  1641. // @Param id query int true "区域ID。"
  1642. // @Param isall query int false "是否同时删除所有下级区域。1为是,反之为否。默认为否"
  1643. // @Success 200 {object} ResultOK 成功
  1644. // @Failure 500 {object} ResultError 失败
  1645. // @router /basic_area/del [post]
  1646. func (c *ApiController) BasicAreaDelete() {
  1647. areaObject := new(bo.BasicArea)
  1648. id, _ := c.GetInt("id")
  1649. //是否批量删除所有下级节点
  1650. isall := c.GetString("isall")
  1651. var err error
  1652. if isall == "1" {
  1653. err = areaObject.DelAreaInfo(id)
  1654. } else {
  1655. err = areaObject.DeleteArea()
  1656. }
  1657. if err != nil {
  1658. c.Data["json"] = c.ResultError(err.Error())
  1659. } else {
  1660. c.Data["json"] = c.ResultOK(nil, 0)
  1661. }
  1662. c.ServeJSON()
  1663. }