作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/mmm-go/oppmg into dev

... ... @@ -8,6 +8,7 @@ import (
serveaudit "oppmg/services/audit"
servecommon "oppmg/services/common"
"strconv"
"time"
)
type AuditController struct {
... ... @@ -27,6 +28,14 @@ func (c *AuditController) AuditList() {
msg = protocol.BadRequestParam("1")
return
}
beginTime, err := time.Parse("2006-01-02", param.CreateTimeBeginS)
if err == nil && len(param.CreateTimeBeginS) > 0 {
param.CreateTimeBegin = beginTime.Unix()
}
endTime, err := time.Parse("2006-01-02", param.CreateTimeEndS)
if err == nil && len(param.CreateTimeEndS) > 0 {
param.CreateTimeEnd = endTime.Unix() + 864399
}
uid := c.GetUserId()
companyId := c.GetCompanyId()
list, err := serveaudit.GetAuditList(param, companyId, uid)
... ... @@ -83,3 +92,33 @@ func (c *AuditController) AuditInfo() {
msg = protocol.NewReturnResponse(list, err)
return
}
//AllowForbidAudit 开启、关闭 机会
//@router /v1/audit/allow_forbid
func (c *AuditController) AllowForbidAudit() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
ChanceId string `json:"chance_id"`
Status int `json:"status"` //[1:开启机会][2:关闭机会]
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
chanceid, _ := strconv.ParseInt(param.ChanceId, 10, 64)
companyId := c.GetCompanyId()
var err error
switch param.Status {
case 1:
err = serveaudit.AllowChanceEnableStatus(chanceid, companyId)
case 2:
err = serveaudit.ForbidChanceEnableStatus(chanceid, companyId)
}
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
... ... @@ -28,8 +28,11 @@ func (this *BaseController) Prepare() {
this.Ctx.WriteString("")
return
}
p := this.Ctx.Input.GetData("RouterPattern")
fmt.Println("====>r:", p)
// p := this.Ctx.Input.GetData("RouterPattern")
// userid := this.GetUserId()
// companyid := this.GetCompanyId()
//权限校验
}
func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) {
... ...
... ... @@ -117,6 +117,3 @@ var LogRouter = func(ctx *context.Context) {
}
//CheckOperation 检查操作权限,beforeController
var CheckOperation = func(ctx *context.Context) {
}
... ...
... ... @@ -71,13 +71,13 @@ var ChanceReviewStatusMap = map[int]string{
//有效状态 0:无效 1:有效
const (
ChanceEnableStatusYes int = 1
ChanceEnableStatusNo int = 0
ChanceEnableStatusYes int8 = 1
ChanceEnableStatusNo int8 = 0
)
var ChanceEnableStatusMap = map[int]string{
0: "无效",
1: "有效",
var ChanceEnableStatusMap = map[int8]string{
ChanceEnableStatusNo: "无效",
ChanceEnableStatusYes: "有效",
}
// AddChance insert a new Chance into database and returns
... ...
... ... @@ -230,15 +230,18 @@ type CategoryEditSortResponse struct {
//RequestAuditList 机会管理-获取机会列表
type RequestAuditList struct {
RequestPageInfo
ChanceTypeId int `json:"chance_type_id"` //一级分类
TempalteId int `json:"template_id"` // 二级分类
PublishStatus int `json:"publish_status"` //公开状态
ReviewStatus int `json:"review_status"` //
EnableStatus int `json:"enable_status"` //关闭状态
DepartmentID int `json:"department_id"` //提交部门
CreateTimeBegin int `json:"create_time_begin"` //
CreateTimeEnd int `json:"create_time_end"` //
Code string `json:"code"` //机会编码
ChanceTypeId int `json:"chance_type_id"` //一级分类
TempalteId int `json:"template_id"` // 二级分类
PublishStatus int `json:"publish_status"` //公开状态
ReviewStatus int `json:"review_status"` //
EnableStatus int `json:"enable_status"` //关闭状态
DepartmentID int `json:"department_id"` //提交部门
CreateTimeBegin int64 `json:"-"` //
CreateTimeBeginS string `json:"create_time_begin"`
CreateTimeEnd int64 `json:"-"` //
CreateTimeEndS string `json:"create_time_end"` //
Code string `json:"code"` //机会编码
UserName string `json:"user_name"` //提交人姓名
}
type ResponseAuditList struct {
... ... @@ -258,7 +261,7 @@ type RspAuditList struct {
PublishStatusName string `json:"publish_status_name"` //
ReviewStatus int `json:"review_status"` //审批状态
ReviewStatusName string `json:"review_status_name"`
EnableStatus int `json:"enable_status"`
EnableStatus int8 `json:"enable_status"`
EnableStatusName string `json:"enable_status_name"`
}
... ...
... ... @@ -101,6 +101,7 @@ func init() {
beego.NSRouter("/list", &controllers.AuditController{}, "post:AuditList"),
beego.NSRouter("/list/before", &controllers.AuditController{}, "post:AuditListBefore"),
beego.NSRouter("/info", &controllers.AuditController{}, "post:AuditInfo"),
beego.NSRouter("/allow_forbid", &controllers.AuditController{}, "post:AllowForbidAudit"),
),
)
... ...
... ... @@ -20,11 +20,10 @@ type SqlData struct {
PublishStatus int `orm:"column(publish_status)"`
CreateAt time.Time `orm:"column(create_at);type(timestamp)"`
ReviewStatus int `orm:"column(review_status)"`
EnableStatus int `orm:"column(enable_status)"`
EnableStatus int8 `orm:"column(enable_status)"`
DiscoveryScore string `orm:"column(discovery_score)"`
CommentTotal string `orm:"column(comment_total)"`
Code string `orm:"column(code)"`
TemplateName string `orm:"column(template_name)"`
}
func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64) (protocol.ResponseAuditList, error) {
... ... @@ -46,13 +45,13 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64
}
datasql.WriteString(`SELECT a.id,a.user_id,a.department_id,a.audit_template_id,a.chance_type_id
,a.publish_status,a.create_at,a.review_status,a.enable_status
,a.discovery_score,a.comment_total,b.name AS template_name ,b.code
,a.discovery_score,a.comment_total ,a.code,d.nick_name
FROM chance AS a
JOIN audit_template AS b on a.audit_template_id = b.id
JOIN user_company AS c ON c.id = a.user_id
JOIN user AS d ON c.user_id = d.id
where a.company_id=? `)
countsql.WriteString(`SELECT count(*) FROM chance as a
JOIN audit_template AS b on a.audit_template_id = b.id
countsql.WriteString(`SELECT count(*) FROM chance as a
where a.company_id=? `)
cond = append(cond, companyid)
if param.ChanceTypeId > 0 {
... ... @@ -86,15 +85,27 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64
datasql.WriteString(s)
countsql.WriteString(s)
}
if param.CreateTimeBegin > 0 && param.CreateTimeEnd > 0 {
cond = append(cond, param.CreateTimeBegin, param.CreateTimeEnd)
s := ` AND (UNIX_TIMESTAMP(a.create_at) BETWEEN ? AND ? ) `
if param.CreateTimeBegin > 0 {
cond = append(cond, param.CreateTimeBegin)
s := ` AND UNIX_TIMESTAMP(a.create_at)>=? `
datasql.WriteString(s)
countsql.WriteString(s)
}
if param.CreateTimeEnd > 0 {
cond = append(cond, param.CreateTimeEnd)
s := ` AND UNIX_TIMESTAMP(a.create_at)<=? `
datasql.WriteString(s)
countsql.WriteString(s)
}
if len(param.Code) > 0 {
cond = append(cond, param.Code)
s := ` And b.code=? `
cond = append(cond, "%"+param.Code+"%")
s := ` And a.code like ? `
datasql.WriteString(s)
countsql.WriteString(s)
}
if len(param.UserName) > 0 {
cond = append(cond, "%"+param.UserName+"%")
s := ` And d.nick_name like ? `
datasql.WriteString(s)
countsql.WriteString(s)
}
... ... @@ -124,7 +135,6 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64
ReviewStatus: v.ReviewStatus,
ReviewStatusName: models.ChanceReviewStatusMap[v.ReviewStatus],
Code: v.Code,
TemplateName: v.TemplateName,
}
if t < 0 {
... ... @@ -141,6 +151,9 @@ func GetAuditList(param protocol.RequestAuditList, companyid int64, userid int64
if ct, err := models.GetChanceTypeById(v.ChanceTypeId); err == nil {
item.ChanceType = ct.Name
}
if tp, err := models.GetAuditTemplateById(v.AuditTemplateId); err == nil {
item.TemplateName = tp.Name
}
returnData.List = append(returnData.List, item)
}
... ... @@ -261,3 +274,51 @@ func getAuditFlowLog(chanceid int64) ([]protocol.ChanceFlowLog, error) {
err = utils.ExecuteQueryAll(&flowLogs, datasql, chanceid)
return flowLogs, err
}
func AllowChanceEnableStatus(chanceid int64, companyid int64) error {
var (
err error
chanceinfo *models.Chance
)
chanceinfo, err = models.GetChanceById(chanceid)
if err != nil {
log.Error("获取机会数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if chanceinfo.CompanyId != companyid {
log.Error("机会的公司不匹配")
return protocol.NewErrWithMessage("1")
}
chanceinfo.EnableStatus = models.ChanceEnableStatusYes
chanceinfo.UpdateAt = time.Now()
err = models.UpdateChanceById(chanceinfo, []string{"EnableStatus", "UpdateAt"})
if err != nil {
log.Error("更新机会数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
func ForbidChanceEnableStatus(chanceid int64, companyid int64) error {
var (
err error
chanceinfo *models.Chance
)
chanceinfo, err = models.GetChanceById(chanceid)
if err != nil {
log.Error("获取机会数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
if chanceinfo.CompanyId != companyid {
log.Error("机会的公司不匹配")
return protocol.NewErrWithMessage("1")
}
chanceinfo.EnableStatus = models.ChanceEnableStatusNo
chanceinfo.UpdateAt = time.Now()
err = models.UpdateChanceById(chanceinfo, []string{"EnableStatus", "UpdateAt"})
if err != nil {
log.Error("更新机会数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
... ...
... ... @@ -8,6 +8,7 @@ import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
serverbac "oppmg/services/rbac"
"oppmg/services/ucenter"
"oppmg/storage/redisdata"
"oppmg/utils"
... ... @@ -237,6 +238,8 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
if err != nil {
log.Error("更新用户数据失败:%s", err)
}
InitPermission(usercompanyid)
return logintoken, err
}
... ... @@ -371,14 +374,21 @@ func GetUserHasMenu(userid, companyid int64) ([]protocol.ResponseUserHasMenu, er
log.Error("EXECUTE SQL err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
paramMap := map[int64]bool{}
for i := range list {
for _, v := range menuids {
if list[i].Id == v {
list[i].StatusYes()
paramMap[list[i].ParentId] = true
break
}
}
}
for i := range list {
if _, ok := paramMap[list[i].Id]; ok {
list[i].StatusYes()
}
}
return list, nil
}
... ... @@ -467,6 +477,7 @@ func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, err
if err != nil {
log.Error("更新用户数据失败:%s", err)
}
InitPermission(usercompanyid)
return logintoken, err
}
... ... @@ -495,3 +506,44 @@ func SmsCodeCheck(phone string, code string) error {
redisdata.SetCaptchAuth(phone, resp.Data.CaptchaCertificate)
return nil
}
//InitPermission 登录时权限初始化
func InitPermission(usercompanyid int64) error {
var (
err error
permissionMap map[string]serverbac.PermissionOptionObject
)
permissionMap, err = serverbac.GetUserPermission(usercompanyid)
if err != nil {
log.Error("获取用户的权限失败")
return err
}
err = redisdata.SetUserPermission(permissionMap, usercompanyid)
if err != nil {
log.Error("缓存用户权限失败:%s", err)
}
return nil
}
func ValidUserPermission(urlPath string, userid int64, companyid int64) bool {
var (
err error
permissionbase serverbac.PermissionBase
ok bool = false
permissionObj serverbac.PermissionOptionObject
)
permissionbase, ok = serverbac.RouterPermission[urlPath]
if !ok {
return true
}
permissionObj, err = redisdata.GetUserPermission(userid, permissionbase.CodeName)
if err != nil {
log.Error("未取到权限数据")
return false
}
ok = permissionObj.GetValidFunc(permissionbase.ActionName)
if ok {
return true
}
return false
}
... ...
... ... @@ -25,7 +25,7 @@ const (
type PermissionOptionObject interface {
// StringUnmarshal(string) error
// ObjectMarshal() string
GetValidFunc(string) func(UserObject) bool
GetValidFunc(string) bool
MergeObject(string) error
}
... ... @@ -35,7 +35,7 @@ type PermissionBase struct {
ActionName string
}
var routerPermission = map[string]PermissionBase{
var RouterPermission = map[string]PermissionBase{
"/v1/department/list": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"},
"/v1/department/add": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"},
"/v1/department/edit": PermissionBase{CodeName: M_ENTERPRISE_ORGANIZATION, ActionName: "default"},
... ... @@ -133,7 +133,7 @@ func GetUserPermission(userCompanyid int64) (map[string]PermissionOptionObject,
if fn, ok := CodePermissionObject[v.Code]; ok {
obj := fn()
if err = json.Unmarshal([]byte(v.Opption), obj); err != nil {
log.Debug("解析权限配置option 失败%s", err)
log.Debug("解析权限配置option:%s %s失败%s", v.Code, v.Opption, err)
}
objMap[v.Code] = obj
} else {
... ...
... ... @@ -25,7 +25,7 @@ func NewPermissionOptionBase() PermissionOptionObject {
}
}
func (p *PermissionOptionBase) ValidDefault(obj UserObject) bool {
func (p *PermissionOptionBase) ValidDefault() bool {
if p.Check == 1 {
return true
}
... ... @@ -33,14 +33,15 @@ func (p *PermissionOptionBase) ValidDefault(obj UserObject) bool {
}
//GetValidFunc PermissionOptionBase 接口实现
func (p *PermissionOptionBase) GetValidFunc(k string) func(UserObject) bool {
m := map[string]func(UserObject) bool{
func (p *PermissionOptionBase) GetValidFunc(k string) bool {
m := map[string]func() bool{
"default": p.ValidDefault,
}
if _, ok := m[k]; ok {
return m[k]
if fn, ok := m[k]; ok {
b := fn()
return b
}
return nil
return false
}
func (p *PermissionOptionBase) MergeObject(jsonString string) error {
... ... @@ -94,7 +95,7 @@ type CheckOpp struct {
//OptionOpportunity 机会管理 高级权限设置
type OptionOpportunity struct {
Check int `json:"check"`
CheckMap map[int]int `json:"-"`
CheckMap map[int]int `json:"check_map,omitempty"`
CheckOption CheckOpp `json:"check_option"`
EditSorce int `json:"edit_sorce"`
EditPublicStatus int `json:"edit_public_status"`
... ... @@ -131,14 +132,15 @@ func NewOptionOpportunity() PermissionOptionObject {
}
//GetValidFunc PermissionOptionBase 接口实现
func (p *OptionOpportunity) GetValidFunc(k string) func(UserObject) bool {
m := map[string]func(UserObject) bool{
func (p *OptionOpportunity) GetValidFunc(k string) bool {
m := map[string]func() bool{
"check": p.ValidCheck,
}
if _, ok := m[k]; ok {
return m[k]
if fn, ok := m[k]; ok {
b := fn()
return b
}
return nil
return false
}
//MergeObject PermissionOptionBase 接口实现
... ... @@ -195,20 +197,20 @@ func (p *OptionOpportunity) MergeObject(jsonString string) error {
return nil
}
func (p *OptionOpportunity) ValidCheck(obj UserObject) bool {
func (p *OptionOpportunity) ValidCheck() bool {
if p.Check > 0 {
return true
}
return false
}
func (p *OptionOpportunity) ValidEditSorce(obj UserObject) bool {
func (p *OptionOpportunity) ValidEditSorce() bool {
if p.EditSorce > 0 {
return true
}
return false
}
func (p *OptionOpportunity) ValidEditPublicStatus(obj UserObject) bool {
func (p *OptionOpportunity) ValidEditPublicStatus() bool {
if p.EditPublicStatus > 0 {
return true
}
... ...
... ... @@ -6,9 +6,10 @@ import (
)
const (
KEY_PREFIX string = "oppmg_" //统一前缀
KEY_USER_TOKEN string = "token" //登录token存储
KEY_CAPTCHA_AUTH string = "captch_auth" //统一用户中心返回的验证码凭证
KEY_PREFIX string = "oppmg_" //统一前缀
KEY_USER_TOKEN string = "token" //登录token存储
KEY_CAPTCHA_AUTH string = "captch_auth" //统一用户中心返回的验证码凭证
KEY_USER_PERMISSION string = "user_permission" //用户操作权限
)
const (
... ... @@ -31,3 +32,8 @@ func GetKeyCaptchAuth(phone string) string {
key := fmt.Sprintf("%s%s:%s", KEY_PREFIX, KEY_CAPTCHA_AUTH, phone)
return key
}
func GetKeyUserPermission(userid int64) string {
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_PERMISSION, userid)
return key
}
... ...
... ... @@ -2,10 +2,13 @@ package redisdata
import (
"encoding/json"
"errors"
"oppmg/common/log"
"oppmg/common/redis"
"oppmg/protocol"
"oppmg/services/rbac"
"strings"
"time"
)
func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) error {
... ... @@ -96,3 +99,38 @@ func GetCaptchAuth(phone string) (string, error) {
r, err := client.Get(key).Result()
return r, err
}
func SetUserPermission(objMap map[string]rbac.PermissionOptionObject, usercompanyid int64) error {
key := GetKeyUserPermission(usercompanyid)
client := redis.GetRedis()
for k := range objMap {
s, err := json.Marshal(objMap[k])
if err != nil {
log.Error("解析错误:%s", err)
continue
}
err = client.HSet(key, k, s).Err()
if err != nil {
log.Error("设置权限缓存失败:%s", err)
}
}
client.Expire(key, 60*60*6*time.Second)
return nil
}
func GetUserPermission(userid int64, field string) (rbac.PermissionOptionObject, error) {
key := GetKeyUserPermission(userid)
client := redis.GetRedis()
str, err := client.HGet(key, field).Result()
if err != nil {
return nil, err
}
var permissionObj rbac.PermissionOptionObject
fn, ok := rbac.CodePermissionObject[field]
if !ok {
return nil, errors.New("cannot get object")
}
permissionObj = fn()
err = json.Unmarshal([]byte(str), permissionObj)
return permissionObj, err
}
... ...