예제 #1
0
        public string GetWeChatQrCode(int pCardID)
        {
            Common.WeChat.IConfig config = new Common.WeChat.ConfigMiniPersonal();
            var api = new Common.WeChat.WeChatMinApi(config);
            var p   = new Dictionary <string, string>();

            p.Add("PCardID", pCardID.ToString());
            return(api.GetWXACodeUnlimit(Common.WeChat.WeChatPagePersonal.CardDetail, p));
        }
예제 #2
0
        public ActionResult ReflashWeCharQrCode()
        {
            Common.WeChat.IConfig config = new Common.WeChat.ConfigMiniPersonal();
            var api = new Common.WeChat.WeChatMinApi(config);
            Func <int, string> getQrCode = pCardID =>
            {
                var p = new Dictionary <string, string>();
                p.Add("PCardID", pCardID.ToString());
                return(api.GetWXACodeUnlimit(Common.WeChat.WeChatPagePersonal.CardDetail, p));
            };
            var cards = db.CardPersonals.Where(s => s.WeChatMiniQrCode == null).ToList();

            foreach (var item in cards)
            {
                item.WeChatMiniQrCode = getQrCode(item.ID);
            }
            db.SaveChanges();
            return(Json("Success", JsonRequestBehavior.AllowGet));
        }
예제 #3
0
 public void WeChatNofity(string userID, decimal amount, string content)
 {
     using (ApplicationDbContext db = new ApplicationDbContext())
     {
         var config = new Common.WeChat.ConfigMiniPersonal();
         Common.WeChat.WeChatMinApi wechat = new Common.WeChat.WeChatMinApi(config);
         var user   = db.Users.FirstOrDefault(s => s.Id == userID);
         var option = new Bll.Users.UserOpenID(user);
         var openID = option.SearchOpenID(config.AppID);
         var form   = db.WeChatMiniNotifyForms.FirstOrDefault(s => s.AppID == config.AppID &&
                                                              s.UserID == userID &&
                                                              s.EndDateTime > DateTime.Now);
         if (form != null)
         {
             var temp = new Common.WeChat.WeChatMessageTemp.PReceivableNotifyWeChatMessage(amount, content, DateTime.Now);
             wechat.SendMessage(openID, form.FormID, null, temp);
             db.WeChatMiniNotifyForms.Remove(form);
             db.SaveChanges();
         }
     }
 }
예제 #4
0
        public ActionResult AddForm(string userID, string[] formIDs)
        {
            if (formIDs.Any(s => s == "the formId is a mock one"))
            {
                return(Json(Common.Comm.ToJsonResult("FormIDInvalid", "FormID无效")));
            }
            if (!db.Vips.Any(s => s.UserID == userID))
            {
                return(Json(Common.Comm.ToJsonResult("VipNoFound", "未注册")));
            }
            var config = new Common.WeChat.ConfigMiniPersonal();
            var user   = db.Users.FirstOrDefault(s => s.Id == userID);
            var openID = new Bll.Users.UserOpenID(user).SearchOpenID(config.AppID);
            var list   = new List <WeChatMiniNotifyForm>();

            foreach (var item in formIDs)
            {
                list.Add(new WeChatMiniNotifyForm
                {
                    AppID          = config.AppID,
                    CreateDateTime = DateTime.Now,
                    EndDateTime    = DateTime.Now.AddDays(7).AddMinutes(-1),
                    FormID         = item,
                    OpenID         = openID,
                    UserID         = userID
                });
            }
            db.WeChatMiniNotifyForms.AddRange(list);
            db.SaveChanges();
            var del = db.WeChatMiniNotifyForms
                      .Where(s => s.EndDateTime < DateTime.Now);

            db.WeChatMiniNotifyForms.RemoveRange(del);
            db.SaveChanges();
            var count = db.WeChatMiniNotifyForms
                        .Where(s => s.UserID == userID)
                        .Count();

            return(Json(Common.Comm.ToJsonResult("Success", "成功", new { Count = count })));
        }
예제 #5
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);
            }
        }
예제 #6
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)));
                }
            }
        }