user.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // user
  2. package admin
  3. import (
  4. . "jy/mongodbutil"
  5. . "jy/util"
  6. "net/http"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gin-contrib/sessions"
  10. "strconv"
  11. "gopkg.in/mgo.v2/bson"
  12. qu "qfw/util"
  13. "encoding/json"
  14. )
  15. func init() {
  16. Admin.GET("/index", func(c *gin.Context) {
  17. c.HTML(http.StatusOK, "index.html",nil)
  18. })
  19. Admin.GET("/user", func(c *gin.Context) {
  20. c.HTML(http.StatusOK, "user.html", gin.H{})
  21. })
  22. Admin.GET("/menu", func(c *gin.Context) {
  23. c.HTML(http.StatusOK, "menu.html", gin.H{})
  24. })
  25. Admin.GET("/role", func(c *gin.Context) {
  26. c.HTML(http.StatusOK, "role.html", gin.H{})
  27. })
  28. Admin.GET("/role/menu", func(c *gin.Context) {
  29. role:=c.Query("role")
  30. c.HTML(http.StatusOK, "rolemenu.html", gin.H{"role":role})
  31. })
  32. Admin.GET("/role/secondmenu", func(c *gin.Context) {
  33. role:=c.Query("role")
  34. _id:=c.Query("_id")
  35. c.HTML(http.StatusOK, "rolesecondmenu.html", gin.H{"role":role,"_id":_id})
  36. })
  37. Admin.GET("/secondmenu", func(c *gin.Context) {
  38. _id := c.Query("id")
  39. c.HTML(http.StatusOK, "secondmenu.html", gin.H{"_id":_id})
  40. })
  41. Admin.POST("/menu", Menu)
  42. Admin.POST("/menu/save",MenuSave)
  43. Admin.POST("/menu/data",MenuData)
  44. Admin.POST("/menu/searchbyid", MenuSearchById)
  45. Admin.POST("/menu/del", MenuDel)
  46. Admin.POST("/role/menu/data",RoleMenuData)
  47. Admin.POST("/role/menu/save",RoleMenuSave)
  48. Admin.POST("/role/select",RoleSelect)
  49. Admin.POST("/role/secondmenu/data",RoleSecondMenuData)
  50. Admin.POST("/role/menu/del",RoleMenuDel)
  51. Admin.POST("/role/secondmenu/del",RoleSecondMenuDel)
  52. Admin.POST("/secondmenu/data",SecondMenuData)
  53. Admin.POST("/secondmenu/save",SecondMenuSave)
  54. Admin.POST("/secondmenu/searchbyid", SecondMenuSearchById)
  55. Admin.POST("/secondmenu/del", SecondMenuDel)
  56. Admin.POST("/user/data", User)
  57. Admin.POST("/user/save", UserSave)
  58. Admin.POST("/user/searchbyid", UserSearchById)
  59. Admin.POST("/user/del", UserDel)
  60. Admin.POST("/user/uppwd", UserUppwd)
  61. }
  62. func User(c *gin.Context) {
  63. data, _ := Mgo.Find("user", `{}`, nil, nil, false, -1, -1)
  64. c.JSON(200, gin.H{"data": data})
  65. }
  66. func MenuData(c *gin.Context) {
  67. data, _ := Mgo.Find("menu", `{}`, nil, nil, false, -1, -1)
  68. c.JSON(200, gin.H{"data": data})
  69. }
  70. func SecondMenuData(c *gin.Context) {
  71. _id,_:= c.GetPostForm("_id")
  72. maps:=map[string]interface{}{
  73. "menuid":_id,
  74. }
  75. data, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
  76. c.JSON(200, gin.H{"data": data})
  77. }
  78. func RoleMenuData(c *gin.Context) {
  79. role,_:=c.GetPostForm("role")
  80. maps := map[string]interface{}{
  81. "role."+role: true,
  82. }
  83. datas, _ := Mgo.Find("menu",maps, nil, nil, false, -1, -1)
  84. list:=[]map[string]interface{}{}
  85. for _,value:= range *datas{
  86. _id:=qu.BsonIdToSId(value["_id"])
  87. maps:=map[string]interface{}{
  88. "menuid": _id,
  89. }
  90. count, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
  91. if len(*count)!=0{
  92. value["secondmenu"]=true
  93. }else{
  94. value["secondmenu"]=false
  95. }
  96. list=append(list,value)
  97. }
  98. c.JSON(200, gin.H{"data": list})
  99. }
  100. func RoleMenuSave(c *gin.Context){
  101. menu,_:=c.GetPostForm("menu")
  102. secondmenuStr, _ := c.GetPostForm("secondmenuStr")
  103. role,_:=c.GetPostForm("role")
  104. secondmenus := make([]string, 0)
  105. err := json.Unmarshal([]byte(secondmenuStr), &secondmenus)
  106. if err == nil && len(secondmenus) > 0 {
  107. for _, v := range secondmenus {
  108. maps:=map[string]interface{}{
  109. "_id":bson.ObjectIdHex(v),
  110. }
  111. data:=map[string]interface{}{
  112. "role."+role:true,
  113. }
  114. data2:=map[string]interface{}{
  115. "$set":data,
  116. }
  117. Mgo.Update("secondmenu", maps, data2, true, false)
  118. }
  119. }
  120. maps:=map[string]interface{}{
  121. "_id":bson.ObjectIdHex(menu),
  122. }
  123. data:=map[string]interface{}{
  124. "role."+role:true,
  125. }
  126. data2:=map[string]interface{}{
  127. "$set":data,
  128. }
  129. b:=Mgo.Update("menu", maps, data2, true, false)
  130. c.JSON(200, gin.H{"rep": b})
  131. }
  132. func RoleSelect(c *gin.Context){
  133. menuid,_:=c.GetPostForm("_id")
  134. maps := map[string]interface{}{
  135. "menuid":menuid,
  136. }
  137. datas, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
  138. c.JSON(200, gin.H{"data": datas})
  139. }
  140. func RoleSecondMenuData(c *gin.Context) {
  141. role,_:=c.GetPostForm("role")
  142. _id,_:=c.GetPostForm("_id")
  143. maps := map[string]interface{}{
  144. "menuid":_id,
  145. "role."+role: true,
  146. }
  147. datas, _ := Mgo.Find("secondmenu",maps, nil, nil, false, -1, -1)
  148. c.JSON(200, gin.H{"data": datas})
  149. }
  150. func Menu(c *gin.Context) {
  151. //管理员0,审核员1.开发员2,超级管理3
  152. session := sessions.Default(c)
  153. role:=session.Get("role").(string)
  154. maps:=map[string]interface{}{
  155. }
  156. if role=="3"{
  157. maps=map[string]interface{}{
  158. }
  159. }else {
  160. maps = map[string]interface{}{
  161. "role." + role: true,
  162. }
  163. }
  164. data, _ := Mgo.Find("menu", maps, nil, nil, false, -1, -1)
  165. list:=[]map[string]interface{}{}
  166. for _,value:=range *data{
  167. _id:=value["_id"]
  168. if role=="3"{
  169. maps=map[string]interface{}{
  170. "menuid":qu.BsonIdToSId(_id),
  171. }
  172. }else {
  173. maps = map[string]interface{}{
  174. "role." + role: true,
  175. "menuid":qu.BsonIdToSId(_id),
  176. }
  177. }
  178. secdatas, _ := Mgo.Find("secondmenu", maps, nil, nil, false, -1, -1)
  179. secmenumap:=map[string]interface{}{}
  180. for index,secdata:=range *secdatas{
  181. secmenumap[strconv.Itoa(index+1)]=secdata
  182. }
  183. if len(secmenumap)!=0{
  184. value["secondmenu"]=secmenumap
  185. }
  186. list=append(list,value)
  187. }
  188. c.JSON(200, gin.H{"data": list,"role":role})
  189. }
  190. func MenuSave(c *gin.Context) {
  191. name, _ := c.GetPostForm("name")
  192. href, _ := c.GetPostForm("href")
  193. pic, _ := c.GetPostForm("pic")
  194. _id,_:=c.GetPostForm("_id")
  195. data := map[string]interface{}{
  196. }
  197. if _id==""{
  198. data = map[string]interface{}{
  199. "name": name,
  200. "href": href,
  201. "pic": pic,
  202. }
  203. b:=Mgo.Save("menu",data)
  204. c.JSON(200, gin.H{"rep": b})
  205. }else{
  206. data = map[string]interface{}{
  207. "name": name,
  208. "href": href,
  209. "pic": pic,
  210. }
  211. data2:=map[string]interface{}{
  212. "$set":data,
  213. }
  214. maps:=map[string]interface{}{
  215. "_id":bson.ObjectIdHex(_id),
  216. }
  217. b := Mgo.Update("menu", maps, data2, true, false)
  218. c.JSON(200, gin.H{"rep": b})
  219. }
  220. }
  221. func SecondMenuSave(c *gin.Context) {
  222. name, _ := c.GetPostForm("name")
  223. href, _ := c.GetPostForm("href")
  224. pic, _ := c.GetPostForm("pic")
  225. _id, _ := c.GetPostForm("_id")
  226. menuid, _ := c.GetPostForm("menuid")
  227. data := map[string]interface{}{
  228. }
  229. if _id==""{
  230. data = map[string]interface{}{
  231. "name": name,
  232. "href": href,
  233. "pic": pic,
  234. "menuid":menuid,
  235. }
  236. b:=Mgo.Save("secondmenu",data)
  237. c.JSON(200, gin.H{"rep": b})
  238. }else {
  239. data = map[string]interface{}{
  240. "name": name,
  241. "href": href,
  242. "pic": pic,
  243. }
  244. data2:=map[string]interface{}{
  245. "$set":data,
  246. }
  247. maps:=map[string]interface{}{
  248. "_id":bson.ObjectIdHex(_id),
  249. }
  250. b := Mgo.Update("secondmenu",maps, data2, true, false)
  251. c.JSON(200, gin.H{"rep": b})
  252. }
  253. }
  254. func UserSave(c *gin.Context) {
  255. email, _ := c.GetPostForm("email")
  256. pwd, _ := c.GetPostForm("pwd")
  257. name, _ := c.GetPostForm("name")
  258. role, _ := c.GetPostForm("role")
  259. data := map[string]interface{}{
  260. "email": email,
  261. "pwd": Se.EncodeString(pwd),
  262. "name": name,
  263. "role": role,
  264. "mtime": time.Now().Unix(),
  265. }
  266. b := Mgo.Update("user", `{"email":"`+email+`"}`, data, true, false)
  267. c.JSON(200, gin.H{"rep": b})
  268. }
  269. func UserSearchById(c *gin.Context) {
  270. _id, _ := c.GetPostForm("_id")
  271. data, _ := Mgo.FindById("user", _id, nil)
  272. (*data)["pwd"] = Se.DecodeString((*data)["pwd"].(string))
  273. c.JSON(200, gin.H{"rep": data})
  274. }
  275. func MenuSearchById(c *gin.Context) {
  276. _id, _ := c.GetPostForm("_id")
  277. data, _ := Mgo.FindById("menu", _id, nil)
  278. c.JSON(200, gin.H{"rep": data})
  279. }
  280. func SecondMenuSearchById(c *gin.Context) {
  281. _id, _ := c.GetPostForm("_id")
  282. data, _ := Mgo.FindById("secondmenu", _id, nil)
  283. c.JSON(200, gin.H{"rep": data})
  284. }
  285. func UserDel(c *gin.Context) {
  286. _id, _ := c.GetPostForm("_id")
  287. b := Mgo.Del("user", `{"_id":"`+_id+`"}`)
  288. c.JSON(200, gin.H{"rep": b})
  289. }
  290. func MenuDel(c *gin.Context) {
  291. _id, _ := c.GetPostForm("_id")
  292. b := Mgo.Del("menu", `{"_id":"`+_id+`"}`)
  293. c.JSON(200, gin.H{"rep": b})
  294. }
  295. func SecondMenuDel(c *gin.Context) {
  296. _id, _ := c.GetPostForm("_id")
  297. b := Mgo.Del("secondmenu", `{"_id":"`+_id+`"}`)
  298. c.JSON(200, gin.H{"rep": b})
  299. }
  300. func RoleMenuDel(c *gin.Context) {
  301. _id, _ := c.GetPostForm("_id")
  302. role, _ := c.GetPostForm("role")
  303. maps:=map[string]interface{}{
  304. "_id":bson.ObjectIdHex(_id),
  305. }
  306. data:=map[string]interface{}{
  307. "role."+role:false,
  308. }
  309. data2:=map[string]interface{}{
  310. "$set":data,
  311. }
  312. b := Mgo.Update("menu", maps, data2, true, false)
  313. maps=map[string]interface{}{
  314. "menuid":_id,
  315. }
  316. count,_:=Mgo.Find("secondmenu",maps,nil,nil,false,-1,-1)
  317. if len(*count)!=0{
  318. for _,c:=range *count{
  319. maps=map[string]interface{}{
  320. "_id":c["_id"],
  321. }
  322. Mgo.Update("secondmenu", maps, data2, true, false)
  323. }
  324. }
  325. c.JSON(200, gin.H{"rep": b})
  326. }
  327. func RoleSecondMenuDel(c *gin.Context) {
  328. _id, _ := c.GetPostForm("_id")
  329. role, _ := c.GetPostForm("role")
  330. maps:=map[string]interface{}{
  331. "_id":bson.ObjectIdHex(_id),
  332. }
  333. data:=map[string]interface{}{
  334. "role."+role:false,
  335. }
  336. data2:=map[string]interface{}{
  337. "$set":data,
  338. }
  339. b := Mgo.Update("secondmenu", maps, data2, true, false)
  340. c.JSON(200, gin.H{"rep": b})
  341. }
  342. func UserUppwd(c *gin.Context) {
  343. _id, _ := c.GetPostForm("_id")
  344. pwd, _ := c.GetPostForm("pwd")
  345. b := Mgo.UpdateById("user", _id, `{"$set":{"pwd":"`+Se.EncodeString(pwd)+`"}}`)
  346. c.JSON(200, gin.H{"rep": b})
  347. }