file.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package tcb
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/util"
  5. )
  6. const (
  7. // 获取文件上传链接
  8. uploadFilePathURL = "https://api.weixin.qq.com/tcb/uploadfile"
  9. // 获取文件下载链接
  10. batchDownloadFileURL = "https://api.weixin.qq.com/tcb/batchdownloadfile"
  11. // 删除文件链接
  12. batchDeleteFileURL = "https://api.weixin.qq.com/tcb/batchdeletefile"
  13. )
  14. // UploadFileReq 上传文件请求值
  15. type UploadFileReq struct {
  16. Env string `json:"env,omitempty"`
  17. Path string `json:"path,omitempty"`
  18. }
  19. // UploadFileRes 上传文件返回结果
  20. type UploadFileRes struct {
  21. util.CommonError
  22. URL string `json:"url"` // 上传url
  23. Token string `json:"token"` // token
  24. Authorization string `json:"authorization"` // authorization
  25. FileID string `json:"file_id"` // 文件ID
  26. CosFileID string `json:"cos_file_id"` // cos文件ID
  27. }
  28. // BatchDownloadFileReq 上传文件请求值
  29. type BatchDownloadFileReq struct {
  30. Env string `json:"env,omitempty"`
  31. FileList []*DownloadFile `json:"file_list,omitempty"`
  32. }
  33. // DownloadFile 文件信息
  34. type DownloadFile struct {
  35. FileID string `json:"fileid"` // 文件ID
  36. MaxAge int64 `json:"max_age"` // 下载链接有效期
  37. }
  38. // BatchDownloadFileRes 上传文件返回结果
  39. type BatchDownloadFileRes struct {
  40. util.CommonError
  41. FileList []struct {
  42. FileID string `json:"file_id"` // 文件ID
  43. DownloadURL string `json:"download_url"` // 下载链接
  44. Status int64 `json:"status"` // 状态码
  45. ErrMsg string `json:"errmsg"` // 该文件错误信息
  46. } `json:"file_list"`
  47. }
  48. // BatchDeleteFileReq 批量删除文件请求参数
  49. type BatchDeleteFileReq struct {
  50. Env string `json:"env,omitempty"`
  51. FileIDList []string `json:"fileid_list,omitempty"`
  52. }
  53. // BatchDeleteFileRes 批量删除文件返回结果
  54. type BatchDeleteFileRes struct {
  55. util.CommonError
  56. DeleteList []struct {
  57. FileID string `json:"fileid"`
  58. Status int64 `json:"status"`
  59. ErrMsg string `json:"errmsg"`
  60. } `json:"delete_list"`
  61. }
  62. // UploadFile 上传文件
  63. //
  64. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html
  65. func (tcb *Tcb) UploadFile(env, path string) (*UploadFileRes, error) {
  66. accessToken, err := tcb.GetAccessToken()
  67. if err != nil {
  68. return nil, err
  69. }
  70. uri := fmt.Sprintf("%s?access_token=%s", uploadFilePathURL, accessToken)
  71. req := &UploadFileReq{
  72. Env: env,
  73. Path: path,
  74. }
  75. response, err := util.PostJSON(uri, req)
  76. if err != nil {
  77. return nil, err
  78. }
  79. uploadFileRes := &UploadFileRes{}
  80. err = util.DecodeWithError(response, uploadFileRes, "UploadFile")
  81. return uploadFileRes, err
  82. }
  83. // BatchDownloadFile 获取文件下载链接
  84. //
  85. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html
  86. func (tcb *Tcb) BatchDownloadFile(env string, fileList []*DownloadFile) (*BatchDownloadFileRes, error) {
  87. accessToken, err := tcb.GetAccessToken()
  88. if err != nil {
  89. return nil, err
  90. }
  91. uri := fmt.Sprintf("%s?access_token=%s", batchDownloadFileURL, accessToken)
  92. req := &BatchDownloadFileReq{
  93. Env: env,
  94. FileList: fileList,
  95. }
  96. response, err := util.PostJSON(uri, req)
  97. if err != nil {
  98. return nil, err
  99. }
  100. batchDownloadFileRes := &BatchDownloadFileRes{}
  101. err = util.DecodeWithError(response, batchDownloadFileRes, "BatchDownloadFile")
  102. return batchDownloadFileRes, err
  103. }
  104. // BatchDeleteFile 批量删除文件
  105. //
  106. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html
  107. func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFileRes, error) {
  108. accessToken, err := tcb.GetAccessToken()
  109. if err != nil {
  110. return nil, err
  111. }
  112. uri := fmt.Sprintf("%s?access_token=%s", batchDeleteFileURL, accessToken)
  113. req := &BatchDeleteFileReq{
  114. Env: env,
  115. FileIDList: fileIDList,
  116. }
  117. response, err := util.PostJSON(uri, req)
  118. if err != nil {
  119. return nil, err
  120. }
  121. batchDeleteFileRes := &BatchDeleteFileRes{}
  122. err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile")
  123. return batchDeleteFileRes, err
  124. }