feat: fix issues

This commit is contained in:
Rogee
2025-04-30 15:59:42 +08:00
parent be605ef603
commit af0507d0c1
8 changed files with 60 additions and 21 deletions

View File

@@ -1,7 +1,6 @@
package http
import (
"fmt"
"net/url"
"time"
@@ -13,7 +12,6 @@ import (
"quyun/providers/wechat"
"github.com/gofiber/fiber/v3"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
@@ -43,6 +41,12 @@ func (ctl *auth) Login(ctx fiber.Ctx, code, state, redirect string) error {
}
log.Debugf("tokenInfo %+v", token)
stableToken, err := ctl.wechat.GetStableAccessToken()
if err != nil {
return errors.Wrap(err, "failed to get stable access token")
}
log.Infof("stableToken %+v", stableToken)
authUserInfo, err := ctl.wechat.AuthorizeUserInfo(token.AccessToken, token.Openid)
if err != nil {
return errors.Wrap(err, "failed to get user info")
@@ -53,8 +57,9 @@ func (ctl *auth) Login(ctx fiber.Ctx, code, state, redirect string) error {
userModel := &model.Users{
Status: fields.UserStatusOk,
OpenID: token.GetOpenID(),
Username: fmt.Sprintf("u_%s", gonanoid.MustGenerate(salt, 8)),
Avatar: nil,
// Username: fmt.Sprintf("u_%s", gonanoid.MustGenerate(salt, 8)),
Username: authUserInfo.Nickname,
Avatar: &authUserInfo.Headimgurl,
Metas: fields.ToJson(fields.UserMetas{
City: authUserInfo.City,
Country: authUserInfo.Country,
@@ -65,6 +70,8 @@ func (ctl *auth) Login(ctx fiber.Ctx, code, state, redirect string) error {
Sex: authUserInfo.Sex,
}),
AuthToken: fields.ToJson(fields.UserAuthToken{
StableAccessToken: stableToken.AccessToken,
StableExpiresAt: time.Now().Add(time.Second * time.Duration(stableToken.ExpiresIn)),
AccessToken: token.AccessToken,
ExpiresAt: time.Now().Add(time.Second * time.Duration(token.ExpiresIn)),
IsSnapshotuser: token.IsSnapshotuser,

View File

@@ -20,7 +20,7 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
return ctx.Next()
}
if f.app.IsDevMode() {
if f.app.IsDevMode() && false {
user, err := models.Users.GetByID(ctx.Context(), 1)
if err != nil {
return ctx.Send([]byte("User not found"))

View File

@@ -1,5 +1,6 @@
[App]
Mode = "development"
# Mode = "prod"
BaseURI = "baseURI"
StoragePath = "/Users/rogee/Projects/self/quyun/fixtures"
DistAdmin = "frontend/wechat/admin"
@@ -9,8 +10,8 @@ DistWeChat = "frontend/wechat/dist"
Port = 8088
[Database]
Host = "localhost"
Database = "postgres"
Host = "10.1.1.2"
Database = "quyun"
Password = "xixi0202"
[JWT]
@@ -34,11 +35,11 @@ Host ="https://assets.jdwan.com"
Region ="cn-beijing"
[WeChat]
DevMode = true
AppID = "wx47649361b6eba174"
AppSecret = "e9cdf19b006cd294a9dae7ad8ae08b72"
Token = "W8Xhw5TivYBgY"
EncodingAesKey = "OlgPgMvsl92zy5oErtEzRcziRT2txoN3jgEHV6RQZMY"
DevMode = false
[WeChat.Pay]
NotifyURL="https://www.baidu.com/go.php"

View File

@@ -17,6 +17,8 @@ type UserMetas struct {
}
type UserAuthToken struct {
StableAccessToken string `json:"stable_access_token,omitempty"`
StableExpiresAt time.Time `json:"stable_expires_at,omitempty"`
AccessToken string `json:"access_token,omitempty"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
IsSnapshotuser int64 `json:"is_snapshotuser,omitempty"`

View File

@@ -174,6 +174,25 @@ func (a *AuthorizeAccessToken) GetUnionID() string {
return a.Unionid
}
type StableAccessToken struct {
AccessToken string `json:"access_token,omitempty"`
ExpiresIn int64 `json:"expires_in,omitempty"`
}
func (we *Client) GetStableAccessToken() (*StableAccessToken, error) {
params := we.wrapParams(map[string]string{
"grant_type": "client_credential",
})
var data StableAccessToken
_, err := we.client.R().SetSuccessResult(&data).SetBodyJsonMarshal(params).Post("/cgi-bin/stable_token")
if err != nil {
return nil, errors.Wrap(err, "call /cgi-bin/stable_token failed")
}
return &data, nil
}
func (we *Client) AuthorizeCode2Token(code string) (*AuthorizeAccessToken, error) {
params := we.wrapParams(map[string]string{
"code": code,

View File

@@ -87,7 +87,7 @@ func TestClient_AuthorizeUserInfo(t *testing.T) {
func Test_GetJsTicket(t *testing.T) {
Convey("Test GetJsTicket", t, func() {
token := ""
token := "91_0pKuAiBFquPdLakDyhYqOyNJkGLr7-Egx-IF4bRzw-2Lpm7wxgz6zVBNJ36FvMXmiu8bz9BTtspVICf1zDZ3XWuVLwTq6T3a6WG1k6NHv6E0PadT-G5x2Y85-xUECBcADATRQ"
ticket, err := getClient().GetJSTicket(token)
So(err, ShouldBeNil)
So(ticket, ShouldNotBeEmpty)
@@ -95,3 +95,13 @@ func Test_GetJsTicket(t *testing.T) {
t.Log("Js Ticket:", ticket)
})
}
func Test_GetStableToken(t *testing.T) {
Convey("Test_GetStableToken GetJsTicket", t, func() {
token, err := getClient().GetStableAccessToken()
So(err, ShouldBeNil)
So(token, ShouldNotBeNil)
t.Logf("Stable Token: %+v", token)
})
}

Binary file not shown.

BIN
frontend/wechat/bun.lockb Executable file

Binary file not shown.