raffle.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package weixin
  2. import (
  3. "math/rand"
  4. "sync"
  5. "time"
  6. "zhiyuan/models"
  7. "zhiyuan/models/raffle"
  8. "zhiyuan/pkg/app"
  9. "zhiyuan/pkg/db"
  10. "zhiyuan/pkg/utils"
  11. "zhiyuan/pkg/weixin/mp"
  12. "zhiyuan/services/form"
  13. "zhiyuan/services/weixin"
  14. "zhiyuan/services/weixin/user"
  15. "github.com/gin-gonic/gin"
  16. )
  17. func GetRaffleInfo(c *gin.Context) {
  18. token := c.GetHeader("Access-Token")
  19. var weixinUser *models.WeixinUser
  20. if token != "" {
  21. weixinUser, _ = weixin.CheckToken(token)
  22. }
  23. if weixinUser == nil {
  24. client, err := mp.NewClient(1)
  25. if err != nil {
  26. app.Error(c, err.Error())
  27. return
  28. }
  29. url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
  30. if err != nil {
  31. app.Error(c, err.Error())
  32. return
  33. }
  34. app.Success(c, gin.H{
  35. "state": 2,
  36. "url": url,
  37. })
  38. return
  39. }
  40. id := utils.StrTo(c.Param("id")).MustInt()
  41. if id <= 0 {
  42. app.ErrorMsg(c, "抽奖id有误", nil)
  43. return
  44. }
  45. var raffles *raffle.Raffle
  46. db.GetModel(map[string]interface{}{
  47. "id": id,
  48. "deleted_at": 0,
  49. }, &raffles)
  50. if raffles == nil || raffles.DeletedAt != 0 {
  51. app.ErrorMsg(c, "抽奖活动不存在", nil)
  52. return
  53. }
  54. raffleItems := make([]raffle.RaffleItem, 0)
  55. db.GetModel(map[string]interface{}{
  56. "raffle_id": raffles.ID,
  57. "deleted_at": 0,
  58. }, &raffleItems)
  59. items := make([]gin.H, 0)
  60. itemMap := map[int64]raffle.RaffleItem{}
  61. for _, item := range raffleItems {
  62. items = append(items, gin.H{
  63. "id": item.ID,
  64. "name": item.Name,
  65. "picture": item.Picture,
  66. "winning": item.Winning,
  67. })
  68. itemMap[item.ID] = item
  69. }
  70. records := make([]raffle.RaffleRecord, 0)
  71. db.GetModel(map[string]interface{}{
  72. "user_id": weixinUser.ID,
  73. "raffle_id": raffles.ID,
  74. "deleted_at": 0,
  75. }, &records)
  76. count := raffles.Counts - int64(len(records))
  77. now := time.Now().Unix()
  78. if now < raffles.StartTime || now >= raffles.EndTime {
  79. count = 0
  80. }
  81. ret := gin.H{
  82. "state": 0,
  83. "items": items,
  84. "raffle": gin.H{
  85. "name": raffles.Name,
  86. "count": count,
  87. },
  88. }
  89. for _, record := range records {
  90. if record.Name == "" || record.Phone == "" {
  91. if item, ok := itemMap[record.ItemId]; ok && item.Winning == 1 {
  92. ret["state"] = 1
  93. ret["record"] = record.ID
  94. break
  95. }
  96. }
  97. }
  98. app.Success(c, ret)
  99. }
  100. var mut sync.Mutex
  101. func GetRaffleGo(c *gin.Context) {
  102. token := c.GetHeader("Access-Token")
  103. var weixinUser *models.WeixinUser
  104. if token != "" {
  105. weixinUser, _ = weixin.CheckToken(token)
  106. }
  107. if weixinUser == nil {
  108. client, err := mp.NewClient(1)
  109. if err != nil {
  110. app.Error(c, err.Error())
  111. return
  112. }
  113. url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
  114. if err != nil {
  115. app.Error(c, err.Error())
  116. return
  117. }
  118. app.Success(c, gin.H{
  119. "state": 2,
  120. "url": url,
  121. })
  122. return
  123. }
  124. id := utils.StrTo(c.Param("id")).MustInt()
  125. if id <= 0 {
  126. app.ErrorMsg(c, "抽奖id有误", nil)
  127. return
  128. }
  129. var raffles *raffle.Raffle
  130. db.GetModel(map[string]interface{}{
  131. "id": id,
  132. "deleted_at": 0,
  133. }, &raffles)
  134. if raffles == nil || raffles.DeletedAt != 0 {
  135. app.ErrorMsg(c, "抽奖活动不存在", nil)
  136. return
  137. }
  138. now := time.Now().Unix()
  139. if now < raffles.StartTime || now >= raffles.EndTime {
  140. app.ErrorMsg(c, "抽奖活动已过期", nil)
  141. return
  142. }
  143. raffleItems := make([]raffle.RaffleItem, 0)
  144. db.GetModel(map[string]interface{}{
  145. "raffle_id": raffles.ID,
  146. "deleted_at": 0,
  147. }, &raffleItems)
  148. items := make([]gin.H, 0)
  149. itemValids := make([]raffle.RaffleItem, 0)
  150. weight := 0
  151. mut.Lock()
  152. defer mut.Unlock()
  153. for _, item := range raffleItems {
  154. items = append(items, gin.H{
  155. "id": item.ID,
  156. "name": item.Name,
  157. "picture": item.Picture,
  158. "winning": item.Winning,
  159. })
  160. valids := true
  161. if item.Limit != 0 {
  162. itemRecords := make([]raffle.RaffleRecord, 0)
  163. db.GetModel(map[string]interface{}{
  164. "raffle_id": raffles.ID,
  165. "item_id": item.ID,
  166. "deleted_at": 0,
  167. }, &itemRecords)
  168. if len(itemRecords) >= int(item.Limit) {
  169. valids = false
  170. }
  171. }
  172. if valids {
  173. itemValids = append(itemValids, item)
  174. weight += int(item.Weight)
  175. }
  176. }
  177. if len(itemValids) == 0 {
  178. app.ErrorMsg(c, "奖品已抽完", nil)
  179. return
  180. }
  181. records := make([]raffle.RaffleRecord, 0)
  182. db.GetModel(map[string]interface{}{
  183. "user_id": weixinUser.ID,
  184. "raffle_id": raffles.ID,
  185. "deleted_at": 0,
  186. }, &records)
  187. count := raffles.Counts - int64(len(records))
  188. if count <= 0 {
  189. app.ErrorMsg(c, "您已无参与次数", nil)
  190. return
  191. }
  192. rand.Seed(time.Now().Unix())
  193. rnd := rand.Intn(weight)
  194. weights := 0
  195. var win *raffle.RaffleItem
  196. for _, item := range itemValids {
  197. if rnd >= weights {
  198. weights += int(item.Weight)
  199. if rnd < weights {
  200. win = &item
  201. break
  202. }
  203. }
  204. }
  205. if win == nil {
  206. app.ErrorMsg(c, "奖品已抽完", nil)
  207. return
  208. }
  209. recordMap := map[string]interface{}{
  210. "user_id": weixinUser.ID,
  211. "raffle_id": raffles.ID,
  212. "item_id": win.ID,
  213. }
  214. ids, err := db.InsertModel(db.Type(raffle.RaffleRecord{}), recordMap)
  215. if err != nil {
  216. app.ErrorMsg(c, "系统错误", nil)
  217. return
  218. }
  219. ret := gin.H{
  220. "state": win.Winning,
  221. "items": items,
  222. "raffle": gin.H{
  223. "name": raffles.Name,
  224. "count": count - 1,
  225. },
  226. "win": gin.H{
  227. "id": win.ID,
  228. "name": win.Name,
  229. "picture": win.Picture,
  230. },
  231. "record": ids,
  232. }
  233. app.Success(c, ret)
  234. }
  235. func GetRaffleRecord(c *gin.Context) {
  236. token := c.GetHeader("Access-Token")
  237. var weixinUser *models.WeixinUser
  238. if token != "" {
  239. weixinUser, _ = weixin.CheckToken(token)
  240. }
  241. if weixinUser == nil {
  242. client, err := mp.NewClient(1)
  243. if err != nil {
  244. app.Error(c, err.Error())
  245. return
  246. }
  247. url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
  248. if err != nil {
  249. app.Error(c, err.Error())
  250. return
  251. }
  252. app.Success(c, gin.H{
  253. "state": 2,
  254. "url": url,
  255. })
  256. return
  257. }
  258. id := utils.StrTo(c.Param("id")).MustInt()
  259. if id <= 0 {
  260. app.ErrorMsg(c, "抽奖id有误", nil)
  261. return
  262. }
  263. var raffles *raffle.Raffle
  264. db.GetModel(map[string]interface{}{
  265. "id": id,
  266. "deleted_at": 0,
  267. }, &raffles)
  268. if raffles == nil || raffles.DeletedAt != 0 {
  269. app.ErrorMsg(c, "抽奖活动不存在", nil)
  270. return
  271. }
  272. raffleRecords := make([]raffle.RaffleItemRecord, 0)
  273. db.GetModel(map[string]interface{}{
  274. "user_id": weixinUser.ID,
  275. "raffle_id": raffles.ID,
  276. "deleted_at": 0,
  277. }, &raffleRecords)
  278. records := make([]gin.H, 0)
  279. for _, record := range raffleRecords {
  280. if record.Winning == 1 {
  281. records = append(records, gin.H{
  282. "id": record.ID,
  283. "name": record.Name,
  284. "picture": record.Picture,
  285. })
  286. }
  287. }
  288. ret := gin.H{
  289. "state": 0,
  290. "records": records,
  291. }
  292. app.Success(c, ret)
  293. }
  294. func RaffleRecordRegister(c *gin.Context) {
  295. var form form.RaffleRecord
  296. if app.Bind(c, &form) != nil {
  297. return
  298. }
  299. token := c.GetHeader("Access-Token")
  300. var weixinUser *models.WeixinUser
  301. if token != "" {
  302. weixinUser, _ = weixin.CheckToken(token)
  303. }
  304. if weixinUser == nil {
  305. client, err := mp.NewClient(1)
  306. if err != nil {
  307. app.Error(c, err.Error())
  308. return
  309. }
  310. url, err := client.GetOauthUrls("snsapi_userinfo", c.Query("url"))
  311. if err != nil {
  312. app.Error(c, err.Error())
  313. return
  314. }
  315. app.Success(c, gin.H{
  316. "state": 2,
  317. "url": url,
  318. })
  319. return
  320. }
  321. id := utils.StrTo(c.Param("id")).MustInt()
  322. if id <= 0 {
  323. app.ErrorMsg(c, "抽奖id有误", nil)
  324. return
  325. }
  326. if form.Name == "" || form.Phone == "" {
  327. app.ErrorMsg(c, "请填写姓名和电话", nil)
  328. return
  329. }
  330. var record *raffle.RaffleRecord
  331. db.GetModel(map[string]interface{}{
  332. "id": form.Record,
  333. "user_id": weixinUser.ID,
  334. "raffle_id": id,
  335. "deleted_at": 0,
  336. }, &record)
  337. if record == nil {
  338. app.ErrorMsg(c, "中奖记录不存在", nil)
  339. return
  340. }
  341. recordMap := map[string]interface{}{
  342. "name": form.Name,
  343. "phone": form.Phone,
  344. }
  345. err := db.UpdateModel(db.Type(raffle.RaffleRecord{}), form.Record, recordMap)
  346. if err != nil {
  347. app.ErrorMsg(c, "系统错误", nil)
  348. return
  349. }
  350. app.Success(c, map[string]string{})
  351. }
  352. func GetCode(c *gin.Context) {
  353. code := c.Query("code")
  354. if code == "" {
  355. app.ErrorMsg(c, "code 不能为空", nil)
  356. return
  357. }
  358. client, err := mp.NewClient(1)
  359. if err != nil {
  360. app.Error(c, err.Error())
  361. return
  362. }
  363. response := client.GetOauthToken(code)
  364. if response.ErrMsg != "" {
  365. app.ErrorMsg(c, response.ErrMsg, nil)
  366. return
  367. }
  368. userInfo := client.GetUserInfo(response.AccessToken, response.OpenID)
  369. if response.ErrMsg != "" {
  370. app.ErrorMsg(c, response.ErrMsg, nil)
  371. return
  372. }
  373. token, err := user.Login(userInfo)
  374. if err != nil {
  375. app.ErrorMsg(c, err.Error(), nil)
  376. return
  377. }
  378. app.Success(c, map[string]string{
  379. "token": token,
  380. })
  381. }