Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <UserInfo> LoginAsync(WechatRequest request)
        {
            var result = new UserInfoEntity();

            if (string.IsNullOrEmpty(request.PhoneNumber))
            {
                var wxConfig = _config.WechatAccount.FirstOrDefault(x => x.AcountId == request.AcountId);
                //微信登录
                var loginInfo = await _oathService.LoginAsync(request.Code, wxConfig);

                result = await _userInfoData.GetUserByAccount(loginInfo.unionid ?? loginInfo.openid);

                if (result == null)
                {
                    result = new UserInfoEntity
                    {
                        UnionId    = loginInfo.unionid,
                        OpenId     = loginInfo.openid,
                        CreateTime = DateTime.Now,
                        HeadImg    = request.HeadImg,
                        NickName   = request.NickName,
                        UpdateTime = DateTime.Now
                    };
                }

                //如果手机号为空
                if (string.IsNullOrEmpty(result.PhoneNumber) && !string.IsNullOrEmpty(request.EncryptedData) && !string.IsNullOrEmpty(request.Iv))
                {
                    //数据解密,序列化获取手机号码
                    var wx_user = _oathService.AESDecrypt <Domian.MiniUserPhone>(request.EncryptedData, loginInfo.session_key, request.Iv);
                    if (wx_user != null && !string.IsNullOrEmpty(wx_user.PhoneNumber))
                    {
                        result.PhoneNumber = wx_user.PhoneNumber;
                    }
                }
                result = await _userInfoData.SaveUpdate(result);
            }
            else
            {
                //账号密码登录
                result = await _userInfoData.GetUserByAccount(request.PhoneNumber);

                if (result == null)
                {
                    throw new ApiException(-1, "账号不存在,请先注册");
                }
                if (string.IsNullOrEmpty(request.PassWord) || request.PassWord != result.PassWord)
                {
                    throw new ApiException(-1, "登录密码错误");
                }
            }
            return(result.Convert <UserInfoEntity, UserInfo>());
        }