From 679f2511f18efe3ed169e78ef6a2ca4964e04cc7 Mon Sep 17 00:00:00 2001 From: Medivh Date: Sun, 1 Aug 2021 12:29:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=90=9C=E7=B4=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 53 ++++++++++++++++++++++++++++++++++++++++-- search_image.go | 37 +++++++++++++++++++++++++++++ search_site.go | 44 +++++++++++++++++++++++++++++++++++ search_submit_pages.go | 8 +++---- 4 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 search_image.go create mode 100644 search_site.go diff --git a/Readme.md b/Readme.md index f8de68c..711f8af 100644 --- a/Readme.md +++ b/Readme.md @@ -144,6 +144,7 @@ go get -u github.com/medivhzhan/weapp/v3 - [运维中心](#运维中心)❌ - [realTimeLogSearch](#realTimeLogSearch) - [小程序搜索](#小程序搜索)❌ + - [imageSearch](#imageSearch) - [siteSearch](#siteSearch) - [submitPages](#submitPages) - [生物认证](#生物认证) @@ -2826,8 +2827,56 @@ fmt.Printf("返回结果: %#v", res) ## 小程序搜索 +### imageSearch + +[官方文档](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/search/search.imageSearch.html) + +```go + +import "github.com/medivhzhan/weapp/v3" + +res, err := cli.searchImage("local-filename") +if err != nil { + // 处理一般错误信息 + return +} + +if err := res.GetResponseError(); err !=nil { + // 处理微信返回错误信息 + return +} + +fmt.Printf("返回结果: %#v", res) + +``` + ### siteSearch +[官方文档](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/search/search.siteSearch.html) + +```go + +import "github.com/medivhzhan/weapp/v3" + +req := weapp.SearchSiteRequest{ + // ... +} + +res, err := cli.SearchSite(&req) +if err != nil { + // 处理一般错误信息 + return +} + +if err := res.GetResponseError(); err !=nil { + // 处理微信返回错误信息 + return +} + +fmt.Printf("返回结果: %#v", res) + +``` + ### submitPages [官方文档](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/search/search.submitPages.html) @@ -2836,7 +2885,7 @@ fmt.Printf("返回结果: %#v", res) import "github.com/medivhzhan/weapp/v3" -sender := weapp.SearchSubmitPages{ +req := weapp.SearchSubmitPagesRequest{ []weapp.SearchSubmitPage{ { Path: "pages/index/index", @@ -2845,7 +2894,7 @@ sender := weapp.SearchSubmitPages{ }, } -res, err := cli.SendSearchSubmitPages(&sender) +res, err := cli.SendSearchSubmitPages(&req) if err != nil { // 处理一般错误信息 return diff --git a/search_image.go b/search_image.go new file mode 100644 index 0000000..10e3bcd --- /dev/null +++ b/search_image.go @@ -0,0 +1,37 @@ +package weapp + +const ( + apiImageSearch = "/wxa/imagesearch" +) + +type SearchImageResponse struct { + CommonError + // 生成的小程序 URL Link + URLLink string `json:"url_link"` +} + +// 本接口提供基于小程序的站内搜商品图片搜索能力 +func (cli *Client) SearchImage(filename string) (*SearchImageResponse, error) { + api := baseURL + apiImageSearch + + token, err := cli.AccessToken() + if err != nil { + return nil, err + } + + return cli.searchImage(api, token, filename) +} + +func (cli *Client) searchImage(api, token, filename string) (*SearchImageResponse, error) { + uri, err := tokenAPI(api, token) + if err != nil { + return nil, err + } + + res := new(SearchImageResponse) + if err := cli.request.FormPostWithFile(uri, "img", filename, res); err != nil { + return nil, err + } + + return res, nil +} diff --git a/search_site.go b/search_site.go new file mode 100644 index 0000000..1208c81 --- /dev/null +++ b/search_site.go @@ -0,0 +1,44 @@ +package weapp + +const ( + apiSiteSearch = "/wxa/sitesearch" +) + +type SearchSiteRequest struct { + // 必填 关键词 + Keyword string `json:"keyword"` + // 必填 请求下一页的参数,开发者无需理解。为空时查询的是第一页内容,如需查询下一页,把返回参数的next_page_info填充到这里即可 + NextPageInfo string `json:"next_page_info"` +} + +type SearchSiteResponse struct { + CommonError + // 生成的小程序 URL Link + URLLink string `json:"url_link"` +} + +// 小程序内部搜索API提供针对页面的查询能力,小程序开发者输入搜索词后,将返回自身小程序和搜索词相关的页面。因此,利用该接口,开发者可以查看指定内容的页面被微信平台的收录情况;同时,该接口也可供开发者在小程序内应用,给小程序用户提供搜索能力。 +func (cli *Client) SearchSite(req *SearchSiteRequest) (*SearchSiteResponse, error) { + api := baseURL + apiSiteSearch + + token, err := cli.AccessToken() + if err != nil { + return nil, err + } + + return cli.searchSite(api, token, req) +} + +func (cli *Client) searchSite(api, token string, req *SearchSiteRequest) (*SearchSiteResponse, error) { + uri, err := tokenAPI(api, token) + if err != nil { + return nil, err + } + + res := new(SearchSiteResponse) + if err := cli.request.Post(uri, req, res); err != nil { + return nil, err + } + + return res, nil +} diff --git a/search_submit_pages.go b/search_submit_pages.go index 86d9659..982eeea 100644 --- a/search_submit_pages.go +++ b/search_submit_pages.go @@ -4,8 +4,8 @@ const ( apiSearchSubmitPages = "/wxa/search/wxaapi_submitpages" ) -// SearchSubmitPages 小程序页面收录请求 -type SearchSubmitPages struct { +// SearchSubmitPagesRequest 小程序页面收录请求 +type SearchSubmitPagesRequest struct { Pages []SearchSubmitPage `json:"pages"` } @@ -16,7 +16,7 @@ type SearchSubmitPage struct { } // Send 提交收录请求 -func (cli *Client) SendSearchSubmitPages(smp *SearchSubmitPages) (*CommonError, error) { +func (cli *Client) SendSearchSubmitPages(smp *SearchSubmitPagesRequest) (*CommonError, error) { api := baseURL + apiSearchSubmitPages token, err := cli.AccessToken() if err != nil { @@ -26,7 +26,7 @@ func (cli *Client) SendSearchSubmitPages(smp *SearchSubmitPages) (*CommonError, return cli.sendSearchSubmitPages(api, token, smp) } -func (cli *Client) sendSearchSubmitPages(api, token string, smp *SearchSubmitPages) (*CommonError, error) { +func (cli *Client) sendSearchSubmitPages(api, token string, smp *SearchSubmitPagesRequest) (*CommonError, error) { api, err := tokenAPI(api, token) if err != nil { return nil, err