NopCommerce 4.2 之微信小程序 - 每日签到功能

时间:2019-11-28 19:02:38   收藏:0   阅读:240
        /// <summary>
        /// 调用接口自动签到,并返回签到结果
        /// </summary>
        /// <returns></returns>
        [Route("index")]
        [APIFilter(true, true)]
        public APIResult<object> index()
        {
            //设置为驼峰命名(即将实体全部首字母小写处理)
            var serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            SignInDetailModel model = new SignInDetailModel();
            //配置信息需要传递
            model.Config = new SignInDetailModel.ConfigModel()
            {
                DayIntegral = _customerSignInSettings.DayIntegral,
                DurationReward = _customerSignInSettings.DurationReward,
                DurationCycle = _customerSignInSettings.DurationCycle,
                IsEnable = _customerSignInSettings.IsEnable
            };
            if (model.Config.IsEnable)
            {
                return Success(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(model, Formatting.None, serializerSettings)), "");
            }

            model.IsCurSign = false;
            var signday = false;//是否当天签到
            if (CanSignInByToday(_workContext.CurrentCustomer.Id))
            {
                //签到
                var signinfo = GetCustomerSignIn(_workContext.CurrentCustomer.Id);
                DateTime oldlastsigntime = signinfo.LastSignTime.HasValue? signinfo.LastSignTime.Value:DateTime.UtcNow.AddYears(-1);//给一个默认的时间点(这里就给一个一年前的时间点)
                signinfo.LastSignTime = DateTime.UtcNow;
                signinfo.SignDaySum += 1;//签到的次数(单纯统计使用)
                //处理积分
                int needAddIntegral = _customerSignInSettings.DayIntegral;  //需要增加的各分
                bool isReward = false;


                //连续登录
                if (oldlastsigntime.Date == (DateTime.UtcNow.AddDays(-1).Date))
                {
                    //连续签到
                    signinfo.DurationDaySum += 1;

                    if (_customerSignInSettings.DurationCycle > 0)
                    {
                        signinfo.DurationDay += 1;
                        if (signinfo.DurationDay > _customerSignInSettings.DurationCycle)
                        {
                      
                            signinfo.DurationDay = 1;
                        }
                    
                        if (signinfo.DurationDay == _customerSignInSettings.DurationCycle)
                        {
                            needAddIntegral += _customerSignInSettings.DurationReward;
                            isReward = true;
                        }
                    }
                    else
                    {
                        signinfo.DurationDay = 1;
                    }
                }
                else
                {
                    //没有连续签到,从一天开始签到
                    signinfo.DurationDay = 1;
                    signinfo.DurationDaySum = 1;
                }
                //签到送积分
                if (_rewardPointsSettings.Enabled)
                {
                    var rewardPoints = needAddIntegral;
                    var customer = _customerService.GetCustomerById(_workContext.CurrentCustomer.Id);
                    _rewardPointService.AddRewardPointsHistoryEntry(customer, rewardPoints, _storeContext.CurrentStore.Id,
                    _localizationService.GetResource("RewardPoints.Message.PointsForCustomerSignIn"));
                }

                //当前签到
                signday = true;

            }
            //当天签到
            if (signday)
            {
                model.IsCurSign = true;
            }
           
            var customerSigninfo = GetCustomerSignIn(_workContext.CurrentCustomer.Id);
            model.CurSignDurationDay = customerSigninfo.DurationDay;
            model.CurSignDaySum = customerSigninfo.SignDaySum;
            //用户信息
            model.CustomerId = _workContext.CurrentCustomer.Id;
            model.CustomerName = _workContext.CurrentCustomer.Username;
            //获取当前用户的可用积分
            int rewardPointsBalance = _rewardPointService.GetRewardPointsBalance(_workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id);
            decimal rewardPointsAmountBase = _orderTotalCalculationService.ConvertRewardPointsToAmount(rewardPointsBalance);//将积分转换为价值
            decimal rewardPointsAmount = _currencyService.ConvertFromPrimaryStoreCurrency(rewardPointsAmountBase, _workContext.WorkingCurrency);
            model.RewardPointsBalance = rewardPointsBalance;//
            model.RewardPointsAmount = _priceFormatter.FormatPrice(rewardPointsAmount, true, false);//价值格式化为本地货币

            return Success(JsonConvert.DeserializeObject(JsonConvert.SerializeObject(model, Formatting.None, serializerSettings)), "");



        }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!