34 lines
804 B
Go
34 lines
804 B
Go
package lib
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/tidwall/gjson"
|
|
"kefu/tools"
|
|
"net/url"
|
|
)
|
|
|
|
type BaiduFanyi struct {
|
|
AppId, AppSec string
|
|
}
|
|
|
|
func (this *BaiduFanyi) Translate(query, from, to string) (string, error) {
|
|
salt := "1435660288"
|
|
data := url.Values{}
|
|
data.Set("q", query)
|
|
data.Set("salt", salt)
|
|
data.Set("appid", this.AppId)
|
|
data.Set("from", from)
|
|
data.Set("to", to)
|
|
data.Set("sign", this.BuildSign(query, salt))
|
|
res, err := tools.PostForm("http://api.fanyi.baidu.com/api/trans/vip/translate", data)
|
|
fmt.Println(res, err)
|
|
res = gjson.Get(res, "trans_result.0.dst").String()
|
|
res = tools.UrlDecode(res)
|
|
return res, err
|
|
}
|
|
func (this *BaiduFanyi) BuildSign(query, salt string) string {
|
|
str := fmt.Sprintf("%s%s%s%s", this.AppId, query, salt, this.AppSec)
|
|
ret := tools.Md5(str)
|
|
return ret
|
|
}
|