예제 #1
0
        public void TestMethod1()
        {
            var wechatCoreDb = new WechatCoreDbContext(new Microsoft.EntityFrameworkCore.DbContextOptions <WechatCoreDbContext>());
            var memberDb     = new MemberDbContext(new Microsoft.EntityFrameworkCore.DbContextOptions <MemberDbContext>());
            var argsModel    = new BindCustomerPhoneByWxopenPhoneArgsModel()
            {
                EncryptedData = "Bbu3OZPldIdKPezC8druWzqZlIA5lhyDEFPpMR20vieOHUxIO+…xhv+IZaw1krTTqyXvikTO91cKGtfIPOBN0VbxIj2JshpsDQ==", Iv = "EPonbgT9+okCcL025u8boQ=="
            };
            var jwtFlag = "jwt";// User.Identity.Name;

            if (string.IsNullOrEmpty(argsModel.EncryptedData))
            {
                throw new ArgumentNullException("encryptedData");
            }
            var jwt = wechatCoreDb.GetSingleMemberLogin(jwtFlag);

            if (jwt == null)
            {
                throw new Exception("未找到登陆纪录");
            }
            var session_key = jwt.GetloginSettingValue <string>("sessionKey");
            var openId      = jwt.GetloginSettingValue <string>("openId");

            var json = Senparc.Weixin.WxOpen.Helpers.EncryptHelper.DecodeEncryptedData(session_key, argsModel.EncryptedData, argsModel.Iv);
            // _logger.LogInformation(json);

            var args = Newtonsoft.Json.JsonConvert.DeserializeObject <Senparc.Weixin.WxOpen.Entities.DecodedPhoneNumber>(json);

            var customerPhone = wechatCoreDb.Query <CustomerPhone>()
                                .Where(m => !m.IsDel)
                                .Where(m => m.Status == CustomerPhoneStatus.已绑定 && m.OpenId == openId)
                                .FirstOrDefault();

            if (customerPhone == null)
            {
                customerPhone = new CustomerPhone()
                {
                    OpenId = openId,
                    Phone  = args.phoneNumber,
                    Status = CustomerPhoneStatus.已绑定
                };
                wechatCoreDb.Add <CustomerPhone>(customerPhone);
                wechatCoreDb.SaveChanges();
            }

            //var email = $"{customerPhone.Phone}@phone";
            //var member = memberDb.Query<Member>()
            //    .Where(m => !m.IsDel)
            //    .Where(m => m.Email == email)
            //    .FirstOrDefault();

            //if (member == null)
            //{
            //    member = new Member()
            //    {
            //        Email = email,
            //        Password = CommonUtil.CreateNoncestr(8),
            //        Status = MemberStatus.正常,
            //        Truename = customerPhone.Phone,
            //        LastLoginTime = DateTime.Now,
            //        RegTime = DateTime.Now
            //    };

            //    memberDb.AddToMember(member);
            //    memberDb.SaveChanges();
            //}

            //改为使用MemberPhone
            var memberId = memberDb.GetMemberIdByMemberPhone(customerPhone.Phone);

            if (memberId <= 0)//说明没有手机绑定的会员,则新建一个
            {
                var member = new Member()
                {
                    Email         = $"{customerPhone.Phone}@phone",
                    Password      = CommonUtil.CreateNoncestr(8),
                    Status        = MemberStatus.正常,
                    Truename      = customerPhone.Phone,
                    LastLoginTime = DateTime.Now,
                    RegTime       = DateTime.Now
                };
                memberDb.AddToMember(member);
                var memberPhone = new MemberPhone()
                {
                    Member = member,
                    Phone  = customerPhone.Phone,
                    State  = MemberPhoneState.已绑定
                };
                memberDb.Add <MemberPhone>(memberPhone);
                memberDb.SaveChanges();

                memberId = member.Id;
            }

            jwt.MemberId = memberId;
            wechatCoreDb.SaveChanges();
        }
예제 #2
0
        public ActionResult Login(string code, string state)
        {
            try
            {
                if (string.IsNullOrEmpty(code))
                {
                    throw new ArgumentNullException("code");
                }
                if (string.IsNullOrEmpty(state) || state != wechatOptions.State)
                {
                    throw new Exception("验证失败!请从正规途径进入!");
                }

                var openId = "string";
                openId = getOpenId(code);
                //判定是否已经绑定手机号
                int?memberId      = null;
                var customerPhone = wechatCoreDb.Query <CustomerPhone>()
                                    .Where(m => !m.IsDel)
                                    .Where(m => m.Status == CustomerPhoneStatus.已绑定 && m.OpenId == openId)
                                    .FirstOrDefault();

                if (customerPhone != null)
                {
                    var email = $"{customerPhone.Phone}@phone";
                    memberId = base.memberDb.Query <Member>()
                               .Where(m => !m.IsDel)
                               .Where(m => m.Email == email)
                               .Select(m => m.Id)
                               .FirstOrDefault();
                    if (memberId <= 0)
                    {
                        throw new Exception("有错误发生,没有找到Member,请重新绑定手机号");
                    }
                }


                var jwt = new MemberLogin()
                {
                    Flag      = CommonUtil.CreateNoncestr(18),
                    LoginType = "Wechat",
                    MemberId  = memberId
                };
                jwt.SetloginSettingValue("openId", openId);
                wechatCoreDb.Add <MemberLogin>(jwt);

                var expiration = TimeSpan.FromMinutes(30 * 2 * 2);
                var encodedJwt = MemberLogin.CreateJwtToken(jwt, expiration);
                wechatCoreDb.SaveChanges();

                var response = new
                {
                    access_token = encodedJwt,
                    expires_in   = (int)expiration.TotalSeconds,
                    isBindPhone  = jwt.MemberId.HasValue,
                    phone        = customerPhone != null ? customerPhone.Phone : ""
                };

                return(Json(response));
            }
            catch (Exception ex)
            {
                return(Json(new { error = ex.Message }));
            }
        }