user.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. )
  10. func init() {
  11. Admin.GET("/index", func(c *gin.Context) {
  12. c.HTML(http.StatusOK, "index.html", gin.H{})
  13. })
  14. Admin.GET("/user", func(c *gin.Context) {
  15. c.HTML(http.StatusOK, "user.html", gin.H{})
  16. })
  17. Admin.POST("/user/data", User)
  18. Admin.POST("/user/save", UserSave)
  19. Admin.POST("/user/searchbyid", UserSearchById)
  20. Admin.POST("/user/del", UserDel)
  21. }
  22. func User(c *gin.Context) {
  23. data, _ := Mgo.Find("user", `{}`, nil, nil, false, -1, -1)
  24. c.JSON(200, gin.H{"data": data})
  25. }
  26. func UserSave(c *gin.Context) {
  27. email, _ := c.GetPostForm("email")
  28. pwd, _ := c.GetPostForm("pwd")
  29. name, _ := c.GetPostForm("name")
  30. role, _ := c.GetPostForm("role")
  31. data := map[string]interface{}{
  32. "email": email,
  33. "pwd": Se.EncodeString(pwd),
  34. "name": name,
  35. "role": role,
  36. "mtime": time.Now().Unix(),
  37. }
  38. b := Mgo.Update("user", `{"email":"`+email+`"}`, data, true, false)
  39. c.JSON(200, gin.H{"rep": b})
  40. }
  41. func UserSearchById(c *gin.Context) {
  42. _id, _ := c.GetPostForm("_id")
  43. data, _ := Mgo.FindById("user", _id, nil)
  44. (*data)["pwd"] = Se.DecodeString((*data)["pwd"].(string))
  45. c.JSON(200, gin.H{"rep": data})
  46. }
  47. func UserDel(c *gin.Context) {
  48. _id, _ := c.GetPostForm("_id")
  49. b := Mgo.Del("user", `{"_id":"`+_id+`"}`)
  50. c.JSON(200, gin.H{"rep": b})
  51. }