Пример #1
0
 public ActionResult CreateJsSign(string url, string noncestr, string timestamp)
 {
     try
     {
         Common.WeChat.IConfig config = new Common.WeChat.ConfigWeChatWork();
         var    wechat     = new Common.WeChat.WeChatApi(config);
         string signstr    = wechat.JsSign(url, noncestr, timestamp);
         var    returndata = new
         {
             data = signstr
         };
         return(Json(Comm.ToJsonResult("Success", "获取成功", returndata), JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         return(Json(Comm.ToJsonResult("Error", ex.Message), JsonRequestBehavior.AllowGet));
     }
 }
Пример #2
0
 public ActionResult UploadWeChatMedia(string mediaID)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(mediaID))
         {
             return(Json(Comm.ToJsonResult("Error", "mediaID不能为null")));
         }
         var server = Common.CommModels.UploadServer.QinQiu;
         Common.WeChat.IConfig config = new Common.WeChat.ConfigWeChatWork();
         var    wechat   = new Common.WeChat.WeChatApi(config);
         string filePath = wechat.GetTempMedia(mediaID, server, ".amr");
         return(Json(Comm.ToJsonResult("Success", "成功", new
         {
             FileUrl = filePath,
             FileFullUrl = server == Common.CommModels.UploadServer.Local ? Url.ContentFull(filePath) : filePath
         })));
     }
     catch (Exception ex)
     {
         return(Json(Comm.ToJsonResult("Error", ex.Message)));
     }
 }
Пример #3
0
        public ActionResult LoginByWeiXin(string code, string state = null, WeChatAccount type = WeChatAccount.AiCardMini)
        {
            Func <string, string, ActionResult> error = (content, detail) =>
            {
                if (type != WeChatAccount.PC)
                {
                    return(Json(Comm.ToJsonResult("Error", content, detail)));
                }
                else
                {
                    return(this.ToError("错误", detail, Url.Action("Login", "Account")));
                }
            };

            if (string.IsNullOrWhiteSpace(code))
            {
                return(error("请求有误", "Code不能为空"));
            }
            if (Request.HttpMethod == "GET")
            {
                type = WeChatAccount.PC;
            }
            if (type != WeChatAccount.AiCardMini && type != WeChatAccount.AiCardPersonalMini)
            {
                //非小程序
                switch (type)
                {
                default:
                case WeChatAccount.PC:
                {
                    Common.WeChat.IConfig           config = new Common.WeChat.ConfigWeChatWork();
                    Common.WeChat.WeChatApi         wechat = new Common.WeChat.WeChatApi(config);
                    Common.WeChat.AccessTokenResult result;
                    try
                    {
                        result = wechat.GetAccessTokenSns(code);
                        var openID = result.OpenID;

                        if (state == "openid")
                        {
                            Response.Cookies["WeChatOpenID"].Value = openID;
                            return(Json(Comm.ToJsonResult("Success", "成功", new { OpenID = openID })));
                        }
                        config.AccessToken = result.AccessToken;
                        var unionid = result.UnionID;
                        var user    = db.Users.FirstOrDefault(s => s.WeChatID == unionid);

                        try
                        {
                            if (user != null)
                            {
                                if (user.UserName == user.NickName)
                                {
                                    var    userInfo = wechat.GetUserInfoSns(openID);
                                    string avart;
                                    try
                                    {
                                        avart = this.Download(userInfo.HeadImgUrl);
                                    }
                                    catch (Exception)
                                    {
                                        avart = "~/Content/Images/404/avatar.png";
                                    }
                                    user.NickName = userInfo.NickName;
                                    user.Avatar   = avart;
                                }
                                var option = new Bll.Users.UserOpenID(user);
                                option.AddOpenID(config.AppID, result.OpenID);
                                user.LastLoginDateTime = DateTime.Now;
                                db.SaveChanges();
                            }
                            else
                            {
                                try
                                {
                                    var userInfo = wechat.GetUserInfoSns(openID);
                                    user = CreateByWeChat(userInfo);
                                }
                                catch (Exception)
                                {
                                    user = CreateByWeChat(new Common.WeChat.UserInfoResult {
                                            UnionID = unionid
                                        });
                                }
                            }
                            if (type != WeChatAccount.PC)
                            {
                                return(Json(Comm.ToJsonResult("Success", "成功", new UserForApiViewModel(user))));
                            }
                            SignInManager.SignIn(user, true, true);
                            switch (state.ToLower())
                            {
                            case null:
                            case "":
                            case "ticketindex":
                                return(RedirectToAction("Index", "Tickets"));

                            case "qrcode":
                                return(RedirectToAction("Index", "Tickets", new { mode = "qrcode" }));

                            case "pagelogin":
                            {
                                var card = db.Cards.FirstOrDefault(s => s.UserID == user.Id);
                                if (card != null)
                                {
                                    return(Redirect($"https://radar.dtoao.com/#/Login?userID={user.Id}&enterpriseID={card.EnterpriseID}&cardID={card.ID}&userName={user.UserName}"));
                                }
                                else
                                {
                                    return(Redirect($"https://radar.dtoao.com/#/Login?error=名片不存在"));
                                }
                            }

                            default:
                                return(Redirect(state));
                            }
                        }
                        catch (Exception ex)
                        {
                            return(error("请求有误", ex.Message));
                        }
                    }
                    catch (Exception ex)
                    {
                        return(error("请求有误", ex.Message));
                    }
                }
                }
            }
            else
            {
                Common.WeChat.IConfig config;
                switch (type)
                {
                case WeChatAccount.AiCardMini:
                    config = new Common.WeChat.ConfigMini();
                    break;

                case WeChatAccount.AiCardPersonalMini:
                    config = new Common.WeChat.ConfigMiniPersonal();
                    break;

                default:
                    return(Json(Comm.ToJsonResult("Error", "Type参数有误")));
                }
                //小程序
                WeChatMinApi wechat = new Common.WeChat.WeChatMinApi(config);
                try
                {
                    var             result = wechat.Jscode2session(code);
                    ApplicationUser user   = null;
                    if (!string.IsNullOrWhiteSpace(result.UnionID))
                    {
                        user = db.Users.FirstOrDefault(s => s.WeChatID == result.UnionID);
                        // 把OpenID存进数据库
                        if (user != null)
                        {
                            var option = new Bll.Users.UserOpenID(user);
                            option.AddOpenID(config.AppID, result.OpenID);
                            db.SaveChanges();
                        }
                    }
                    return(Json(Comm.ToJsonResult("Success", "成功", new
                    {
                        result.OpenID,
                        result.UnionID,
                        User = user == null ? null : new UserForApiViewModel(user)
                    })));
                }
                catch (Exception ex)
                {
                    return(Json(Comm.ToJsonResult("Error", ex.Message)));
                }
            }
        }
Пример #4
0
        public ActionResult VerufucatuibCard(string phonenumber, string verificationcode, string wxcode, int cardid)
        {
            var  vcodemodel = db.VerificationCodes.FirstOrDefault(s => s.To == phonenumber.Replace(" ", "") && s.Code == verificationcode);
            Card cardmodel  = new Card();

            if (cardid <= 0)
            {
                cardmodel = db.Cards.FirstOrDefault(s => s.Mobile == phonenumber.Replace(" ", ""));
            }
            else
            {
                cardmodel = db.Cards.FirstOrDefault(s => s.ID == cardid);
            }

            //验证手机短信验证码是否正确
            if (vcodemodel == null || vcodemodel.Code != verificationcode)
            {
                return(Json(Comm.ToJsonResult("Error", "验证码不正确")));
            }
            //验证手机短信验证码是否过时
            else
            {
                DateTime date1       = DateTime.Now;
                DateTime date2       = vcodemodel.CreateDate;
                TimeSpan timeSpan    = date1 - date2;
                int      timespanint = Convert.ToInt32(timeSpan.TotalMinutes);
                if (timespanint > 30)
                {
                    return(Json(Comm.ToJsonResult("Error", "验证码超时")));
                }
            }
            if (cardmodel == null)
            {
                return(Json(Comm.ToJsonResult("Error", "名片不存在")));
            }
            else
            {
                Common.WeChat.IConfig           config = new Common.WeChat.ConfigOpen();
                Common.WeChat.WeChatApi         wechat = new Common.WeChat.WeChatApi(config);
                Common.WeChat.AccessTokenResult result;

                try
                {
                    //根据微信code获取到accesstoken、openid
                    result = wechat.GetAccessTokenSns(wxcode);
                    var openID = result.OpenID;
                    config.AccessToken = result.AccessToken;
                    var unionid = result.UnionID;
                    var user    = db.Users.FirstOrDefault(s => s.WeChatID == unionid);
                    try
                    {
                        if (user != null)
                        {
                            if (user.UserName == user.NickName)
                            {
                                var    userInfo = wechat.GetUserInfoSns(openID);
                                string avart;
                                try
                                {
                                    avart = this.Download(userInfo.HeadImgUrl);
                                }
                                catch (Exception)
                                {
                                    avart = "~/Content/Images/404/avatar.png";
                                }
                                user.NickName = userInfo.NickName;
                                user.Avatar   = avart;
                            }
                            var option = new Bll.Users.UserOpenID(user);
                            option.AddOpenID(config.AppID, result.OpenID);
                            user.LastLoginDateTime = DateTime.Now;
                            db.SaveChanges();
                            cardmodel.UserID  = user.Id;
                            cardmodel.Enable  = true;
                            user.EnterpriseID = cardmodel.EnterpriseID.Value;
                            db.SaveChanges();
                        }
                        else
                        {
                            try
                            {
                                var userInfo = wechat.GetUserInfoSns(openID);
                                user              = CreateByWeChat(userInfo);
                                cardmodel.UserID  = user.Id;
                                cardmodel.Enable  = true;
                                user.EnterpriseID = cardmodel.EnterpriseID.Value;
                                db.SaveChanges();
                            }
                            catch (Exception)
                            {
                                user = CreateByWeChat(new Common.WeChat.UserInfoResult {
                                    UnionID = unionid
                                });
                                cardmodel.UserID  = user.Id;
                                cardmodel.Enable  = true;
                                user.EnterpriseID = cardmodel.EnterpriseID.Value;
                                db.SaveChanges();
                            }
                        }
                        return(Json(Comm.ToJsonResult("Success", "激活成功", new UserForApiViewModel(user))));
                    }
                    catch (Exception ex)
                    {
                        return(Json(Comm.ToJsonResult("Error", ex.Message)));
                    }
                }
                catch (Exception ex)
                {
                    return(Json(Comm.ToJsonResult("Error", ex.Message)));
                }
            }
        }