/// <summary>
        /// 商品兑换
        /// </summary>
        public override async Task RedeemAsync()
        {
            if (await GetCreditAsync() < Gift.Value)
            {
                throw new Exception(Errors.NotEnoughCredit);
            }

            if (string.IsNullOrWhiteSpace(User.Email))
            {
                throw new Exception(Errors.EmailNonExistent);
            }

            var order = new Models.CouponGiftOrder
            {
                UserId      = User.Id,
                GiftId      = Gift.Id,
                RedeemPrice = Gift.Price
            };

            _dbContext.CouponGiftOrders.Add(order);
            await _dbContext.SaveChangesAsync();

            await _coupon.UpdateAsync(User, CouponEvent.兑换商品, -Gift.Price, new { CouponGiftId = Gift.Id });

            var userManager = Global.Container.GetInstance <KeylolUserManager>();
            await userManager.SendEmailAsync(User.Id, "你的 Steam 钱包卡兑换订单", GenerateEmail(order));

            await userManager.SendSteamChatMessageAsync(User,
                                                        $"你的 Steam 钱包卡兑换订单已生成,详细信息已发送到你登记的邮箱 ({User.Email}) 中。若五分钟内没有收到任何邮件,请检查你的垃圾邮件文件夹,或与我们的站务职员邮箱 ([email protected]) 联络。");

            await userManager.EmailService.SendAsync(new IdentityMessage
            {
                Destination = "[email protected]; [email protected]",
                Subject     = $"用户兑换 Steam 充值卡通知 - {User.UserName}",
                Body        = $@"<p>订单编号:{order.Id}<br />
UIC:{User.IdCode}<br />
用户名:{User.UserName}<br />
Email:{User.Email}<br />
兑换时间:{order.RedeemTime.ToString("yyyy-MM-dd HH:mm:ss")}<br />
面值:¥{Gift.Value} CNY<br />
价格:{order.RedeemPrice} ◆</p>"
            });
        }
示例#2
0
        /// <summary>
        /// 创建 <see cref="CurrentUser"/>
        /// </summary>
        /// <param name="user">用户对象</param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="coupon"><see cref="CouponProvider"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <returns><see cref="CurrentUser"/></returns>
        public static async Task <CurrentUser> CreateAsync(KeylolUser user, KeylolUserManager userManager,
                                                           KeylolDbContext dbContext, CouponProvider coupon, CachedDataProvider cachedData)
        {
            // 每日访问奖励
            if (DateTime.Now.Date > user.LastDailyRewardTime.Date)
            {
                user.LastDailyRewardTime = DateTime.Now;
                user.FreeLike            = 5; // 免费认可重置
                try
                {
                    await dbContext.SaveChangesAsync();

                    await coupon.UpdateAsync(user, CouponEvent.每日访问);
                }
                catch (DbUpdateConcurrencyException)
                {
                }
            }

            // Steam 游戏记录更新
            SteamCrawlerProvider.UpdateUserSteamGameRecords(user.Id);

            // Steam 好友列表更新
            SteamCrawlerProvider.UpdateUserSteamFrineds(user.Id);

            return(new CurrentUser
            {
                Id = user.Id,
                UserName = user.UserName,
                IdCode = user.IdCode,
                Roles = (await userManager.GetRolesAsync(user.Id)).ToList(),
                AvatarImage = user.AvatarImage,
                MessageCount = await cachedData.Messages.GetUserUnreadMessageCountAsync(user.Id),
                Coupon = user.Coupon,
                PreferredPointName = user.PreferredPointName,
                OpenInNewWindow = user.OpenInNewWindow
            });
        }