Procházet zdrojové kódy

fix: 个人授权记录查询无结果时不当作接口失败

契约锁返回「未查询到静默签授权记录」时视为空列表;EFFECT 无匹配时再查全部状态。

Co-authored-by: Cursor <cursoragent@cursor.com>
郭铭泽 před 4 dny
rodič
revize
45de600ec8
2 změnil soubory, kde provedl 36 přidání a 3 odebrání
  1. 12 1
      errors.go
  2. 24 2
      user.go

+ 12 - 1
errors.go

@@ -1,6 +1,9 @@
 package qiyuesuosdk
 
-import "errors"
+import (
+	"errors"
+	"strings"
+)
 
 var (
 	// ErrCompanyNotRegistered 契约锁返回 code=2002002,表示 openCompanyId 尚未注册。
@@ -29,3 +32,11 @@ func (r apiResponse) err() error {
 	}
 	return errors.New("qiyuesuo: " + r.Message)
 }
+
+// IsPersonalSignAuthRecordNotFound 个人静默签授权查询无记录(契约锁业务码,非系统故障)。
+func IsPersonalSignAuthRecordNotFound(err error) bool {
+	if err == nil {
+		return false
+	}
+	return strings.Contains(err.Error(), "未查询到静默签授权记录")
+}

+ 24 - 2
user.go

@@ -122,6 +122,9 @@ func (c *Client) QueryPersonalSignAuthRecords(openUserID, status string) ([]Auth
 		return nil, err
 	}
 	if err := resp.err(); err != nil {
+		if IsPersonalSignAuthRecordNotFound(err) {
+			return []AuthRecord{}, nil
+		}
 		return nil, err
 	}
 	out := make([]AuthRecord, 0, len(resp.Result))
@@ -152,12 +155,31 @@ func (c *Client) PersonalSignAuthRecordForCompany(openUserID string, company App
 	if err != nil {
 		return nil, false, err
 	}
+	if rec, ok := findAuthRecordForCompany(records, company, status); ok {
+		return rec, true, nil
+	}
+	if status != "" {
+		all, err := c.QueryPersonalSignAuthRecords(openUserID, "")
+		if err != nil {
+			return nil, false, err
+		}
+		if rec, ok := findAuthRecordForCompany(all, company, status); ok {
+			return rec, true, nil
+		}
+	}
+	return nil, false, nil
+}
+
+func findAuthRecordForCompany(records []AuthRecord, company ApplyCompanyParams, wantStatus string) (*AuthRecord, bool) {
 	for i := range records {
+		if wantStatus != "" && records[i].Status != wantStatus {
+			continue
+		}
 		if authRecordMatchesCompany(&records[i], company) {
-			return &records[i], true, nil
+			return &records[i], true
 		}
 	}
-	return nil, false, nil
+	return nil, false
 }
 
 func authRecordMatchesCompany(rec *AuthRecord, company ApplyCompanyParams) bool {