mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	wechat login is ready
This commit is contained in:
		@@ -446,14 +446,9 @@ func (h *MidJourneyHandler) getData(finish bool, userId uint, page int, pageSize
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if item.Progress < 100 && item.ImgURL == "" && item.OrgURL != "" {
 | 
			
		||||
			// discord 服务器图片需要使用代理转发图片数据流
 | 
			
		||||
			if strings.HasPrefix(item.OrgURL, "https://cdn.discordapp.com") {
 | 
			
		||||
				image, err := utils.DownloadImage(item.OrgURL, h.App.Config.ProxyURL)
 | 
			
		||||
				if err == nil {
 | 
			
		||||
					job.ImgURL = "data:image/png;base64," + base64.StdEncoding.EncodeToString(image)
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				job.ImgURL = job.OrgURL
 | 
			
		||||
			image, err := utils.DownloadImage(item.OrgURL, h.App.Config.ProxyURL)
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				job.ImgURL = "data:image/png;base64," + base64.StdEncoding.EncodeToString(image)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -285,9 +285,99 @@ func (h *UserHandler) CLoginRequest(c *gin.Context) {
 | 
			
		||||
 | 
			
		||||
// CLoginCallback 第三方登录回调
 | 
			
		||||
func (h *UserHandler) CLoginCallback(c *gin.Context) {
 | 
			
		||||
	//platform := h.GetTrim(c, "type")
 | 
			
		||||
	//code := h.GetTrim(c, "code")
 | 
			
		||||
	loginType := h.GetTrim(c, "login_type")
 | 
			
		||||
	code := h.GetTrim(c, "code")
 | 
			
		||||
 | 
			
		||||
	var res types.BizVo
 | 
			
		||||
	apiURL := fmt.Sprintf("%s/api/clogin/info", h.App.Config.ApiConfig.ApiURL)
 | 
			
		||||
	r, err := req.C().R().SetBody(gin.H{"login_type": loginType, "code": code}).
 | 
			
		||||
		SetHeader("AppId", h.App.Config.ApiConfig.AppId).
 | 
			
		||||
		SetHeader("Authorization", fmt.Sprintf("Bearer %s", h.App.Config.ApiConfig.Token)).
 | 
			
		||||
		SetSuccessResult(&res).
 | 
			
		||||
		Post(apiURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		resp.ERROR(c, err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	if r.IsErrorState() {
 | 
			
		||||
		resp.ERROR(c, "error with login http status: "+r.Status)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if res.Code != types.Success {
 | 
			
		||||
		resp.ERROR(c, "error with http response: "+res.Message)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// login successfully
 | 
			
		||||
	data := res.Data.(map[string]interface{})
 | 
			
		||||
	session := gin.H{}
 | 
			
		||||
	var user model.User
 | 
			
		||||
	tx := h.DB.Debug().Where("openid", data["openid"]).First(&user)
 | 
			
		||||
	if tx.Error != nil { // user not exist, create new user
 | 
			
		||||
		// 检测最大注册人数
 | 
			
		||||
		var totalUser int64
 | 
			
		||||
		h.DB.Model(&model.User{}).Count(&totalUser)
 | 
			
		||||
		if h.licenseService.GetLicense().Configs.UserNum > 0 && int(totalUser) >= h.licenseService.GetLicense().Configs.UserNum {
 | 
			
		||||
			resp.ERROR(c, "当前注册用户数已达上限,请请升级 License")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		salt := utils.RandString(8)
 | 
			
		||||
		password := fmt.Sprintf("%d", utils.RandomNumber(8))
 | 
			
		||||
		user = model.User{
 | 
			
		||||
			Username:   fmt.Sprintf("%s@%d", loginType, utils.RandomNumber(10)),
 | 
			
		||||
			Password:   utils.GenPassword(password, salt),
 | 
			
		||||
			Avatar:     fmt.Sprintf("%s", data["avatar"]),
 | 
			
		||||
			Salt:       salt,
 | 
			
		||||
			Status:     true,
 | 
			
		||||
			ChatRoles:  utils.JsonEncode([]string{"gpt"}),               // 默认只订阅通用助手角色
 | 
			
		||||
			ChatModels: utils.JsonEncode(h.App.SysConfig.DefaultModels), // 默认开通的模型
 | 
			
		||||
			Power:      h.App.SysConfig.InitPower,
 | 
			
		||||
			OpenId:     fmt.Sprintf("%s", data["openid"]),
 | 
			
		||||
			Nickname:   fmt.Sprintf("%s", data["nickname"]),
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		tx = h.DB.Create(&user)
 | 
			
		||||
		if tx.Error != nil {
 | 
			
		||||
			resp.ERROR(c, "保存数据失败")
 | 
			
		||||
			logger.Error(tx.Error)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		session["username"] = user.Username
 | 
			
		||||
		session["password"] = password
 | 
			
		||||
	} else { // login directly
 | 
			
		||||
		// 更新最后登录时间和IP
 | 
			
		||||
		user.LastLoginIp = c.ClientIP()
 | 
			
		||||
		user.LastLoginAt = time.Now().Unix()
 | 
			
		||||
		h.DB.Model(&user).Updates(user)
 | 
			
		||||
 | 
			
		||||
		h.DB.Create(&model.UserLoginLog{
 | 
			
		||||
			UserId:       user.Id,
 | 
			
		||||
			Username:     user.Username,
 | 
			
		||||
			LoginIp:      c.ClientIP(),
 | 
			
		||||
			LoginAddress: utils.Ip2Region(h.searcher, c.ClientIP()),
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 创建 token
 | 
			
		||||
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
 | 
			
		||||
		"user_id": user.Id,
 | 
			
		||||
		"expired": time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge)).Unix(),
 | 
			
		||||
	})
 | 
			
		||||
	tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		resp.ERROR(c, "Failed to generate token, "+err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// 保存到 redis
 | 
			
		||||
	key := fmt.Sprintf("users/%d", user.Id)
 | 
			
		||||
	if _, err := h.redis.Set(c, key, tokenString, 0).Result(); err != nil {
 | 
			
		||||
		resp.ERROR(c, "error with save token: "+err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	session["token"] = tokenString
 | 
			
		||||
	resp.SUCCESS(c, session)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Session 获取/验证会话
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user