wechat.go 3.7 KB

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