Ejemplo n.º 1
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.º 2
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.º 3
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();

            for (int i = 0; i < dgAgent.RowCount ; i++)
            {
                WechatJsonUser wechatJsonUser = new WechatJsonUser();
                wechatJsonUser.userid = this.dgAgent[0, i].Value.ToString();
                wechatJsonUser.name = this.dgAgent[1, i].Value.ToString();
                wechatJsonUser.email = this.dgAgent[2, i].Value.ToString();
                wechatJsonUser.mobile = this.dgAgent[3, i].Value.ToString();
                wechatJsonUser.weixinid = this.dgAgent[4, i].Value.ToString();
                wechatJsonUser.department = new List<int>();
                wechatJsonUser.department.Add(Settings.Default.Wechat_Agent_Department);
                worker.ReportProgress(2, "同步微信账号" + wechatJsonUser.userid + "\r\n");

                //Check the Wechat rule
                #region
                String email = "";
                String mobile = this.dgAgent[3, i].Value.ToString();
                String weixinid = this.dgAgent[4, 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 = "";
                        }
                        else
                        {
                            mobile = "";
                        } 
                    }
                

               // this.dgAgent[2, i].Value = email;
               // this.dgAgent[3, i].Value = mobile;
               // this.dgAgent[4, i].Value = weixinid;

             

                   
                #endregion


                var userData = new
                {
                    userid = wechatJsonUser.userid,
                    name = wechatJsonUser.name,
                    department = wechatJsonUser.department,                    
                    mobile = mobile,
                    email = email,
                    weixinid = 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 = JsonConvert.DeserializeObject<WechatJsonUser>(result.Html);
                    if (!String.IsNullOrEmpty(wechatJsonUser.userid))
                    {
                        if (this.dgAgent[5, i].Value.ToString().Equals("账号已经停用"))
                        {                          
                            wechatJsonUser.department.Remove(Settings.Default.Wechat_Agent_Department);                           
                        }
                        else
                        {
                            wechatJsonUser.department.Add(Settings.Default.Wechat_Agent_Department);                           
                        }
                        var updateUserData = new
                        {
                            userid = this.dgAgent[0, i].Value.ToString(),
                            name = this.dgAgent[1, i].Value.ToString(),
                            email = email,
                            mobile = mobile,
                            weixinid = weixinid,
                            department = wechatJsonUser.department
                        };

                        string updateUserJson = JsonConvert.SerializeObject(updateUserData, 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(this.dgAgent[2, i].Value.ToString()))
                            {
                              //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") && !String.IsNullOrEmpty(this.dgAgent[2, i].Value.ToString()))
                        {
                          this.sendEmail(this.dgAgent[2, i].Value.ToString());
                        }
                    }
                    ReturnMessage returnMessage = (ReturnMessage)JsonConvert.DeserializeObject(result.Html, typeof(ReturnMessage));
                    if (returnMessage != null && returnMessage.errcode != "0")
                    {
                        this.dgAgent[6, i].Value = returnMessage.getErrorDescrition();
                    }
                    else
                    {
                        this.dgAgent[6, i].Value = "同步成功";
                    }
                }

            }



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


        }