kefu.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/wenstudio/gofly/models"
  5. "github.com/wenstudio/gofly/tools"
  6. "strconv"
  7. )
  8. func GetKefuInfo(c *gin.Context) {
  9. kefuId, _ := c.Get("kefu_id")
  10. user := models.FindUserById(kefuId)
  11. info := make(map[string]interface{})
  12. info["name"] = user.Nickname
  13. info["id"] = user.Name
  14. info["avator"] = user.Avator
  15. c.JSON(200, gin.H{
  16. "code": 200,
  17. "msg": "ok",
  18. "result": info,
  19. })
  20. }
  21. func GetKefuInfoAll(c *gin.Context) {
  22. id, _ := c.Get("kefu_id")
  23. userinfo := models.FindUserRole("user.avator,user.name,user.id, role.name role_name", id)
  24. c.JSON(200, gin.H{
  25. "code": 200,
  26. "msg": "验证成功",
  27. "result": userinfo,
  28. })
  29. }
  30. func GetKefuInfoSetting(c *gin.Context) {
  31. kefuId := c.Query("kefu_id")
  32. user := models.FindUserById(kefuId)
  33. c.JSON(200, gin.H{
  34. "code": 200,
  35. "msg": "ok",
  36. "result": user,
  37. })
  38. }
  39. func PostKefuInfo(c *gin.Context) {
  40. id := c.PostForm("id")
  41. name := c.PostForm("name")
  42. password := c.PostForm("password")
  43. avator := c.PostForm("avator")
  44. nickname := c.PostForm("nickname")
  45. roleId := c.PostForm("role_id")
  46. senabled := c.PostForm("enabled")
  47. enabled64, err := strconv.ParseUint(senabled, 10, 32)
  48. if err != nil {
  49. enabled64 = 0
  50. }
  51. enabled := uint(enabled64)
  52. if roleId == "" {
  53. c.JSON(200, gin.H{
  54. "code": 400,
  55. "msg": "请选择角色!",
  56. })
  57. return
  58. }
  59. if len(password) <= 0 {
  60. c.JSON(200, gin.H{
  61. "code": 400,
  62. "msg": "请设置密码",
  63. })
  64. return
  65. }
  66. //插入新用户
  67. if id == "" {
  68. uid := models.CreateUser(name, tools.Md5(password), avator, nickname, enabled)
  69. if uid == 0 {
  70. c.JSON(200, gin.H{
  71. "code": 400,
  72. "msg": "增加用户失败",
  73. "result": "",
  74. })
  75. return
  76. }
  77. roleIdInt, _ := strconv.Atoi(roleId)
  78. models.CreateUserRole(uid, uint(roleIdInt))
  79. } else {
  80. //更新用户
  81. if password != "" {
  82. password = tools.Md5(password)
  83. }
  84. models.UpdateUser(id, name, password, avator, nickname, enabled)
  85. roleIdInt, _ := strconv.Atoi(roleId)
  86. uid, _ := strconv.Atoi(id)
  87. models.DeleteRoleByUserId(uid)
  88. models.CreateUserRole(uint(uid), uint(roleIdInt))
  89. }
  90. c.JSON(200, gin.H{
  91. "code": 200,
  92. "msg": "ok",
  93. "result": "",
  94. })
  95. }
  96. func GetKefuList(c *gin.Context) {
  97. users := models.FindUsers()
  98. c.JSON(200, gin.H{
  99. "code": 200,
  100. "msg": "获取成功",
  101. "result": users,
  102. })
  103. }
  104. func GetKefuListEnabled(c *gin.Context) {
  105. users := models.FindUsers()
  106. enabledUsers := []models.User{}
  107. for _, user := range users {
  108. if user.Enabled == 1 {
  109. enabledUsers = append(enabledUsers, user)
  110. }
  111. }
  112. c.JSON(200, gin.H{
  113. "code": 200,
  114. "msg": "获取成功",
  115. "result": enabledUsers,
  116. })
  117. }
  118. func DeleteKefuInfo(c *gin.Context) {
  119. kefuId := c.Query("id")
  120. models.DeleteUserById(kefuId)
  121. models.DeleteRoleByUserId(kefuId)
  122. c.JSON(200, gin.H{
  123. "code": 200,
  124. "msg": "删除成功",
  125. "result": "",
  126. })
  127. }