Example #1
0
/**
投票搜索详情
*/
func (c *PollController) PollHomeSearch() {
	if c.Ctx.Input.IsGet() {
		beego.Debug("PollHomeSearch Get")
	}
	if c.Ctx.Input.IsPost() {
		beego.Debug("PollHomeSearch Post")
	}
	openid := getPollCookie(c)
	pollsid := c.Input().Get("pollsid")
	search := c.Input().Get("search")
	beego.Debug("pollsid:", pollsid)
	beego.Debug("search:", search)
	c.Data["PollsId"] = pollsid
	c.Data["Search"] = search
	op := c.Input().Get("op")
	beego.Debug("op:", op)
	switch op {
	case "vote":
		pollid := c.Input().Get("pollid")
		err := models.AddVote(openid, pollsid, pollid)
		if err != nil {
			beego.Debug(err)
		}
		url := fmt.Sprintf("/poll/pollhomesearch?pollsid=%s&search=%s", pollsid, search)
		beego.Debug("url:", url)
		c.Redirect(url, 302)
		return
	}
	objs, err := models.GetAllPollOr(search)
	if err != nil {
		beego.Debug(err)
	}
	for i := 0; i < len(objs); i++ {
		num, err := models.GetVoteNum(pollsid, objs[i].Id)
		if err != nil {
			beego.Error(err)
		}
		objs[i].VoteNum = num
	}
	polls, err := models.GetOnePolls(pollsid)
	if err != nil {
		beego.Error(err)
	}
	c.Data["Time"] = polls.EndTimeLong
	c.Data["Polls"] = polls
	c.Data["Objs"] = objs
	c.TplName = "pollhomesearch.html"
}
Example #2
0
/**
查看排名
*/
func (c *PollController) PollHomeRanking() {
	if c.Ctx.Input.IsGet() {
		beego.Debug("PollHomeRanking Get")
	}
	if c.Ctx.Input.IsPost() {
		beego.Debug("PollHomeRanking Post")
	}
	pollsid := c.Input().Get("pollsid")
	beego.Debug("pollsid:", pollsid)
	c.Data["PollsId"] = pollsid
	if len(pollsid) != 0 {
		objs, err := models.GetAllPollState(pollsid, 1)
		if err != nil {
			beego.Debug(err)
		}
		for i := 0; i < len(objs); i++ {
			num, err := models.GetVoteNum(pollsid, objs[i].Id)
			if err != nil {
				beego.Error(err)
			}
			objs[i].VoteNum = num
		}
		for i := 0; i < len(objs); i++ {
			for j := 0; j < len(objs)-i-1; j++ {
				if objs[j].VoteNum < objs[j+1].VoteNum {
					objs[j], objs[j+1] = objs[j+1], objs[j]
				}
			}
		}
		for i := 0; i < len(objs); i++ {
			objs[i].Ranking = int32(i)
		}
		beego.Debug("objs :", objs)
		c.Data["Objs"] = objs
	}
	c.TplName = "pollhomeranking.html"
}
Example #3
0
/**
添加投票
*/
func (c *PollController) AddPoll() {
	openid := getPollCookie(c)
	pollsid := c.Input().Get("pollsid")
	op := c.Input().Get("op")
	switch op {
	case "del":
		id := c.Input().Get("id")
		err := models.DelPoll(pollsid, id)
		if err != nil {
			beego.Error(err)
		}
		beego.Debug("add poll del :", id)
		url := fmt.Sprintf("/poll/addpoll?pollsid=%s", pollsid)
		c.Redirect(url, 302)
		return
	}
	poll, err := models.GetMyPoll(pollsid, openid)
	if err != nil {
		beego.Error(err)
	}
	if len(poll) > 0 {
		c.Data["IsAdd"] = true //是否已经添加过
		mypoll := poll[0]
		num, err := models.GetVoteNum(pollsid, mypoll.Id)
		if err != nil {
			beego.Error(err)
		}
		mypoll.VoteNum = num
		c.Data["Poll"] = mypoll
	} else {
		c.Data["IsAdd"] = false
	}
	if c.Ctx.Input.IsGet() {
		beego.Debug("PollHome Get")
	}
	if c.Ctx.Input.IsPost() {
		beego.Debug("PollHome Post")
		image_name := ""
		title := c.Input().Get("title")
		info := c.Input().Get("info")
		contactway := c.Input().Get("contactway")
		if len(pollsid) != 0 && len(title) != 0 && len(info) != 0 && len(contactway) != 0 {
			// 获取附件
			_, fh, err := c.GetFile("image")
			// beego.Debug("上传图片:", fh)
			if err != nil {
				beego.Error(err)
			}
			var attachment string
			if fh != nil {
				// 保存附件
				attachment = fh.Filename
				t := time.Now().Unix()
				str2 := fmt.Sprintf("%d", t)
				s := []string{attachment, str2}
				h := md5.New()
				h.Write([]byte(strings.Join(s, ""))) // 需要加密的字符串
				image_name = hex.EncodeToString(h.Sum(nil))
				beego.Info(image_name) // 输出加密结果
				err = c.SaveToFile("image", path.Join("imagehosting", image_name))
				if err != nil {
					beego.Error(err)
					image_name = ""
				}
			}
			err = models.AddPoll(openid, pollsid, title, info, image_name, contactway)
			if err != nil {
				beego.Error(err)
			}
			url := fmt.Sprintf("/poll/pollhome?pollsid=%s", pollsid)
			c.Redirect(url, 302)
			return
		}
	}
	c.Data["PollsId"] = pollsid
	c.TplName = "addpoll.html"
}
Example #4
0
/**
投票主页
*/
func (c *PollController) PollHome() {
	if c.Ctx.Input.IsGet() {
		beego.Debug("PollHome Get")
	}
	if c.Ctx.Input.IsPost() {
		beego.Debug("PollHome Post")
	}
	pollsid := c.Input().Get("pollsid")
	beego.Debug("pollsid :", pollsid)
	state := c.Input().Get("state")
	code := c.Input().Get("code")
	beego.Debug("/poll/pollhem state :", state)
	beego.Debug("/poll/pollhem code :", code)
	share_url := "http://www.baoguangguang.cn/poll/pollhome"
	if len(code) != 0 && len(state) != 0 {
		pollsid = state
		_, err := getPollWxOpenId(c, pollsid, code)
		if err != nil {
			beego.Error(err)
		}
		share_url = fmt.Sprintf("http://www.baoguangguang.cn/poll/pollhome?code=%s&state=%s", code, state)
	}
	beego.Debug("/poll/pollhem pollsid :", pollsid)
	openid := getPollCookie(c)
	//测试openid
	isdebug := "false"
	iniconf, err := config.NewConfig("json", "conf/myconfig.json")
	if err != nil {
		beego.Debug(err)
	} else {
		isdebug = iniconf.String("qax580::isdebug")
	}
	if isdebug == "true" {
		openid = "o3AhEuB_wdTELvlErL4F1Em4Nck4"
		c.Data["OpenId"] = openid
	}

	op := c.Input().Get("op")
	beego.Debug("op :", op)
	switch op {
	case "vote":
		id := c.Input().Get("id")
		err := models.AddVote(openid, pollsid, id)
		if err != nil {
			beego.Error(err)
		}
		url := fmt.Sprintf("/poll/pollhome?pollsid=%s", pollsid)
		c.Redirect(url, 302)
		return
	}
	c.Data["Time"] = int64(0)
	if len(pollsid) != 0 {
		err := models.AddPollsPv(pollsid)
		if err != nil {
			beego.Error(err)
		}
		polls, err := models.GetOnePolls(pollsid)
		if err != nil {
			beego.Error(err)
		}
		beego.Debug("polls", polls)
		c.Data["Time"] = polls.EndTimeLong
		c.Data["Polls"] = polls
		pv, err := models.GetPollsPv(pollsid)
		c.Data["PV"] = pv
		pollnum, err := models.GetPollAllNum(pollsid)
		c.Data["PollNum"] = pollnum
		votenum, err := models.GetVoteAllNum(pollsid)
		c.Data["VoteNum"] = votenum

		endtime := polls.EndTimeLong
		curtime := time.Now().Unix()
		timestr := "活动已过期"
		if endtime-curtime > 0 {
			t := time.Unix(endtime, 0)

			_, mon, day := t.Date()
			_, cmon, cday := time.Now().Date()
			hour, min, _ := t.Clock()
			chour, cmin, _ := time.Now().Clock()
			timestr = fmt.Sprintf("%d月%d天%02d小时%02d分", mon-cmon, day-cday, hour-chour, min-cmin)
			// beego.Debug(timestr)
		}
		c.Data["TimeStr"] = timestr

		objs, err := models.GetAllPollState(pollsid, 1)
		if err != nil {
			beego.Debug(err)
		}
		for i := 0; i < len(objs); i++ {
			num, err := models.GetVoteNum(pollsid, objs[i].Id)
			if err != nil {
				beego.Error(err)
			}
			objs[i].VoteNum = num
		}
		// beego.Debug("objs :", objs)
		c.Data["Objs"] = objs
		wxShareCon := models.WxShareCon{}
		wxShareCon.Title = polls.Title
		wxShareCon.Link = fmt.Sprintf("http://www.baoguangguang.cn/poll/pollwx?id=%s", pollsid)
		wxShareCon.ImgUrl = fmt.Sprintf("http://182.92.167.29:8080/imagehosting/%s", polls.Image)
		getPollShare(polls.Appid, polls.Secret, share_url, wxShareCon, c)
	}
	c.Data["PollsId"] = pollsid
	c.TplName = "pollhome.html"
}
Example #5
0
func (c *PollController) Adminpollscon() {
	bool, username := chackAccount(c.Ctx)
	if bool {
		c.Data["isUser"] = bool
		c.Data["User"] = username
	} else {
		c.Redirect("/admin", 302)
		return
	}
	pollsid := c.Input().Get("pollsid")
	if c.Ctx.Input.IsGet() {
		beego.Debug("Adminpollscon Get")
		op := c.Input().Get("op")
		beego.Debug("op:", op)
		switch op {
		case "state":
			id := c.Input().Get("id")
			if len(id) == 0 {
				break
			}
			id = c.Input().Get("id")
			err := models.UpdatePollState(pollsid, id, 1)
			if err != nil {
				beego.Error(err)
			}
			url := fmt.Sprintf("/poll/adminpollscon?pollsid=%s", pollsid)
			c.Redirect(url, 302)
			return
		case "state1":
			id := c.Input().Get("id")
			if len(id) == 0 {
				break
			}
			id = c.Input().Get("id")
			err := models.UpdatePollState(pollsid, id, 0)
			if err != nil {
				beego.Error(err)
			}
			url := fmt.Sprintf("/poll/adminpollscon?pollsid=%s", pollsid)
			c.Redirect(url, 302)
			return
		}
	}

	if c.Ctx.Input.IsPost() {
		beego.Debug("Adminpollscon Post")
	}
	objs, err := models.GetAllPoll(pollsid)
	if err != nil {
		beego.Error(err)
	}
	for i := 0; i < len(objs); i++ {
		num, err := models.GetVoteNum(pollsid, objs[i].Id)
		if err != nil {
			beego.Error(err)
		}
		objs[i].VoteNum = num
	}
	beego.Debug("pollsid:", pollsid)
	beego.Debug("objs:", objs)
	c.Data["Objs"] = objs
	c.Data["PollsId"] = pollsid
	c.TplName = "adminpollscon.html"
}