f
This commit is contained in:
@@ -79,6 +79,13 @@ func (s *ShumaiService) generateRequestID() string {
|
||||
return fmt.Sprintf("shumai_%x", hash[:8])
|
||||
}
|
||||
|
||||
// generateRequestIDWithAppID 根据指定的 AppID 生成请求ID(用于不依赖全局状态的情况)
|
||||
func (s *ShumaiService) generateRequestIDWithAppID(appID string) string {
|
||||
timestamp := time.Now().UnixNano()
|
||||
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, appID)))
|
||||
return fmt.Sprintf("shumai_%x", hash[:8])
|
||||
}
|
||||
|
||||
// getCurrentAppID 获取当前使用的 AppID
|
||||
func (s *ShumaiService) getCurrentAppID() string {
|
||||
if s.useGovernment && s.config.AppID2 != "" {
|
||||
@@ -118,12 +125,33 @@ func (s *ShumaiService) GetConfig() ShumaiConfig {
|
||||
// CallAPIForm 以表单方式调用数脉 API(application/x-www-form-urlencoded)
|
||||
// 在方法内部将 reqFormData 转为表单:先写入业务参数,再追加 appid、timestamp、sign。
|
||||
// 签名算法:md5(appid×tamp&app_security),32 位小写,不足补 0。
|
||||
func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqFormData map[string]interface{}) ([]byte, error) {
|
||||
// useGovernment 可选参数:true 表示使用政务接口(app_id2),false 表示使用实时接口(app_id)
|
||||
// 如果未提供参数,则使用全局状态(通过 UseGovernment()/UseNormal() 设置)
|
||||
func (s *ShumaiService) CallAPIForm(ctx context.Context, apiPath string, reqFormData map[string]interface{}, useGovernment ...bool) ([]byte, error) {
|
||||
// 确定是否使用政务接口:如果提供了参数则使用参数值,否则使用全局状态
|
||||
var useGov bool
|
||||
if len(useGovernment) > 0 {
|
||||
useGov = useGovernment[0]
|
||||
} else {
|
||||
// 未提供参数时,使用全局状态以保持向后兼容
|
||||
useGov = s.useGovernment
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
requestID := s.generateRequestID()
|
||||
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
|
||||
appID := s.getCurrentAppID()
|
||||
appSecret := s.getCurrentAppSecret()
|
||||
|
||||
// 根据参数选择使用的 AppID 和 AppSecret,而不是依赖全局状态
|
||||
var appID, appSecret string
|
||||
if useGov && s.config.AppID2 != "" {
|
||||
appID = s.config.AppID2
|
||||
appSecret = s.config.AppSecret2
|
||||
} else {
|
||||
appID = s.config.AppID
|
||||
appSecret = s.config.AppSecret
|
||||
}
|
||||
|
||||
// 使用指定的 AppID 生成请求ID
|
||||
requestID := s.generateRequestIDWithAppID(appID)
|
||||
sign := GenerateSignForm(appID, timestamp, appSecret)
|
||||
|
||||
var transactionID string
|
||||
|
||||
Reference in New Issue
Block a user