Пример #1
0
func CheckAccessToken(token string) (*Devices, error) {
	frags := strings.Split(token, "/")
	if len(frags) != 4 {
		return nil, errors.New("Frags error")
	}
	devices := gatsby.NewRecord(&Devices{}).(*Devices)
	var deviceId int64
	var err error
	if deviceId, err = strconv.ParseInt(frags[0], 10, 64); err != nil {
		return nil, err
	}
	res := devices.Load(deviceId)
	if res.Error != nil {
		return nil, res.Error
	} else {
		if res.IsEmpty {
			return nil, errors.New("empty result")
		}
	}
	d := Devices{
		Id:        devices.Id,
		DeviceId:  devices.DeviceId,
		RegId:     devices.RegId,
		SauthSalt: devices.SauthSalt,
		CreatedAt: devices.CreatedAt,
		CreateIp:  devices.CreateIp,
	}
	return &d, nil
}
Пример #2
0
func NewsReadAction(w http.ResponseWriter, r *http.Request) {

	var newsId int64
	var err error
	output := ApiResponseJson{}
	freg := r.URL.Path[18:]

	if freg == "" {
		output.Error(404, "No news id.")
		writeResponseJson(w, output, r.FormValue("callback"))
		w.(http.Flusher).Flush()
		return
	}

	if newsId, err = strconv.ParseInt(freg, 10, 64); err != nil {
		output.Error(502, err.Error())
		writeResponseJson(w, output, r.FormValue("callback"))
		w.(http.Flusher).Flush()
		return
	}

	header := w.Header()
	header.Set("Content-Type", "application/javascript; charset=utf-8")
	header.Set("X-Content-Type-Options", "nosniff")
	header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
	header.Set("Pragma", "no-cache")
	header.Set("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")

	news := gatsby.NewRecord(&News{}).(*News)
	res := news.Load(newsId)
	if res.Error != nil {
		output.Error(501, res.Error.Error())
	} else {
		if res.IsEmpty {
			output.Error(404, "empty result")
		}
	}

	if output.NoError() {
		data, err := GetNewsMeta(news.Id)
		if err == nil {
			(*data)["news"] = News{
				Id:          news.Id,
				Title:       news.Title,
				Body:        news.Body,
				PublishTime: news.PublishTime,
				Url:         news.Url,
				Guid:        news.Guid,
				OgImage:     news.OgImage,
				PicPath:     UrlDomain + news.PicPath[2:],
				ThumbPath:   UrlDomain + news.ThumbPath[2:],
				Referral:    news.Referral,
				CreateTime:  news.CreateTime,
				IsSupport:   news.IsSupport,
				IsHeadline:  news.IsHeadline,
			}
			output.Data = *data
		} else {
			output.Error(501, err.Error())
		}
	}
	writeResponseJson(w, output, r.FormValue("callback"))
	w.(http.Flusher).Flush()
}