wechat.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package controller
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "message-pusher/common"
  8. "message-pusher/model"
  9. "net/http"
  10. "strconv"
  11. "time"
  12. )
  13. type wechatLoginResponse struct {
  14. Success bool `json:"success"`
  15. Message string `json:"message"`
  16. Data string `json:"data"`
  17. }
  18. func getWeChatIdByCode(code string) (string, error) {
  19. if code == "" {
  20. return "", errors.New("无效的参数")
  21. }
  22. req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/wechat/user?code=%s", common.WeChatServerAddress, code), nil)
  23. if err != nil {
  24. return "", err
  25. }
  26. req.Header.Set("Authorization", common.WeChatServerToken)
  27. client := http.Client{
  28. Timeout: 5 * time.Second,
  29. }
  30. httpResponse, err := client.Do(req)
  31. if err != nil {
  32. return "", err
  33. }
  34. defer httpResponse.Body.Close()
  35. var res wechatLoginResponse
  36. err = json.NewDecoder(httpResponse.Body).Decode(&res)
  37. if err != nil {
  38. return "", err
  39. }
  40. if !res.Success {
  41. return "", errors.New(res.Message)
  42. }
  43. if res.Data == "" {
  44. return "", errors.New("验证码错误或已过期")
  45. }
  46. return res.Data, nil
  47. }
  48. func WeChatAuth(c *gin.Context) {
  49. if !common.WeChatAuthEnabled {
  50. c.JSON(http.StatusOK, gin.H{
  51. "message": "管理员未开启通过微信登录以及注册",
  52. "success": false,
  53. })
  54. return
  55. }
  56. code := c.Query("code")
  57. wechatId, err := getWeChatIdByCode(code)
  58. if err != nil {
  59. c.JSON(http.StatusOK, gin.H{
  60. "message": err.Error(),
  61. "success": false,
  62. })
  63. return
  64. }
  65. user := model.User{
  66. WeChatId: wechatId,
  67. }
  68. if model.IsWeChatIdAlreadyTaken(wechatId) {
  69. user.FillUserByWeChatId()
  70. } else {
  71. if common.RegisterEnabled {
  72. user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
  73. user.DisplayName = "WeChat User"
  74. user.Role = common.RoleCommonUser
  75. user.Status = common.UserStatusEnabled
  76. if err := user.Insert(); err != nil {
  77. c.JSON(http.StatusOK, gin.H{
  78. "success": false,
  79. "message": err.Error(),
  80. })
  81. return
  82. }
  83. } else {
  84. c.JSON(http.StatusOK, gin.H{
  85. "success": false,
  86. "message": "管理员关闭了新用户注册",
  87. })
  88. return
  89. }
  90. }
  91. if user.Status != common.UserStatusEnabled {
  92. c.JSON(http.StatusOK, gin.H{
  93. "message": "用户已被封禁",
  94. "success": false,
  95. })
  96. return
  97. }
  98. setupLogin(&user, c)
  99. }
  100. func WeChatBind(c *gin.Context) {
  101. if !common.WeChatAuthEnabled {
  102. c.JSON(http.StatusOK, gin.H{
  103. "message": "管理员未开启通过微信登录以及注册",
  104. "success": false,
  105. })
  106. return
  107. }
  108. code := c.Query("code")
  109. wechatId, err := getWeChatIdByCode(code)
  110. if err != nil {
  111. c.JSON(http.StatusOK, gin.H{
  112. "message": err.Error(),
  113. "success": false,
  114. })
  115. return
  116. }
  117. if model.IsWeChatIdAlreadyTaken(wechatId) {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": "该微信账号已被绑定",
  121. })
  122. return
  123. }
  124. id := c.GetInt("id")
  125. user := model.User{
  126. Id: id,
  127. }
  128. user.FillUserById()
  129. user.WeChatId = wechatId
  130. err = user.Update(false)
  131. if err != nil {
  132. c.JSON(http.StatusOK, gin.H{
  133. "success": false,
  134. "message": err.Error(),
  135. })
  136. return
  137. }
  138. c.JSON(http.StatusOK, gin.H{
  139. "success": true,
  140. "message": "",
  141. })
  142. return
  143. }