You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.2 KiB
78 lines
2.2 KiB
4 years ago
|
package search_ali
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
util "github.com/alibabacloud-go/tea-utils/service"
|
||
|
"github.com/alibabacloud-go/tea/tea"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func SearchByAliES(keyWords string, limit, page int) (goodsList []uint, total uint, err error) {
|
||
|
// 创建请求用客户端实例
|
||
|
// Endpoint 为 要访问服务的区域域名.
|
||
|
// AccessKeyId 及AccessKeySecret 用于构造鉴权信息.
|
||
|
config := &Config{
|
||
|
Endpoint: tea.String("opensearch-cn-shanghai.aliyuncs.com"),
|
||
|
AccessKeyId: tea.String("LTAI5tBeJdJBq2R9quhwGeQH"),
|
||
|
AccessKeySecret: tea.String("sKvHrPwNWbVeDgUARAiGTluW06AQ4c"),
|
||
|
}
|
||
|
|
||
|
// New 一个client, 用以发送请求.
|
||
|
client, _clientErr := NewClient(config)
|
||
|
|
||
|
// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
|
||
|
if _clientErr != nil {
|
||
|
fmt.Println(_clientErr)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// requestParams 信息
|
||
|
requestParams := map[string]interface{}{
|
||
|
"query": fmt.Sprintf("config=start:%v,hit:%v,format:fulljson&&query=default:'%s'&&filter=publish_status=1", int(20*(page-1)), limit, keyWords),
|
||
|
}
|
||
|
|
||
|
// 请求发送的配置参数. 用以请求配置及连接池配置.
|
||
|
runtime := &util.RuntimeOptions{
|
||
|
ConnectTimeout: tea.Int(5000),
|
||
|
ReadTimeout: tea.Int(10000),
|
||
|
Autoretry: tea.Bool(false),
|
||
|
IgnoreSSL: tea.Bool(false),
|
||
|
MaxIdleConns: tea.Int(50),
|
||
|
}
|
||
|
|
||
|
// 搜索接口需要提供 appName.
|
||
|
// App 可以是 app 的版本信息. 也可以是 app 名称.
|
||
|
appName := "recook"
|
||
|
|
||
|
// 发送请求的方法调用.
|
||
|
response, _requestErr := client.Request(
|
||
|
tea.String("GET"),
|
||
|
tea.String("/v3/openapi/apps/"+appName+"/search"),
|
||
|
requestParams,
|
||
|
nil,
|
||
|
nil,
|
||
|
runtime)
|
||
|
|
||
|
// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
|
||
|
if _requestErr != nil {
|
||
|
fmt.Println(_requestErr)
|
||
|
return []uint{}, 0, errors.New("网络异常")
|
||
|
}
|
||
|
|
||
|
// 输出正常返回的 response 内容.
|
||
|
body, _ := json.MarshalIndent(response, "", " ")
|
||
|
var gl RecookSearchModel
|
||
|
err = json.Unmarshal(body, &gl)
|
||
|
if err != nil {
|
||
|
return []uint{}, 0, errors.New("网络异常2")
|
||
|
}
|
||
|
for _, item := range gl.Body.Result.Items {
|
||
|
n, _ := strconv.Atoi(item.Fields.Id)
|
||
|
goodsList = append(goodsList, uint(n))
|
||
|
}
|
||
|
total = gl.Body.Result.Total
|
||
|
return
|
||
|
}
|