wechat.go 3.4 KB

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