123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- package weixin
- import (
- "math/rand"
- "sync"
- "time"
- "zhiyuan/models"
- "zhiyuan/models/raffle"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/pkg/weixin/mp"
- "zhiyuan/services/form"
- "zhiyuan/services/weixin"
- "zhiyuan/services/weixin/user"
- "github.com/gin-gonic/gin"
- )
- func GetRaffleInfo(c *gin.Context) {
- token := c.GetHeader("Access-Token")
- var weixinUser *models.WeixinUser
- if token != "" {
- weixinUser, _ = weixin.CheckToken(token)
- }
- if weixinUser == nil {
- client, err := mp.NewClient(1)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{
- "state": 2,
- "url": url,
- })
- return
- }
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.ErrorMsg(c, "抽奖id有误", nil)
- return
- }
- var raffles *raffle.Raffle
- db.GetModel(map[string]interface{}{
- "id": id,
- "deleted_at": 0,
- }, &raffles)
- if raffles == nil || raffles.DeletedAt != 0 {
- app.ErrorMsg(c, "抽奖活动不存在", nil)
- return
- }
- raffleItems := make([]raffle.RaffleItem, 0)
- db.GetModel(map[string]interface{}{
- "raffle_id": raffles.ID,
- "deleted_at": 0,
- }, &raffleItems)
- items := make([]gin.H, 0)
- itemMap := map[int64]raffle.RaffleItem{}
- for _, item := range raffleItems {
- items = append(items, gin.H{
- "id": item.ID,
- "name": item.Name,
- "picture": item.Picture,
- "winning": item.Winning,
- })
- itemMap[item.ID] = item
- }
- records := make([]raffle.RaffleRecord, 0)
- db.GetModel(map[string]interface{}{
- "user_id": weixinUser.ID,
- "raffle_id": raffles.ID,
- "deleted_at": 0,
- }, &records)
- count := raffles.Counts - int64(len(records))
- now := time.Now().Unix()
- if now < raffles.StartTime || now >= raffles.EndTime {
- count = 0
- }
- ret := gin.H{
- "state": 0,
- "items": items,
- "raffle": gin.H{
- "name": raffles.Name,
- "count": count,
- },
- }
- for _, record := range records {
- if record.Name == "" || record.Phone == "" {
- if item, ok := itemMap[record.ItemId]; ok && item.Winning == 1 {
- ret["state"] = 1
- ret["record"] = record.ID
- break
- }
- }
- }
- app.Success(c, ret)
- }
- var mut sync.Mutex
- func GetRaffleGo(c *gin.Context) {
- token := c.GetHeader("Access-Token")
- var weixinUser *models.WeixinUser
- if token != "" {
- weixinUser, _ = weixin.CheckToken(token)
- }
- if weixinUser == nil {
- client, err := mp.NewClient(1)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{
- "state": 2,
- "url": url,
- })
- return
- }
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.ErrorMsg(c, "抽奖id有误", nil)
- return
- }
- var raffles *raffle.Raffle
- db.GetModel(map[string]interface{}{
- "id": id,
- "deleted_at": 0,
- }, &raffles)
- if raffles == nil || raffles.DeletedAt != 0 {
- app.ErrorMsg(c, "抽奖活动不存在", nil)
- return
- }
- now := time.Now().Unix()
- if now < raffles.StartTime || now >= raffles.EndTime {
- app.ErrorMsg(c, "抽奖活动已过期", nil)
- return
- }
- raffleItems := make([]raffle.RaffleItem, 0)
- db.GetModel(map[string]interface{}{
- "raffle_id": raffles.ID,
- "deleted_at": 0,
- }, &raffleItems)
- items := make([]gin.H, 0)
- itemValids := make([]raffle.RaffleItem, 0)
- weight := 0
- mut.Lock()
- defer mut.Unlock()
- for _, item := range raffleItems {
- items = append(items, gin.H{
- "id": item.ID,
- "name": item.Name,
- "picture": item.Picture,
- "winning": item.Winning,
- })
- valids := true
- if item.Limit != 0 {
- itemRecords := make([]raffle.RaffleRecord, 0)
- db.GetModel(map[string]interface{}{
- "raffle_id": raffles.ID,
- "item_id": item.ID,
- "deleted_at": 0,
- }, &itemRecords)
- if len(itemRecords) >= int(item.Limit) {
- valids = false
- }
- }
- if valids {
- itemValids = append(itemValids, item)
- weight += int(item.Weight)
- }
- }
- if len(itemValids) == 0 {
- app.ErrorMsg(c, "奖品已抽完", nil)
- return
- }
- records := make([]raffle.RaffleRecord, 0)
- db.GetModel(map[string]interface{}{
- "user_id": weixinUser.ID,
- "raffle_id": raffles.ID,
- "deleted_at": 0,
- }, &records)
- count := raffles.Counts - int64(len(records))
- if count <= 0 {
- app.ErrorMsg(c, "您已无参与次数", nil)
- return
- }
- rand.Seed(time.Now().Unix())
- rnd := rand.Intn(weight)
- weights := 0
- var win *raffle.RaffleItem
- for _, item := range itemValids {
- if rnd >= weights {
- weights += int(item.Weight)
- if rnd < weights {
- win = &item
- break
- }
- }
- }
- if win == nil {
- app.ErrorMsg(c, "奖品已抽完", nil)
- return
- }
- recordMap := map[string]interface{}{
- "user_id": weixinUser.ID,
- "raffle_id": raffles.ID,
- "item_id": win.ID,
- }
- ids, err := db.InsertModel(db.Type(raffle.RaffleRecord{}), recordMap)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- ret := gin.H{
- "state": win.Winning,
- "items": items,
- "raffle": gin.H{
- "name": raffles.Name,
- "count": count - 1,
- },
- "win": gin.H{
- "id": win.ID,
- "name": win.Name,
- "picture": win.Picture,
- },
- "record": ids,
- }
- app.Success(c, ret)
- }
- func GetRaffleRecord(c *gin.Context) {
- token := c.GetHeader("Access-Token")
- var weixinUser *models.WeixinUser
- if token != "" {
- weixinUser, _ = weixin.CheckToken(token)
- }
- if weixinUser == nil {
- client, err := mp.NewClient(1)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{
- "state": 2,
- "url": url,
- })
- return
- }
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.ErrorMsg(c, "抽奖id有误", nil)
- return
- }
- var raffles *raffle.Raffle
- db.GetModel(map[string]interface{}{
- "id": id,
- "deleted_at": 0,
- }, &raffles)
- if raffles == nil || raffles.DeletedAt != 0 {
- app.ErrorMsg(c, "抽奖活动不存在", nil)
- return
- }
- raffleRecords := make([]raffle.RaffleItemRecord, 0)
- db.GetModel(map[string]interface{}{
- "user_id": weixinUser.ID,
- "raffle_id": raffles.ID,
- "deleted_at": 0,
- }, &raffleRecords)
- records := make([]gin.H, 0)
- for _, record := range raffleRecords {
- if record.Winning == 1 {
- records = append(records, gin.H{
- "id": record.ID,
- "name": record.Name,
- "picture": record.Picture,
- })
- }
- }
- ret := gin.H{
- "state": 0,
- "records": records,
- }
- app.Success(c, ret)
- }
- func RaffleRecordRegister(c *gin.Context) {
- var form form.RaffleRecord
- if app.Bind(c, &form) != nil {
- return
- }
- token := c.GetHeader("Access-Token")
- var weixinUser *models.WeixinUser
- if token != "" {
- weixinUser, _ = weixin.CheckToken(token)
- }
- if weixinUser == nil {
- client, err := mp.NewClient(1)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{
- "state": 2,
- "url": url,
- })
- return
- }
- id := utils.StrTo(c.Param("id")).MustInt()
- if id <= 0 {
- app.ErrorMsg(c, "抽奖id有误", nil)
- return
- }
- if form.Name == "" || form.Phone == "" {
- app.ErrorMsg(c, "请填写姓名和电话", nil)
- return
- }
- var record *raffle.RaffleRecord
- db.GetModel(map[string]interface{}{
- "id": form.Record,
- "user_id": weixinUser.ID,
- "raffle_id": id,
- "deleted_at": 0,
- }, &record)
- if record == nil {
- app.ErrorMsg(c, "中奖记录不存在", nil)
- return
- }
- recordMap := map[string]interface{}{
- "name": form.Name,
- "phone": form.Phone,
- }
- err := db.UpdateModel(db.Type(raffle.RaffleRecord{}), form.Record, recordMap)
- if err != nil {
- app.ErrorMsg(c, "系统错误", nil)
- return
- }
- app.Success(c, map[string]string{})
- }
- func GetCode(c *gin.Context) {
- code := c.Query("code")
- if code == "" {
- app.ErrorMsg(c, "code 不能为空", nil)
- return
- }
- client, err := mp.NewClient(1)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- response := client.GetOauthToken(code)
- if response.ErrMsg != "" {
- app.ErrorMsg(c, response.ErrMsg, nil)
- return
- }
- userInfo := client.GetUserInfo(response.AccessToken, response.OpenID)
- if response.ErrMsg != "" {
- app.ErrorMsg(c, response.ErrMsg, nil)
- return
- }
- token, err := user.Login(userInfo)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, map[string]string{
- "token": token,
- })
- }
|