Esempio n. 1
0
// 保存晨读
func SaveReading(form url.Values, username string) (errMsg string, err error) {
	reading := model.NewMorningReading()
	err = util.ConvertAssign(reading, form)
	if err != nil {
		logger.Errorln("reading SaveReading error", err)
		errMsg = err.Error()
		return
	}

	reading.Moreurls = strings.TrimSpace(reading.Moreurls)
	if strings.Contains(reading.Moreurls, "\n") {
		reading.Moreurls = strings.Join(strings.Split(reading.Moreurls, "\n"), ",")
	}

	reading.Username = username

	logger.Infoln(reading.Rtype, "id=", reading.Id)
	if reading.Id != 0 {
		err = reading.Persist(reading)
	} else {
		_, err = reading.Insert()
	}

	if err != nil {
		errMsg = "内部服务器错误"
		logger.Errorln("reading save:", errMsg, ":", err)
		return
	}

	return
}
Esempio n. 2
0
// 【我要晨读】
func IReading(id string) string {
	_, err := strconv.Atoi(id)
	if err != nil {
		return "/readings"
	}

	reading := model.NewMorningReading()
	err = reading.Where("id=?", id).Find()

	if err != nil {
		logger.Errorln("reading service IReading error:", err)
		return "/readings"
	}

	if reading.Id == 0 {
		return "/readings"
	}

	go reading.Where("id=?", id).Increment("clicknum", 1)

	if reading.Inner == 0 {
		return "/wr?u=" + reading.Url
	}

	return "/articles/" + strconv.Itoa(reading.Inner)
}
Esempio n. 3
0
// 获取单条晨读
func FindReadingById(id int) (*model.MorningReading, error) {
	reading := model.NewMorningReading()
	err := reading.Where("id=?", id).Find()
	if err != nil {
		logger.Errorln("reading service FindReadingById Error:", err)
	}

	return reading, err
}
Esempio n. 4
0
// 获取晨读列表(分页)
func FindReadings(lastId, limit string, rtype int) []*model.MorningReading {
	reading := model.NewMorningReading()

	cond := "rtype=?"
	args := make([]interface{}, 0, 2)
	args = append(args, rtype)
	if lastId != "0" {
		cond += " AND id<?"
		args = append(args, lastId)
	}

	readingList, err := reading.Where(cond, args...).Order("id DESC").Limit(limit).
		FindAll()
	if err != nil {
		logger.Errorln("reading service FindReadings Error:", err)
		return nil
	}

	return readingList
}
Esempio n. 5
0
// 获取晨读列表(分页)
func FindReadingByPage(conds map[string]string, curPage, limit int) ([]*model.MorningReading, int) {
	conditions := make([]string, 0, len(conds))
	for k, v := range conds {
		conditions = append(conditions, k+"="+v)
	}

	reading := model.NewMorningReading()

	limitStr := strconv.Itoa((curPage-1)*limit) + "," + strconv.Itoa(limit)
	readingList, err := reading.Where(strings.Join(conditions, " AND ")).Order("id DESC").Limit(limitStr).
		FindAll()
	if err != nil {
		logger.Errorln("reading service FindArticleByPage Error:", err)
		return nil, 0
	}

	total, err := reading.Count()
	if err != nil {
		logger.Errorln("reading service FindReadingByPage COUNT Error:", err)
		return nil, 0
	}

	return readingList, total
}
Esempio n. 6
0
// 订阅邮件通知
func EmailNotice() {

	beginDate := time.Now().Add(-7 * 24 * time.Hour).Format("2006-01-02")
	endDate := time.Now().Add(-24 * time.Hour).Format("2006-01-02")

	beginTime := beginDate + " 00:00:00"

	// 本周晨读(过去 7 天)
	readings, err := model.NewMorningReading().Where("ctime>? AND rtype=0", beginTime).Order("id DESC").FindAll()
	if err != nil {
		logger.Errorln("find morning reading error:", err)
	}

	// 本周精彩文章
	articles, err := model.NewArticle().Where("ctime>? AND status!=2", beginTime).Order("cmtnum DESC, likenum DESC, viewnum DESC").Limit("10").FindAll()
	if err != nil {
		logger.Errorln("find article error:", err)
	}

	// 本周热门主题
	topics, err := model.NewTopic().Where("ctime>? AND flag IN(0,1)", beginTime).Order("tid DESC").Limit("10").FindAll()
	if err != nil {
		logger.Errorln("find topic error:", err)
	}

	data := map[string]interface{}{
		"readings":  readings,
		"articles":  articles,
		"topics":    topics,
		"beginDate": beginDate,
		"endDate":   endDate,
	}

	// 给所有用户发送邮件
	userModel := model.NewUser()

	var (
		lastUid = 0
		limit   = "500"
		users   []*model.User
	)

	for {
		users, err = userModel.Where("uid>?", lastUid).Order("uid ASC").Limit(limit).FindAll()
		if err != nil {
			logger.Errorln("find user error:", err)
			continue
		}

		if len(users) == 0 {
			break
		}

		for _, user := range users {
			if user.Unsubscribe == 1 {
				logger.Infoln("user unsubscribe", user)
				continue
			}

			data["email"] = user.Email
			data["token"] = GenUnsubscribeToken(user)

			content, err := genEmailContent(data)
			if err != nil {
				logger.Errorln("from email.html gen email content error:", err)
				continue
			}

			SendMail("每周精选", content, []string{user.Email})

			if lastUid < user.Uid {
				lastUid = user.Uid
			}

			// 控制发信速度
			time.Sleep(30 * time.Second)
		}
	}

}