|
@@ -12,6 +12,7 @@ import (
|
|
|
"net/http"
|
|
|
"net/http/cookiejar"
|
|
|
"net/url"
|
|
|
+ "regexp"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
@@ -21,12 +22,76 @@ type sussBi struct {
|
|
|
pwd string
|
|
|
Url *url.URL
|
|
|
jar *cookiejar.Jar
|
|
|
+ prm *ParamReplaceManager
|
|
|
}
|
|
|
|
|
|
-func InitSussBi(address, user, password string) *sussBi {
|
|
|
- sussCookie, _ := cookiejar.New(&cookiejar.Options{
|
|
|
+//参数替换
|
|
|
+type ParamReplace struct {
|
|
|
+ Replace []ParamReplaceSetting
|
|
|
+ Match []ParamReplaceSetting
|
|
|
+}
|
|
|
+
|
|
|
+type ParamReplaceSetting struct {
|
|
|
+ Key, Value string
|
|
|
+}
|
|
|
+
|
|
|
+type ParamReplaceManager struct {
|
|
|
+ eqRouters map[string]*ParamReplace
|
|
|
+ regexRouter map[*regexp.Regexp]*ParamReplace
|
|
|
+}
|
|
|
+
|
|
|
+func InitSussBi(config map[string]interface{}) (*sussBi, error) {
|
|
|
+ address := gconv.String(config["addr"])
|
|
|
+ user := gconv.String(config["user"])
|
|
|
+ password := gconv.String(config["password"])
|
|
|
+ paramReplace := gconv.Map(config["paramReplace"])
|
|
|
+ if address == "" {
|
|
|
+ return nil, fmt.Errorf("配置异常")
|
|
|
+ }
|
|
|
+ sussCookie, err := cookiejar.New(&cookiejar.Options{
|
|
|
PublicSuffixList: publicsuffix.List,
|
|
|
})
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("初始化cookie异常")
|
|
|
+ }
|
|
|
+ prManager := &ParamReplaceManager{
|
|
|
+ eqRouters: map[string]*ParamReplace{},
|
|
|
+ regexRouter: map[*regexp.Regexp]*ParamReplace{},
|
|
|
+ }
|
|
|
+
|
|
|
+ for url, setting := range paramReplace {
|
|
|
+ pr := &ParamReplace{}
|
|
|
+ settingMap := gconv.Map(setting)
|
|
|
+ if settingMap == nil || len(settingMap) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if replaceMap := gconv.Map(settingMap["replace"]); replaceMap != nil && len(replaceMap) > 0 {
|
|
|
+ for k, v := range replaceMap {
|
|
|
+ pr.Replace = append(pr.Replace, ParamReplaceSetting{
|
|
|
+ Key: k,
|
|
|
+ Value: gconv.String(v),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if replaceMap := gconv.Map(settingMap["match"]); replaceMap != nil && len(replaceMap) > 0 {
|
|
|
+ for k, v := range replaceMap {
|
|
|
+ pr.Match = append(pr.Replace, ParamReplaceSetting{
|
|
|
+ Key: k,
|
|
|
+ Value: gconv.String(v),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(pr.Match) == 0 && len(pr.Replace) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if regexp.QuoteMeta(url) == url {
|
|
|
+ prManager.eqRouters[url] = pr
|
|
|
+ } else {
|
|
|
+ if reg, err := regexp.Compile(url); err == nil {
|
|
|
+ prManager.regexRouter[reg] = pr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
u, _ := url.Parse(address)
|
|
|
return &sussBi{
|
|
|
addr: address,
|
|
@@ -34,7 +99,8 @@ func InitSussBi(address, user, password string) *sussBi {
|
|
|
Url: u,
|
|
|
pwd: password,
|
|
|
jar: sussCookie,
|
|
|
- }
|
|
|
+ prm: prManager,
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
// AutoLogin 自动登录
|
|
@@ -71,50 +137,58 @@ func (s *sussBi) CheckLoginOut(r *ghttp.Request) bool {
|
|
|
func (s *sussBi) Filter(r *ghttp.Request) error {
|
|
|
ctx := router.GetGContext(r.GetCtx())
|
|
|
if ctx.Sess.NewUid != 0 {
|
|
|
+ replaceMap := map[string]interface{}{
|
|
|
+ "jyUserPositionId": ctx.Sess.UserPositionId,
|
|
|
+ "jyUserAccountId": ctx.Sess.UserAccountId,
|
|
|
+ "jyEntPositionId": ctx.Sess.EntPositionId,
|
|
|
+ "jyEntAccountId": ctx.Sess.EntAccountId,
|
|
|
+ "jyUserName": ctx.Sess.UserName,
|
|
|
+ "jyEntName": ctx.Sess.EntName,
|
|
|
+ "jyEntId": ctx.Sess.EntId,
|
|
|
+ "jyUserId": ctx.Sess.NewUid,
|
|
|
+ }
|
|
|
if r.Request.Method == http.MethodPost {
|
|
|
bodyBytes, err := io.ReadAll(r.Request.Body)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if len(bodyBytes) > 0 {
|
|
|
- replaceMap := map[string]interface{}{
|
|
|
- "jyUserPositionId": ctx.Sess.UserPositionId,
|
|
|
- "jyUserAccountId": ctx.Sess.UserAccountId,
|
|
|
- "jyEntPositionId": ctx.Sess.EntPositionId,
|
|
|
- "jyEntAccountId": ctx.Sess.EntAccountId,
|
|
|
- "jyUserName": ctx.Sess.UserName,
|
|
|
- "jyEntName": ctx.Sess.EntName,
|
|
|
- "jyEntId": ctx.Sess.EntId,
|
|
|
- }
|
|
|
- finalBytes := bytes.ReplaceAll(bodyBytes, []byte(`"jyUserId"`), []byte(fmt.Sprintf(`"%d"`, ctx.Sess.NewUid)))
|
|
|
+ finalBytes := bodyBytes
|
|
|
for k, v := range replaceMap {
|
|
|
finalBytes = bytes.ReplaceAll(finalBytes, []byte(`"`+k+`"`), []byte(`"`+fmt.Sprint(v)+`"`))
|
|
|
}
|
|
|
r.ContentLength = gconv.Int64(len(finalBytes))
|
|
|
r.Request.Header.Set("Content-Length", fmt.Sprintf("%d", len(finalBytes)))
|
|
|
- //fmt.Printf("before len:%d value:%s\nafter: len:%d value:%s\n", len(bodyBytes), string(bodyBytes), len(finalBytes), string(finalBytes))
|
|
|
- //fmt.Printf("header %+v\n", r.Request.Header)
|
|
|
r.Request.Body = ioutil.NopCloser(bytes.NewReader(finalBytes))
|
|
|
}
|
|
|
} else if r.Request.Method == http.MethodGet {
|
|
|
- // 年终报告pdf增加职位id
|
|
|
- if strings.HasPrefix(r.RequestURI, "/api/meta/services/convertFileToPDF") {
|
|
|
- if newValues, err := url.ParseQuery(r.URL.RawQuery); err == nil && len(newValues) > 0 {
|
|
|
- if _, ok := newValues["QUERY_PARAM_M_POSITION_ID"]; ok {
|
|
|
- newValues["QUERY_PARAM_M_POSITION_ID"] = []string{fmt.Sprintf("%d", ctx.Sess.UserPositionId)}
|
|
|
- }
|
|
|
- if _, ok := newValues["position_id"]; ok {
|
|
|
- newValues["position_id"] = []string{fmt.Sprintf("%d", ctx.Sess.UserPositionId)}
|
|
|
+ var prArr *ParamReplace
|
|
|
+ if rule, ok := s.prm.eqRouters[r.URL.Path]; ok && rule != nil {
|
|
|
+ prArr = rule
|
|
|
+ } else {
|
|
|
+ for reg, rule := range s.prm.regexRouter {
|
|
|
+ if reg.MatchString(r.URL.Path) {
|
|
|
+ prArr = rule
|
|
|
+ break
|
|
|
}
|
|
|
- r.URL.RawQuery = newValues.Encode()
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- if strings.HasPrefix(r.RequestURI, "/api/dw/services/downloadAttachment") || strings.HasPrefix(r.RequestURI, "/api/dw/services/getDwAttachments") {
|
|
|
+ if prArr != nil {
|
|
|
if newValues, err := url.ParseQuery(r.URL.RawQuery); err == nil && len(newValues) > 0 {
|
|
|
- if value, ok := newValues["params"]; ok && len(value) > 0 {
|
|
|
- newValue := strings.ReplaceAll(value[0], "jyUserPositionId", fmt.Sprintf("%d", ctx.Sess.UserPositionId))
|
|
|
- newValues["params"] = []string{newValue}
|
|
|
+ for _, replace := range prArr.Replace {
|
|
|
+ if replace.Key != "" && replace.Value != "" && replaceMap[replace.Value] != nil {
|
|
|
+ newValues[replace.Key] = []string{fmt.Sprintf("%v", replaceMap[replace.Value])}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, match := range prArr.Match {
|
|
|
+ if match.Key != "" && match.Value != "" {
|
|
|
+ if arr := strings.Split(match.Key, "."); len(arr) == 2 && replaceMap[match.Value] != nil {
|
|
|
+ if value, ok := newValues[arr[0]]; ok && len(value) > 0 {
|
|
|
+ newValue := strings.ReplaceAll(value[0], arr[1], fmt.Sprintf("%v", replaceMap[match.Value]))
|
|
|
+ newValues[arr[0]] = []string{newValue}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
r.URL.RawQuery = newValues.Encode()
|
|
|
}
|