Skip to content

Commit

Permalink
1、更新依赖;2、上传文件调整;
Browse files Browse the repository at this point in the history
  • Loading branch information
smartwalle committed Sep 8, 2023
1 parent a5154be commit 085a9c7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ p.Encrypt = false // 文件上传不支持接口内容加密
p.AddParam("biz_code", "content_creation")

// 添加文件
p.AddFile("file_content", "a.jpg", "path/a.jpg")
p.AddFilePath("file_content", "a.jpg", "path/a.jpg")
// 或者
p.AddFileObject("file_content", "a.jpg", io.Reader)

var result map[string]interface{}
var err = client.Request(p, &result)
Expand Down
8 changes: 2 additions & 6 deletions alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,8 @@ func (this *Client) doRequest(method string, param Param, result interface{}) (e
if err != nil {
return err
}
req.SetParams(values)

var files = param.FileParams()
for _, file := range files {
req.AddFile(file.Name, file.Filename, file.Filepath)
}
req.SetForm(values)
req.SetFileForm(param.FileParams())
}

rsp, err := req.Do(context.Background())
Expand Down
43 changes: 29 additions & 14 deletions alipay_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package alipay
import (
"encoding/json"
"fmt"
"github.com/smartwalle/ngx"
"io"
)

const (
Expand Down Expand Up @@ -71,7 +73,7 @@ type Param interface {
Params() map[string]string

// FileParams 文件参数
FileParams() map[string]*FileItem
FileParams() ngx.FormFiles

// NeedEncrypt 该接口是否支持内容加密,有的接口不支持内容加密,比如文件上传接口:alipay.open.file.upload
NeedEncrypt() bool
Expand All @@ -84,7 +86,7 @@ type Param interface {
type AuxParam struct {
}

func (this AuxParam) FileParams() map[string]*FileItem {
func (this AuxParam) FileParams() ngx.FormFiles {
return nil
}

Expand All @@ -96,19 +98,13 @@ func (this AuxParam) NeedVerify() bool {
return true
}

type FileItem struct {
Name string
Filename string
Filepath string
}

type Payload struct {
method string // 接口名称
Encrypt bool // 是否进行内容加密
Verify bool // 是否验证签名
param map[string]string // 请求参数
biz map[string]interface{} // biz_content 请求参数
files map[string]*FileItem // 文件参数
files ngx.FormFiles // 文件参数
}

func NewPayload(method string) *Payload {
Expand All @@ -129,7 +125,7 @@ func (this *Payload) Params() map[string]string {
return this.param
}

func (this *Payload) FileParams() map[string]*FileItem {
func (this *Payload) FileParams() ngx.FormFiles {
return this.files
}

Expand Down Expand Up @@ -176,15 +172,34 @@ func (this *Payload) Set(key string, value interface{}) *Payload {
// filename: 文件名称。
//
// filepath: 本地文件完整路径。
//
// Deprecated: use AddFilePath instead.
func (this *Payload) AddFile(name, filename, filepath string) {
if this.files == nil {
this.files = make(map[string]*FileItem)
this.files = ngx.FormFiles{}
}
this.files.AddFilePath(name, filename, filepath)
}

if filename == "" {
filename = name
// AddFilePath 添加需要上传的文件。
//
// name: 参数名称。
//
// filename: 文件名称。
//
// filepath: 本地文件完整路径。
func (this *Payload) AddFilePath(name, filename, filepath string) {
if this.files == nil {
this.files = ngx.FormFiles{}
}
this.files.AddFilePath(name, filename, filepath)
}

func (this *Payload) AddFileObject(name, filename string, file io.Reader) {
if this.files == nil {
this.files = ngx.FormFiles{}
}
this.files[name] = &FileItem{Name: name, Filename: filename, Filepath: filepath}
this.files.AddFileObject(name, filename, file)
}

func (this *Payload) MarshalJSON() ([]byte, error) {
Expand Down
5 changes: 2 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ go 1.12

require (
github.com/smartwalle/alipay/v3 v3.2.0
github.com/smartwalle/ncrypto v1.0.2 // indirect
github.com/smartwalle/ngx v1.0.7
github.com/smartwalle/nsign v1.0.8
github.com/smartwalle/xid v1.0.6
github.com/smartwalle/ngx v1.0.6
)

replace github.com/smartwalle/alipay/v3 => ../
replace github.com/smartwalle/alipay/v3 => ../
4 changes: 4 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ github.com/smartwalle/ncrypto v1.0.1 h1:CVe/h/srt6knLiF/9V5OnDDhguskWR801meHizq7
github.com/smartwalle/ncrypto v1.0.1/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ncrypto v1.0.2 h1:pTAhCqtPCMhpOwFXX+EcMdR6PNzruBNoGQrN2S1GbGI=
github.com/smartwalle/ncrypto v1.0.2/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ncrypto v1.0.3 h1:fnzjoriZt2LZeD8ljEtRe2eU33Au7i8vIF4Gafz5RuI=
github.com/smartwalle/ncrypto v1.0.3/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ngx v1.0.6 h1:JPNqNOIj+2nxxFtrSkJO+vKJfeNUSEQueck/Wworjps=
github.com/smartwalle/ngx v1.0.6/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/ngx v1.0.7 h1:BIQo6wmAnERehogNKUnthoxwBavTWxbR9oLFcGjWXKQ=
github.com/smartwalle/ngx v1.0.7/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/nsign v0.0.1 h1:fpEwoThIjr1JRPqz5hEwbH5ADX/so6G/zcwvj0ywYyM=
github.com/smartwalle/nsign v0.0.1/go.mod h1:eY6I4CJlyNdVMP+t6z1H6Jpd4m5/V+8xi44ufSTxXgc=
github.com/smartwalle/nsign v1.0.8 h1:78KWtwKPrdt4Xsn+tNEBVxaTLIJBX9YRX0ZSrMUeuHo=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/smartwalle/alipay/v3

require (
github.com/smartwalle/ncrypto v1.0.2
github.com/smartwalle/ncrypto v1.0.3
github.com/smartwalle/ngx v1.0.7
github.com/smartwalle/nsign v1.0.8
github.com/smartwalle/ngx v1.0.6
)

go 1.12
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ github.com/smartwalle/ncrypto v1.0.1 h1:CVe/h/srt6knLiF/9V5OnDDhguskWR801meHizq7
github.com/smartwalle/ncrypto v1.0.1/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ncrypto v1.0.2 h1:pTAhCqtPCMhpOwFXX+EcMdR6PNzruBNoGQrN2S1GbGI=
github.com/smartwalle/ncrypto v1.0.2/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ncrypto v1.0.3 h1:fnzjoriZt2LZeD8ljEtRe2eU33Au7i8vIF4Gafz5RuI=
github.com/smartwalle/ncrypto v1.0.3/go.mod h1:Dwlp6sfeNaPMnOxMNayMTacvC5JGEVln3CVdiVDgbBk=
github.com/smartwalle/ngx v1.0.6 h1:JPNqNOIj+2nxxFtrSkJO+vKJfeNUSEQueck/Wworjps=
github.com/smartwalle/ngx v1.0.6/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/ngx v1.0.7 h1:BIQo6wmAnERehogNKUnthoxwBavTWxbR9oLFcGjWXKQ=
github.com/smartwalle/ngx v1.0.7/go.mod h1:mx/nz2Pk5j+RBs7t6u6k22MPiBG/8CtOMpCnALIG8Y0=
github.com/smartwalle/nsign v0.0.1 h1:fpEwoThIjr1JRPqz5hEwbH5ADX/so6G/zcwvj0ywYyM=
github.com/smartwalle/nsign v0.0.1/go.mod h1:eY6I4CJlyNdVMP+t6z1H6Jpd4m5/V+8xi44ufSTxXgc=
github.com/smartwalle/nsign v1.0.8 h1:78KWtwKPrdt4Xsn+tNEBVxaTLIJBX9YRX0ZSrMUeuHo=
Expand Down

0 comments on commit 085a9c7

Please sign in to comment.