123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- // user
- package admin
- import (
- . "jy/mongodbutil"
- . "jy/util"
- "net/http"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/gin-contrib/sessions"
- "strconv"
- "gopkg.in/mgo.v2/bson"
- qu "qfw/util"
- "encoding/json"
- )
- func init() {
- Admin.GET("/index", func(c *gin.Context) {
- c.HTML(http.StatusOK, "index.html",nil)
- })
- Admin.GET("/user", func(c *gin.Context) {
- c.HTML(http.StatusOK, "user.html", gin.H{})
- })
- Admin.GET("/menu", func(c *gin.Context) {
- c.HTML(http.StatusOK, "menu.html", gin.H{})
- })
- Admin.GET("/role", func(c *gin.Context) {
- c.HTML(http.StatusOK, "role.html", gin.H{})
- })
- Admin.GET("/role/menu", func(c *gin.Context) {
- role:=c.Query("role")
- c.HTML(http.StatusOK, "rolemenu.html", gin.H{"role":role})
- })
- Admin.GET("/role/secondmenu", func(c *gin.Context) {
- role:=c.Query("role")
- _id:=c.Query("_id")
- c.HTML(http.StatusOK, "rolesecondmenu.html", gin.H{"role":role,"_id":_id})
- })
- Admin.GET("/secondmenu", func(c *gin.Context) {
- _id := c.Query("id")
- c.HTML(http.StatusOK, "secondmenu.html", gin.H{"_id":_id})
- })
- Admin.POST("/menu", Menu)
- Admin.POST("/menu/save",MenuSave)
- Admin.POST("/menu/data",MenuData)
- Admin.POST("/menu/searchbyid", MenuSearchById)
- Admin.POST("/menu/del", MenuDel)
- Admin.POST("/role/menu/data",RoleMenuData)
- Admin.POST("/role/menu/save",RoleMenuSave)
- Admin.POST("/role/select",RoleSelect)
- Admin.POST("/role/secondmenu/data",RoleSecondMenuData)
- Admin.POST("/role/menu/del",RoleMenuDel)
- Admin.POST("/role/secondmenu/del",RoleSecondMenuDel)
- Admin.POST("/secondmenu/data",SecondMenuData)
- Admin.POST("/secondmenu/save",SecondMenuSave)
- Admin.POST("/secondmenu/searchbyid", SecondMenuSearchById)
- Admin.POST("/secondmenu/del", SecondMenuDel)
- Admin.POST("/user/data", User)
- Admin.POST("/user/save", UserSave)
- Admin.POST("/user/searchbyid", UserSearchById)
- Admin.POST("/user/del", UserDel)
- Admin.POST("/user/uppwd", UserUppwd)
- }
- func User(c *gin.Context) {
- data, _ := Mgo.Find("user", `{}`, nil, nil, false, -1, -1)
- c.JSON(200, gin.H{"data": data})
- }
- func MenuData(c *gin.Context) {
- data, _ := Mgo.Find("menu", `{}`, nil, nil, false, -1, -1)
- c.JSON(200, gin.H{"data": data})
- }
- func SecondMenuData(c *gin.Context) {
- _id,_:= c.GetPostForm("_id")
- maps:=map[string]interface{}{
- "menuid":_id,
- }
- data, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
- c.JSON(200, gin.H{"data": data})
- }
- func RoleMenuData(c *gin.Context) {
- role,_:=c.GetPostForm("role")
- maps := map[string]interface{}{
- "role."+role: true,
- }
- datas, _ := Mgo.Find("menu",maps, nil, nil, false, -1, -1)
- list:=[]map[string]interface{}{}
- for _,value:= range *datas{
- _id:=qu.BsonIdToSId(value["_id"])
- maps:=map[string]interface{}{
- "menuid": _id,
- }
- count, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
- if len(*count)!=0{
- value["secondmenu"]=true
- }else{
- value["secondmenu"]=false
- }
- list=append(list,value)
- }
- c.JSON(200, gin.H{"data": list})
- }
- func RoleMenuSave(c *gin.Context){
- menu,_:=c.GetPostForm("menu")
- secondmenuStr, _ := c.GetPostForm("secondmenuStr")
- role,_:=c.GetPostForm("role")
- secondmenus := make([]string, 0)
- err := json.Unmarshal([]byte(secondmenuStr), &secondmenus)
- if err == nil && len(secondmenus) > 0 {
- for _, v := range secondmenus {
- maps:=map[string]interface{}{
- "_id":bson.ObjectIdHex(v),
- }
- data:=map[string]interface{}{
- "role."+role:true,
- }
- data2:=map[string]interface{}{
- "$set":data,
- }
- Mgo.Update("secondmenu", maps, data2, true, false)
- }
- }
- maps:=map[string]interface{}{
- "_id":bson.ObjectIdHex(menu),
- }
- data:=map[string]interface{}{
- "role."+role:true,
- }
- data2:=map[string]interface{}{
- "$set":data,
- }
- b:=Mgo.Update("menu", maps, data2, true, false)
- c.JSON(200, gin.H{"rep": b})
- }
- func RoleSelect(c *gin.Context){
- menuid,_:=c.GetPostForm("_id")
- maps := map[string]interface{}{
- "menuid":menuid,
- }
- datas, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
- c.JSON(200, gin.H{"data": datas})
- }
- func RoleSecondMenuData(c *gin.Context) {
- role,_:=c.GetPostForm("role")
- _id,_:=c.GetPostForm("_id")
- maps := map[string]interface{}{
- "menuid":_id,
- "role."+role: true,
- }
- datas, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
- c.JSON(200, gin.H{"data": datas})
- }
- func Menu(c *gin.Context) {
- //管理员0,审核员1.开发员2,超级管理3
- session := sessions.Default(c)
- role:=session.Get("role").(string)
- maps:=map[string]interface{}{
- }
- if role=="3"{
- maps=map[string]interface{}{
- }
- }else {
- maps = map[string]interface{}{
- "role." + role: true,
- }
- }
- data, _ := Mgo.Find("menu", maps, nil, nil, false, -1, -1)
- list:=[]map[string]interface{}{}
- for _,value:=range *data{
- _id:=value["_id"]
- if role=="3"{
- maps=map[string]interface{}{
- "menuid":qu.BsonIdToSId(_id),
- }
- }else {
- maps = map[string]interface{}{
- "role." + role: true,
- "menuid":qu.BsonIdToSId(_id),
- }
- }
- secdatas, _ := Mgo.Find("secondmenu", maps, nil, nil, false, -1, -1)
- secmenumap:=map[string]interface{}{}
- for index,secdata:=range *secdatas{
- secmenumap[strconv.Itoa(index+1)]=secdata
- }
- if len(secmenumap)!=0{
- value["secondmenu"]=secmenumap
- }
- list=append(list,value)
- }
- c.JSON(200, gin.H{"data": list,"role":role})
- }
- func MenuSave(c *gin.Context) {
- name, _ := c.GetPostForm("name")
- href, _ := c.GetPostForm("href")
- pic, _ := c.GetPostForm("pic")
- _id,_:=c.GetPostForm("_id")
- data := map[string]interface{}{
- }
- if _id==""{
- data = map[string]interface{}{
- "name": name,
- "href": href,
- "pic": pic,
- }
- b:=Mgo.Save("menu",data)
- c.JSON(200, gin.H{"rep": b})
- }else{
- data = map[string]interface{}{
- "name": name,
- "href": href,
- "pic": pic,
- }
- data2:=map[string]interface{}{
- "$set":data,
- }
- maps:=map[string]interface{}{
- "_id":bson.ObjectIdHex(_id),
- }
- b := Mgo.Update("menu", maps, data2, true, false)
- c.JSON(200, gin.H{"rep": b})
- }
- }
- func SecondMenuSave(c *gin.Context) {
- name, _ := c.GetPostForm("name")
- href, _ := c.GetPostForm("href")
- pic, _ := c.GetPostForm("pic")
- _id, _ := c.GetPostForm("_id")
- menuid, _ := c.GetPostForm("menuid")
- data := map[string]interface{}{
- }
- if _id==""{
- data = map[string]interface{}{
- "name": name,
- "href": href,
- "pic": pic,
- "menuid":menuid,
- }
- b:=Mgo.Save("secondmenu",data)
- c.JSON(200, gin.H{"rep": b})
- }else {
- data = map[string]interface{}{
- "name": name,
- "href": href,
- "pic": pic,
- }
- data2:=map[string]interface{}{
- "$set":data,
- }
- maps:=map[string]interface{}{
- "_id":bson.ObjectIdHex(_id),
- }
- b := Mgo.Update("secondmenu",maps, data2, true, false)
- c.JSON(200, gin.H{"rep": b})
- }
- }
- func UserSave(c *gin.Context) {
- email, _ := c.GetPostForm("email")
- pwd, _ := c.GetPostForm("pwd")
- name, _ := c.GetPostForm("name")
- role, _ := c.GetPostForm("role")
- data := map[string]interface{}{
- "email": email,
- "pwd": Se.EncodeString(pwd),
- "name": name,
- "role": role,
- "mtime": time.Now().Unix(),
- }
- b := Mgo.Update("user", `{"email":"`+email+`"}`, data, true, false)
- c.JSON(200, gin.H{"rep": b})
- }
- func UserSearchById(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- data, _ := Mgo.FindById("user", _id, nil)
- (*data)["pwd"] = Se.DecodeString((*data)["pwd"].(string))
- c.JSON(200, gin.H{"rep": data})
- }
- func MenuSearchById(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- data, _ := Mgo.FindById("menu", _id, nil)
- c.JSON(200, gin.H{"rep": data})
- }
- func SecondMenuSearchById(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- data, _ := Mgo.FindById("secondmenu", _id, nil)
- c.JSON(200, gin.H{"rep": data})
- }
- func UserDel(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- b := Mgo.Del("user", `{"_id":"`+_id+`"}`)
- c.JSON(200, gin.H{"rep": b})
- }
- func MenuDel(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- b := Mgo.Del("menu", `{"_id":"`+_id+`"}`)
- c.JSON(200, gin.H{"rep": b})
- }
- func SecondMenuDel(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- b := Mgo.Del("secondmenu", `{"_id":"`+_id+`"}`)
- c.JSON(200, gin.H{"rep": b})
- }
- func RoleMenuDel(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- role, _ := c.GetPostForm("role")
- maps:=map[string]interface{}{
- "_id":bson.ObjectIdHex(_id),
- }
- data:=map[string]interface{}{
- "role."+role:false,
- }
- data2:=map[string]interface{}{
- "$set":data,
- }
- b := Mgo.Update("menu", maps, data2, true, false)
- maps=map[string]interface{}{
- "menuid":_id,
- }
- count,_:=Mgo.Find("secondmenu",maps,nil,nil,false,-1,-1)
- if len(*count)!=0{
- for _,c:=range *count{
- maps=map[string]interface{}{
- "_id":c["_id"],
- }
- Mgo.Update("secondmenu", maps, data2, true, false)
- }
- }
- c.JSON(200, gin.H{"rep": b})
- }
- func RoleSecondMenuDel(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- role, _ := c.GetPostForm("role")
- maps:=map[string]interface{}{
- "_id":bson.ObjectIdHex(_id),
- }
- data:=map[string]interface{}{
- "role."+role:false,
- }
- data2:=map[string]interface{}{
- "$set":data,
- }
- b := Mgo.Update("secondmenu", maps, data2, true, false)
- c.JSON(200, gin.H{"rep": b})
- }
- func UserUppwd(c *gin.Context) {
- _id, _ := c.GetPostForm("_id")
- pwd, _ := c.GetPostForm("pwd")
- b := Mgo.UpdateById("user", _id, `{"$set":{"pwd":"`+Se.EncodeString(pwd)+`"}}`)
- c.JSON(200, gin.H{"rep": b})
- }
|