global_sys_param.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package bo
  2. import (
  3. "scd_check_tools/email"
  4. "scd_check_tools/global"
  5. "scd_check_tools/logger"
  6. "scd_check_tools/models/enum"
  7. "scd_check_tools/mqtt"
  8. "scd_check_tools/tools"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "io/ioutil"
  13. "log"
  14. "os"
  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 == "log_alarm_email" && obj.Param_value != "" {
  96. //邮箱格式校验
  97. if !tools.VerifyEmailFormat(obj.Param_value) {
  98. return 0, errors.New("邮箱格式不正确!")
  99. }
  100. if !email.EmailConfig.Enable {
  101. return 0, errors.New("系统还未配置邮件发送!")
  102. }
  103. }
  104. if obj.Param_name == "log_size_max" {
  105. //日志容量阈值配置
  106. v, er := strconv.Atoi(obj.Param_value)
  107. if er != nil {
  108. return 0, errors.New("容量阈值只能为数值!")
  109. }
  110. if v <= 0 {
  111. return 0, errors.New("容量阈值必须大于0!")
  112. }
  113. }
  114. if obj.Param_name == "log_size_alarm1" {
  115. //日志预警阈值配置
  116. v, er := strconv.Atoi(obj.Param_value)
  117. if er != nil {
  118. return 0, errors.New("预警阈值只能为数值!")
  119. }
  120. if v <= 0 {
  121. return 0, errors.New("预警阈值必须大于0!")
  122. }
  123. v2, _ := GetSysParamValue("log_size_alarm2", "0")
  124. if v2 != "0" {
  125. v2v, _ := strconv.Atoi(v2)
  126. if v > v2v {
  127. return 0, errors.New("预警阈值不能大于告警阈值!")
  128. }
  129. }
  130. }
  131. if obj.Param_name == "log_size_alarm2" {
  132. //日志告警阈值配置
  133. v, er := strconv.Atoi(obj.Param_value)
  134. if er != nil {
  135. return 0, errors.New("告警阈值只能为数值!")
  136. }
  137. if v <= 0 {
  138. return 0, errors.New("告警阈值必须大于0!")
  139. }
  140. v2, _ := GetSysParamValue("log_size_alarm1", "0")
  141. if v2 != "0" {
  142. v2v, _ := strconv.Atoi(v2)
  143. if v < v2v {
  144. return 0, errors.New("告警阈值不能小于预警阈值!")
  145. }
  146. }
  147. }
  148. dblog := new(SystemLog)
  149. if len(userinfo) == 0 {
  150. dblog.SetUserInfo(map[string]interface{}{"name": "", "ip": "127.0.0.1"})
  151. dblog.Eventtype = enum.OptEventType_System
  152. } else {
  153. dblog.SetUserInfo(userinfo[0])
  154. dblog.Eventtype = enum.OptEventType_Bus
  155. }
  156. dblog.Audittype = enum.AuditType_admin_system_paramater
  157. dblog.Logtype = enum.LogType_Update
  158. dblog.Eventlevel = enum.OptEventLevel_Hight
  159. dblog.Description = fmt.Sprintf("保存系统参数,数据:%+v", obj)
  160. o := orm.NewOrm()
  161. readObj := Global_sys_param{Param_name: obj.Param_name}
  162. has := o.Read(&readObj)
  163. var id int64
  164. if has == nil {
  165. id, err = o.Update(&obj)
  166. } else if has == orm.ErrNoRows {
  167. dblog.Logtype = enum.LogType_Insert
  168. id, err = o.Insert(&obj)
  169. } else {
  170. return 0, err
  171. }
  172. if err != nil {
  173. logger.Logger.Error(err)
  174. dblog.Fail2()
  175. return 0, nil
  176. }
  177. dblog.Success2()
  178. global.GoCahce.Set(obj.Param_name, obj.Param_value, -1)
  179. switch obj.Param_name {
  180. case "session_timeout":
  181. if tools.IsEmpty(obj.Param_value) == "" {
  182. global.SessionTimeout = 0
  183. } else {
  184. global.SessionTimeout, _ = strconv.Atoi(obj.Param_value)
  185. }
  186. case "login_fail_maxcount":
  187. global.LoginFailMaxCount, _ = strconv.Atoi(obj.Param_value)
  188. case "login_fail_locktime":
  189. global.LoginFailLockTime, _ = strconv.Atoi(obj.Param_value)
  190. if global.LoginFailLockTime <= 0 {
  191. global.LoginFailLockTime = 180
  192. }
  193. case "user_pwd_expire_day":
  194. if obj.Param_value == "0" {
  195. //永不过期,设置所有用户的密码过期时间
  196. o.Raw("update t_data_user set pwd_expire=?", "1970-01-01 00:00:00").Exec()
  197. } else {
  198. o.Raw("update t_data_user set pwd_expire=date_add(now(),interval ? DAY)", obj.Param_value).Exec()
  199. }
  200. case "user_login_client_limt":
  201. if obj.Param_value != "none" && obj.Param_value != "o2o" {
  202. log.Println("无效的user_login_client_limt参数值:" + obj.Param_value)
  203. } else {
  204. global.UserLoginClientLimt = obj.Param_value
  205. }
  206. case "allow_access_ips":
  207. if obj.Param_value == "" {
  208. obj.Param_value = "*"
  209. }
  210. global.AllowAccessIps = obj.Param_value
  211. //重新初始化已经访问ip队列
  212. global.AccessedIps = sync.Map{}
  213. }
  214. return id, err
  215. }
  216. func GetSysParamValue(param_name string, defaultvalue string) (result string, err error) {
  217. if v, has := global.GoCahce.Get(param_name); has {
  218. return tools.IsEmpty(v), nil
  219. }
  220. o := orm.NewOrm()
  221. v := Global_sys_param{Param_name: param_name}
  222. if err = o.Read(&v); err == nil {
  223. global.GoCahce.Set(param_name, v.Param_value, -1)
  224. return v.Param_value, nil
  225. } else {
  226. v.Param_value = defaultvalue
  227. SaveSysParam(v)
  228. return defaultvalue, nil
  229. }
  230. }
  231. func GetSysParamList(obj Global_sys_param, userinfo map[string]interface{}) (maps []orm.Params, err error) {
  232. dblog := new(SystemLog)
  233. dblog.SetUserInfo(userinfo)
  234. dblog.Audittype = enum.AuditType_admin_system_paramater
  235. dblog.Logtype = enum.LogType_Query
  236. dblog.Eventlevel = enum.OptEventLevel_Low
  237. o := orm.NewOrm()
  238. var num int64
  239. var dberr error
  240. if obj.Param_name != "" {
  241. sql := "select * FROM global_sys_param WHERE param_name=?"
  242. dblog.Description = fmt.Sprintf("SQL:%s,参数:%+v", sql, obj.Param_name)
  243. num, dberr = o.Raw(sql, obj.Param_name).Values(&maps)
  244. } else {
  245. sql := "select * FROM global_sys_param"
  246. dblog.Description = sql
  247. num, dberr = o.Raw(sql).Values(&maps)
  248. }
  249. if dberr == nil && num > 0 {
  250. dblog.Success2()
  251. return maps, nil
  252. } else {
  253. dblog.Fail2()
  254. logger.Logger.Error(dberr, dblog.Description)
  255. }
  256. return nil, dberr
  257. }