feat: fix issues
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,7 +12,6 @@ import (
|
|||||||
"quyun/providers/wechat"
|
"quyun/providers/wechat"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
gonanoid "github.com/matoous/go-nanoid/v2"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
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)
|
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)
|
authUserInfo, err := ctl.wechat.AuthorizeUserInfo(token.AccessToken, token.Openid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed to get user info")
|
return errors.Wrap(err, "failed to get user info")
|
||||||
@@ -51,10 +55,11 @@ func (ctl *auth) Login(ctx fiber.Ctx, code, state, redirect string) error {
|
|||||||
log.Debugf("Auth User Info: %+v", authUserInfo)
|
log.Debugf("Auth User Info: %+v", authUserInfo)
|
||||||
|
|
||||||
userModel := &model.Users{
|
userModel := &model.Users{
|
||||||
Status: fields.UserStatusOk,
|
Status: fields.UserStatusOk,
|
||||||
OpenID: token.GetOpenID(),
|
OpenID: token.GetOpenID(),
|
||||||
Username: fmt.Sprintf("u_%s", gonanoid.MustGenerate(salt, 8)),
|
// Username: fmt.Sprintf("u_%s", gonanoid.MustGenerate(salt, 8)),
|
||||||
Avatar: nil,
|
Username: authUserInfo.Nickname,
|
||||||
|
Avatar: &authUserInfo.Headimgurl,
|
||||||
Metas: fields.ToJson(fields.UserMetas{
|
Metas: fields.ToJson(fields.UserMetas{
|
||||||
City: authUserInfo.City,
|
City: authUserInfo.City,
|
||||||
Country: authUserInfo.Country,
|
Country: authUserInfo.Country,
|
||||||
@@ -65,11 +70,13 @@ func (ctl *auth) Login(ctx fiber.Ctx, code, state, redirect string) error {
|
|||||||
Sex: authUserInfo.Sex,
|
Sex: authUserInfo.Sex,
|
||||||
}),
|
}),
|
||||||
AuthToken: fields.ToJson(fields.UserAuthToken{
|
AuthToken: fields.ToJson(fields.UserAuthToken{
|
||||||
AccessToken: token.AccessToken,
|
StableAccessToken: stableToken.AccessToken,
|
||||||
ExpiresAt: time.Now().Add(time.Second * time.Duration(token.ExpiresIn)),
|
StableExpiresAt: time.Now().Add(time.Second * time.Duration(stableToken.ExpiresIn)),
|
||||||
IsSnapshotuser: token.IsSnapshotuser,
|
AccessToken: token.AccessToken,
|
||||||
RefreshToken: token.RefreshToken,
|
ExpiresAt: time.Now().Add(time.Second * time.Duration(token.ExpiresIn)),
|
||||||
Scope: token.Scope,
|
IsSnapshotuser: token.IsSnapshotuser,
|
||||||
|
RefreshToken: token.RefreshToken,
|
||||||
|
Scope: token.Scope,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
user, err := models.Users.GetUserByOpenIDOrCreate(ctx.Context(), token.GetOpenID(), userModel)
|
user, err := models.Users.GetUserByOpenIDOrCreate(ctx.Context(), token.GetOpenID(), userModel)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
|
|||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.app.IsDevMode() {
|
if f.app.IsDevMode() && false {
|
||||||
user, err := models.Users.GetByID(ctx.Context(), 1)
|
user, err := models.Users.GetByID(ctx.Context(), 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.Send([]byte("User not found"))
|
return ctx.Send([]byte("User not found"))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
[App]
|
[App]
|
||||||
Mode = "development"
|
Mode = "development"
|
||||||
|
# Mode = "prod"
|
||||||
BaseURI = "baseURI"
|
BaseURI = "baseURI"
|
||||||
StoragePath = "/Users/rogee/Projects/self/quyun/fixtures"
|
StoragePath = "/Users/rogee/Projects/self/quyun/fixtures"
|
||||||
DistAdmin = "frontend/wechat/admin"
|
DistAdmin = "frontend/wechat/admin"
|
||||||
@@ -9,8 +10,8 @@ DistWeChat = "frontend/wechat/dist"
|
|||||||
Port = 8088
|
Port = 8088
|
||||||
|
|
||||||
[Database]
|
[Database]
|
||||||
Host = "localhost"
|
Host = "10.1.1.2"
|
||||||
Database = "postgres"
|
Database = "quyun"
|
||||||
Password = "xixi0202"
|
Password = "xixi0202"
|
||||||
|
|
||||||
[JWT]
|
[JWT]
|
||||||
@@ -34,11 +35,11 @@ Host ="https://assets.jdwan.com"
|
|||||||
Region ="cn-beijing"
|
Region ="cn-beijing"
|
||||||
|
|
||||||
[WeChat]
|
[WeChat]
|
||||||
|
DevMode = true
|
||||||
AppID = "wx47649361b6eba174"
|
AppID = "wx47649361b6eba174"
|
||||||
AppSecret = "e9cdf19b006cd294a9dae7ad8ae08b72"
|
AppSecret = "e9cdf19b006cd294a9dae7ad8ae08b72"
|
||||||
Token = "W8Xhw5TivYBgY"
|
Token = "W8Xhw5TivYBgY"
|
||||||
EncodingAesKey = "OlgPgMvsl92zy5oErtEzRcziRT2txoN3jgEHV6RQZMY"
|
EncodingAesKey = "OlgPgMvsl92zy5oErtEzRcziRT2txoN3jgEHV6RQZMY"
|
||||||
DevMode = false
|
|
||||||
|
|
||||||
[WeChat.Pay]
|
[WeChat.Pay]
|
||||||
NotifyURL="https://www.baidu.com/go.php"
|
NotifyURL="https://www.baidu.com/go.php"
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ type UserMetas struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserAuthToken struct {
|
type UserAuthToken struct {
|
||||||
AccessToken string `json:"access_token,omitempty"`
|
StableAccessToken string `json:"stable_access_token,omitempty"`
|
||||||
ExpiresAt time.Time `json:"expires_at,omitempty"`
|
StableExpiresAt time.Time `json:"stable_expires_at,omitempty"`
|
||||||
IsSnapshotuser int64 `json:"is_snapshotuser,omitempty"`
|
AccessToken string `json:"access_token,omitempty"`
|
||||||
RefreshToken string `json:"refresh_token,omitempty"`
|
ExpiresAt time.Time `json:"expires_at,omitempty"`
|
||||||
Scope string `json:"scope,omitempty"`
|
IsSnapshotuser int64 `json:"is_snapshotuser,omitempty"`
|
||||||
|
RefreshToken string `json:"refresh_token,omitempty"`
|
||||||
|
Scope string `json:"scope,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,6 +174,25 @@ func (a *AuthorizeAccessToken) GetUnionID() string {
|
|||||||
return a.Unionid
|
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) {
|
func (we *Client) AuthorizeCode2Token(code string) (*AuthorizeAccessToken, error) {
|
||||||
params := we.wrapParams(map[string]string{
|
params := we.wrapParams(map[string]string{
|
||||||
"code": code,
|
"code": code,
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func TestClient_AuthorizeUserInfo(t *testing.T) {
|
|||||||
|
|
||||||
func Test_GetJsTicket(t *testing.T) {
|
func Test_GetJsTicket(t *testing.T) {
|
||||||
Convey("Test GetJsTicket", t, func() {
|
Convey("Test GetJsTicket", t, func() {
|
||||||
token := ""
|
token := "91_0pKuAiBFquPdLakDyhYqOyNJkGLr7-Egx-IF4bRzw-2Lpm7wxgz6zVBNJ36FvMXmiu8bz9BTtspVICf1zDZ3XWuVLwTq6T3a6WG1k6NHv6E0PadT-G5x2Y85-xUECBcADATRQ"
|
||||||
ticket, err := getClient().GetJSTicket(token)
|
ticket, err := getClient().GetJSTicket(token)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(ticket, ShouldNotBeEmpty)
|
So(ticket, ShouldNotBeEmpty)
|
||||||
@@ -95,3 +95,13 @@ func Test_GetJsTicket(t *testing.T) {
|
|||||||
t.Log("Js Ticket:", ticket)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
BIN
backend/quyun
BIN
backend/quyun
Binary file not shown.
BIN
frontend/wechat/bun.lockb
Executable file
BIN
frontend/wechat/bun.lockb
Executable file
Binary file not shown.
Reference in New Issue
Block a user