fix wxpayment and login

This commit is contained in:
liangzai 2025-04-26 15:10:01 +08:00
parent ad6f343473
commit d72ed7d0a3
12 changed files with 35 additions and 24 deletions

View File

@ -40,7 +40,7 @@ Alipay:
ReturnURL: "http://192.168.10.13:5679/payment/result" ReturnURL: "http://192.168.10.13:5679/payment/result"
Wxpay: Wxpay:
AppID: "wxba8424db4771cc18" AppID: "wx442ee1ac1ee75917"
MchID: "1682635136" MchID: "1682635136"
MchCertificateSerialNumber: "5369B8AEEBDCF7AF274510252E6A8C0659C30F61" MchCertificateSerialNumber: "5369B8AEEBDCF7AF274510252E6A8C0659C30F61"
MchApiv3Key: "e3ea4cf0765f1e71b01bb387dfcdbc9f" MchApiv3Key: "e3ea4cf0765f1e71b01bb387dfcdbc9f"

View File

@ -2,6 +2,7 @@ package agent
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"qnc-server/app/user/model" "qnc-server/app/user/model"
jwtx "qnc-server/common/jwt" jwtx "qnc-server/common/jwt"
@ -58,12 +59,12 @@ func (l *ApplyForAgentLogic) ApplyForAgent(req *types.AgentApplyReq) (resp *type
var userID int64 var userID int64
transErr := l.svcCtx.AgentAuditModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error { transErr := l.svcCtx.AgentAuditModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error {
// 两种情况1. 已注册账号然后申请代理 2. 未注册账号申请代理 // 两种情况1. 已注册账号然后申请代理 2. 未注册账号申请代理
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile) user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
if findUserErr != nil && !errors.Is(findUserErr, model.ErrNotFound) { if findUserErr != nil && !errors.Is(findUserErr, model.ErrNotFound) {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err) return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
} }
if user == nil { if user == nil {
user = &model.User{Mobile: encryptedMobile} user = &model.User{Mobile: sql.NullString{String: encryptedMobile, Valid: true}}
// if len(user.Nickname) == 0 { // if len(user.Nickname) == 0 {
// user.Nickname = encryptedMobile // user.Nickname = encryptedMobile
// } // }

View File

@ -2,6 +2,7 @@ package query
import ( import (
"context" "context"
"database/sql"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -1401,13 +1402,13 @@ func (l *QueryServiceLogic) GetOrCreateUser(mobile string) (int64, error) {
return userID, nil return userID, nil
} }
userModel, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, mobile) userModel, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: mobile, Valid: true})
if err != nil && !errors.Is(err, model.ErrNotFound) { if err != nil && !errors.Is(err, model.ErrNotFound) {
return 0, err return 0, err
} }
// 没有则创建账号 // 没有则创建账号
if userModel == nil { if userModel == nil {
userModel = &model.User{Mobile: mobile} userModel = &model.User{Mobile: sql.NullString{String: mobile, Valid: true}}
// if len(userModel.Nickname) == 0 { // if len(userModel.Nickname) == 0 {
// userModel.Nickname = mobile // userModel.Nickname = mobile
// } // }

View File

@ -2,6 +2,7 @@ package user
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"qnc-server/app/user/cmd/api/internal/svc" "qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types" "qnc-server/app/user/cmd/api/internal/types"
@ -51,12 +52,12 @@ func (l *AgentMobileCodeLoginLogic) AgentMobileCodeLogin(req *types.MobileCodeLo
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确") return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确")
} }
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile) user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
if findUserErr != nil && findUserErr != model.ErrNotFound { if findUserErr != nil && findUserErr != model.ErrNotFound {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
} }
if user == nil { if user == nil {
user = &model.User{Mobile: encryptedMobile} user = &model.User{Mobile: sql.NullString{String: encryptedMobile, Valid: true}}
// if len(user.Nickname) == 0 { // if len(user.Nickname) == 0 {
// user.Nickname = "" // user.Nickname = ""
// } // }

View File

@ -46,10 +46,12 @@ func (l *DetailLogic) Detail() (resp *types.UserInfoResp, err error) {
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, 用户信息结构体复制失败, %v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, 用户信息结构体复制失败, %v", err)
} }
userInfo.Mobile, err = crypto.DecryptMobile(userInfo.Mobile, l.svcCtx.Config.Encrypt.SecretKey) if user.Mobile.Valid {
userInfo.Mobile, err = crypto.DecryptMobile(user.Mobile.String, l.svcCtx.Config.Encrypt.SecretKey)
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, 解密手机号失败, %v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, 解密手机号失败, %v", err)
} }
}
return &types.UserInfoResp{ return &types.UserInfoResp{
UserInfo: userInfo, UserInfo: userInfo,
}, nil }, nil

View File

@ -2,6 +2,7 @@ package user
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"qnc-server/app/user/cmd/api/internal/svc" "qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types" "qnc-server/app/user/cmd/api/internal/types"
@ -53,12 +54,12 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r
} }
} }
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile) user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
if findUserErr != nil && findUserErr != model.ErrNotFound { if findUserErr != nil && findUserErr != model.ErrNotFound {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
} }
if user == nil { if user == nil {
user = &model.User{Mobile: encryptedMobile} user = &model.User{Mobile: sql.NullString{String: encryptedMobile, Valid: true}}
// if len(user.Nickname) == 0 { // if len(user.Nickname) == 0 {
// user.Nickname = encryptedMobile // user.Nickname = encryptedMobile
// } // }

View File

@ -2,6 +2,7 @@ package user
import ( import (
"context" "context"
"database/sql"
"qnc-server/app/user/model" "qnc-server/app/user/model"
jwtx "qnc-server/common/jwt" jwtx "qnc-server/common/jwt"
"qnc-server/common/tool" "qnc-server/common/tool"
@ -38,7 +39,7 @@ func (l *MobileLoginLogic) MobileLogin(req *types.MobileLoginReq) (resp *types.M
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 加密手机号失败: %+v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 加密手机号失败: %+v", err)
} }
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile) user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
if findUserErr != nil && findUserErr != model.ErrNotFound { if findUserErr != nil && findUserErr != model.ErrNotFound {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile%s, err: %+v", encryptedMobile, err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile%s, err: %+v", encryptedMobile, err)
} }

View File

@ -2,6 +2,7 @@ package user
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"qnc-server/app/user/cmd/api/internal/svc" "qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types" "qnc-server/app/user/cmd/api/internal/types"
@ -52,7 +53,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
if cacheCode != req.Code { if cacheCode != req.Code {
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机注册, 验证码不正确: %s", encryptedMobile) return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机注册, 验证码不正确: %s", encryptedMobile)
} }
hasUser, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile) hasUser, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
if findUserErr != nil && findUserErr != model.ErrNotFound { if findUserErr != nil && findUserErr != model.ErrNotFound {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 读取数据库获取用户失败, mobile%s, err: %+v", encryptedMobile, err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 读取数据库获取用户失败, mobile%s, err: %+v", encryptedMobile, err)
} }
@ -62,7 +63,7 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
var userId int64 var userId int64
if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error { if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
user := new(model.User) user := new(model.User)
user.Mobile = encryptedMobile user.Mobile = sql.NullString{String: encryptedMobile, Valid: true}
// if len(user.Nickname) == 0 { // if len(user.Nickname) == 0 {
// user.Nickname = encryptedMobile // user.Nickname = encryptedMobile
// } // }

View File

@ -40,7 +40,9 @@ func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthRe
if err != nil { if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err) return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err)
} }
if accessTokenResp.AccessToken == "" || accessTokenResp.Openid == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err)
}
// Step 2: 查找用户授权信息 // Step 2: 查找用户授权信息
userAuth, findErr := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxh5, accessTokenResp.Openid) userAuth, findErr := l.svcCtx.UserAuthModel.FindOneByAuthTypeAuthKey(l.ctx, model.UserAuthTypeWxh5, accessTokenResp.Openid)
if findErr != nil && !errors.Is(findErr, model.ErrNotFound) { if findErr != nil && !errors.Is(findErr, model.ErrNotFound) {
@ -59,7 +61,6 @@ func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthRe
} else { } else {
// 授权信息不存在,创建新用户 // 授权信息不存在,创建新用户
user = &model.User{} user = &model.User{}
user.Mobile = accessTokenResp.Openid
if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(context context.Context, session sqlx.Session) error { if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(context context.Context, session sqlx.Session) error {
// 插入数据库 // 插入数据库
insertResult, insertErr := l.svcCtx.UserModel.Insert(l.ctx, session, user) insertResult, insertErr := l.svcCtx.UserModel.Insert(l.ctx, session, user)
@ -108,7 +109,7 @@ type AccessTokenResp struct {
// GetAccessToken 通过code获取access_token // GetAccessToken 通过code获取access_token
func GetAccessToken(code string) (*AccessTokenResp, error) { func GetAccessToken(code string) (*AccessTokenResp, error) {
appID := "wxba8424db4771cc18" appID := "wx442ee1ac1ee75917"
appSecret := "c80474909db42f63913b7a307b3bee17" appSecret := "c80474909db42f63913b7a307b3bee17"
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code) url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code)

View File

@ -135,7 +135,6 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
if err != nil { if err != nil {
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode) return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
} }
// 返回预支付交易会话标识 // 返回预支付交易会话标识
return resp, nil return resp, nil
} }
@ -159,6 +158,9 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
return "", findAuthModelErr return "", findAuthModelErr
} }
prepayData, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, userAuthModel.AuthKey) prepayData, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, userAuthModel.AuthKey)
if err != nil {
return "", err
}
case "h5-weixin": case "h5-weixin":
userID, getUidErr := ctxdata.GetUidFromCtx(ctx) userID, getUidErr := ctxdata.GetUidFromCtx(ctx)
if getUidErr != nil { if getUidErr != nil {

View File

@ -34,7 +34,7 @@ type (
userModel interface { userModel interface {
Insert(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error) Insert(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*User, error) FindOne(ctx context.Context, id int64) (*User, error)
FindOneByMobile(ctx context.Context, mobile string) (*User, error) FindOneByMobile(ctx context.Context, mobile sql.NullString) (*User, error)
Update(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error) Update(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error)
UpdateWithVersion(ctx context.Context, session sqlx.Session, data *User) error UpdateWithVersion(ctx context.Context, session sqlx.Session, data *User) error
Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error Trans(ctx context.Context, fn func(context context.Context, session sqlx.Session) error) error
@ -62,7 +62,7 @@ type (
DeleteTime sql.NullTime `db:"delete_time"` // 删除时间 DeleteTime sql.NullTime `db:"delete_time"` // 删除时间
DelState int64 `db:"del_state"` DelState int64 `db:"del_state"`
Version int64 `db:"version"` // 版本号 Version int64 `db:"version"` // 版本号
Mobile string `db:"mobile"` Mobile sql.NullString `db:"mobile"`
Password sql.NullString `db:"password"` Password sql.NullString `db:"password"`
Nickname sql.NullString `db:"nickname"` Nickname sql.NullString `db:"nickname"`
Info string `db:"info"` Info string `db:"info"`
@ -107,7 +107,7 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error)
} }
} }
func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile string) (*User, error) { func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile sql.NullString) (*User, error) {
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, mobile) qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, mobile)
var resp User var resp User
err := m.QueryRowIndexCtx(ctx, &resp, qncUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { err := m.QueryRowIndexCtx(ctx, &resp, qncUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {

View File

@ -29,9 +29,9 @@ $tables = @(
# "product", # "product",
# "product_feature", # "product_feature",
# "query", # "query",
# "user" "user"
# "user_auth" # "user_auth"
"example" # "example"
) )
# 为每个表生成模型 # 为每个表生成模型