附近小店
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 3 Monaten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package weapp
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "io"
  7. "log"
  8. "math/rand"
  9. "mime/multipart"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "time"
  14. )
  15. // tokenAPI 获取带 token 的 API 地址
  16. func tokenAPI(api, token string) (string, error) {
  17. queries := requestQueries{
  18. "access_token": token,
  19. }
  20. return encodeURL(api, queries)
  21. }
  22. // encodeURL add and encode parameters.
  23. func encodeURL(api string, params requestQueries) (string, error) {
  24. url, err := url.Parse(api)
  25. if err != nil {
  26. return "", err
  27. }
  28. query := url.Query()
  29. for k, v := range params {
  30. query.Set(k, v)
  31. }
  32. url.RawQuery = query.Encode()
  33. return url.String(), nil
  34. }
  35. // randomString random string generator
  36. //
  37. // ln length of return string
  38. func randomString(ln int) string {
  39. letters := []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  40. b := make([]rune, ln)
  41. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  42. for i := range b {
  43. b[i] = letters[r.Intn(len(letters))]
  44. }
  45. return string(b)
  46. }
  47. // postJSON perform a HTTP/POST request with json body
  48. func postJSON(url string, params interface{}, response interface{}) error {
  49. resp, err := postJSONWithBody(url, params)
  50. if err != nil {
  51. return err
  52. }
  53. defer resp.Body.Close()
  54. return json.NewDecoder(resp.Body).Decode(response)
  55. }
  56. func getJSON(url string, response interface{}) error {
  57. resp, err := httpClient().Get(url)
  58. if err != nil {
  59. return err
  60. }
  61. defer resp.Body.Close()
  62. return json.NewDecoder(resp.Body).Decode(response)
  63. }
  64. // postJSONWithBody return with http body.
  65. func postJSONWithBody(url string, params interface{}) (*http.Response, error) {
  66. b := &bytes.Buffer{}
  67. if params != nil {
  68. enc := json.NewEncoder(b)
  69. enc.SetEscapeHTML(false)
  70. err := enc.Encode(params)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. return httpClient().Post(url, "application/json; charset=utf-8", b)
  76. }
  77. func postFormByFile(url, field, filename string, response interface{}) error {
  78. // Add your media file
  79. file, err := os.Open(filename)
  80. if err != nil {
  81. return err
  82. }
  83. defer file.Close()
  84. return postForm(url, field, filename, file, response)
  85. }
  86. func postForm(url, field, filename string, reader io.Reader, response interface{}) error {
  87. // Prepare a form that you will submit to that URL.
  88. buf := new(bytes.Buffer)
  89. w := multipart.NewWriter(buf)
  90. fw, err := w.CreateFormFile(field, filename)
  91. if err != nil {
  92. return err
  93. }
  94. if _, err = io.Copy(fw, reader); err != nil {
  95. return err
  96. }
  97. // Don't forget to close the multipart writer.
  98. // If you don't close it, your request will be missing the terminating boundary.
  99. w.Close()
  100. // Now that you have a form, you can submit it to your handler.
  101. req, err := http.NewRequest("POST", url, buf)
  102. if err != nil {
  103. return err
  104. }
  105. // Don't forget to set the content type, this will contain the boundary.
  106. req.Header.Set("Content-Type", w.FormDataContentType())
  107. // Submit the request
  108. client := httpClient()
  109. resp, err := client.Do(req)
  110. if err != nil {
  111. return err
  112. }
  113. defer resp.Body.Close()
  114. return json.NewDecoder(resp.Body).Decode(response)
  115. }
  116. func httpClient() *http.Client {
  117. log.Print("myweapp use http")
  118. return &http.Client{
  119. Timeout: 10 * time.Second,
  120. Transport: &http.Transport{
  121. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  122. },
  123. }
  124. }