|
@@ -0,0 +1,310 @@
|
|
|
+package manage
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "log"
|
|
|
+ "qfw/util"
|
|
|
+ . "qfw/util/mongodb"
|
|
|
+ "qfw/util/redis"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
+ "gopkg.in/mgo.v2/bson"
|
|
|
+)
|
|
|
+
|
|
|
+//后台管理
|
|
|
+type Rbac struct {
|
|
|
+ *xweb.Action
|
|
|
+ //目录管理
|
|
|
+ index xweb.Mapper `xweb:"/manage/rbac/index"` //后台目录首页
|
|
|
+ catalogList xweb.Mapper `xweb:"/manage/rbac/catalogList"` //获取后台目录列表
|
|
|
+ saveCL xweb.Mapper `xweb:"/manage/rbac/saveCL"` //更新目录
|
|
|
+ //管理员管理
|
|
|
+ mgIndex xweb.Mapper `xweb:"/manage/rbac/mgIndex"` //后台管理员管理首页
|
|
|
+ mgUsersList xweb.Mapper `xweb:"/manage/rbac/mgUsersList"` //获取管理员列表
|
|
|
+ saveMG xweb.Mapper `xweb:"/manage/rbac/saveMG"` //更新管理员目录
|
|
|
+ //获取各自管理员的目录
|
|
|
+ gEachCL xweb.Mapper `xweb:"/manage/rbac/gEachCL"`
|
|
|
+ //管理分转
|
|
|
+ toEachPage xweb.Mapper `xweb:"/manage/rbac/toEachPage"`
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ xweb.AddAction(&Rbac{})
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (this *Rbac) ToEachPage() error {
|
|
|
+ defer util.Catch()
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ return this.Redirect("/")
|
|
|
+ }
|
|
|
+ var resultMap *[]map[string]interface{}
|
|
|
+ userId := util.ObjToString(this.GetSession("userId"))
|
|
|
+ u := FindOne("user", "{'_id':'"+userId+"'}")
|
|
|
+ u_type := (*u)["i_type"]
|
|
|
+ if u_type == 0 {
|
|
|
+ pids := util.ObjToString((*u)["s_power"])
|
|
|
+ if len(strings.Split(pids, ",")) > 0 {
|
|
|
+ var ids []bson.ObjectId
|
|
|
+ for _, chid := range strings.Split(pids, ",") {
|
|
|
+ ids = append(ids, bson.ObjectIdHex(chid))
|
|
|
+ }
|
|
|
+ resultMap = Find("catalog", bson.M{"_id": bson.M{"$in": ids}}, nil, nil, false, -1, -1)
|
|
|
+ }
|
|
|
+ firstUrl := "/"
|
|
|
+ if len(*resultMap) > 0 {
|
|
|
+ firstUrl = util.ObjToString((*resultMap)[0]["s_url"])
|
|
|
+ }
|
|
|
+ return this.Redirect(firstUrl)
|
|
|
+ } else if u_type == -1 {
|
|
|
+ return this.Render("/manage/rbac/systemsetting.html")
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (this *Rbac) GEachCL() {
|
|
|
+ defer util.Catch()
|
|
|
+ r := func() *FuncResult {
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ return &FuncResult{false, errors.New("未登录"), nil}
|
|
|
+ }
|
|
|
+ if this.Method() != "POST" {
|
|
|
+ return &FuncResult{false, errors.New("请求方式有误"), nil}
|
|
|
+ }
|
|
|
+ userId := util.ObjToString(this.GetSession("userId"))
|
|
|
+ var redisMap = map[string]interface{}{}
|
|
|
+ redisData, err := redis.GetBytes("other", "userP_"+userId)
|
|
|
+ if err != nil || len(*redisData) > 0 {
|
|
|
+ var resultMap *[]map[string]interface{}
|
|
|
+ rData := FindById("user", userId, `{"s_power":1}`)
|
|
|
+ if (*rData)["s_power"] != nil {
|
|
|
+ pids := util.ObjToString((*rData)["s_power"])
|
|
|
+ if len(strings.Split(pids, ",")) > 0 {
|
|
|
+ var ids []bson.ObjectId
|
|
|
+ for _, chid := range strings.Split(pids, ",") {
|
|
|
+ ids = append(ids, bson.ObjectIdHex(chid))
|
|
|
+ }
|
|
|
+ resultMap = Find("catalog", bson.M{"_id": bson.M{"$in": ids}}, nil, nil, false, -1, -1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(*resultMap) > 0 {
|
|
|
+ redisMap = map[string]interface{}{
|
|
|
+ "data": *resultMap,
|
|
|
+ }
|
|
|
+ rM, _ := json.Marshal(redisMap)
|
|
|
+ redis.PutBytes("other", "userP_"+userId, &rM, 120*60)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ json.Unmarshal(*redisData, &redisMap)
|
|
|
+ }
|
|
|
+ return &FuncResult{true, nil, redisMap}
|
|
|
+ }()
|
|
|
+
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf(" EACHCL err:%v\n", r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
+}
|
|
|
+
|
|
|
+//管理员管理
|
|
|
+func (this *Rbac) MgIndex() error {
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ return this.Redirect("/")
|
|
|
+ }
|
|
|
+ return this.Render("/manage/rbac/mgIndex.html")
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (this *Rbac) SaveMG() {
|
|
|
+ defer util.Catch()
|
|
|
+ r := func() *FuncResult {
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ return &FuncResult{false, errors.New("未登录"), nil}
|
|
|
+ }
|
|
|
+ if this.Method() != "POST" {
|
|
|
+ return &FuncResult{false, errors.New("请求方式有误"), nil}
|
|
|
+ }
|
|
|
+ _id := this.GetString("_id") //userid
|
|
|
+ if _id == "" {
|
|
|
+ return &FuncResult{false, errors.New("userid不能为空"), nil}
|
|
|
+ }
|
|
|
+ MGStr := this.GetString("MGStr") //目录id
|
|
|
+ if MGStr == "" {
|
|
|
+ return &FuncResult{false, errors.New("目录id不能为空"), nil}
|
|
|
+ }
|
|
|
+ i_type, _ := this.GetInteger("i_type")
|
|
|
+ //
|
|
|
+ mData := map[string]interface{}{
|
|
|
+ "s_power": MGStr,
|
|
|
+ "i_type": i_type,
|
|
|
+ "l_powerdate": time.Now().Unix(),
|
|
|
+ }
|
|
|
+ if !Update("user", &map[string]interface{}{
|
|
|
+ "_id": util.StringTOBsonId(_id),
|
|
|
+ }, &map[string]interface{}{
|
|
|
+ "$set": mData,
|
|
|
+ }, false, false) {
|
|
|
+ return &FuncResult{false, errors.New("更新管理员信息失败"), nil}
|
|
|
+ } else {
|
|
|
+ redis.Del("other", "userP_"+_id)
|
|
|
+ return &FuncResult{true, nil, nil}
|
|
|
+ }
|
|
|
+ return &FuncResult{false, errors.New("未知操作"), nil}
|
|
|
+ }()
|
|
|
+
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf("MG err:%v\n", r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (this *Rbac) MgUsersList() {
|
|
|
+ defer util.Catch()
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ this.ServeJson(map[string]interface{}{
|
|
|
+ "msg": "未登录",
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if this.Method() != "POST" {
|
|
|
+ this.ServeJson(map[string]interface{}{
|
|
|
+ "msg": "请求方式有误",
|
|
|
+ })
|
|
|
+ }
|
|
|
+ size, _ := this.GetInteger("perPage")
|
|
|
+ if size > 100 {
|
|
|
+ this.ServeJson(map[string]interface{}{
|
|
|
+ "msg": "请求太多",
|
|
|
+ })
|
|
|
+ }
|
|
|
+ currentPage, _ := this.GetInteger("currentPage")
|
|
|
+ query := map[string]interface{}{}
|
|
|
+ if this.GetString("normal") != "" && this.GetString("openid") != "" {
|
|
|
+ query["s_m_openid"] = this.GetString("openid")
|
|
|
+ } else {
|
|
|
+ query["i_type"] = 0
|
|
|
+ }
|
|
|
+ res := map[string]interface{}{}
|
|
|
+ res["totalRows"] = Count("user", query)
|
|
|
+ data := *Find("user", query, `{"_id":-1}`, nil, false, (currentPage-1)*size, size)
|
|
|
+ for _, v := range data {
|
|
|
+ data_1 := v["l_registedate"]
|
|
|
+ v["l_registedate"] = util.FormatDateWithObj(&data_1, util.Date_Full_Layout)
|
|
|
+ }
|
|
|
+ res["data"] = data
|
|
|
+ res["currentPage"] = currentPage
|
|
|
+ this.ServeJson(res)
|
|
|
+}
|
|
|
+
|
|
|
+//目录管理
|
|
|
+func (this *Rbac) Index() error {
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ return this.Redirect("/")
|
|
|
+ }
|
|
|
+ return this.Render("/manage/rbac/systemsetting.html")
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (this *Rbac) SaveCL() {
|
|
|
+ defer util.Catch()
|
|
|
+ Dotype := this.GetString("dotype") //删除 or 新增 or 修改
|
|
|
+ r := func() *FuncResult {
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ return &FuncResult{false, errors.New("未登录"), nil}
|
|
|
+ }
|
|
|
+ if this.Method() != "POST" {
|
|
|
+ return &FuncResult{false, errors.New("请求方式有误"), nil}
|
|
|
+ }
|
|
|
+ if Dotype == "D" {
|
|
|
+ cl_id := this.GetString("clid") //目录id
|
|
|
+ if cl_id == "" {
|
|
|
+ return &FuncResult{false, errors.New("目录id不能为空"), nil}
|
|
|
+ }
|
|
|
+ ok := DelById("catalog", cl_id)
|
|
|
+ if ok {
|
|
|
+ return &FuncResult{true, nil, nil}
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cl_name := this.GetString("clname") //目录名称
|
|
|
+ if cl_name == "" {
|
|
|
+ return &FuncResult{false, errors.New("目录名称不能为空"), nil}
|
|
|
+ }
|
|
|
+ cl_url := this.GetString("clurl") //目录url
|
|
|
+ if cl_url == "" {
|
|
|
+ return &FuncResult{false, errors.New("目录地址不能为空"), nil}
|
|
|
+ }
|
|
|
+ //
|
|
|
+ mData := map[string]interface{}{
|
|
|
+ "s_name": cl_name,
|
|
|
+ "s_url": cl_url,
|
|
|
+ "s_remark": this.GetString("clremark"), //目录说明
|
|
|
+ "s_icon": this.GetString("cllcon"), //目录图标
|
|
|
+ "l_createdate": time.Now().Unix(),
|
|
|
+ }
|
|
|
+ if Dotype == "S" {
|
|
|
+ if Save("catalog", mData) == "" {
|
|
|
+ return &FuncResult{false, errors.New("增加目录失败"), nil}
|
|
|
+ } else {
|
|
|
+ return &FuncResult{true, nil, nil}
|
|
|
+ }
|
|
|
+ } else if Dotype == "U" {
|
|
|
+ cl_id := this.GetString("clid")
|
|
|
+ if cl_id == "" {
|
|
|
+ return &FuncResult{false, errors.New("目录id不能为空"), nil}
|
|
|
+ }
|
|
|
+ mData["l_updatedate"] = time.Now().Unix()
|
|
|
+ if !Update("catalog", &map[string]interface{}{
|
|
|
+ "_id": util.StringTOBsonId(cl_id),
|
|
|
+ }, &map[string]interface{}{
|
|
|
+ "$set": mData,
|
|
|
+ }, false, false) {
|
|
|
+ return &FuncResult{false, errors.New("更新目录失败"), nil}
|
|
|
+ } else {
|
|
|
+ return &FuncResult{true, nil, nil}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return &FuncResult{false, errors.New("未知操作"), nil}
|
|
|
+ }()
|
|
|
+
|
|
|
+ if r.Err != nil {
|
|
|
+ log.Printf(" catalog%s err:%v\n", Dotype, r.Err.Error())
|
|
|
+ }
|
|
|
+ this.ServeJson(r.Format())
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (this *Rbac) CatalogList() {
|
|
|
+ defer util.Catch()
|
|
|
+ if this.GetSession("userId") == nil {
|
|
|
+ this.ServeJson(map[string]interface{}{
|
|
|
+ "msg": "未登录",
|
|
|
+ })
|
|
|
+ }
|
|
|
+ if this.Method() != "POST" {
|
|
|
+ this.ServeJson(map[string]interface{}{
|
|
|
+ "msg": "请求方式有误",
|
|
|
+ })
|
|
|
+ }
|
|
|
+ size, _ := this.GetInteger("perPage")
|
|
|
+ if size > 100 {
|
|
|
+ this.ServeJson(map[string]interface{}{
|
|
|
+ "msg": "请求太多",
|
|
|
+ })
|
|
|
+ }
|
|
|
+ currentPage, _ := this.GetInteger("currentPage")
|
|
|
+ res := map[string]interface{}{}
|
|
|
+ res["totalRows"] = Count("catalog", nil)
|
|
|
+ data := *Find("catalog", nil, []string{"-l_createdate"}, nil, false, (currentPage-1)*size, size)
|
|
|
+ for _, v := range data {
|
|
|
+ data_1 := v["l_createdate"]
|
|
|
+ v["l_createdate"] = util.FormatDateWithObj(&data_1, util.Date_Full_Layout)
|
|
|
+ }
|
|
|
+ res["data"] = data
|
|
|
+ res["currentPage"] = currentPage
|
|
|
+ this.ServeJson(res)
|
|
|
+}
|