public function getAudiencePackage($res){ $url = 'https://ad.oceanengine.com/open_api/2/audience_package/get/'; $filtering = [ 'landing_type'=>$res['landing_type'], 'delivery_range'=>$res['delivery_range'], // 'landing_type'=>$request->landing_type, // 'delivery_range'=>$request->delivery_range, 'ad_type'=>'ALL' ]; $data = [ 'advertiser_id'=>1748895409387534, 'page'=>$res['page'], 'page_size'=>intval($res['page_size']), 'filtering'=>json_decode(json_encode($filtering)), ]; foreach ($data as $k=>$v){ if(!is_string($v)){ $data[$k] = json_encode($v); } } $request_url = $url.'?'.http_build_query($data); $token = '01c0dd4bef2fc5a2c38eac3f07eb1915bb338a7f'; $list = $this->getClient($request_url,$token); $list = json_decode($list,true); return $list; }这段代码怎么改成使用goframe2.4.3框架重写?goframe2.4.3框架使用了"github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gclient" "github.com/gogf/gf/v2/net/ghttp"扩展,请详细一点
时间: 2024-04-18 11:32:27 浏览: 156
基于遗传算法的LQR控制器优化设计.zip_2FMJ_audience898_lqr_matlab lqr_控制
在GoFrame 2.4.3框架中,你可以重写代码如下:
```go
package controller
import (
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
type AudienceController struct{}
func (c *AudienceController) GetAudiencePackage(r *ghttp.Request) {
url := "https://ad.oceanengine.com/open_api/2/audience_package/get/"
filtering := g.Map{
"landing_type": r.Get("landing_type"),
"delivery_range": r.Get("delivery_range"),
"ad_type": "ALL",
}
data := g.Map{
"advertiser_id": 1748895409387534,
"page": r.Get("page"),
"page_size": r.GetInt("page_size"),
"filtering": filtering,
}
for k, v := range data {
if !gconv.IsString(v) {
data[k] = gjson.New(v).MustToJsonString()
}
}
requestURL := url + "?" + ghttp.BuildQuery(data)
token := "01c0dd4bef2fc5a2c38eac3f07eb1915bb338a7f"
client := ghttp.NewClient()
response, err := client.SetHeader("Authorization", "Bearer "+token).Get(requestURL)
if err != nil {
g.Log().Error(err)
r.Response.WriteJson(g.Map{
"code": -1,
"message": "Request failed",
"data": nil,
})
return
}
list := gjson.New(response.ReadAllString()).MustToJson()
r.Response.WriteJson(g.Map{
"code": 0,
"message": "Request success",
"data": list,
})
}
```
以上代码是一个AudienceController的示例,你可以将其添加到goframe2.4.3框架的控制器中。在该示例中,我们使用`ghttp`包发送HTTP请求,`gjson`包处理JSON数据,`g`包提供了日志记录功能。
请注意,你可能需要根据实际情况调整代码中的导入语句和其他细节,以适应你的项目。
阅读全文