file.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package tcb
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/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. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html
  64. func (tcb *Tcb) UploadFile(env, path string) (*UploadFileRes, error) {
  65. accessToken, err := tcb.GetAccessToken()
  66. if err != nil {
  67. return nil, err
  68. }
  69. uri := fmt.Sprintf("%s?access_token=%s", uploadFilePathURL, accessToken)
  70. req := &UploadFileReq{
  71. Env: env,
  72. Path: path,
  73. }
  74. response, err := util.PostJSON(uri, req)
  75. if err != nil {
  76. return nil, err
  77. }
  78. uploadFileRes := &UploadFileRes{}
  79. err = util.DecodeWithError(response, uploadFileRes, "UploadFile")
  80. return uploadFileRes, err
  81. }
  82. //BatchDownloadFile 获取文件下载链接
  83. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html
  84. func (tcb *Tcb) BatchDownloadFile(env string, fileList []*DownloadFile) (*BatchDownloadFileRes, error) {
  85. accessToken, err := tcb.GetAccessToken()
  86. if err != nil {
  87. return nil, err
  88. }
  89. uri := fmt.Sprintf("%s?access_token=%s", batchDownloadFileURL, accessToken)
  90. req := &BatchDownloadFileReq{
  91. Env: env,
  92. FileList: fileList,
  93. }
  94. response, err := util.PostJSON(uri, req)
  95. if err != nil {
  96. return nil, err
  97. }
  98. batchDownloadFileRes := &BatchDownloadFileRes{}
  99. err = util.DecodeWithError(response, batchDownloadFileRes, "BatchDownloadFile")
  100. return batchDownloadFileRes, err
  101. }
  102. //BatchDeleteFile 批量删除文件
  103. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html
  104. func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFileRes, error) {
  105. accessToken, err := tcb.GetAccessToken()
  106. if err != nil {
  107. return nil, err
  108. }
  109. uri := fmt.Sprintf("%s?access_token=%s", batchDeleteFileURL, accessToken)
  110. req := &BatchDeleteFileReq{
  111. Env: env,
  112. FileIDList: fileIDList,
  113. }
  114. response, err := util.PostJSON(uri, req)
  115. if err != nil {
  116. return nil, err
  117. }
  118. batchDeleteFileRes := &BatchDeleteFileRes{}
  119. err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile")
  120. return batchDeleteFileRes, nil
  121. }