fonchain-fiee/pkg/service/auth/sso/wechat.go

160 lines
3.4 KiB
Go
Raw Normal View History

2025-02-19 06:24:15 +00:00
package sso
import (
"fmt"
"github.com/fonchain_enterprise/fonchain-main/api/account"
"github.com/fonchain_enterprise/fonchain-main/pkg/cache"
"github.com/fonchain_enterprise/fonchain-main/pkg/config"
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
"github.com/fonchain_enterprise/fonchain-main/pkg/service"
"github.com/fonchain_enterprise/fonchain-main/pkg/utils/secret"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"io/ioutil"
"net/http"
"net/url"
"time"
)
var ghid = "gh_d838598b1a23"
func WechatNowPhone(c *gin.Context) {
wxInfo, err := getOpenIdFromCookie(c)
if err != nil {
service.Error(c, e.InvalidParams, err)
return
}
service.Success(c, wxInfo)
return
}
func WechatBind(c *gin.Context) {
var req account.LoginRequest
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
service.Error(c, e.InvalidParams, err)
return
}
wxBoxInfo, err := getOpenIdFromCookie(c)
if err != nil {
service.Error(c, e.InvalidParams, err)
return
}
req.Ip = c.ClientIP()
info, err := service.AccountProvider.Login(c, &req)
if err != nil {
service.Error(c, e.InvalidParams, err)
return
}
//获取openid
wxUpdate := &account.WxUserUpdateRequest{
WxID: wxBoxInfo.WxUserId,
UserID: uint32(info.AccountInfo.ID),
}
updateRes, err := service.AccountProvider.WxUserUpdate(c, wxUpdate)
if err != nil {
service.Error(c, e.InvalidParams, err)
return
}
service.Success(c, updateRes)
return
}
func WechatLoginHtml(c *gin.Context) {
//是否有
scode, _ := c.Cookie("scode")
code := c.Query("code")
if scode == "" && code == "" { //重定向到
c.Redirect(http.StatusFound, "https://appointteam.szjixun.cn/api/appointment/auth/wx?notifyUrl="+url.QueryEscape(config.ApiHost+c.Request.URL.Path))
return
}
if code != "" {
openIdReq := &account.WxGetOpenIdByCodeRequest{Code: code, GhId: ghid}
fmt.Println(openIdReq)
openIdRes, err := service.AccountProvider.WxGetOpenIdByCode(c, openIdReq)
fmt.Println(openIdRes, err)
if err != nil {
service.Error(c, e.Error, err)
return
}
//创建 gitea
wxCreate := &account.WxUserOrCreateRequest{
OpenID: openIdRes.OpenId,
GhID: ghid,
}
_, err = service.AccountProvider.WxUserCreate(c, wxCreate)
if err != nil {
service.Error(c, e.InvalidParams, err)
return
}
scode, err = secret.CommonCombineSecret(openIdRes.OpenId)
if err != nil {
service.Error(c, e.InvalidParams, err)
return
}
c.SetCookie("scode", scode, 43200, "/", "", false, true)
c.Redirect(http.StatusFound, c.Request.URL.Path) //重定向到无code位置
return
}
//如果不存在同时 code和statue不存在则跳转
key := cache.GetSsoHtml("wechat")
exists := cache.RedisClient.Exists(key).Val()
if exists != 1 {
b, err := ioutil.ReadFile("./data/static/bind.html")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
cache.RedisClient.Set(key, string(b), 300*time.Second)
}
htmlContent := cache.RedisClient.Get(key).Val()
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(htmlContent))
return
}
func getOpenIdFromCookie(c *gin.Context) (*account.WxBoxUserInfo, error) {
scode, err := c.Cookie("scode")
if err != nil {
return nil, err
}
openId, err := secret.CommonDec(scode)
if err != nil {
return nil, err
}
wxUserInfo := &account.WxBoxUserInfoRequest{Openid: openId, GhId: ghid}
return service.AccountProvider.WxBoxUserInfoByOpenId(c, wxUserInfo)
}