码迷,mamicode.com
首页 > 微信 > 详细

微信大转盘

时间:2014-08-29 10:49:32      阅读:445      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   java   io   ar   

1、创建大转盘对象:

  

bubuko.com,布布扣
//添加大转盘对象并添加大转盘奖项对象(一等奖,二等奖,三等奖等等)
public ActionResult WheelCreate(WheelViewModel form)
        {
            var UserId = WebSecurity.GetUserId(User.Identity.Name);
            var user = UserProfileRepository.Find(Specification<EasyWeixin.Model.UserProfile>.Eval(o => o.UserId == UserId));
            form.ResponseImageTextViewModel.ImageTextName = form.WheelTitle;
            form.ResponseImageTextViewModel.ImageTextType = 101;
            form.ResponseImageTextViewModel.UserId = WebSecurity.GetUserId(User.Identity.Name);
            form.ResponseImageTextViewModel.AddTime = DateTime.Now;
            form.UserId = WebSecurity.GetUserId(User.Identity.Name);
            form.AddDate = DateTime.Now;
            if (ModelState.IsValid)
            {
                Wheel Wheel = Mapper.Map<WheelViewModel, Wheel>(form);
                Wheel.WheelStyle = "Wheel.css";
                Wheel.ResponseImageText = Mapper.Map<ResponseImageTextViewModel, ResponseImageText>(form.ResponseImageTextViewModel);
               
                WheelRepository.Add(Wheel);
                WheelRepository.Context.Commit();
                Wheel.ResponseImageText.Url = "http://xxx/ActivityWheel/WheelIndex?WheelID=" + Wheel.ID + "&ImageTextID=" + Wheel.ResponseImageText.ID;
                Wheel.GetURL = "http://xxx/ActivityWheel/WheelIndex?WheelID=" + Wheel.ID + "&ImageTextID=" + Wheel.ResponseImageText.ID + "&User_ID=" + user.ID;
                for (int i = 0; i < 3; i++)
                {
                    var Angle = GetAngle(i, 3, 5);
                    WheelItem wi = new WheelItem();
                    wi.WheelItemScale = i + 1;
                    wi.WheelItemName = GetChineseNum(i + 1) + "等奖";
                    wi.WheelItemAward = "";
                    wi.isOrder = i;
                    wi.MaxAngle = Angle.Split(|)[1];
                    wi.MinAngle = Angle.Split(|)[0];
                    wi.WheelID = Wheel.WheelID;
                    wi.AddDate = DateTime.Now;
                    WheelItemRepository.Add(wi);
                    WheelItemRepository.Context.Commit();
                }
                WheelRepository.Update(Wheel);
                WheelRepository.Context.Commit();

            }
            return Redirect("/Wheel/WheelIndex");
        }


         /// <summary>
        /// 
        /// </summary>
        /// <param name="i">角度索引</param>
        /// <param name="num">角度平均分块</param>
        /// <param name="f">角度波动</param>
        /// <returns></returns>
        public string GetAngle(int i, int num, int f)
        {
            //获取平均角度区域
            var average = 360 / num;
            var averagehalf = 360 / (num * 2);
            var half = i * average;
            var str = (half - f).ToString() + "|" + (half + f).ToString();
            return str;
        }

        public string GetChineseNum(int i)
        {
            var str = "";
            if (i == 1)
            {
                str = "";
            }
            if (i == 2)
            {
                str = "";
            }
            if (i == 3)
            {
                str = "";
            }
            if (i == 4)
            {
                str = "";
            }
            if (i == 5)
            {
                str = "";
            } if (i == 6)
            {
                str = "";
            }
            if (i == 7)
            {
                str = "";
            }
            if (i == 8)
            {
                str = "";
            }
            return str;
        }
one

 

2、大转盘运行:

  界面:

bubuko.com,布布扣
@{
    ViewBag.Title = "WheelIndex";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta content="telephone=no" name="format-detection" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title></title>
    <link href="/Content/Wheel/global.css" rel="stylesheet" type=text/css />
    <link href="/Content/Wheel/turntable.css" rel="stylesheet" type=text/css />
    <script type=text/javascript src="/Scripts/jquery-1.7.2.min.js"></script>
    <script type=text/javascript src="/Scripts/easing.min.js"></script>
    <script type=text/javascript src="/Scripts/rotate.2.2.js"></script>
    <script type=text/javascript>
        var turntable_obj = {
            turntable_init: function () {
                $(#startbtn).rotate({
                    bind: {
                        click: function () {
                            var WheelID = $("#WheelID").val();
                            var UserWexinID = $("#UserWexinID").val();
                            $.post("/ActivityWheel/GetWheelitem", { WheelID: WheelID, UserWexinID: UserWexinID }, function (data) {
                                if (data.message == undefined) {
                                    var Max = data.MaxAngle;
                                    var Min = data.MinAngle;
                                    var range = GetRandomNum(Min, Max);
                                    $(#startbtn).rotate({
                                        duration: 6000,
                                        angle: range,
                                        animateTo: 5400 + range,
                                        easing: $.easing.easeOutSine,
                                        callback: function () {
                                            if (data.WheelItemID > 0) {
                                                win_alert(data.WheelItemName + SN码为: + data.WheelCode, function () {
                                                    $(#WinPrize).slideDown(500);
                                                    $(#PrizeClass).html(data.WheelItemName);
                                                    $(#SnNumber).html(data.WheelCode);
                                                    $(#GetPrize input[name=MobilePhone]).focus();
                                                });
                                            } else {
                                                win_alert("谢谢参与,请继续努力", function() {
                                                });
                                            }
                                        }
                                    });
                                } else {
                                    win_alert(data.message);
                                    return false;
                                }
                            }, json);
                        }
                    }
                });

                var Phone = $(input[name=MobilePhone]);
                $(#GetPrize).submit(function () { return false; });
                $(#GetPrize input:submit).click(function () {
                    if (Phone.val() == ‘‘) {
                        win_alert(请填写手机号码!, function () { Phone.focus() });
                        return false;
                    }
                    if (!(/^13\d{9}$/g.test(Phone.val()) || /^14[57]\d{8}$/g.test(Phone.val()) || /^15[0-35-9]\d{8}$/g.test(Phone.val()) || /^18\d{9}$/g.test(Phone.val()))) {
                        win_alert("" + Phone.val() + "‘不是一个有效的手机号码!", function () { Phone.focus() });
                        return false;
                    }

                    $(this).attr(disabled, true).val(提交中...);
                    $.post(/ActivityWheel/WheelAdd, { WheelUserPhone: Phone.val() }, function (data) {
                        
                            win_alert(data.message, function () {
                                location.href = location.href;
                            });

                        $(#GetPrize input:submit).attr(disabled, false).val(提交);
                    }, json);
                });
            }
        }
        function GetRandomNum(Min, Max) {
            var Range = Max - Min;
            var Rand = Math.random();
            var r = parseInt(Min) + parseInt(Math.round(Rand * Range));
            return r;
        }
        function win_alert(tips, handle) {
            $(body).prepend(<div id="global_win_alert"><div> + tips + </div><h1>确定</h1></div>);
            $(#global_win_alert).css({
                position: fixed,
                left: $(window).width() / 2 - 125,
                top: 30%,
                background: #fff,
                border: 1px solid #ccc,
                opacity: 0.95,
                width: 250,
                z-index: 10000,
                border-radius: 8px
            }).children(div).css({
                text-align: center,
                padding: 30px 10px,
                font-size: 16
            }).siblings(h1).css({
                height: 40,
                line-height: 40px,
                text-align: center,
                border-top: 1px solid #ddd,
                font-weight: bold,
                font-size: 20
            });
            $(#global_win_alert h1).click(function () {
                //if (tips != "已经提交过相同的手机号,请重新填写!") {
                    $(#global_win_alert).remove();
               // }
               
            });
            if ($.isFunction(handle)) {
                if (tips != "您提交的手机号码已经中过奖了!")
                {
                    $(#global_win_alert h1).click(handle);
                }
                
            }
        }

    </script>


    <script language="javascript">$(document).ready(turntable_obj.turntable_init);</script>
</head>
@model EasyWeixin.Model.Wheel
<body>
    <input id="WheelID" type="hidden" value="@Request["WheelID"]" />
    <input id="UserWexinID" type="hidden" value="@Request["UserWexinID"]" />
    <div id="turntable">
        <div id="WheelEvent">
            <div class="demo">
                <div id="disk"></div>
                <div id="start">
                    <img id="startbtn" src="/images/Activity/Wheel/wheel_arrow[1].png">
                </div>
            </div>
            <div id="WinPrize" class="refer none">
                <span>恭喜您抽中 <font id="PrizeClass"></font>(SN码:<font id="SnNumber"></font>),请输入您的手机号并记录SN码,作为领奖凭证!<font style="color:red;">逾期无效!!</font></span>
                <div>
                    <form id="GetPrize">
                        <div class="input">
                            <input type="input" name="MobilePhone" value="" class="form_input" pattern="[0-9]*" maxlength="11" />
                        </div>
                        
                        <div class="input">
                            <input type="submit" value="提交" class="submit" />
                        </div>
                            
                    </form>
                </div>
            </div>
            <div id="turntable_success">提交成功!</div>
            <div class="refer">
                <span>兑换说明(<font color="#FF0000">亲,中奖后请务必输入您的手机号并记录SN码,否则无法领奖喔!</font>)</span>
                <div>
                    @if (Model.WheelItems != null)
                    {
                        foreach (var item in Model.WheelItems)
                        {
                            //var html = item.WheelItemName + ":" + item.WheelItemAward + "。奖品数量:" + item.WheelItemScale + "";
                            var html = item.WheelItemName + ":" + item.WheelItemAward;
                        @html;<br />
                        }
                    }
                </div>
            </div>
            <div class="refer">
                <span>活动说明</span>
                <div>@Html.Raw(Model.WheelDesc)</div>
            </div>
        </div>
    </div>
</body>
</html>
two

  

  后台:

  

bubuko.com,布布扣
 #region 大转盘
        /// <summary>
        /// 大转盘首页
        /// </summary>
        /// <param name="WheelID"></param>
        /// <param name="ImageTextID"></param>
        /// <param name="User_ID"></param>
        /// <param name="UserWexinID"></param>
        /// <returns></returns>
        public ActionResult WheelIndex(Guid WheelID, Guid ImageTextID, Guid User_ID, string UserWexinID = "")
        {
            var user = UserProfileRepository.GetByKey(User_ID);
            Session["UserId"] = user.UserId;
            var Wheel = WheelRepository.GetByKey(WheelID);
            return View(Wheel);
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetWheelitem(Guid WheelID, string UserWexinID = "")
        {
            var ip = Request.UserHostAddress;
            var dt = DateTime.Now.Date;
            var Wheel = WheelRepository.GetByKey(WheelID);
            GetWheelCode(WheelID);

            //声明日志,
            WheelLog WheelCode;
            var logs = WheelLogRepository.FindAll(Specification<WheelLog>.Eval(o => o.WheelID == Wheel.WheelID && o.IsAward > 0)).ToList();
            int num = 0;
            foreach (var item in Wheel.WheelItems)
            {
                num += item.WheelItemScale;
            }

            if (logs.Count >= num)
            {
                WheelCode = new WheelLog();
                WheelCode.WheelCode = "0";
                WheelCode.IsAward = 0;
                WheelCode.AddDate = DateTime.Now;
                WheelCode.WheelID = Wheel.WheelID;
            }
            else
            {
                WheelCode = GetWheelCode(WheelID);
            }



           
            //var WheelCode = GetWheelCode(WheelID);
            WheelCode.IP = ip;
            Session["WheelCode"] = WheelCode;
            int IsAward = WheelCode.IsAward;
            WheelItemViewModel Wheelitem = new WheelItemViewModel();
            if (IsAward > 0)
            {
                var Si = Wheel.WheelItems.ToList()[IsAward - 1];
                Wheelitem = Mapper.Map<WheelItem, WheelItemViewModel>(Si);
                Wheelitem.WheelCode = WheelCode.WheelCode;
            }
            else
            {
                //由于大转盘有十二个位置点,按照索引从0开始到11,其中,0,4,8为中将点,所以设置一个没有0,4,8的不中间数组,而后进行随机处理
                int[] arr = { 1, 2, 3, 5, 6, 7, 9, 10, 11 };
                Random randow = new Random();
                int ran = randow.Next(0, arr.Length - 1);
                string Angle = GetAngle(arr[ran], 12, 5);
                Wheelitem.WheelCode = WheelCode.WheelCode;
                Wheelitem.WheelItemID = IsAward;
                Wheelitem.MaxAngle = Angle.Split(|)[1];
                Wheelitem.MinAngle = Angle.Split(|)[0];
                Wheelitem.WheelItemName = "谢谢惠顾";
            }
            
            var log = WheelLogRepository.FindAll(Specification<WheelLog>.Eval(o => o.IP == ip && o.AddDate > dt && o.WheelID == Wheel.WheelID)).ToList();
            if (log.Count < Wheel.EveryDayTimes && Wheel.StartDate < DateTime.Now && Wheel.EndDate.AddDays(1) > DateTime.Now)
            {
                ///添加日志,记录抽奖结果,不管中奖还是没中都记录
                WheelLogRepository.Add(WheelCode);
                WheelLogRepository.Context.Commit();
                return Json(Wheelitem);
            }
            else if (DateTime.Now < Wheel.StartDate || DateTime.Now > Wheel.EndDate.AddDays(1))
            {
                return Json(new { message = "活动已经结束了!" }, JsonRequestBehavior.AllowGet);

            }
            else
            {
                return Json(new { message = "每日只能提交" + Wheel.EveryDayTimes.ToString() + "" }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 获取一个角度的最大值和最小值字符串
        /// </summary>
        /// <param name="i">角度索引</param>
        /// <param name="num">角度平均分块</param>
        /// <param name="f">角度波动</param>
        /// <returns></returns>
        public string GetAngle(int i, int num, int f)
        {
            //获取平均角度区域
            var average = 360 / num;
            var averagehalf = 360 / (num * 2);
            var half = i * average;
            var str = (half - f).ToString() + "|" + (half + f).ToString();
            return str;
        }
        /// <summary>
        /// 用户提交数据 
        /// </summary>
        /// <param name="WheelUser">大转盘玩家实体,实体中只传递过来玩家的手机号码</param>
        /// <returns></returns>
        public JsonResult WheelAdd(WheelUser WheelUser)
        {


            if (Session["WheelCode"] != null)
            {
                var WheelCode = (WheelLog)Session["WheelCode"];
                var Wheel = WheelRepository.Find(Specification<Wheel>.Eval(o => o.WheelID == WheelCode.WheelID));

                //循环判断是否已存在相同的手机号码
                var users = WheelUserRepository.FindAll(Specification<WheelUser>.Eval(o => o.WheelID == Wheel.WheelID)).ToList();
                foreach (var item in users)
                {
                    
                    if (item.WheelUserPhone.Equals(WheelUser.WheelUserPhone))
                    {

                        return Json(new { message = "您提交的手机号码已经中过奖了!" });
                    }
                    
                   
                }
             
                var ip = Request.UserHostAddress;
                var dt = DateTime.Now.Date;
                WheelUser.WheelItemID = Wheel.WheelItems.ToList()[WheelCode.IsAward - 1].WheelItemID;
                WheelUser.WheelID = WheelCode.WheelID;
                WheelUser.IP = Request.UserHostAddress;
                WheelUser.WheelCode = WheelCode.WheelCode;
                WheelUser.UserId = int.Parse(Session["UserId"].ToString());
                WheelUser.AddDate = DateTime.Now;
                WheelUser.WheelLogID = WheelCode.WheelLogID;
                WheelUserRepository.Add(WheelUser);
                WheelUserRepository.Context.Commit();
                Session["WheelCode"] = null;
                return Json(new { message = "提交成功" });
            }
            else
            {

                return Json(new { message = "已超时!" });
            }
        }
        /// <summary>
        /// 获取一个随机日志实体
        /// </summary>
        /// <param name="WheelID"></param>
        /// <returns></returns>
        public WheelLog GetWheelCode(Guid WheelID)
        {
            //DateTime dt = DateTime.Now.AddHours(-1);
            DateTime dt= DateTime.Now.AddMinutes(-10);
            var Wheel = WheelRepository.GetByKey(WheelID);
            var users = WheelUserRepository.FindAll(Specification<WheelUser>.Eval(o => o.WheelID == Wheel.WheelID)).ToList();
            //清理日志  如果用户中奖 10分钟里没有提交信息 就将日志其删除
            var logs = WheelLogRepository.FindAll(Specification<WheelLog>.Eval(o => o.WheelID == Wheel.WheelID && o.IsAward > 0 && o.AddDate < dt)).ToList();
            foreach (var item in logs)
            {
                if (!users.Exists(o => o.WheelLogID == item.WheelLogID))
                {
                    WheelLogRepository.Remove(item);
                    WheelLogRepository.Context.Commit();
                }
            }
            var userWheelCodes = users.Select(o => o.WheelCode).ToList();
            //获取奖品的概率容量
            int container = Wheel.WheelScale;
            var WheelItems = Wheel.WheelItems.ToList();

            for (int i = 0; i < WheelItems.Count; i++)
            {
                container = container - WheelItems[i].WheelItemScale;

            }
            //设定一个string动态数组 添加中奖代号
            List<WheelLog> WheelCodes = new List<WheelLog>();
            for (int i = 0; i < WheelItems.Count; i++)
            {
                //剩余奖品数量
                var k = i + 1;
                var itemlogs = WheelLogRepository.FindAll(Specification<WheelLog>.Eval(o => o.WheelID == Wheel.WheelID && o.IsAward == k)).ToList();
                var sum = WheelItems[i].WheelItemScale - itemlogs.Count;
                for (int j = 0; j < sum; j++)
                {
                    WheelLog log = new WheelLog();
                    log.WheelCode = Function.GuidTo16String(Guid.NewGuid());
                    log.IsAward = k;
                    log.AddDate = DateTime.Now;
                    log.WheelID = Wheel.WheelID;
                    WheelCodes.Add(log);
                }
            }

            //不中奖的概率容量,从而添加不中奖的日志信息
            if (container > 0)
            {
                for (int i = 0; i < container; i++)
                {
                    WheelLog log = new WheelLog();
                    log.WheelCode = "0";
                    log.IsAward = 0;
                    log.AddDate = DateTime.Now;
                    log.WheelID = Wheel.WheelID;
                    WheelCodes.Add(log);
                }
            }
            //获取一个随机数 随机抽奖
            Random randow = new Random();
            int ran = randow.Next(0, WheelCodes.Count - 1);
            return WheelCodes[ran];
        }
        #endregion
three

 

微信大转盘

标签:des   style   blog   http   color   os   java   io   ar   

原文地址:http://www.cnblogs.com/angelgril/p/3944282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!