liuxiaolong
2020-08-13 c6caf128002a1c0929aa7464ba5ae1f0dd968e7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package controllers
 
import (
    "car-service/extend/code"
    "car-service/extend/util"
    "car-service/models"
    "encoding/json"
    "fmt"
    "github.com/astaxie/beego"
    "net/http"
    "github.com/robfig/cron"
    "strconv"
    "strings"
    "time"
)
 
type RestrictionController struct {
    beego.Controller
}
 
func InitRestriction() {
    date := time.Now().Format("2006-01-02")
    var r models.Restriction
    err := r.SelectByDate(date)
    if err == nil {
        //今日已获取过限行信息
        todayRes = &RestrictionResult{
            Date: date,
            Week: r.Week,
            CityName: r.CityName,
            IsXianXing: r.IsXianXing,
        }
        arr := strings.Split(r.WeiHao, ",")
        if len(arr) >0 {
            for _,s := range arr {
                n, e := strconv.Atoi(s)
                if e ==nil {
                    todayRes.XxWeiHao = append(todayRes.XxWeiHao,n)
                }
            }
        }
    } else {
        getRestrictionInfo()
    }
}
 
// @Title 查询限行尾号
// @Description 查询限行尾号
// @Success 200 {object} controllers.RestrictionResult
// @Failure 403 {string} json ""
// @router /restriction [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= "数据请求失败,请稍后重试"
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
var todayRes *RestrictionResult
 
func Schedule() {
    c := cron.New()
    //每天凌晨5点执行一次,获取当日数据
    c.AddFunc("0 0 1 * * ?", func() {
        todayRes = nil
        getRestrictionInfo()
    })
    c.Start()
}
 
type RestrictionResult struct {
    Date         string         `json:"date"`
    Week         string         `json:"week"`
    CityName     string        `json:"cityName"`
    IsXianXing     int         `json:"isxianxing"`
    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 := beego.AppConfig.String("juheweihaokey")
    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 {
        fmt.Println("err:", err)
        return
    }
    var result JuHeResult
    err = json.Unmarshal(b, &result)
    if err != nil {
        fmt.Println("unmarshal err:", err)
        return
    }
    if result.ErrorCode == 0 {
        todayRes = &RestrictionResult{
            Date: result.Result.Date,
            Week: result.Result.Week,
            CityName: result.Result.CityName,
            IsXianXing: result.Result.IsXianXing,
            XxWeiHao: result.Result.XXWeiHao,
        }
        rc := models.Restriction{
            Date: result.Result.Date,
            Week: result.Result.Week,
            CityName: result.Result.CityName,
            IsXianXing: result.Result.IsXianXing,
        }
        var arr []string
        for _,n := range result.Result.XXWeiHao {
            arr=append(arr, strconv.Itoa(n))
        }
        rc.WeiHao = strings.Join(arr, ",")
        rc.Insert()
    }
}