Skip to content

Commit

Permalink
完善搜索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
royalrick committed Aug 1, 2021
1 parent a2ba3e7 commit 679f251
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
53 changes: 51 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ go get -u github.com/medivhzhan/weapp/v3
- [运维中心](#运维中心)
- [realTimeLogSearch](#realTimeLogSearch)
- [小程序搜索](#小程序搜索)
- [imageSearch](#imageSearch)
- [siteSearch](#siteSearch)
- [submitPages](#submitPages)
- [生物认证](#生物认证)
Expand Down Expand Up @@ -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)
Expand All @@ -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",
Expand All @@ -2845,7 +2894,7 @@ sender := weapp.SearchSubmitPages{
},
}

res, err := cli.SendSearchSubmitPages(&sender)
res, err := cli.SendSearchSubmitPages(&req)
if err != nil {
// 处理一般错误信息
return
Expand Down
37 changes: 37 additions & 0 deletions search_image.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions search_site.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions search_submit_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const (
apiSearchSubmitPages = "/wxa/search/wxaapi_submitpages"
)

// SearchSubmitPages 小程序页面收录请求
type SearchSubmitPages struct {
// SearchSubmitPagesRequest 小程序页面收录请求
type SearchSubmitPagesRequest struct {
Pages []SearchSubmitPage `json:"pages"`
}

Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 679f251

Please sign in to comment.