cloudfunction.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package tcb
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/util"
  5. )
  6. const (
  7. //触发云函数
  8. invokeCloudFunctionURL = "https://api.weixin.qq.com/tcb/invokecloudfunction"
  9. )
  10. //InvokeCloudFunctionRes 云函数调用返回结果
  11. type InvokeCloudFunctionRes struct {
  12. util.CommonError
  13. RespData string `json:"resp_data"` //云函数返回的buffer
  14. }
  15. //InvokeCloudFunction 云函数调用
  16. //reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html
  17. func (tcb *Tcb) InvokeCloudFunction(env, name, args string) (*InvokeCloudFunctionRes, error) {
  18. accessToken, err := tcb.GetAccessToken()
  19. if err != nil {
  20. return nil, err
  21. }
  22. uri := fmt.Sprintf("%s?access_token=%s&env=%s&name=%s", invokeCloudFunctionURL, accessToken, env, name)
  23. response, err := util.HTTPPost(uri, args)
  24. if err != nil {
  25. return nil, err
  26. }
  27. invokeCloudFunctionRes := &InvokeCloudFunctionRes{}
  28. err = util.DecodeWithError(response, invokeCloudFunctionRes, "InvokeCloudFunction")
  29. return invokeCloudFunctionRes, err
  30. }