feat(auth): add mini program test code endpoint (#51)
This commit is contained in:
@@ -155,6 +155,11 @@ type updateProfileRequest struct {
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
}
|
||||
|
||||
type miniProgramCodeQuery struct {
|
||||
Path string `form:"path"`
|
||||
Width int `form:"width"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) UpdateProfile(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
@@ -181,3 +186,23 @@ func (h *AuthHandler) UpdateProfile(c *gin.Context) {
|
||||
"avatar_url": updated.AvatarURL,
|
||||
}))
|
||||
}
|
||||
|
||||
func (h *AuthHandler) GetMiniProgramTestCode(c *gin.Context) {
|
||||
user := middleware.MustCurrentUser(c)
|
||||
|
||||
var query miniProgramCodeQuery
|
||||
if err := c.ShouldBindQuery(&query); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "参数错误"))
|
||||
return
|
||||
}
|
||||
|
||||
codeBytes, err := h.authService.GetMiniProgramTestCode(c.Request.Context(), user.ID, query.Path, query.Width)
|
||||
if err != nil {
|
||||
log.Printf("[get_mini_program_test_code] error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取小程序码失败"))
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("Cache-Control", "no-store")
|
||||
c.Data(http.StatusOK, "image/png", codeBytes)
|
||||
}
|
||||
|
||||
@@ -265,3 +265,32 @@ func normalizeAvatarURL(raw string) string {
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (s *AuthService) GetMiniProgramTestCode(ctx context.Context, userID uint, path string, width int) ([]byte, error) {
|
||||
var user model.User
|
||||
if err := s.db.WithContext(ctx).Select("id, mini_program_id").First(&user, userID).Error; err != nil {
|
||||
return nil, fmt.Errorf("find user: %w", err)
|
||||
}
|
||||
if user.MiniProgramID == 0 {
|
||||
return nil, fmt.Errorf("user mini program id missing")
|
||||
}
|
||||
|
||||
miniProgram, err := s.miniProgramSvc.GetByID(ctx, user.MiniProgramID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load mini program: %w", err)
|
||||
}
|
||||
|
||||
if strings.TrimSpace(path) == "" {
|
||||
path = "pages/nsti/test?resume=0"
|
||||
}
|
||||
if width <= 0 {
|
||||
width = 280
|
||||
}
|
||||
|
||||
client := s.getWeChatClient(miniProgram)
|
||||
codeBytes, err := client.GetWXACode(ctx, path, width)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get mini program test code: %w", err)
|
||||
}
|
||||
return codeBytes, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"wx_service/internal/model"
|
||||
)
|
||||
|
||||
func newAuthTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(&model.User{}); err != nil {
|
||||
t.Fatalf("auto migrate: %v", err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func TestAuthServiceUpdateProfilePersistsUserFields(t *testing.T) {
|
||||
db := newAuthTestDB(t)
|
||||
svc := NewAuthService(db, nil)
|
||||
|
||||
user := model.User{
|
||||
MiniProgramID: 1,
|
||||
OpenID: "openid-1",
|
||||
NickName: "旧昵称",
|
||||
AvatarURL: "https://example.com/old.png",
|
||||
SessionKey: "session-1",
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
t.Fatalf("seed user: %v", err)
|
||||
}
|
||||
|
||||
updated, err := svc.UpdateProfile(context.Background(), user.ID, "新昵称", "https://example.com/new.png")
|
||||
if err != nil {
|
||||
t.Fatalf("update profile: %v", err)
|
||||
}
|
||||
|
||||
if updated.NickName != "新昵称" {
|
||||
t.Fatalf("expected updated nickname, got %q", updated.NickName)
|
||||
}
|
||||
if updated.AvatarURL != "https://example.com/new.png" {
|
||||
t.Fatalf("expected updated avatar, got %q", updated.AvatarURL)
|
||||
}
|
||||
|
||||
var persisted model.User
|
||||
if err := db.First(&persisted, user.ID).Error; err != nil {
|
||||
t.Fatalf("reload user: %v", err)
|
||||
}
|
||||
|
||||
if persisted.NickName != "新昵称" {
|
||||
t.Fatalf("expected persisted nickname, got %q", persisted.NickName)
|
||||
}
|
||||
if persisted.AvatarURL != "https://example.com/new.png" {
|
||||
t.Fatalf("expected persisted avatar, got %q", persisted.AvatarURL)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,19 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
const weChatCode2SessionURL = "https://api.weixin.qq.com/sns/jscode2session"
|
||||
const weChatAccessTokenURL = "https://api.weixin.qq.com/cgi-bin/token"
|
||||
const weChatGetWXACodeURL = "https://api.weixin.qq.com/wxa/getwxacode"
|
||||
|
||||
// WeChatClient 调用微信接口获取 session/openid。
|
||||
type WeChatClient struct {
|
||||
@@ -30,6 +34,13 @@ type weChatSessionResponse struct {
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
type weChatAccessTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
// WeChatError 表示微信接口级错误。
|
||||
type WeChatError struct {
|
||||
Code int
|
||||
@@ -87,3 +98,98 @@ func (c *WeChatClient) Code2Session(ctx context.Context, code string) (*WeChatSe
|
||||
|
||||
return &raw.WeChatSession, nil
|
||||
}
|
||||
|
||||
func (c *WeChatClient) GetAccessToken(ctx context.Context) (string, error) {
|
||||
query := url.Values{}
|
||||
query.Set("grant_type", "client_credential")
|
||||
query.Set("appid", c.appID)
|
||||
query.Set("secret", c.appSecret)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s?%s", weChatAccessTokenURL, query.Encode()), nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build access token request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("call wechat access token api: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("wechat access token api unexpected status: %s", resp.Status)
|
||||
}
|
||||
|
||||
var raw weChatAccessTokenResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil {
|
||||
return "", fmt.Errorf("decode access token response: %w", err)
|
||||
}
|
||||
if raw.ErrCode != 0 {
|
||||
return "", &WeChatError{Code: raw.ErrCode, Msg: raw.ErrMsg}
|
||||
}
|
||||
if raw.AccessToken == "" {
|
||||
return "", fmt.Errorf("wechat access token missing")
|
||||
}
|
||||
return raw.AccessToken, nil
|
||||
}
|
||||
|
||||
type getWXACodeRequest struct {
|
||||
Path string `json:"path"`
|
||||
Width int `json:"width,omitempty"`
|
||||
AutoColor bool `json:"auto_color"`
|
||||
IsHyaline bool `json:"is_hyaline"`
|
||||
}
|
||||
|
||||
type weChatAPIErrorResponse struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
|
||||
func (c *WeChatClient) GetWXACode(ctx context.Context, path string, width int) ([]byte, error) {
|
||||
accessToken, err := c.GetAccessToken(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := json.Marshal(getWXACodeRequest{
|
||||
Path: path,
|
||||
Width: width,
|
||||
AutoColor: false,
|
||||
IsHyaline: true,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal getwxacode request: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf("%s?access_token=%s", weChatGetWXACodeURL, url.QueryEscape(accessToken)),
|
||||
bytes.NewReader(body),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build getwxacode request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("call wechat getwxacode api: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
payload, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read getwxacode response: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("wechat getwxacode api unexpected status: %s", resp.Status)
|
||||
}
|
||||
|
||||
var apiErr weChatAPIErrorResponse
|
||||
if err := json.Unmarshal(payload, &apiErr); err == nil && apiErr.ErrCode != 0 {
|
||||
return nil, &WeChatError{Code: apiErr.ErrCode, Msg: apiErr.ErrMsg}
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user