From 1efa07a4422cb82edce26a66dfc041d450ae0228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Mon, 3 Jan 2022 13:45:40 +0800 Subject: [PATCH] update wechatminiprogram --- service/wechatminiprogram/save_img.go | 31 +++++++++++++++++++++++++++ utils/gohttp/gohttp.go | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 service/wechatminiprogram/save_img.go diff --git a/service/wechatminiprogram/save_img.go b/service/wechatminiprogram/save_img.go new file mode 100644 index 00000000..e97feb94 --- /dev/null +++ b/service/wechatminiprogram/save_img.go @@ -0,0 +1,31 @@ +package wechatminiprogram + +import ( + "gopkg.in/dtapps/go-library.v3/utils/gohttp" + "os" +) + +func (app *App) SaveImg(resp gohttp.Response, dir, saveName string) string { + // 返回是二进制图片,或者json错误 + if resp.Header.Get("Content-Type") == "image/jpeg" || resp.Header.Get("Content-Type") == "image/png" { + // 保存在output目录 + outputFileName := saveName + + if resp.Header.Get("Content-Type") == "image/jpeg" { + outputFileName = outputFileName + ".jpg" + } else { + outputFileName = outputFileName + ".png" + } + here: + f, err := os.OpenFile(dir+outputFileName, os.O_CREATE|os.O_RDWR, 0666) + if err != nil { + os.Mkdir(dir, 0666) + goto here + } + f.Write(resp.Body) + f.Close() + return dir + outputFileName + } else { + return "" + } +} diff --git a/utils/gohttp/gohttp.go b/utils/gohttp/gohttp.go index 105fb78f..2634e846 100644 --- a/utils/gohttp/gohttp.go +++ b/utils/gohttp/gohttp.go @@ -16,7 +16,7 @@ import ( type Response struct { Status string StatusCode int - Header map[string][]string + Header http.Header Body []byte ContentLength int64 }