Ejemplo n.º 1
0
        public HttpResult sendTextMessageToWechat(String toUser, String content, String Wechat_Secret, int agentid)
        {
            WechatUtil wechatUtil = new WechatUtil();
            String accessToken = wechatUtil.GetAccessTokenNoCache(corpId, Wechat_Secret);

            var msgUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", accessToken);


            var msgData = new
            {

                touser = toUser,
                msgtype = "text",
                agentid = agentid,
                safe = 0,
                text = new
                {

                    content = content
                }
            };

            string msgJson = JsonConvert.SerializeObject(msgData, Formatting.Indented);



            HttpHelper httpHelper = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                Encoding = Encoding.GetEncoding("UTF-8"),
                URL = msgUrl,
                Method = "post",//URL     可选项 默认为Get
                Postdata = msgJson,
                PostEncoding = Encoding.GetEncoding("UTF-8")//可以发送中文消息了,开心

            };

            HttpResult result = httpHelper.GetHtml(item);

            return result;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步 开始事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //需要执行的代码

            worker.ReportProgress(1, "开始同步微信账号...\r\n");

            WechatAction wechatAction = new WechatAction();

            List<String> feeRightUserIdsAll = new List<string>();
            List<String> policyRightUserIdsAll = new List<string>();
            List<String> performanceRightUserIdsAll = new List<string>();
            List<String> studyRightUserIdsAll = new List<string>();
            List<String> complainRightUserIdsAll = new List<string>();
            List<String> monitorRightUserIdsAll = new List<string>();
            List<String> errorRightUserIdsAll = new List<string>();
            List<String> contactRightUserIdsAll = new List<string>();

            List<String> feeRightUserIds = new List<string>();
            List<String> policyRightUserIds = new List<string>();
            List<String> performanceRightUserIds = new List<string>();
            List<String> studyRightUserIds = new List<string>();
            List<String> complainRightUserIds = new List<string>();
            List<String> monitorRightUserIds = new List<string>();
            List<String> errorRightUserIds = new List<string>();
            List<String> contactRightUserIds = new List<string>();

            for (int i = 0; i < dgAgentWechatAccount.RowCount; i++)
            {
                String userid = this.dgAgentWechatAccount[6, i].Value.ToString();
                feeRightUserIdsAll.Add(userid);
                policyRightUserIdsAll.Add(userid);
                performanceRightUserIdsAll.Add(userid);
                studyRightUserIdsAll.Add(userid);
                complainRightUserIdsAll.Add(userid);
                monitorRightUserIdsAll.Add(userid);
                errorRightUserIdsAll.Add(userid);
                contactRightUserIdsAll.Add(userid);



                if (this.dgAgentWechatAccount[12, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    feeRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[13, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    policyRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[14, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    performanceRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[15, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    studyRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[16, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    complainRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[17, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    monitorRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[18, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    errorRightUserIds.Add(userid);
                }
                if (this.dgAgentWechatAccount[19, i].Value.ToString().ToUpper().Equals("Y"))
                {
                    contactRightUserIds.Add(userid);
                }

                WechatJsonUser wechatJsonUser = new WechatJsonUser();
                wechatJsonUser.userid = this.dgAgentWechatAccount[6, i].Value.ToString();

                wechatJsonUser.name = this.dgAgentWechatAccount[7, i].Value.ToString();
                if (string.IsNullOrEmpty(wechatJsonUser.name))
                {
                    wechatJsonUser.name = wechatJsonUser.userid;
                }
                wechatJsonUser.email = this.dgAgentWechatAccount[8, i].Value.ToString();
                wechatJsonUser.mobile = this.dgAgentWechatAccount[9, i].Value.ToString();
                wechatJsonUser.weixinid = this.dgAgentWechatAccount[10, i].Value.ToString();
                wechatJsonUser.position = this.dgAgentWechatAccount[4, i].Value.ToString();
                if (String.IsNullOrEmpty(wechatJsonUser.position))
                {
                    wechatJsonUser.position = this.dgAgentWechatAccount[1, i].Value.ToString();
                }
                wechatJsonUser.department = new List<int>();
                wechatJsonUser.department.Add(1);
                worker.ReportProgress(2, "总共" + dgAgentWechatAccount.RowCount + "条记录,正在处理第" + (i + 1) + "条,同步微信账号" + wechatJsonUser.userid + "\r\n");


                //Check the Wechat rule
                #region

                String mobile = wechatJsonUser.mobile;
                String weixinid = wechatJsonUser.weixinid;

                // ^[1]+[3,5,8]+\d{9}
                if (Regex.IsMatch(weixinid, @"^[1]+[3,5,8]+\d{9}"))
                {
                    mobile = weixinid;
                    weixinid = "";
                }
                else
                {
                    if (Regex.IsMatch(weixinid, @"^\d+$"))
                    {
                        weixinid = "QQ" + weixinid;
                        mobile = "";
                    }

                }



                #endregion


                var userData = new
                {
                    userid = wechatJsonUser.userid,
                    name = wechatJsonUser.name,
                    department = wechatJsonUser.department,
                    mobile = mobile,//wechatJsonUser.mobile,
                    email = "",// wechatJsonUser.email,
                    position = wechatJsonUser.position,
                    weixinid = weixinid// wechatJsonUser.weixinid
                };

                string InsertUserJson = JsonConvert.SerializeObject(userData, Formatting.Indented);

                HttpResult result = wechatAction.getUserFromWechat(wechatJsonUser.userid, Settings.Default.Wechat_Secret);
                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {

                    //表示访问成功,具体的大家就参考HttpStatusCode类
                    WechatJsonUser wechatJsonUserFromWechat = JsonConvert.DeserializeObject<WechatJsonUser>(result.Html);
                    if (!String.IsNullOrEmpty(wechatJsonUserFromWechat.userid))
                    {
                        string updateUserJson = JsonConvert.SerializeObject(userData, Formatting.Indented);

                        // if (wechatJsonUser.department.Count == 0)
                        // {
                        //  result = wechatAction.deleteUserFromWechat(wechatJsonUser.userid, Settings.Default.Wechat_Secret);
                        // }
                        // else
                        // {
                        result = wechatAction.updateUserToWechat(Settings.Default.Wechat_Secret, updateUserJson);
                        if (!String.IsNullOrEmpty(wechatJsonUser.email))
                        {
                            //this.sendEmail(this.dgAgent[2, i].Value.ToString());
                        }
                        // }
                    }
                    else
                    {
                        result = wechatAction.addUserToWechat(Settings.Default.Wechat_Secret, InsertUserJson);
                        ReturnMessage returnMessage1 = (ReturnMessage)JsonConvert.DeserializeObject(result.Html, typeof(ReturnMessage));
                        if (returnMessage1 != null && returnMessage1.errcode.Equals("0"))
                        {
                            if (!String.IsNullOrEmpty(wechatJsonUser.email))
                            {
                                this.sendEmail(wechatJsonUser.email);
                            }
                        }
                        else
                        {
                            AgentWechatAccount agentWechatAccount = new ChinaUnion_BO.AgentWechatAccount();
                            agentWechatAccount.contactId = wechatJsonUser.userid;

                            agentWechatAccount.wechatImportStatus = returnMessage1.getErrorDescrition();
                            agentWechatAccountDao.UpdateWechatImportStatus(agentWechatAccount);
                        }
                    }



                    ReturnMessage returnMessage = (ReturnMessage)JsonConvert.DeserializeObject(result.Html, typeof(ReturnMessage));
                    if (returnMessage != null && returnMessage.errcode != "0")
                    {


                        this.dgAgentWechatAccount[11, i].Value = returnMessage.getErrorDescrition();
                    }
                    else
                    {
                        var userInviteData = new
                        {
                            userid = wechatJsonUser.userid
                        };

                        string inviteUserJson = JsonConvert.SerializeObject(userInviteData, Formatting.Indented);

                        wechatAction.inviteUserToWechat(Settings.Default.Wechat_Secret, inviteUserJson);
                        this.dgAgentWechatAccount[11, i].Value = "同步成功";
                    }
                }



            }

            deleteTagUser(feeRightUserIdsAll, 2);
            addTagUser(feeRightUserIds, 2);

            deleteTagUser(policyRightUserIdsAll, 3);
            addTagUser(policyRightUserIds, 3);

            deleteTagUser(performanceRightUserIdsAll, 4);
            addTagUser(performanceRightUserIds, 4);

            deleteTagUser(studyRightUserIdsAll, 5);
            addTagUser(studyRightUserIds, 5);

            deleteTagUser(complainRightUserIdsAll, 6);
            addTagUser(complainRightUserIds, 6);

            deleteTagUser(monitorRightUserIdsAll, 7);
            addTagUser(monitorRightUserIds, 7);

            deleteTagUser(errorRightUserIdsAll, 8);
            addTagUser(errorRightUserIds, 8);

            deleteTagUser(contactRightUserIdsAll, 9);
            addTagUser(contactRightUserIds, 9);





            worker.ReportProgress(2, "同步微信账号完毕...\r\n");


        }
Ejemplo n.º 3
0
        private void addTagUser(List<String> RightUserIds, int tagId)
        {
            WechatAction wechatAction = new WechatAction();
            List<String> rightUserIdsBuffer = new List<string>();
            for (int i = 1; i <= RightUserIds.Count; i++)
            {
                rightUserIdsBuffer.Add(RightUserIds[i - 1]);
                if (i % 300 == 0 || i == RightUserIds.Count)
                {
                    var userTagData = new
                    {
                        tagid = tagId,
                        userlist = rightUserIdsBuffer
                    };

                    string updateTagJson = JsonConvert.SerializeObject(userTagData, Formatting.Indented);
                    wechatAction.addTagUsers(Settings.Default.Wechat_Secret, updateTagJson);

                    rightUserIdsBuffer.Clear();
                }

            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 异步 开始事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //需要执行的代码
            WechatAction wechatAction = new Wechat.WechatAction();

            worker.ReportProgress(1, "开始同步微信账号...\r\n");


            bool isNewUser = false;
            bool isDisableUser = false;


            for (int i = 0; i < this.dgWechat.RowCount; i++)
            {
                isNewUser = false;

                //Check the Wechat rule
                #region
                String email = this.dgWechat[4, i].Value.ToString();
                String mobile = this.dgWechat[3, i].Value.ToString();
                String weixinid = this.dgWechat[2, i].Value.ToString();
                
                // ^[1]+[3,5,8]+\d{9}
                if (Regex.IsMatch(weixinid, @"^[1]+[3,5,8]+\d{9}"))
                {
                    mobile = weixinid;
                    weixinid = "";
                }
                else
                {
                    if (Regex.IsMatch(weixinid, @"^\d+$"))
                    {
                        weixinid = "QQ" + weixinid;
                      //  mobile = "";
                    }
                   
                }



                #endregion


                WechatJsonUser toWechatJsonUser = new WechatJsonUser();

                String userId = this.dgWechat[2, i].Value.ToString().Trim();
                if (String.IsNullOrEmpty(this.dgWechat[2, i].Value.ToString().Trim()))
                {
                    if (String.IsNullOrEmpty(this.dgWechat[3, i].Value.ToString().Trim()))
                    {
                        userId = this.dgWechat[4, i].Value.ToString();
                    }
                    else
                    {
                        userId = this.dgWechat[3, i].Value.ToString();
                    }
                }
                else
                {
                    userId = this.dgWechat[2, i].Value.ToString();
                }

               

                toWechatJsonUser.userid = userId;
                worker.ReportProgress(2, "同步微信账号" + this.dgWechat[1, i].Value.ToString() + "\r\n");
                HttpResult result = wechatAction.getUserFromWechat(toWechatJsonUser.userid, Settings.Default.Wechat_Secret);


                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //表示访问成功,具体的大家就参考HttpStatusCode类
                    toWechatJsonUser = JsonConvert.DeserializeObject<WechatJsonUser>(result.Html);
                    if (String.IsNullOrEmpty(toWechatJsonUser.userid))
                    {
                        isNewUser = true;
                    }

                    if (!String.IsNullOrEmpty(this.dgWechat[5, i].Value.ToString()) && this.dgWechat[5, i].Value.ToString().ToUpper()=="Y")
                    {
                        isDisableUser = true;
                    }

                  
                    toWechatJsonUser.position = this.dgWechat[0, i].Value.ToString();
                    toWechatJsonUser.name = this.dgWechat[1, i].Value.ToString();
                    toWechatJsonUser.weixinid = weixinid;
                    toWechatJsonUser.mobile = mobile;
                    toWechatJsonUser.email = email;
                    toWechatJsonUser.userid = userId;
                   
                   
                   

                    if (toWechatJsonUser.department == null)
                    {
                        toWechatJsonUser.department = new List<int>();
                    }

                    WechatUser fromWechatUser = wechatAction.getUserFromWechatByDepartment(Settings.Default.Wechat_Error_Department, Settings.Default.Wechat_Secret);
                    if (fromWechatUser.userlist.Count <= 1000)
                    {
                        toWechatJsonUser.department.Add(Settings.Default.Wechat_Error_Sub_Department_1);
                    }
                    else
                    {
                        if (fromWechatUser.userlist.Count > 1000 && fromWechatUser.userlist.Count <= 2000)
                        {
                           toWechatJsonUser.department.Add(Settings.Default.Wechat_Error_Sub_Department_2);
                        }
                        else
                        {
                            if (fromWechatUser.userlist.Count > 2000 && fromWechatUser.userlist.Count <= 3000)
                            {
                                toWechatJsonUser.department.Add(Settings.Default.Wechat_Error_Sub_Department_3);
                            }
                            else
                            {
                               toWechatJsonUser.department.Add(Settings.Default.Wechat_Error_Department);
                            }
                        }
                    }
                    var userData = new
                    {
                        userid = toWechatJsonUser.userid,
                        name = toWechatJsonUser.name,
                        department = toWechatJsonUser.department,
                        position = toWechatJsonUser.position,
                        mobile = mobile,
                        email = email,
                        weixinid = weixinid
                    };

                    string userJson = JsonConvert.SerializeObject(userData, Formatting.Indented);

                    if (!isNewUser)
                    {
                        result = wechatAction.updateUserToWechat(Settings.Default.Wechat_Secret, userJson);
                        
                        if (!String.IsNullOrEmpty(toWechatJsonUser.email))
                        {
                           // this.sendEmail(toWechatJsonUser.email);
                        }
                    }
                    else
                    {
                       
                        result = wechatAction.addUserToWechat(Settings.Default.Wechat_Secret, userJson);
                        ReturnMessage returnMessage1 = (ReturnMessage)JsonConvert.DeserializeObject(result.Html, typeof(ReturnMessage));
                        
                        if (returnMessage1 != null && returnMessage1.errcode.Equals( "0")&&!String.IsNullOrEmpty(toWechatJsonUser.email))
                        {
                           this.sendEmail(toWechatJsonUser.email);
                        }
                    }
                    if (isDisableUser)
                    {
                        result = wechatAction.deleteUserFromWechat(toWechatJsonUser.userid, Settings.Default.Wechat_Secret);
                    }
                   
                   ReturnMessage returnMessage= (ReturnMessage) JsonConvert.DeserializeObject(result.Html,typeof(ReturnMessage));
                   if (returnMessage != null && returnMessage.errcode != "0")
                   {
                       this.dgWechat[6, i].Value = returnMessage.getErrorDescrition(); ;
                   }
                   else
                   {
                       this.dgWechat[6, i].Value = "导入成功";
                   }
                }

            }
           // dgWechat.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

           
            worker.ReportProgress(2, "同步微信账号完毕...\r\n");


        }