user_controllers.go 6.4 KB

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