mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-02 11:20:18 +08:00
feat: support WeChat Pay via JSAPI (#2488)
* feat: support wechat jsapi payment * feat: add log * feat: update sign * feat: process wechat pay result * feat: process wechat pay result * feat: save wechat openid for different app * feat: save wechat openid for different app * feat: add SetUserOAuthProperties for signup * feat: fix openid for wechat * feat: get user extra property in buyproduct * feat: remove log * feat: remove log * feat: gofumpt code * feat: change lr->crlf * feat: change crlf->lf * feat: improve code
This commit is contained in:
@ -20,6 +20,8 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
"github.com/casdoor/casdoor/idp"
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"github.com/xorm-io/core"
|
||||
@ -142,6 +144,25 @@ func setUserProperty(user *User, field string, value string) {
|
||||
}
|
||||
}
|
||||
|
||||
func getUserProperty(user *User, field string) string {
|
||||
if user.Properties == nil {
|
||||
return ""
|
||||
}
|
||||
return user.Properties[field]
|
||||
}
|
||||
|
||||
func getUserExtraProperty(user *User, providerType, key string) (string, error) {
|
||||
extraJson := getUserProperty(user, fmt.Sprintf("oauth_%s_extra", providerType))
|
||||
if extraJson == "" {
|
||||
return "", nil
|
||||
}
|
||||
extra := make(map[string]string)
|
||||
if err := jsoniter.Unmarshal([]byte(extraJson), &extra); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return extra[key], nil
|
||||
}
|
||||
|
||||
func SetUserOAuthProperties(organization *Organization, user *User, providerType string, userInfo *idp.UserInfo) (bool, error) {
|
||||
if userInfo.Id != "" {
|
||||
propertyName := fmt.Sprintf("oauth_%s_id", providerType)
|
||||
@ -185,6 +206,27 @@ func SetUserOAuthProperties(organization *Organization, user *User, providerType
|
||||
}
|
||||
}
|
||||
|
||||
if userInfo.Extra != nil {
|
||||
// Save extra info as json string
|
||||
propertyName := fmt.Sprintf("oauth_%s_extra", providerType)
|
||||
oldExtraJson := getUserProperty(user, propertyName)
|
||||
extra := make(map[string]string)
|
||||
if oldExtraJson != "" {
|
||||
if err := jsoniter.Unmarshal([]byte(oldExtraJson), &extra); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
for k, v := range userInfo.Extra {
|
||||
extra[k] = v
|
||||
}
|
||||
|
||||
newExtraJson, err := jsoniter.Marshal(extra)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
setUserProperty(user, propertyName, string(newExtraJson))
|
||||
}
|
||||
|
||||
return UpdateUserForAllFields(user.GetId(), user)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user