Sfoglia il codice sorgente

新增用户等级及报告权限判断

hsun 4 giorni fa
parent
commit
d6aaa5e045
7 ha cambiato i file con 230 aggiunte e 26 eliminazioni
  1. 27 9
      controllers/report.go
  2. 4 0
      models/constants.go
  3. 2 0
      models/db.go
  4. 70 0
      models/user_level.go
  5. 51 0
      models/user_level_mapping.go
  6. 27 17
      models/users.go
  7. 49 0
      services/report.go

+ 27 - 9
controllers/report.go

@@ -46,7 +46,7 @@ func (this *ReportController) List() {
 		reportPars, outsidePars []interface{}
 	)
 	// 仅取常规和智能布局的报告, 且是公开发布
-	reportCond += ` AND r.report_layout IN (1,2) AND r.is_public_publish = 1`
+	reportCond += ` AND r.report_layout IN (1,2) AND r.is_public_publish = 1 AND r.mini_show = 1`
 
 	params.Keywords = strings.TrimSpace(params.Keywords)
 	if params.Keywords != "" {
@@ -217,6 +217,7 @@ func (this *ReportOpenController) Detail() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
+	users := this.User
 	reportId, _ := this.GetInt("ReportId")
 	if reportId <= 0 {
 		br.Msg = "参数有误"
@@ -224,12 +225,6 @@ func (this *ReportOpenController) Detail() {
 	}
 	resp := new(response.ReportDetailResp)
 
-	// 报告权限
-	users := this.User
-	if users != nil {
-		resp.Status = users.AuthStatus
-	}
-
 	report, err := models.GetReportById(reportId)
 	if err != nil {
 		if err.Error() == utils.ErrNoRow() {
@@ -300,6 +295,23 @@ func (this *ReportOpenController) Detail() {
 		}
 	}
 
+	// 报告权限
+	var classifyId int
+	if report.ClassifyIdThird > 0 {
+		classifyId = report.ClassifyIdThird
+	} else if report.ClassifyIdSecond > 0 {
+		classifyId = report.ClassifyIdSecond
+	} else {
+		classifyId = report.ClassifyIdFirst
+	}
+	reportAuth, e := services.CheckUserReportAuth(users, classifyId)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = fmt.Sprintf("判断用户报告权限失败, %v", e)
+		return
+	}
+	resp.Status = reportAuth
+
 	report.ChapterContent = chapters
 	resp.Report = report
 
@@ -339,9 +351,15 @@ func (this *ReportOpenController) OutsideDetail() {
 		return
 	}
 	resp := outsideReport.Format2Item()
-	if users != nil {
-		resp.Status = users.AuthStatus
+
+	// 报告权限
+	reportAuth, e := services.CheckUserReportAuth(users, outsideReport.ClassifyId)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = fmt.Sprintf("判断用户报告权限失败, %v", e)
+		return
 	}
+	resp.Status = reportAuth
 
 	br.Data = resp
 	br.Ret = 200

+ 4 - 0
models/constants.go

@@ -19,3 +19,7 @@ const (
 )
 
 const ConfAreaCodeListKey = "AreaCodeList" // 配置-手机号区号列表
+
+const (
+	UserLevelEnabled = 1 // 用户等级状态-启用
+)

+ 2 - 0
models/db.go

@@ -43,6 +43,8 @@ func init() {
 		new(UserRecord),
 		//new(UserTemplateRecord),
 		new(UserReadRecord),
+		new(UserLevel),
+		new(UserLevelMapping),
 	)
 
 	// 初始化后的操作

+ 70 - 0
models/user_level.go

@@ -0,0 +1,70 @@
+package models
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"strings"
+	"time"
+)
+
+// UserLevel 用户等级表
+type UserLevel struct {
+	UserLevelId     int       `orm:"column(user_level_id);pk"`
+	LevelRank       int       `description:"等级编号"`
+	LevelName       string    `description:"等级名称"`
+	State           int       `description:"状态:0-禁用;1-启用"`
+	SysUserId       int       `description:"创建人ID"`
+	SysUserRealName string    `description:"创建人姓名"`
+	CreateTime      time.Time `description:"创建时间"`
+	ModifyTime      time.Time `description:"修改时间"`
+}
+
+func (m *UserLevel) TableName() string {
+	return "user_level"
+}
+
+type UserLevelCols struct {
+	PrimaryId       string
+	LevelRank       string
+	LevelName       string
+	State           string
+	SysUserId       string
+	SysUserRealName string
+	CreateTime      string
+	ModifyTime      string
+}
+
+func (m *UserLevel) Cols() UserLevelCols {
+	return UserLevelCols{
+		PrimaryId:       "user_level_id",
+		LevelRank:       "level_rank",
+		LevelName:       "level_name",
+		State:           "state",
+		SysUserId:       "sys_user_id",
+		SysUserRealName: "sys_user_real_name",
+		CreateTime:      "create_time",
+		ModifyTime:      "modify_time",
+	}
+}
+
+func (m *UserLevel) GetItemById(id int) (item *UserLevel, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 AND %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}
+
+func (m *UserLevel) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*UserLevel, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars).QueryRows(&items)
+	return
+}

+ 51 - 0
models/user_level_mapping.go

@@ -0,0 +1,51 @@
+package models
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"strings"
+	"time"
+)
+
+// UserLevelMapping 用户等级分类关联表
+type UserLevelMapping struct {
+	Id          int       `orm:"column(id);pk"`
+	UserLevelId int       `description:"用户等级ID"`
+	ClassifyId  int       `description:"分类ID"`
+	CreateTime  time.Time `description:"创建时间"`
+}
+
+func (m *UserLevelMapping) TableName() string {
+	return "user_level_mapping"
+}
+
+type UserLevelMappingCols struct {
+	PrimaryId   string
+	UserLevelId string
+	ClassifyId  string
+	CreateTime  string
+}
+
+func (m *UserLevelMapping) Cols() UserLevelMappingCols {
+	return UserLevelMappingCols{
+		PrimaryId:   "id",
+		UserLevelId: "user_level_id",
+		ClassifyId:  "classify_id",
+		CreateTime:  "create_time",
+	}
+}
+
+func (m *UserLevelMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*UserLevelMapping, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars).QueryRows(&items)
+	return
+}

+ 27 - 17
models/users.go

@@ -32,6 +32,8 @@ type Users struct {
 	IsDeleted       int       `description:"删除状态:0-正常;1-已删除"`
 	CreateTime      time.Time `description:"创建时间"`
 	ModifyTime      time.Time `description:"修改时间"`
+	UserLevelId     int       `description:"用户等级ID"`
+	UserLevelName   string    `description:"用户等级名称"`
 }
 
 func (m *Users) TableName() string {
@@ -163,23 +165,27 @@ func (m *Users) GetPageItemsByCondition(condition string, pars []interface{}, fi
 
 // UsersItem 用户信息
 type UsersItem struct {
-	UserId       int    `description:"用户ID"`
-	RealName     string `description:"用户姓名"`
-	Mobile       string `description:"手机号"`
-	MobileHidden string `description:"手机号(隐藏中间四位)"`
-	AreaCode     string `description:"区号"`
-	SellerName   string `description:"销售名称"`
-	CompanyName  string `description:"所属机构名称"`
-	Status       int    `description:"用户类型:1-试用用户;2-正式用户"`
-	AuthStatus   int    `description:"权限状态:0-关闭;1-开启"`
-	IsRegistered int    `description:"注册状态:0-未注册;1-已注册"`
-	RegisterTime string `description:"注册时间"`
-	Subscribe    int    `description:"是否关注公众号:0-未关注;1-已关注"`
-	LastReadTime string `description:"最后一次阅读时间"`
-	ReadTimes    int    `description:"阅读次数"`
-	TrialExpired bool   `description:"试用到期:true-已到期"`
-	CreateTime   string `description:"创建时间"`
-	ModifyTime   string `description:"修改时间"`
+	UserId        int    `description:"用户ID"`
+	RealName      string `description:"用户姓名"`
+	Mobile        string `description:"手机号"`
+	MobileHidden  string `description:"手机号(隐藏中间四位)"`
+	AreaCode      string `description:"区号"`
+	SellerId      int    `description:"销售ID"`
+	SellerName    string `description:"销售名称"`
+	CompanyId     int    `description:"所属机构ID"`
+	CompanyName   string `description:"所属机构名称"`
+	Status        int    `description:"用户类型:1-试用用户;2-正式用户"`
+	AuthStatus    int    `description:"权限状态:0-关闭;1-开启"`
+	IsRegistered  int    `description:"注册状态:0-未注册;1-已注册"`
+	RegisterTime  string `description:"注册时间"`
+	Subscribe     int    `description:"是否关注公众号:0-未关注;1-已关注"`
+	LastReadTime  string `description:"最后一次阅读时间"`
+	ReadTimes     int    `description:"阅读次数"`
+	TrialExpired  bool   `description:"试用到期:true-已到期"`
+	CreateTime    string `description:"创建时间"`
+	ModifyTime    string `description:"修改时间"`
+	UserLevelId   int    `description:"用户等级ID"`
+	UserLevelName string `description:"用户等级名称"`
 }
 
 func (m *Users) Format2Item() (item *UsersItem) {
@@ -189,7 +195,9 @@ func (m *Users) Format2Item() (item *UsersItem) {
 	item.Mobile = m.Mobile
 	item.MobileHidden = utils.HideMobileMiddle(m.Mobile)
 	item.AreaCode = m.AreaCode
+	item.SellerId = m.SellerId
 	item.SellerName = m.SellerName
+	item.CompanyId = m.CompanyId
 	item.CompanyName = m.CompanyName
 	item.Status = m.Status
 	item.AuthStatus = m.AuthStatus
@@ -208,6 +216,8 @@ func (m *Users) Format2Item() (item *UsersItem) {
 	}
 	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
 	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
+	item.UserLevelId = m.UserLevelId
+	item.UserLevelName = m.UserLevelName
 	return
 }
 

+ 49 - 0
services/report.go

@@ -185,3 +185,52 @@ func CheckIsOpenChartExpired() (isOpenChartExpired bool, err error) {
 	}
 	return
 }
+
+// CheckUserReportAuth 校验用户是否有报告权限
+func CheckUserReportAuth(users *models.UsersItem, classifyId int) (pass int, err error) {
+	pass = models.AuthStatusClose
+	if users == nil {
+		return
+	}
+
+	// 试用未到期的有3天的所有权限
+	if users.Status == models.UserStatusTrial && !users.TrialExpired {
+		pass = models.AuthStatusOpen
+		return
+	}
+
+	// 权限被关闭不再判断用户等级
+	if users.AuthStatus != models.AuthStatusOpen {
+		return
+	}
+	if users.UserLevelId <= 0 {
+		return
+	}
+
+	// 用户等级如果被禁用那么也是无权限
+	levelOb := new(models.UserLevel)
+	levelItem, e := levelOb.GetItemById(users.UserLevelId)
+	if e != nil {
+		err = fmt.Errorf("获取用户等级失败, %v", e)
+		return
+	}
+	if levelItem.State != models.UserLevelEnabled {
+		return
+	}
+	mappingOb := new(models.UserLevelMapping)
+	cond := fmt.Sprintf(` AND %s = ?`, mappingOb.Cols().UserLevelId)
+	pars := make([]interface{}, 0)
+	pars = append(pars, users.UserLevelId)
+	mappings, e := mappingOb.GetItemsByCondition(cond, pars, []string{}, "")
+	if e != nil {
+		err = fmt.Errorf("获取用户等级权限失败, %v", e)
+		return
+	}
+	for _, m := range mappings {
+		if m.ClassifyId == classifyId {
+			pass = models.AuthStatusOpen
+			break
+		}
+	}
+	return
+}