Ejemplo n.º 1
0
        public async Task <GetLearnerInfoResponse> InspectGetLearnerInfo(string token)
        {
            try
            {
                await Task.Run(() =>
                               LogRequest(token, "InspectGetLearnerInfo", Request.HttpContext.Connection.RemoteIpAddress.ToString()));

                if (string.IsNullOrEmpty(token))
                {
                    Log.Error("InspectGetLearnerInfo,{0},token={1}, from {2}",
                              Global.Status[responseCode.TokenError].Description, "invalid", Request.HttpContext.Connection.RemoteIpAddress);
                    return(new GetLearnerInfoResponse
                    {
                        StatusCode = Global.Status[responseCode.TokenError].StatusCode,
                        Description = Global.Status[responseCode.TokenError].Description
                    });
                }
                Log.Information("InspectGetLearnerInfo,input={0},from {1}",
                                token, Request.HttpContext.Connection.RemoteIpAddress);
                var found    = false;
                var identity = string.Empty;
                foreach (var a in tokens)
                {
                    if (a.Token == token)
                    {
                        identity = a.Identity;
                        found    = true;
                        break;
                    }
                }
                if (!found)
                {
                    Log.Error("InspectGetLearnerInfo,{0},token={1},no corresponding identity",
                              Global.Status[responseCode.TokenError].Description, token);
                    return(new GetLearnerInfoResponse
                    {
                        StatusCode = Global.Status[responseCode.TokenError].StatusCode,
                        Description = Global.Status[responseCode.TokenError].Description + token
                    });
                }
                var cryptographicid = CryptographyHelpers.StudyEncrypt(identity);
                var theuser         = _db1.User.FirstOrDefault(async => async.Identity == identity || async.Identity == cryptographicid);
                // var theuser = _db1.User.FirstOrDefault(async => async.Identity == identity);
                if (theuser == null)
                {
                    Log.Error("InspectGetLearnerInfo,{0},identity={1}",
                              Global.Status[responseCode.InvalidIdentiy].Description, identity);
                    return(new GetLearnerInfoResponse
                    {
                        StatusCode = Global.Status[responseCode.InvalidIdentiy].StatusCode,
                        Description = Global.Status[responseCode.InvalidIdentiy].Description
                    });
                }
                var pic     = new byte[1];
                var photook = true;
                try
                {
                    Log.Information("InspectGetLearnerInfo,{0},={1}", Global.PhotoPath, Global.PhotoPath);
                    var ff = identity + ".jpg";
                    if (!string.IsNullOrEmpty(theuser.Photofile))
                    {
                        ff = theuser.Photofile;
                        var filename = Path.Combine(Global.PhotoPath, ff);
                        pic = CryptographyHelpers.StudyFileDecrypt(filename);
                    }
                    else
                    {
                        var filename = Path.Combine(Global.PhotoPath, ff);
                        pic = System.IO.File.ReadAllBytes(filename);
                    }
                }
                catch (Exception ex)
                {
                    photook = false;
                    Log.Error("InspectGetLearnerInfo,{0},={1}", identity, ex.Message);
                }
                theuser.Token    = token;
                theuser.Syncdate = DateTime.Now;
                _db1.SaveChanges();
                return(new GetLearnerInfoResponse
                {
                    StatusCode = Global.Status[responseCode.ok].StatusCode,
                    Description = Global.Status[responseCode.ok].Description,
                    DrivingLicenseType = string.IsNullOrEmpty(theuser.Licensetype) ? DrivingLicenseType.Unknown : (DrivingLicenseType)int.Parse(theuser.Licensetype),

                    //   Identity = theuser.Identity,
                    Name = theuser.Name,
                    PhotoOk = photook,
                    Photo = pic
                });
            }
            catch (Exception ex)
            {
                Log.Error("InspectGetLearnerInfo,{0},{2},exception={1}",
                          Global.Status[responseCode.ProgramError].Description, ex, Global.PhotoPath);
                return(new GetLearnerInfoResponse
                {
                    StatusCode = Global.Status[responseCode.ProgramError].StatusCode,
                    Description = Global.Status[responseCode.ProgramError].Description
                });
            }
        }
Ejemplo n.º 2
0
        public async Task <LoginAndQueryResponse> LoginAndQuery([FromBody] LoginAndQueryRequest inputRequest)
        {
            try
            {
                var input = JsonConvert.SerializeObject(inputRequest);
                await Task.Run(() =>
                               LogRequest(input, "LoginAndQuery", Request.HttpContext.Connection.RemoteIpAddress.ToString()));

                if (inputRequest == null)
                {
                    Log.Error("LoginAndQuery,{0}", Global.Status[responseCode.studyRequestError].Description);
                    return(new LoginAndQueryResponse
                    {
                        StatusCode = Global.Status[responseCode.studyRequestError].StatusCode,
                        Description = Global.Status[responseCode.studyRequestError].Description
                    });
                }
                Log.Information("LoginAndQuery,input={0},from {1}", input, Request.HttpContext.Connection.RemoteIpAddress);
                var allstatus       = string.Empty;
                var allow           = true;
                var completed       = true;
                var signed          = true;
                var firstsigned     = true;
                var drivinglicense  = string.Empty;
                var deductedmarks   = 0;
                var identity        = inputRequest.Identity;
                var fname           = identity + ".jpg";
                var cryptographicid = CryptographyHelpers.StudyEncrypt(identity);
                var pic             = new byte[8];


                //token process
                var toke1n    = GetToken();
                var found     = false;
                var lasttoken = string.Empty;
                foreach (var a in tokens)
                {
                    if (a.Identity == identity)
                    {
                        //  lasttoken = a.Token;
                        a.Token = toke1n;
                        found   = true;
                        break;
                    }
                }
                if (!found)
                {
                    tokens.Add(new Ptoken {
                        Identity = identity, Token = toke1n
                    });
                }

                var theuser = _db1.User.FirstOrDefault(async => (async.Identity == identity || async.Identity == cryptographicid) &&
                                                       async.Inspect == "1"
                                                       );
                if (theuser == null)
                {
                    var his = _db1.History.Where(async => async.Identity == identity || async.Identity == cryptographicid)
                              .OrderBy(al => al.Finishdate).LastOrDefault();
                    if (his == null)
                    {
                        Log.Error("LoginAndQuery,{0}", Global.Status[responseCode.studyNotNecessary].Description + identity);
                        return(new LoginAndQueryResponse
                        {
                            StatusCode = Global.Status[responseCode.studyNotNecessary].StatusCode,
                            Description = Global.Status[responseCode.studyNotNecessary].Description + identity
                        });
                    }
                    allow = his.Drugrelated != "1" ? true : false;
                    if (!string.IsNullOrEmpty(his.Status) && (his.Status.Contains("H") || his.Status.Contains("M")))
                    {
                        allow = false;
                    }
                    completed   = his.Completed == "1" ? true : false;
                    signed      = his.Signed == "1" ? true : false;
                    firstsigned = his.Firstsigned == "1" ? true : false;
                    if (!string.IsNullOrEmpty(his.Licensetype))
                    {
                        drivinglicense = his.Licensetype;
                    }
                    if (his.Deductedmarks != null)
                    {
                        deductedmarks = (int)his?.Deductedmarks;
                    }
                    try
                    {
                        if (!string.IsNullOrEmpty(his.Photofile))
                        {
                            fname = his.Photofile;
                            pic   = CryptographyHelpers.StudyFileDecrypt(Path.Combine(Global.PhotoPath, fname));
                        }
                        else
                        {
                            var filename = Path.Combine(Global.PhotoPath, fname);
                            pic = System.IO.File.ReadAllBytes(filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("loginandquery,{0},={1}", identity, ex.Message);
                    }
                    if (allow)
                    {
                        allstatus = his.Studylog;
                    }
                    else
                    {
                        allstatus = "您不能参加网络学习,可以参加现场学习";
                    }
                }
                else
                {
                    //drugrelated judge
                    allow = theuser.Drugrelated != "1" ? true : false;
                    if (!string.IsNullOrEmpty(theuser.Status) && (theuser.Status.Contains("H") || theuser.Status.Contains("M")))
                    {
                        allow = false;
                    }
                    completed   = theuser.Completed == "1" ? true : false;
                    signed      = theuser.Signed == "1" ? true : false;
                    firstsigned = theuser.Firstsigned == "1" ? true : false;
                    if (!string.IsNullOrEmpty(theuser.Licensetype))
                    {
                        drivinglicense = theuser.Licensetype;
                    }
                    if (theuser.Deductedmarks != null)
                    {
                        deductedmarks = (int)theuser?.Deductedmarks;
                    }
                    if (allow)
                    {
                        allstatus = theuser.Studylog;
                        //need update?
                        if (!string.IsNullOrEmpty(inputRequest.Name))
                        {
                            theuser.Name = inputRequest.Name;
                        }
                        //  theuser.Licensetype = ((int)inputRequest.DrivingLicenseType).ToString();//elements?
                        if (!string.IsNullOrEmpty(inputRequest.Phone))
                        {
                            theuser.Phone = inputRequest.Phone;
                        }
                        // theuser.Wechat = inputRequest.Wechat;
                        if (theuser.Startdate == null)
                        {
                            theuser.Startdate = DateTime.Now;
                        }
                        if (!string.IsNullOrEmpty(theuser.Token))
                        {
                            lasttoken = theuser.Token;
                        }
                        _db1.SaveChanges();
                    }
                    else
                    {
                        allstatus = "您不能参加网络学习,可以参加现场学习";
                    }
                    try
                    {
                        if (!string.IsNullOrEmpty(theuser.Photofile))
                        {
                            fname = theuser.Photofile;
                            pic   = CryptographyHelpers.StudyFileDecrypt(Path.Combine(Global.PhotoPath, fname));
                        }
                        else
                        {
                            var filename = Path.Combine(Global.PhotoPath, fname);
                            pic = System.IO.File.ReadAllBytes(filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("loginandquery,{0},={1}", identity, ex.Message);
                    }
                }



                return(new LoginAndQueryResponse
                {
                    Token = toke1n,
                    LastToken = lasttoken,
                    StatusCode = Global.Status[responseCode.studyOk].StatusCode,
                    Description = Global.Status[responseCode.studyOk].Description,
                    AllowedToStudy = allow,
                    Completed = completed,
                    Signed = signed,
                    FirstSigned = firstsigned,
                    DrivingLicense = drivinglicense,
                    DeductedMarks = deductedmarks,
                    Photo = pic,
                    AllStatus = allstatus
                });
            }
            catch (Exception ex)
            {
                Log.Error("LoginAndQuery,{0}", ex);
                return(new LoginAndQueryResponse
                {
                    StatusCode = Global.Status[responseCode.studyProgramError].StatusCode,
                    Description = Global.Status[responseCode.studyProgramError].Description
                });
            }
        }