コード例 #1
0
ファイル: ChatImController.cs プロジェクト: radtek/Aicard
        public ActionResult SendMessage(string content, string fromUserID, string formID, string toUserID)
        {
            Common.WeChat.IConfig config = new Common.WeChat.ConfigMini();
            var    appID    = config.AppID;
            var    toUser   = db.Users.FirstOrDefault(s => s.Id == toUserID);
            var    option   = new Bll.Users.UserOpenID(toUser);
            var    e        = db.Enterprises.FirstOrDefault(s => s.ID == toUser.EnterpriseID);
            string openID   = option.SearchOpenID(appID);
            var    fromUser = db.Users.FirstOrDefault(s => s.Id == fromUserID);
            var    temp     = new Common.WeChat.WeChatMessageTemp.EDefaultNotifyWeChatMessage(e.Name, fromUser.NickName, content, DateTime.Now);
            var    result   = new Common.WeChat.WeChatMinApi(config)
                              .SendMessage(openID, formID, null, temp);

            return(Json(Comm.ToJsonResult("Success", "", result)));
        }
コード例 #2
0
ファイル: UserLogs.cs プロジェクト: radtek/Aicard
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="log"></param>
        public static void Add(UserLog log)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                if (!db.Users.Any(s => s.Id == log.UserID))
                {
                    throw new Exception("用户不存在");
                }
                log.CreateDateTime = DateTime.Now;
                ///验证
                switch (log.Type)
                {
                case UserLogType.FollowUp:
                {
                    var a = db.EnterpriseCustomers.FirstOrDefault(s => s.ID == log.RelationID);
                    if (a.UserID != log.UserID)
                    {
                        log.TargetEnterpriseID = a.EnterpriseID;
                        log.TargetUserID       = a.UserID;
                        var b = db.EnterpriseUserCustomer.FirstOrDefault(s => s.OwnerID == log.UserID && s.CustomerID == a.ID);
                        b.State = Common.Enums.EnterpriseUserCustomerState.Follow;
                        db.SaveChanges();
                    }
                }
                break;

                case UserLogType.AddCustTab:
                {
                    var a = db.EnterpriseCustomers.FirstOrDefault(s => s.ID == log.RelationID);
                    if (a.UserID != log.UserID)
                    {
                        log.TargetEnterpriseID = a.EnterpriseID;
                        log.TargetUserID       = a.UserID;
                    }
                }
                break;

                case UserLogType.ArticleLike:
                case UserLogType.ArticleComment:
                case UserLogType.ArticleRead:
                case UserLogType.ArticleShare:
                {
                    var a = db.Articles.FirstOrDefault(s => s.ID == log.RelationID);
                    if (a == null || a.State != ArticleState.Released)
                    {
                        throw new Exception("文章不存在");
                    }
                    if (a.UserID != log.UserID)
                    {
                        log.TargetEnterpriseID = a.EnterpriseID;
                        log.TargetUserID       = a.UserID;
                    }
                }
                break;

                case UserLogType.ProductRead:
                case UserLogType.ProductCon:
                {
                    var p = db.Products.FirstOrDefault(s => s.ID == log.RelationID);
                    if (p == null)
                    {
                        throw new Exception("商品不存在");
                    }
                    log.TargetEnterpriseID = p.EnterpriseID;
                }
                break;

                case UserLogType.HomePageRead:
                case UserLogType.ShopRead:
                case UserLogType.CardRead:
                case UserLogType.Communication:
                case UserLogType.WeChatOpen:
                case UserLogType.CardShare:
                case UserLogType.CardSave:
                case UserLogType.CardLike:
                case UserLogType.PhoneCall:
                case UserLogType.EmailSend:
                case UserLogType.VoicePlay:
                case UserLogType.VideoPlay:
                {
                    var c = db.Cards.FirstOrDefault(s => s.ID == log.RelationID);

                    if (c == null || !c.Enable)
                    {
                        throw new Exception("卡片不存在");
                    }
                    if (c.UserID != log.UserID)
                    {
                        log.TargetEnterpriseID = c.EnterpriseID;
                        log.TargetUserID       = c.UserID;
                    }
                }
                break;

                case UserLogType.CardTab:
                {
                    if (db.UserLogs.Any(s => s.Type == UserLogType.CardTab &&
                                        s.RelationID == log.RelationID &&
                                        s.UserID == log.UserID))
                    {
                        throw new Exception("该用户已经点击过此名片标签");
                    }
                    var t = db.CardTabs.FirstOrDefault(s => s.ID == log.RelationID);
                    if (t == null)
                    {
                        throw new Exception("卡片标签不存在");
                    }
                    var c = db.Cards.FirstOrDefault(s => s.ID == t.CardID);
                    if (c.UserID != log.UserID)
                    {
                        log.TargetEnterpriseID = c.EnterpriseID;
                        log.TargetUserID       = c.UserID;
                    }
                    break;
                }

                case UserLogType.CardPersonalAddressNav:
                case UserLogType.CardPersonalEmailCopy:
                case UserLogType.CardPersonalEnterpriseCopy:
                case UserLogType.CardPersonalLike:
                case UserLogType.CardPersonalMobileCall:
                case UserLogType.CardPersonalPhoneCall:
                case UserLogType.CardPersonalRead:
                case UserLogType.CardPersonalSave:
                case UserLogType.CardPersonalShare:
                case UserLogType.CardPersonalWechat:
                {
                    var card = db.CardPersonals.FirstOrDefault(s => s.ID == log.RelationID && s.Enable);
                    if (card == null)
                    {
                        throw new Exception("个人卡片不存在");
                    }
                    if (card.UserID != log.UserID)
                    {
                        log.TargetUserID = card.UserID;
                    }
                    break;
                }

                default:
                    break;
                }
                //点赞处理
                if (log.Type == UserLogType.ArticleLike || log.Type == UserLogType.CardLike || log.Type == UserLogType.CardPersonalLike)
                {
                    var dbLog = db.UserLogs.FirstOrDefault(s => s.RelationID == log.RelationID &&
                                                           s.UserID == log.UserID &&
                                                           s.Type == log.Type);
                    if (dbLog == null)
                    {
                        log.Total = db.UserLogs.Count(s => s.RelationID == log.RelationID && s.Type == log.Type && s.UserID == log.UserID);
                        db.UserLogs.Add(log);
                    }
                    else
                    {
                        db.UserLogs.Remove(dbLog);
                    }
                }
                else
                {
                    int tempcount = db.UserLogs.Count(s => s.RelationID == log.RelationID && s.Type == log.Type && s.UserID == log.UserID);
                    log.Total = tempcount == 0 ? 1 : tempcount + 1;
                    db.UserLogs.Add(log);
                }
                db.SaveChanges();//修改log
                switch (log.Type)
                {
                case UserLogType.ArticleLike:
                case UserLogType.ArticleShare:
                {
                    var art   = db.Articles.FirstOrDefault(s => s.ID == log.RelationID);
                    var count = db.UserLogs.Count(s => s.RelationID == log.RelationID && s.Type == log.Type);
                    if (log.Type == UserLogType.ArticleShare)
                    {
                        art.Share = count;
                    }
                    if (log.Type == UserLogType.ArticleLike)
                    {
                        art.Like = count;
                    }
                    db.SaveChanges();        //更新点赞数量或分享数
                    break;
                }

                case UserLogType.CardLike:
                case UserLogType.CardRead:
                case UserLogType.CardShare:
                {
                    var card = db.Cards.FirstOrDefault(s => s.ID == log.RelationID);
                    if (log.Type == UserLogType.CardRead)
                    {
                        //算人头
                        card.Like = db.UserLogs
                                    .Where(s => s.Type == log.Type && s.RelationID == log.RelationID)
                                    .GroupBy(s => s.UserID)
                                    .Count();
                    }
                    else
                    {
                        var count = db.UserLogs
                                    .Count(s => s.RelationID == log.RelationID &&
                                           s.Type == log.Type);
                        if (log.Type == UserLogType.CardLike)
                        {
                            card.View = count;
                        }
                    }

                    db.SaveChanges();        //更新点赞数量或阅读数量
                }
                break;

                case UserLogType.CardTab:
                {
                    var t = db.CardTabs.FirstOrDefault(s => s.ID == log.RelationID);
                    t.Count = db.UserLogs.Count(s => s.RelationID == log.RelationID &&
                                                s.Type == UserLogType.CardTab);
                    db.SaveChanges();        //更新卡片标签
                    break;
                }

                case UserLogType.CardPersonalLike:
                case UserLogType.CardPersonalRead:
                {
                    var c     = db.CardPersonals.FirstOrDefault(s => s.ID == log.RelationID);
                    var count = db.UserLogs.Where(s => s.RelationID == log.RelationID &&
                                                  s.Type == log.Type)
                                .GroupBy(s => s.UserID)
                                .Count();
                    if (log.Type == UserLogType.CardPersonalLike)
                    {
                        c.Like = count;
                    }
                    else if (log.Type == UserLogType.CardPersonalRead)
                    {
                        c.View = count;
                    }
                    db.SaveChanges();
                    break;
                }

                default:
                    break;
                }
            }
            Common.WeChat.IConfig config = null;
            if ((int)log.Type < 200)
            {
                config = new Common.WeChat.ConfigMini();
            }
            else if ((int)log.Type >= 200)
            {
                config = new Common.WeChat.ConfigMiniPersonal();
            }
            if (config != null)
            {
                NotifyByLog(log, config);
            }
        }
コード例 #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)));
                }
            }
        }