user_controllers.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package controllers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. "xugu_license/internal/global"
  7. middlewares "xugu_license/internal/middleware"
  8. "xugu_license/internal/models"
  9. "xugu_license/internal/utils"
  10. tokens "xugu_license/internal/utils/token"
  11. "github.com/gin-gonic/gin"
  12. "golang.org/x/crypto/bcrypt"
  13. )
  14. // /api/register的请求体
  15. type ReqRegister struct {
  16. Username string `json:"username" binding:"required"`
  17. Account string `json:"Account" binding:"required"`
  18. Password string `json:"password" binding:"required"`
  19. TELEPHONE string `json:"TELEPHONE" binding:"required"`
  20. EMAIL string `json:"EMAIL" binding:"required"`
  21. }
  22. func Register(c *gin.Context) {
  23. var req ReqRegister
  24. if err := c.ShouldBindBodyWithJSON(&req); err != nil {
  25. c.JSON(http.StatusBadRequest, gin.H{
  26. "error": err.Error(),
  27. })
  28. return
  29. }
  30. //TODO判断密码,邮箱,电话 合法性
  31. //定义正则表达式,匹配仅包含字母和数字的字符串
  32. reg := regexp.MustCompile("^[a-zA-Z0-9]+$")
  33. if !reg.MatchString(req.Account) {
  34. global.Logger.Errorln("账户仅能包含字母和数字 ")
  35. c.JSON(http.StatusBadRequest, gin.H{
  36. "error": "账户仅能包含字母和数字",
  37. })
  38. return
  39. }
  40. if len(req.Password) < 6 {
  41. global.Logger.Errorln("密码长度不能小于6位 ")
  42. c.JSON(http.StatusBadRequest, gin.H{
  43. "error": "密码长度不能小于6位",
  44. })
  45. return
  46. }
  47. if len(req.TELEPHONE) != 11 {
  48. global.Logger.Errorln("电话号码长度不正确 ")
  49. c.JSON(http.StatusBadRequest, gin.H{
  50. "error": "电话号码长度不正确",
  51. })
  52. return
  53. }
  54. // if len(req.EMAIL) < 6 {
  55. // c.JSON(http.StatusBadRequest, gin.H{
  56. // "error": "邮箱长度不能小于6位",
  57. // })
  58. // return
  59. // }
  60. //TODO判断用户名,邮箱,电话 是否重复
  61. userTmp, err := models.CheckEmailOrTelphoneOrAccount(req.Username, req.Account, req.EMAIL, req.TELEPHONE)
  62. if err != nil {
  63. global.Logger.Errorln("数据库查询重复用户失败 ", err.Error())
  64. c.JSON(http.StatusBadRequest, gin.H{
  65. "error": fmt.Sprintln("数据库查询重复用户失败: ", err.Error()),
  66. })
  67. return
  68. }
  69. if userTmp.Username == req.Username {
  70. c.JSON(http.StatusBadRequest, gin.H{
  71. "error": "用户名重复",
  72. })
  73. return
  74. }
  75. //fmt.Printf("userTmp: %#v ", userTmp)
  76. if userTmp.Account == req.Account {
  77. c.JSON(http.StatusBadRequest, gin.H{
  78. "error": "账号重复",
  79. })
  80. return
  81. }
  82. if userTmp.Email == req.EMAIL {
  83. c.JSON(http.StatusBadRequest, gin.H{
  84. "error": "邮箱重复",
  85. })
  86. return
  87. }
  88. if userTmp.Telephone == req.TELEPHONE {
  89. c.JSON(http.StatusBadRequest, gin.H{
  90. "error": "电话重复",
  91. })
  92. return
  93. }
  94. uId := utils.GenerateShortIdentifier(req.Account, req.EMAIL, req.TELEPHONE)
  95. //加密密码
  96. hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
  97. if err != nil {
  98. return
  99. }
  100. req.Password = string(hashedPassword)
  101. _, err = global.XuguDB.Exec("INSERT INTO lic_USER (UNIQUEID,username,Account, password,TELEPHONE,email) VALUES (?, ?,?,?,?,?)", uId, req.Username, req.Account, req.Password, req.TELEPHONE, req.EMAIL)
  102. if err != nil {
  103. global.Logger.Errorln("数据库创建新用户插入失败 ", err.Error())
  104. c.JSON(http.StatusBadRequest, gin.H{
  105. "error": fmt.Sprintln("创建用户失败: ", err.Error()),
  106. })
  107. return
  108. }
  109. c.JSON(http.StatusOK, gin.H{
  110. "success": "register success",
  111. "data": req,
  112. })
  113. }
  114. // api/login 的请求体
  115. type ReqLogin struct {
  116. Account string `json:"Account" binding:"required"`
  117. Password string `json:"password" binding:"required"`
  118. }
  119. func Login(c *gin.Context) {
  120. var req ReqLogin
  121. if err := c.ShouldBindBodyWithJSON(&req); err != nil {
  122. global.Logger.Errorln("解析请求失败 ", err.Error())
  123. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("解析请求失败: ", err.Error())})
  124. return
  125. }
  126. token, err := models.LoginCheck(req.Account, req.Password)
  127. if err != nil {
  128. c.JSON(http.StatusBadRequest, gin.H{
  129. "error": "账号或密码不对",
  130. })
  131. return
  132. }
  133. c.JSON(http.StatusOK, gin.H{
  134. "token": token,
  135. })
  136. }
  137. func GetUserInfo(c *gin.Context) {
  138. // 从token中解析出user_id
  139. userClaims, err := tokens.ExtractTokenID(c)
  140. if err != nil {
  141. c.JSON(http.StatusBadRequest, gin.H{
  142. "error": err.Error(),
  143. })
  144. return
  145. }
  146. u, err := models.GetUserInfoByID(userClaims.UserId)
  147. if err != nil {
  148. c.JSON(http.StatusBadRequest, gin.H{
  149. "error": err.Error(),
  150. })
  151. return
  152. }
  153. fmt.Println("sdsad u", u)
  154. c.JSON(http.StatusOK, gin.H{
  155. "message": "success",
  156. "data": u,
  157. })
  158. }
  159. func GetAllUserInfo(c *gin.Context) {
  160. // 从token中解析出user_id
  161. u, err := models.GetAllUser()
  162. if err != nil {
  163. c.JSON(http.StatusBadRequest, gin.H{
  164. "error": err.Error(),
  165. })
  166. return
  167. }
  168. fmt.Println("sdsad u", u)
  169. type allUserInfo struct {
  170. }
  171. c.JSON(http.StatusOK, gin.H{
  172. "message": "success",
  173. "data": u,
  174. })
  175. }
  176. func UpdateUserInfo(c *gin.Context) {
  177. //获取用户信息和权限
  178. userInfo, err := getLoginInfo(c)
  179. if err != nil {
  180. c.JSON(http.StatusUnauthorized, gin.H{"error": "用户信息不存在"})
  181. c.Abort()
  182. }
  183. var req models.UserInfo
  184. if err := c.ShouldBindBodyWithJSON(&req); err != nil {
  185. c.JSON(http.StatusBadRequest, gin.H{
  186. "error": err.Error(),
  187. })
  188. return
  189. }
  190. if userInfo.Permission[middlewares.UpdateRole] != middlewares.UpdateRole {
  191. req.Role = userInfo.Role
  192. }
  193. fmt.Println("req", req)
  194. if err := models.UpdateUserInfo(req); err != nil {
  195. c.JSON(http.StatusBadRequest, gin.H{
  196. "error": err.Error(),
  197. })
  198. }
  199. c.JSON(http.StatusOK, gin.H{
  200. "success": true,
  201. "message": "用户信息更新成功",
  202. })
  203. }
  204. func DeleteUser(c *gin.Context) {
  205. type ReqDeleteUser struct {
  206. UniqueID string `json:"UniqueID" `
  207. }
  208. var UniqueID ReqDeleteUser
  209. if err := c.ShouldBindBodyWithJSON(&UniqueID); err != nil {
  210. c.JSON(http.StatusBadRequest, gin.H{
  211. "error": fmt.Sprintln("解析请求失败: ", err.Error()),
  212. })
  213. return
  214. }
  215. if err := models.DeleteUserInfo(UniqueID.UniqueID); err != nil {
  216. c.JSON(http.StatusBadRequest, gin.H{
  217. "error": fmt.Sprintln("数据库操作失败: ", err.Error()),
  218. })
  219. }
  220. c.JSON(http.StatusOK, gin.H{
  221. "success": true,
  222. "message": "已删除用户",
  223. })
  224. }