wechat.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. err := user.FillUserByWeChatId()
  70. if err != nil {
  71. c.JSON(http.StatusOK, gin.H{
  72. "success": false,
  73. "message": err.Error(),
  74. })
  75. return
  76. }
  77. } else {
  78. if common.RegisterEnabled {
  79. user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
  80. user.DisplayName = "WeChat User"
  81. user.Role = common.RoleCommonUser
  82. user.Status = common.UserStatusEnabled
  83. if err := user.Insert(); err != nil {
  84. c.JSON(http.StatusOK, gin.H{
  85. "success": false,
  86. "message": err.Error(),
  87. })
  88. return
  89. }
  90. } else {
  91. c.JSON(http.StatusOK, gin.H{
  92. "success": false,
  93. "message": "管理员关闭了新用户注册",
  94. })
  95. return
  96. }
  97. }
  98. if user.Status != common.UserStatusEnabled {
  99. c.JSON(http.StatusOK, gin.H{
  100. "message": "用户已被封禁",
  101. "success": false,
  102. })
  103. return
  104. }
  105. setupLogin(&user, c)
  106. }
  107. func WeChatBind(c *gin.Context) {
  108. if !common.WeChatAuthEnabled {
  109. c.JSON(http.StatusOK, gin.H{
  110. "message": "管理员未开启通过微信登录以及注册",
  111. "success": false,
  112. })
  113. return
  114. }
  115. code := c.Query("code")
  116. wechatId, err := getWeChatIdByCode(code)
  117. if err != nil {
  118. c.JSON(http.StatusOK, gin.H{
  119. "message": err.Error(),
  120. "success": false,
  121. })
  122. return
  123. }
  124. if model.IsWeChatIdAlreadyTaken(wechatId) {
  125. c.JSON(http.StatusOK, gin.H{
  126. "success": false,
  127. "message": "该微信账号已被绑定",
  128. })
  129. return
  130. }
  131. id := c.GetInt("id")
  132. user := model.User{
  133. Id: id,
  134. }
  135. err = user.FillUserById()
  136. if err != nil {
  137. c.JSON(http.StatusOK, gin.H{
  138. "success": false,
  139. "message": err.Error(),
  140. })
  141. return
  142. }
  143. user.WeChatId = wechatId
  144. err = user.Update(false)
  145. if err != nil {
  146. c.JSON(http.StatusOK, gin.H{
  147. "success": false,
  148. "message": err.Error(),
  149. })
  150. return
  151. }
  152. c.JSON(http.StatusOK, gin.H{
  153. "success": true,
  154. "message": "",
  155. })
  156. return
  157. }