Exemple #1
0
        public async Task <ApiResult> CodeLogin(UserLoginModel1 model)
        {
            if (string.IsNullOrEmpty(model.Code))
            {
                return(new ApiResult {
                    status = 0, msg = "微信code不能为空"
                });
            }
            HttpClient httpClient = new HttpClient();
            List <KeyValuePair <string, string> > parmArray = new List <KeyValuePair <string, string> >();

            parmArray.Add(new KeyValuePair <string, string>("appid", appid));
            parmArray.Add(new KeyValuePair <string, string>("secret", secret));
            parmArray.Add(new KeyValuePair <string, string>("js_code", model.Code));
            string result = await HttpClientHelper.GetResponseByGetAsync(httpClient, parmArray, "https://api.weixin.qq.com/sns/jscode2session");

            if (result.Contains("errcode"))
            {
                WeChatErrorResultModel errorModel = JsonConvert.DeserializeObject <WeChatErrorResultModel>(result);
                return(new ApiResult {
                    status = 0, msg = "微信返回errcode:" + errorModel.errcode + ",errmsg:" + errorModel.errmsg
                });
            }
            WeChatResultModel rightModel = JsonConvert.DeserializeObject <WeChatResultModel>(result);
            User user    = JwtHelper.JwtDecrypt <User>(ControllerContext);
            var  userDTO = await userService.GetModelAsync(user.Id);

            if (userDTO == null)
            {
                return(new ApiResult {
                    status = 0, msg = "会员不存在"
                });
            }
            if (string.IsNullOrEmpty(userDTO.ShareCode))
            {
                string getTokenUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
                string res         = await HttpClientHelper.GetResponseByGetAsync(httpClient, getTokenUrl);

                if (res.Contains(@"errcode\"))
                {
                    return(new ApiResult {
                        status = 1, data = res
                    });
                }
                GetAccessToken getAccessToken = JsonConvert.DeserializeObject <GetAccessToken>(res);
                Parm           parm           = new Parm();
                parm.scene = userDTO.Mobile;
                string getCodeUrl = string.Format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}", getAccessToken.access_token);
                var    result1    = await HttpClientHelper.GetResponseStringByPostJsonAsync(httpClient, parm, getCodeUrl);

                string path = ImageHelper.SaveByte(result1);
                await userService.UpdateShareCodeAsync(user.Id, path);
            }
            if (rightModel.OpenId != user.Code.Substring(3, 28))
            {
                return(new ApiResult {
                    status = 0, msg = "登录失败"
                });
            }
            return(new ApiResult {
                status = 1, msg = "登录成功"
            });
        }
Exemple #2
0
        public async Task <ApiResult> Login(UserLoginModel model)
        {
            if (string.IsNullOrEmpty(model.Mobile))
            {
                return(new ApiResult {
                    status = 0, msg = "登录手机号不能为空"
                });
            }
            if (!Regex.IsMatch(model.Mobile, @"^1\d{10}$"))
            {
                return(new ApiResult {
                    status = 0, msg = "登录手机号格式不正确"
                });
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                return(new ApiResult {
                    status = 0, msg = "密码不能为空"
                });
            }
            if (string.IsNullOrEmpty(model.Code))
            {
                return(new ApiResult {
                    status = 0, msg = "微信code不能为空"
                });
            }
            long userId = await userService.CheckLoginAsync(model.Mobile, model.Password);

            if (userId == -1 || userId == -2)
            {
                return(new ApiResult {
                    status = 0, msg = "登录账号或密码错误"
                });
            }
            if (userId == -3)
            {
                return(new ApiResult {
                    status = 0, msg = "会员账号已经被冻结"
                });
            }
            HttpClient httpClient = new HttpClient();
            List <KeyValuePair <string, string> > parmArray = new List <KeyValuePair <string, string> >();

            parmArray.Add(new KeyValuePair <string, string>("appid", appid));
            parmArray.Add(new KeyValuePair <string, string>("secret", secret));
            parmArray.Add(new KeyValuePair <string, string>("js_code", model.Code));
            string result = await HttpClientHelper.GetResponseByGetAsync(httpClient, parmArray, "https://api.weixin.qq.com/sns/jscode2session");

            if (result.Contains("errcode"))
            {
                WeChatErrorResultModel errorModel = JsonConvert.DeserializeObject <WeChatErrorResultModel>(result);
                return(new ApiResult {
                    status = 0, msg = "微信返回errcode:" + errorModel.errcode + ",errmsg:" + errorModel.errmsg
                });
            }
            WeChatResultModel rightModel = JsonConvert.DeserializeObject <WeChatResultModel>(result);
            User setUser = new User();

            setUser.Id = userId;
            if (string.IsNullOrEmpty(rightModel.OpenId))
            {
                setUser.Code = "";
            }
            setUser.Code = CommonHelper.GetCaptcha(3) + rightModel.OpenId + CommonHelper.GetCaptcha(2);
            log.Debug($"登录中获得的Code:{setUser.Code}");
            string token   = JwtHelper.JwtEncrypt <User>(setUser);
            long   tokenId = await userTokenService.UpdateAsync(userId, token);

            var userDTO = await userService.GetModelAsync(userId);

            if (userDTO == null)
            {
                return(new ApiResult {
                    status = 0, msg = "会员不存在"
                });
            }
            //if (string.IsNullOrEmpty(userDTO.ShareCode))
            //{
            //    string getTokenUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appid, secret);
            //    string res = await HttpClientHelper.GetResponseByGetAsync(httpClient, getTokenUrl);
            //    if (res.Contains(@"errcode\"))
            //    {
            //        return new ApiResult { status = 1, data = res };
            //    }
            //    GetAccessToken getAccessToken = JsonConvert.DeserializeObject<GetAccessToken>(res);
            //    Parm parm = new Parm();
            //    parm.scene = userDTO.Mobile;
            //    string getCodeUrl = string.Format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}", getAccessToken.access_token);
            //    var result1 = await HttpClientHelper.GetResponseStringByPostJsonAsync(httpClient, parm, getCodeUrl);
            //    string path = ImageHelper.SaveByte(result1);
            //    await userService.UpdateShareCodeAsync(userId, path);
            //}
            return(new ApiResult {
                status = 1, msg = "登录成功", data = new { token = token }
            });
        }