apiController.go 61 KB

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