New file |
| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "car-service/extend/code" |
| | | "car-service/extend/util" |
| | | "encoding/json" |
| | | "github.com/astaxie/beego" |
| | | "net/http" |
| | | "github.com/robfig/cron" |
| | | ) |
| | | |
| | | type RestrictionController struct { |
| | | beego.Controller |
| | | } |
| | | |
| | | // @Title 查询限行尾号 |
| | | // @Description 查询限行尾号 |
| | | // @Success 200 {object} controllers.RestrictionResult |
| | | // @Failure 403 {string} json "" |
| | | // @router /spaceNo [get] |
| | | func (c *RestrictionController) Restriction() { |
| | | resp := code.Code{} |
| | | if todayRes !=nil { |
| | | resp.Success= true |
| | | resp.Status= http.StatusOK |
| | | resp.Data= *todayRes |
| | | } else { |
| | | resp.Success= false |
| | | resp.Status= http.StatusBadRequest |
| | | resp.Data= nil |
| | | } |
| | | c.Data["json"] = resp |
| | | c.ServeJSON() |
| | | } |
| | | |
| | | var todayRes *RestrictionResult |
| | | |
| | | func Schedule() { |
| | | c := cron.New() |
| | | //每小时监测一次是否过期 |
| | | c.AddFunc("0 0 * * * ?", func() { |
| | | todayRes = nil |
| | | getRestrictionInfo() |
| | | }) |
| | | c.Start() |
| | | } |
| | | |
| | | type RestrictionResult struct { |
| | | Date string `json:"date"` |
| | | Week string `json:"week"` |
| | | CityName string `json:"cityName"` |
| | | XxWeiHao []int `json:"xxWeiHao"` |
| | | } |
| | | |
| | | type JuHeResult struct { |
| | | Reason string `json:"reason"` |
| | | Result CityRestriction `json:"result"` |
| | | ErrorCode int `json:"error_code"` |
| | | } |
| | | |
| | | type CityRestriction struct { |
| | | Date string `json:"date"` |
| | | Week string `json:"week"` |
| | | City string `json:"city"` |
| | | CityName string `json:"cityname"` |
| | | Des []interface{} `json:"des"` |
| | | Fine string `json:"fine"` |
| | | Remarks string `json:"remarks"` |
| | | IsXianXing int `json:"isxianxing"` |
| | | XXWeiHao []int `json:"xxweihao"` |
| | | Holiday string `json:"holiday"` |
| | | } |
| | | |
| | | func getRestrictionInfo() { |
| | | juHeAppKey := "" |
| | | url := "http://v.juhe.cn/xianxing/index?key="+ juHeAppKey +"&city=beijing&type=1" |
| | | b, err := util.DoPostRequest(url, util.CONTENT_TYPE_JSON, nil, nil, nil) |
| | | if err != nil { |
| | | return |
| | | } |
| | | var result JuHeResult |
| | | err = json.Unmarshal(b, &result) |
| | | if err != nil { |
| | | return |
| | | } |
| | | if result.ErrorCode == 0 { |
| | | todayRes = &RestrictionResult{ |
| | | Date: result.Result.Date, |
| | | Week: result.Result.Week, |
| | | CityName: result.Result.CityName, |
| | | XxWeiHao: result.Result.XXWeiHao, |
| | | } |
| | | } |
| | | } |
| | | |