妹子鎮(zhèn)樓
我們這里使用Go語(yǔ)言編寫(xiě),使用beego作為mvc框架坏怪,基于新浪的短鏈接api贝润,來(lái)吧 開(kāi)工!~
關(guān)于beego怎么使用我這就不說(shuō)了铝宵,很簡(jiǎn)單打掘,官方文檔也寫(xiě)的很詳細(xì),https://beego.me/鹏秋,我們來(lái)看看業(yè)務(wù)邏輯:
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/httplib"
)
//新浪接口地址和source(需要開(kāi)發(fā)者賬號(hào)申請(qǐng))
const api = "https://api.weibo.com/2/short_url/shorten.json?"
const source = "1950792609"
type UrlController struct {
beego.Controller
}
// 獲取前端數(shù)據(jù)傳回來(lái)的長(zhǎng)連接
func (this *UrlController) Get() {
url := this.GetString("url")
//請(qǐng)求新浪接口
req := httplib.Post(api)
req.Param("source",source)
req.Param("url_long",url)
s, _ := req.String()
this.Data["json"] = map[string]interface{}{"code": 0,"data": s, "message": err}
this.ServeJSON()
return
}
沒(méi)看錯(cuò)這樣就可以了 是不是很簡(jiǎn)單尊蚁,嘖嘖嘖!
代碼中的 source 需要去新浪注冊(cè)開(kāi)發(fā)者賬號(hào)侣夷,就會(huì)有了横朋!
最終效果:
可愛(ài)即是正義