business_conf.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package models
  2. import (
  3. "eta/eta_mini_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "html"
  6. "strconv"
  7. "time"
  8. )
  9. var (
  10. BusinessConfMap map[string]string
  11. )
  12. const (
  13. BusinessConfReportChartExpiredTime = "ReportChartExpiredTime" // 图表有效期鉴权时间,单位:分钟
  14. BusinessConfIsOpenChartExpired = "IsOpenChartExpired" // 是否开启图表权限
  15. )
  16. // BusinessConf 商户配置表
  17. type BusinessConf struct {
  18. Id int `orm:"column(id);pk" gorm:"column:id;primaryKey;autoIncrement"` //`orm:"column(id);pk" gorm:"primaryKey" `
  19. ConfKey string `gorm:"column:conf_key"` //`description:"配置Key"`
  20. ConfVal string `gorm:"column:conf_val"` //`description:"配置值"`
  21. ValType int `gorm:"column:val_type"` //`description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  22. Necessary int `gorm:"column:necessary"` //`description:"是否必填:0-否;1-是"`
  23. Remark string `gorm:"column:remark"` // `description:"备注"`
  24. CreateTime time.Time `gorm:"column:create_time"`
  25. }
  26. func (b *BusinessConf) TableName() string {
  27. return "business_conf"
  28. }
  29. // GetItemByConfKey 获取配置项
  30. func (b *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err error) {
  31. sql := `SELECT * FROM business_conf WHERE conf_key = ? `
  32. err = orm.NewOrmUsingDB("eta").Raw(sql, key).QueryRow(&item)
  33. return
  34. }
  35. // GetBusinessConf 获取商家配置
  36. func GetBusinessConf() (list map[string]string, err error) {
  37. list = make(map[string]string)
  38. var items []*BusinessConf
  39. sql := `SELECT * FROM business_conf`
  40. _, err = orm.NewOrmUsingDB("eta").Raw(sql).QueryRows(&items)
  41. if err != nil {
  42. return
  43. }
  44. for _, v := range items {
  45. if v.ValType == 4 {
  46. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  47. continue
  48. }
  49. list[v.ConfKey] = v.ConfVal
  50. }
  51. return
  52. }
  53. func InitBusinessConf() {
  54. var e error
  55. BusinessConfMap, e = GetBusinessConf()
  56. if e != nil {
  57. return
  58. }
  59. // 图表有效期的过期时间
  60. if BusinessConfMap[BusinessConfReportChartExpiredTime] != "" {
  61. reportChartExpiredTime, _ := strconv.Atoi(BusinessConfMap[BusinessConfReportChartExpiredTime])
  62. if reportChartExpiredTime <= 0 {
  63. reportChartExpiredTime = 30
  64. }
  65. utils.BusinessConfReportChartExpiredTime = time.Duration(reportChartExpiredTime) * time.Minute
  66. } else {
  67. utils.BusinessConfReportChartExpiredTime = 30 * time.Minute
  68. }
  69. }