微信注册和绑定增加nickname
This commit is contained in:
parent
21d56e0d30
commit
faaa9a476a
@ -16,15 +16,16 @@ type wechatLoginResponse struct {
|
|||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
|
Nickname string `json:"nickname"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWeChatIdByCode(code string) (string, error) {
|
func getWeChatIdByCode(code string) (string, string, error) {
|
||||||
if code == "" {
|
if code == "" {
|
||||||
return "", errors.New("无效的参数")
|
return "", "", errors.New("无效的参数")
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/wechat/user?code=%s", common.WeChatServerAddress, code), nil)
|
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/wechat/user?code=%s", common.WeChatServerAddress, code), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
req.Header.Set("Authorization", common.WeChatServerToken)
|
req.Header.Set("Authorization", common.WeChatServerToken)
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
@ -32,21 +33,21 @@ func getWeChatIdByCode(code string) (string, error) {
|
|||||||
}
|
}
|
||||||
httpResponse, err := client.Do(req)
|
httpResponse, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
defer httpResponse.Body.Close()
|
defer httpResponse.Body.Close()
|
||||||
var res wechatLoginResponse
|
var res wechatLoginResponse
|
||||||
err = json.NewDecoder(httpResponse.Body).Decode(&res)
|
err = json.NewDecoder(httpResponse.Body).Decode(&res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
if !res.Success {
|
if !res.Success {
|
||||||
return "", errors.New(res.Message)
|
return "", "", errors.New(res.Message)
|
||||||
}
|
}
|
||||||
if res.Data == "" {
|
if res.Data == "" {
|
||||||
return "", errors.New("验证码错误或已过期")
|
return "", "", errors.New("验证码错误或已过期")
|
||||||
}
|
}
|
||||||
return res.Data, nil
|
return res.Data, res.Nickname, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WeChatAuth(c *gin.Context) {
|
func WeChatAuth(c *gin.Context) {
|
||||||
@ -58,7 +59,7 @@ func WeChatAuth(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
code := c.Query("code")
|
code := c.Query("code")
|
||||||
wechatId, err := getWeChatIdByCode(code)
|
wechatId, nickname, err := getWeChatIdByCode(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
@ -81,7 +82,7 @@ func WeChatAuth(c *gin.Context) {
|
|||||||
} else {
|
} else {
|
||||||
if common.RegisterEnabled {
|
if common.RegisterEnabled {
|
||||||
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
|
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
|
||||||
user.DisplayName = "WeChat User"
|
user.DisplayName = nickname
|
||||||
user.Role = common.RoleCommonUser
|
user.Role = common.RoleCommonUser
|
||||||
user.Status = common.UserStatusEnabled
|
user.Status = common.UserStatusEnabled
|
||||||
// 开始处理邀请码逻辑 类似 https://aiapi2d.com/login?aff=XqgY 带邀请码的链接,按照邀请码寻找邀请者。
|
// 开始处理邀请码逻辑 类似 https://aiapi2d.com/login?aff=XqgY 带邀请码的链接,按照邀请码寻找邀请者。
|
||||||
@ -128,7 +129,7 @@ func WeChatBind(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
code := c.Query("code")
|
code := c.Query("code")
|
||||||
wechatId, err := getWeChatIdByCode(code)
|
wechatId, nickname, err := getWeChatIdByCode(code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
@ -156,6 +157,7 @@ func WeChatBind(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.WeChatId = wechatId
|
user.WeChatId = wechatId
|
||||||
|
user.DisplayName = nickname
|
||||||
err = user.Update(false)
|
err = user.Update(false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
Loading…
Reference in New Issue
Block a user