Beispiel #1
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="users">用户</param>
        /// <returns></returns>
        public async Task <int> AddUsers(Users users)
        {
            int count = 0;

            try
            {
                await Task.Run(() =>
                {
                    DB.Users.Add(users);
                    count = DB.SaveChanges();
                });
            }
            catch (Exception ex) { Console.WriteLine("出错啦!" + ex.Message.ToString()); }
            return(count);
        }
Beispiel #2
0
        /// <summary>
        /// 添加到购物车
        /// </summary>
        /// <param name="buyGoods"></param>
        /// <returns></returns>
        public async Task <int> AddBuyGoods(BuyGoods buyGoods)
        {
            int count = 0;

            try
            {
                await Task.Run(() =>
                {
                    DB.Add(buyGoods);
                    count = DB.SaveChanges();
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("获取错误信息:{0}", ex.Message.ToString());
            }
            return(count);
        }
        /// <summary>
        ///管理人员登陆后台
        /// </summary>
        /// <param name="adminId">管理人员账户</param>
        /// <param name="password">管理登陆密码</param>
        /// <returns></returns>
        public int GetAdministrator(string adminId, string password)
        {
            List <Administrators> listadmin = DB.Administrators.ToList();

            if (listadmin.Count == 0)
            {
                Administrators a = new Administrators
                {
                    AdminId       = "admin",
                    AdminPassword = "******"
                };
                DB.Administrators.Add(a);
                DB.SaveChanges();
            }
            listadmin = DB.Administrators.ToList();
            listadmin = listadmin.Where(x => x.AdminId == adminId && x.AdminPassword == password).ToList();
            return(listadmin.Count);
        }
        public TokenGenerator(UserManager <ApplicationUser> userManager, OnlineMarketContext db)
        {
            _userManager = userManager;
            _db          = db;
            var expiredTokens = _db.Tokens.Where(x => (DateTime.Now - x.RegDateTime).TotalMinutes > 5 || x.Used == true).ToList();

            if (expiredTokens.Count > 0)
            {
                _db.Tokens.RemoveRange(expiredTokens);
                _db.SaveChanges();
            }
        }
Beispiel #5
0
        }//end Signup

        public async Task <IActionResult> SignupConfirm(SignupViewModel model, List <IFormFile> img)
        {
            string nvm;

            try
            {
                if (!User.Identity.IsAuthenticated)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Require_Login, contentRootPath);
                    return(RedirectToAction("Signin", new { notification = nvm }));
                }
                if (!ModelState.IsValid)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                    return(RedirectToAction("Signup", new { notification = nvm }));
                }
                else
                {
                    if (model != null)
                    {
                        var chkUser = await _userManager.FindByNameAsync(model.Username);

                        ApplicationUser chkEmail;
                        if (model.Email != null)
                        {
                            chkEmail = await _userManager.FindByEmailAsync(model.Email);
                        }
                        else
                        {
                            chkEmail = null;
                        }
                        if (chkUser == null && chkEmail == null)
                        {
                            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                            var greDate = CustomizeCalendar.PersianToGregorian(model.Dateofbirth ?? DateTime.Now);

                            //create new user
                            ApplicationUser newUser = new ApplicationUser()
                            {
                                FirstName     = model.Firstname,
                                LastName      = model.Lastname,
                                UserName      = model.Username,
                                Email         = model.Email,
                                PhoneNumber   = model.Phonenumber,
                                SpecialUser   = model.Specialuser,
                                Status        = model.Status,
                                DefinedByUser = currentUser,
                                DateOfBirth   = greDate,
                                Rank          = 1,
                                NationalCode  = model.Nationalcode
                            };
                            if (model.Gender == true)
                            {
                                newUser.Gendre = 1; //male
                            }
                            else if (model.Gender == false)
                            {
                                newUser.Gendre = 2; //female
                            }
                            else
                            {
                                newUser.Gendre = null; //not specified
                            }

                            var user_status = await _userManager.CreateAsync(newUser, model.Password);

                            if (user_status.Succeeded)
                            {
                                await _userManager.AddToRoleAsync(newUser, "SuperVisor");

                                //Add image of user in 'UserImage' table, seperately
                                if (img.Count > 0)
                                {
                                    var newAddedUser = await _userManager.FindByNameAsync(newUser.UserName);

                                    if (newAddedUser != null)
                                    {
                                        try
                                        {
                                            UserImage userImage = new UserImage()
                                            {
                                                UserId  = newAddedUser.Id,
                                                Caption = newAddedUser.LastName + "_" + DateTime.Now
                                            };
                                            img.ForEach(x =>
                                            {
                                                if (x != null)
                                                {
                                                    byte[] b = new byte[x.Length];
                                                    x.OpenReadStream().Read(b, 0, b.Length);
                                                    userImage.Image = b;

                                                    //Make Thumbnail
                                                    MemoryStream mem1 = new MemoryStream(b);
                                                    Image img2        = Image.FromStream(mem1);
                                                    Bitmap bmp        = new Bitmap(img2, 120, 120);
                                                    MemoryStream mem2 = new MemoryStream();
                                                    bmp.Save(mem2, System.Drawing.Imaging.ImageFormat.Jpeg);
                                                    userImage.ImageThumbnail = mem2.ToArray();
                                                }
                                            });
                                            _db.UserImage.Add(userImage);
                                            _db.SaveChanges();
                                        }
                                        catch (Exception)
                                        {
                                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert_Image, contentRootPath);
                                            return(RedirectToAction("Signup", new { notification = nvm }));
                                        }
                                    }
                                }

                                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Insert, contentRootPath);
                                return(RedirectToAction("Signup", new { notification = nvm }));
                            }
                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                            return(RedirectToAction("Signup", new { notification = nvm }));
                        }
                        else
                        {
                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                            return(RedirectToAction("Signup", new { notification = nvm }));
                        }
                    }
                    else
                    {
                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                        return(RedirectToAction("Signup", new { notification = nvm }));
                    }
                }
            }
            catch (Exception)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("Signup", new { notification = nvm }));
            }
        }//end SignupConfirm
        /// <summary>
        /// Return Parameters: 1 => SentBySMS, 2 => SentByEmail, 0 => nothing happens, -1 => Error
        /// </summary>
        /// <param name="_User"></param>
        /// <returns></returns>
        public async Task <int> SendVerificationToken(ApplicationUser _User)
        {
            try
            {
                if (_User != null)
                {
                    var    setting = _db.Setting.FirstOrDefault();
                    string token   = await GenerateForPhoneNumber(_User);

                    //string message = $"کد تایید شما: {token}";
                    string message    = token;
                    var    lastTokens = _db.Tokens.Where(x => x.UserId == _User.Id).ToList();
                    if (lastTokens.Count > 0)
                    {
                        _db.Tokens.RemoveRange(lastTokens);
                        _db.SaveChanges();
                    }

                    Tokens tokens = new Tokens()
                    {
                        UserId = _User.Id,
                        Token  = token,
                        Used   = false
                    };
                    await _db.Tokens.AddAsync(tokens);

                    await _db.SaveChangesAsync();

                    var isUserName_Number = double.TryParse(_User.UserName, out double r) ? r : 0;
                    if (isUserName_Number > 0)
                    {
                        //متن پیامک
                        //string message = smsText;
                        //شماره مقصد
                        string mobileNumber = _User.UserName;
                        var    result       = SmsIrService.SendVerificationCode(setting.SMSApiAddress, setting.SMSUsername, message, mobileNumber);

                        //SMSService.SMSService SMS = new SMSService.SMSService(_db);
                        //SMS.SendSMS(new List<string> { _User.UserName }, message);
                        return(1);
                    }
                    else
                    {
                        //still not complete....

                        EmailViewModel emailViewModel = new EmailViewModel()
                        {
                            Subject       = "ایمیل تاییدیه ثبت نام",
                            ReceiverEmail = _User.UserName,
                            Content       = message,
                            SenderEmail   = setting.AdminEmail,
                            Password      = setting.AdminEmailPassword
                        };
                        var port = int.TryParse(setting.EmailPort, out int rr) ? rr : 587;
                        EmailService.EmailService.Send(emailViewModel, setting.EmailServiceProvider, port);

                        return(2);
                    }
                }
                return(0);
            }
            catch (Exception ex)
            {
                return(-1);
            }
        }//end SendVerificationToken
Beispiel #7
0
        public async Task <IActionResult> FillAdditionalTables(AdditionalTablesViewModel model)
        {
            string nvm;
            bool   countryOK = false, provinceOK = false, cityOK = false;

            try
            {
                if (ModelState.IsValid)
                {
                    string folderPath = _configuration.GetSection("DefaultPaths").GetSection("DataSet").Value;
                    if (model.Country != null)
                    {
                        var tempFile = await FileManager.ReadAndSaveFile(contentRootPath, folderPath, model.Country);

                        var            combinedPath       = Path.Combine(contentRootPath, tempFile);
                        var            renderedCountyFile = ReadExcelFiles.Read(combinedPath);
                        List <Country> countries          = new List <Country>();
                        for (int i = 0; i < renderedCountyFile.Count; i++)
                        {
                            Country country = new Country()
                            {
                                Name = renderedCountyFile[i][0]
                            };
                            countries.Add(country);
                        }
                        if (model.RewriteCountry == true)
                        {
                            _db.Country.RemoveRange(_db.Country.ToList());
                            _db.SaveChanges();
                        }
                        countries = countries.OrderBy(x => x.Name).ToList();
                        _db.Country.AddRange(countries);
                        var result  = _db.SaveChanges();
                        var deleted = FileManager.DeleteFile(contentRootPath, tempFile);
                        if (result == 1)
                        {
                            countryOK = true;
                        }
                    }//end Country

                    if (model.Province != null)
                    {
                        var tempFile = await FileManager.ReadAndSaveFile(contentRootPath, folderPath, model.Province);

                        var             combinedPath         = Path.Combine(contentRootPath, tempFile);
                        var             renderedProvinceFile = ReadExcelFiles.Read(combinedPath);
                        List <Province> provinces            = new List <Province>();
                        for (int i = 0; i < renderedProvinceFile.Count; i++)
                        {
                            Province province = new Province()
                            {
                                Name = renderedProvinceFile[i][0]
                            };
                            provinces.Add(province);
                        }
                        if (model.RewriteProvince == true)
                        {
                            _db.Province.RemoveRange(_db.Province.ToList());
                            _db.SaveChanges();
                        }
                        provinces = provinces.OrderBy(x => x.Name).ToList();
                        _db.Province.AddRange(provinces);
                        var result  = _db.SaveChanges();
                        var deleted = FileManager.DeleteFile(contentRootPath, tempFile);
                        if (result == 1)
                        {
                            provinceOK = true;
                        }
                    }//end Province

                    if (model.City != null)
                    {
                        var tempFile = await FileManager.ReadAndSaveFile(contentRootPath, folderPath, model.City);

                        var         combinedPath     = Path.Combine(contentRootPath, tempFile);
                        var         renderedCityFile = ReadExcelFiles.Read(combinedPath);
                        List <City> cities           = new List <City>();
                        for (int i = 0; i < renderedCityFile.Count; i++)
                        {
                            City city = new City()
                            {
                                Name = renderedCityFile[i][0]
                            };
                            cities.Add(city);
                        }
                        if (model.RewriteCity == true)
                        {
                            _db.City.RemoveRange(_db.City.ToList());
                            _db.SaveChanges();
                        }
                        cities = cities.OrderBy(x => x.Name).ToList();
                        _db.City.AddRange(cities);
                        var result  = _db.SaveChanges();
                        var deleted = FileManager.DeleteFile(contentRootPath, tempFile);
                        if (result == 1)
                        {
                            cityOK = true;
                        }
                    }//end City

                    if (countryOK == true && provinceOK == true && cityOK == true)
                    {
                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Insert, contentRootPath);
                        return(RedirectToAction("FillTables", new { notification = nvm })); //successful insert
                    }
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                return(RedirectToAction("FillTables", new { notification = nvm })); //failed insert
            }
            catch (Exception ex)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("FillTables", new { notification = nvm })); //failed insert
            }
        }