global_sys_param.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package bo
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "scd_check_tools/email"
  10. "scd_check_tools/global"
  11. "scd_check_tools/logger"
  12. "scd_check_tools/models/enum"
  13. "scd_check_tools/mqtt"
  14. "scd_check_tools/tools"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "github.com/astaxie/beego/orm"
  19. _ "github.com/astaxie/beego/orm"
  20. )
  21. type Global_sys_param struct {
  22. Param_name string `orm:"pk"`
  23. Param_value string `orm:"size(2000)"`
  24. Param_memo string `orm:"size(50)"`
  25. }
  26. func init() {
  27. orm.RegisterModel(new(Global_sys_param))
  28. }
  29. //初始化加载系统参数。建议在系统启动时调用
  30. func LoadSysParam() {
  31. //用户密码规则及强度:simple|strong
  32. GetSysParamValue("user_pwd_rule", "simple")
  33. //初始化用户密码过期时间(天)。默认为60天,为0时永不过期
  34. GetSysParamValue("user_pwd_expire_day", "60")
  35. //初始化用户密码修改模式。默认只能管理员修改修改。可配置成自己可修改
  36. GetSysParamValue("user_pwd_edit_mod", "admin") //admin|self
  37. //初始化登录失败锁定次数
  38. GetSysParamValue("login_fail_maxcount", "5") //默认为5次
  39. //初始化登录失败锁定时长,单位:秒
  40. GetSysParamValue("login_fail_locktime", "180") //默认180秒
  41. r, _ := GetSysParamValue("session_timeout", "600")
  42. rInt, _ := strconv.Atoi(r)
  43. if rInt > 60 {
  44. global.SessionTimeout = rInt
  45. } else {
  46. //永不过期
  47. global.SessionTimeout = 0
  48. }
  49. loginFailMaxCount, _ := GetSysParamValue("login_fail_maxcount", "5")
  50. global.LoginFailMaxCount, _ = strconv.Atoi(loginFailMaxCount)
  51. global.UserLoginClientLimt, _ = GetSysParamValue("user_login_client_limt", "none")
  52. global.AllowAccessIps, _ = GetSysParamValue("allow_access_ips", "*")
  53. //初始化人像比对相似度
  54. //r, _ = GetSysParamValue("FaceSameValue", "60")
  55. //global.FaceSameValue = r
  56. //初始化Mqtt 地址参数。前端页面连接时的信息
  57. //获取配置的mqtt地址
  58. mqttinfo, _ := GetSysParamValue("Mqtt_JS_URL", "")
  59. if mqttinfo == "" {
  60. cnffile := "conf/mqtt.cnf"
  61. fileHanlder, err := os.Open(cnffile)
  62. if err == nil {
  63. txt, _ := ioutil.ReadAll(fileHanlder)
  64. fileHanlder.Close()
  65. txtStr := string(txt)
  66. if txtStr != "" {
  67. cfgdata := mqtt.Config{}
  68. err = json.Unmarshal(txt, &cfgdata)
  69. if err == nil {
  70. mqttinfo = cfgdata.Host + ":8083" //mqtt的js协议默认端口是8083
  71. if cfgdata.Host == "127.0.0.1" || cfgdata.Host == "" {
  72. //获取本机地址
  73. mqttinfo, _ := tools.LocalIPv4()
  74. mqttinfo = mqttinfo + ":8083"
  75. }
  76. sp := Global_sys_param{Param_name: "Mqtt_JS_URL", Param_value: mqttinfo, Param_memo: "mqtt的js库连接地址"}
  77. SaveSysParam(sp)
  78. }
  79. }
  80. }
  81. }
  82. }
  83. func SaveSysParam(obj Global_sys_param, userinfo ...map[string]interface{}) (result int64, err error) {
  84. if obj.Param_name == "log_fliterrule_staff_list" && obj.Param_value != "" {
  85. //需要验证用户名称有效性
  86. usernames := strings.ReplaceAll(obj.Param_value, ",", ",")
  87. usernameList := strings.Split(usernames, ",")
  88. for _, uname := range usernameList {
  89. has, _ := HasUserName(uname)
  90. if !has {
  91. return 0, errors.New("用户名称" + uname + "不存在!")
  92. }
  93. }
  94. }
  95. if obj.Param_name == "allow_access_ips" {
  96. if obj.Param_value != "" && obj.Param_value != "*" {
  97. // 判断ip有效性
  98. if !tools.VerifyIPFormat(obj.Param_value) {
  99. return 0, errors.New("无效的IP或IP段格式!")
  100. }
  101. }
  102. }
  103. if obj.Param_name == "log_alarm_email" && obj.Param_value != "" {
  104. //邮箱格式校验
  105. if !tools.VerifyEmailFormat(obj.Param_value) {
  106. return 0, errors.New("邮箱格式不正确!")
  107. }
  108. if !email.EmailConfig.Enable {
  109. return 0, errors.New("系统还未配置邮件发送!")
  110. }
  111. }
  112. if obj.Param_name == "log_size_max" {
  113. //日志容量阈值配置
  114. v, er := strconv.Atoi(obj.Param_value)
  115. if er != nil {
  116. return 0, errors.New("容量阈值只能为数值!")
  117. }
  118. if v <= 0 {
  119. return 0, errors.New("容量阈值必须大于0!")
  120. }
  121. }
  122. if obj.Param_name == "log_size_alarm1" {
  123. //日志预警阈值配置
  124. v, er := strconv.Atoi(obj.Param_value)
  125. if er != nil {
  126. return 0, errors.New("预警阈值只能为数值!")
  127. }
  128. if v <= 0 {
  129. return 0, errors.New("预警阈值必须大于0!")
  130. }
  131. v2, _ := GetSysParamValue("log_size_alarm2", "0")
  132. if v2 != "0" {
  133. v2v, _ := strconv.Atoi(v2)
  134. if v > v2v {
  135. return 0, errors.New("预警阈值不能大于告警阈值!")
  136. }
  137. }
  138. }
  139. if obj.Param_name == "log_size_alarm2" {
  140. //日志告警阈值配置
  141. v, er := strconv.Atoi(obj.Param_value)
  142. if er != nil {
  143. return 0, errors.New("告警阈值只能为数值!")
  144. }
  145. if v <= 0 {
  146. return 0, errors.New("告警阈值必须大于0!")
  147. }
  148. v2, _ := GetSysParamValue("log_size_alarm1", "0")
  149. if v2 != "0" {
  150. v2v, _ := strconv.Atoi(v2)
  151. if v < v2v {
  152. return 0, errors.New("告警阈值不能小于预警阈值!")
  153. }
  154. }
  155. }
  156. dblog := new(SystemLog)
  157. if len(userinfo) == 0 {
  158. dblog.SetUserInfo(map[string]interface{}{"name": "", "ip": "127.0.0.1"})
  159. dblog.Eventtype = enum.OptEventType_System
  160. } else {
  161. dblog.SetUserInfo(userinfo[0])
  162. dblog.Eventtype = enum.OptEventType_Bus
  163. }
  164. dblog.Audittype = enum.AuditType_admin_system_paramater
  165. dblog.Logtype = enum.LogType_Update
  166. dblog.Eventlevel = enum.OptEventLevel_Hight
  167. dblog.Description = fmt.Sprintf("保存系统参数,数据:%+v", obj)
  168. o := orm.NewOrm()
  169. readObj := Global_sys_param{Param_name: obj.Param_name}
  170. has := o.Read(&readObj)
  171. var id int64
  172. if has == nil {
  173. id, err = o.Update(&obj)
  174. } else if has == orm.ErrNoRows {
  175. dblog.Logtype = enum.LogType_Insert
  176. id, err = o.Insert(&obj)
  177. } else {
  178. return 0, err
  179. }
  180. if err != nil {
  181. logger.Logger.Error(err)
  182. dblog.Fail2()
  183. return 0, nil
  184. }
  185. dblog.Success2()
  186. global.GoCahce.Set(obj.Param_name, obj.Param_value, -1)
  187. switch obj.Param_name {
  188. case "session_timeout":
  189. if tools.IsEmpty(obj.Param_value) == "" {
  190. global.SessionTimeout = 0
  191. } else {
  192. global.SessionTimeout, _ = strconv.Atoi(obj.Param_value)
  193. }
  194. case "login_fail_maxcount":
  195. global.LoginFailMaxCount, _ = strconv.Atoi(obj.Param_value)
  196. case "login_fail_locktime":
  197. global.LoginFailLockTime, _ = strconv.Atoi(obj.Param_value)
  198. if global.LoginFailLockTime <= 0 {
  199. global.LoginFailLockTime = 180
  200. }
  201. case "user_pwd_expire_day":
  202. if obj.Param_value == "0" {
  203. //永不过期,设置所有用户的密码过期时间
  204. o.Raw("update t_data_user set pwd_expire=?", "1970-01-01 00:00:00").Exec()
  205. } else {
  206. o.Raw("update t_data_user set pwd_expire=date_add(now(),interval ? DAY)", obj.Param_value).Exec()
  207. }
  208. case "user_login_client_limt":
  209. if obj.Param_value != "none" && obj.Param_value != "o2o" {
  210. log.Println("无效的user_login_client_limt参数值:" + obj.Param_value)
  211. } else {
  212. global.UserLoginClientLimt = obj.Param_value
  213. }
  214. case "allow_access_ips":
  215. if obj.Param_value == "" {
  216. obj.Param_value = "*"
  217. }
  218. global.AllowAccessIps = obj.Param_value
  219. //重新初始化已经访问ip队列
  220. global.AccessedIps = sync.Map{}
  221. }
  222. return id, err
  223. }
  224. func GetSysParamValue(param_name string, defaultvalue string) (result string, err error) {
  225. if v, has := global.GoCahce.Get(param_name); has {
  226. return tools.IsEmpty(v), nil
  227. }
  228. o := orm.NewOrm()
  229. v := Global_sys_param{Param_name: param_name}
  230. if err = o.Read(&v); err == nil {
  231. global.GoCahce.Set(param_name, v.Param_value, -1)
  232. return v.Param_value, nil
  233. } else {
  234. v.Param_value = defaultvalue
  235. SaveSysParam(v)
  236. return defaultvalue, nil
  237. }
  238. }
  239. func GetSysParamList(obj Global_sys_param, userinfo map[string]interface{}) (maps []orm.Params, err error) {
  240. dblog := new(SystemLog)
  241. dblog.SetUserInfo(userinfo)
  242. dblog.Audittype = enum.AuditType_admin_system_paramater
  243. dblog.Logtype = enum.LogType_Query
  244. dblog.Eventlevel = enum.OptEventLevel_Low
  245. o := orm.NewOrm()
  246. var num int64
  247. var dberr error
  248. if obj.Param_name != "" {
  249. sql := "select * FROM global_sys_param WHERE param_name=?"
  250. dblog.Description = fmt.Sprintf("SQL:%s,参数:%+v", sql, obj.Param_name)
  251. num, dberr = o.Raw(sql, obj.Param_name).Values(&maps)
  252. } else {
  253. sql := "select * FROM global_sys_param"
  254. dblog.Description = sql
  255. num, dberr = o.Raw(sql).Values(&maps)
  256. }
  257. if dberr == nil && num > 0 {
  258. dblog.Success2()
  259. return maps, nil
  260. } else {
  261. dblog.Fail2()
  262. logger.Logger.Error(dberr, dblog.Description)
  263. }
  264. return nil, dberr
  265. }