Beispiel #1
0
        public CheckUserLoginResult CheckUserLogin()
        {
            // login
            // /api/rest/checkuserlogin
            // method: get

            var history = new MongoHistoryAPI()
            {
                CreateTime = DateTime.Now,
                APIUrl     = "/api/user/checkuserlogin"
            };

            HttpRequestHeaders headers = Request.Headers;

            if (!headers.Contains("Authorization"))
            {
                throw new Exception("Nead authorization info");
            }

            string token;

            try
            {
                string base64Auth = headers.GetValues("Authorization").First().Replace("Basic", "").Trim();
                token = XString.FromBase64(base64Auth);
            }
            catch
            {
                throw new Exception("Wrong authorization info");
            }

            var arrtok = token.Split(':');

            if (arrtok.Length != 2)
            {
                throw new Exception("Wrong authorization format");
            }

            string user  = arrtok[0];
            string phone = arrtok[1];

            var result = new CheckUserLoginResult()
            {
                id   = "1",
                msg  = "success",
                user = user
            };

            try
            {
                var checkUser = db.AspNetUsers.Where(p => p.UserName == user).FirstOrDefault();

                if (checkUser == null)
                {
                    throw new Exception("Tài khoản không hợp lệ");
                }

                if (checkUser.AccountType == "STAFF")
                {
                    var haiStaff = db.HaiStaffs.Where(p => p.UserLogin == user).FirstOrDefault();

                    if (haiStaff != null)
                    {
                        if (haiStaff.IsLock == 1)
                        {
                            throw new Exception("Tài khoản đang tạm khóa");
                        }

                        result.id = "1";
                    }
                    else
                    {
                        throw new Exception("Tài khoản không hợp lệ");
                    }
                }
                else
                {
                    var check = db.CInfoCommons.Where(p => p.UserLogin == user).FirstOrDefault();
                    if (check != null)
                    {
                        result.id    = "2";
                        result.name  = check.CDeputy;
                        result.store = check.CName;
                        result.code  = check.CCode;
                        result.phone = check.Phone;

                        if (check.Phone != null)
                        {
                            // kiem tra phone
                            var phoneOrige = check.Phone;
                            if (check.Phone.Substring(0, 2) == "84")
                            {
                                phoneOrige = "0" + check.Phone.Substring(2, check.Phone.Length - 2);
                            }
                            if (phone == phoneOrige)
                            {
                                result.id = "3";
                                // cho dang nhap luon
                                bool isActive = false;

                                var staff = db.HaiStaffs.Where(p => p.UserLogin == user).FirstOrDefault();
                                if (staff != null)
                                {
                                    if (staff.IsLock != 1)
                                    {
                                        isActive = true;
                                    }
                                }
                                else
                                {
                                    var agency = db.CInfoCommons.Where(p => p.UserLogin == user).FirstOrDefault();
                                    if (agency != null)
                                    {
                                        if (check.CType == "CII")
                                        {
                                            var checkC2 = check.C2Info.FirstOrDefault();
                                            if (checkC2 != null)
                                            {
                                                if (checkC2.IsActive == 0)
                                                {
                                                    isActive = false;
                                                }
                                            }
                                        }
                                    }
                                }


                                if (!isActive)
                                {
                                    throw new Exception("Tài khoản bị khóa");
                                }

                                var info = updateAuth(user);

                                result.role  = info.Role;
                                result.token = info.token;
                                result.type  = info.type;
                            }
                            else
                            {
                                string Msg     = string.Empty;
                                var    account = db.SmsAccounts.Find(1);
                                Random random  = new Random();
                                var    otp     = random.Next(100000, 999999);

                                // update otp old
                                var allOtp = db.SMSCodes.Where(p => p.UserLogin == user && p.CStatus == 0).ToList();
                                foreach (var item in allOtp)
                                {
                                    item.CStatus         = 1;
                                    db.Entry(item).State = EntityState.Modified;
                                    db.SaveChanges();
                                }

                                SMSCode smsCode = new SMSCode()
                                {
                                    Id        = Guid.NewGuid().ToString(),
                                    Code      = Convert.ToString(otp),
                                    CreateAt  = DateTime.Now,
                                    CStatus   = 0,
                                    UserLogin = user
                                };

                                db.SMSCodes.Add(smsCode);
                                db.SaveChanges();

                                // send sms
                                SMScore _smsCore = new SMScore(account.BrandName, account.UserName, account.Pass);
                                _smsCore.IPserver   = account.AddressSend;
                                _smsCore.Port       = Convert.ToInt32(account.PortSend);
                                _smsCore.SendMethod = account.Method;

                                _smsCore.SendSMS("Cam on quy khach da dang ky, ma kich hoat cua quy khach la : " + otp, check.Phone, ref Msg);
                            }
                        }

                        else
                        {
                            throw new Exception("Quý khách chưa đăng kí số điện thoại với HAI để nhận mà kích hoặt");
                        }
                    }
                    else
                    {
                        throw new Exception("Tài khoản không hợp lệ");
                    }
                }
            }
            catch (Exception e)
            {
                result.id  = "0";
                result.msg = e.Message;
            }

            history.ReturnInfo = new JavaScriptSerializer().Serialize(result);
            mongoHelper.createHistoryAPI(history);

            return(result);
        }
Beispiel #2
0
        public ActionResult SendSMS(string phone, string messenge)
        {
            if (!Utitl.CheckUser(db, User.Identity.Name, "ManageNotification", 1))
            {
                return(RedirectToAction("relogin", "home"));
            }

            var    account = db.SmsAccounts.Find(1);
            string Msg     = string.Empty;

            if (account != null)
            {
                SMScore _smsCore = new SMScore(account.BrandName, account.UserName, account.Pass);
                _smsCore.IPserver   = account.AddressSend;
                _smsCore.Port       = Convert.ToInt32(account.PortSend);
                _smsCore.SendMethod = account.Method;

                var listPhone = phone.Split(';');


                if (listPhone.Count() == 1)
                {
                    var result = _smsCore.SendSMS(messenge, listPhone[0], ref Msg);
                    if (result)
                    {
                        var history = new SendSmsHistory()
                        {
                            Id         = Guid.NewGuid().ToString(),
                            Phone      = listPhone[0],
                            Messenge   = messenge,
                            UserSend   = User.Identity.Name,
                            CreateTime = DateTime.Now,
                            StatusSend = "Đã gửi thành công"
                        };
                        db.SendSmsHistories.Add(history);
                        db.SaveChanges();
                    }
                    else
                    {
                        var history = new SendSmsHistory()
                        {
                            Id         = Guid.NewGuid().ToString(),
                            Phone      = listPhone[0],
                            Messenge   = messenge,
                            UserSend   = User.Identity.Name,
                            CreateTime = DateTime.Now,
                            StatusSend = Msg
                        };
                        db.SendSmsHistories.Add(history);
                        db.SaveChanges();
                    }
                }
                else if (listPhone.Count() > 1)
                {
                    List <SMSUtl.SendMessageResult> SMSMessageResult = null;
                    List <SMSUtl.Message>           SMSMessages      = new List <SMSUtl.Message>();
                    foreach (var item in listPhone)
                    {
                        if (!String.IsNullOrEmpty(item))
                        {
                            SMSUtl.Message msg = new SMSUtl.Message();
                            msg.Phone   = item;
                            msg.Content = messenge;
                            SMSMessages.Add(msg);
                        }
                    }

                    SMSMessageResult = _smsCore.SendMultiSMS(SMSMessages, ref Msg);

                    for (var i = 0; i < SMSMessageResult.Count(); i++)
                    {
                        if (SMSMessageResult[i].Status == 1)
                        {
                            var history = new SendSmsHistory()
                            {
                                Id         = Guid.NewGuid().ToString(),
                                Phone      = listPhone[i],
                                Messenge   = messenge,
                                UserSend   = User.Identity.Name,
                                CreateTime = DateTime.Now,
                                StatusSend = "Đã gửi thành công"
                            };
                            db.SendSmsHistories.Add(history);
                        }
                        else
                        {
                            var history = new SendSmsHistory()
                            {
                                Id         = Guid.NewGuid().ToString(),
                                Phone      = listPhone[i],
                                Messenge   = messenge,
                                UserSend   = User.Identity.Name,
                                CreateTime = DateTime.Now,
                                StatusSend = SMSMessageResult[i].Message
                            };
                            db.SendSmsHistories.Add(history);
                        }
                    }
                    db.SaveChanges();
                }
            }

            return(RedirectToAction("sendsms", "notification"));
        }