/// <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"); }
/// <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"); }
private void frmAddWechatUser_Load(object sender, EventArgs e) { if (String.IsNullOrEmpty(wechatJsonUser.userid)) { this.txtUserId.Enabled = true; } else { this.txtUserId.Enabled = false; this.txtUserId.Text = wechatJsonUser.userid; HttpResult userResult = wechatAction.getUserFromWechat(wechatJsonUser.userid, this.secret); if (userResult.StatusCode == System.Net.HttpStatusCode.OK) { //表示访问成功,具体的大家就参考HttpStatusCode类 wechatJsonUser = JsonConvert.DeserializeObject<WechatJsonUser>(userResult.Html); this.txtName.Text = wechatJsonUser.name; this.txtEmail.Text = wechatJsonUser.email; this.txtMobile.Text = wechatJsonUser.mobile; this.txtPosition.Text = wechatJsonUser.position; this.txtWeixinId.Text = wechatJsonUser.weixinid; } } HttpResult result = wechatAction.getDepartmentListFromWechat(secret); if (result.StatusCode == System.Net.HttpStatusCode.OK) { //表示访问成功,具体的大家就参考HttpStatusCode类 DepartmentList departmentList = JsonConvert.DeserializeObject<DepartmentList>(result.Html); if (departmentList != null) { DataTable tb = new DataTable(); DataColumn col1 = new DataColumn("id", typeof(int)); tb.Columns.Add(col1); DataColumn col2 = new DataColumn("name", typeof(String)); tb.Columns.Add(col2); DataColumn col3 = new DataColumn("parentid", typeof(int)); tb.Columns.Add(col3); foreach (Department department in departmentList.department) { DataRow dr = tb.NewRow(); dr["id"] = department.id; dr["name"] = department.name; dr["parentid"] = department.parentid; tb.Rows.Add(dr); } tvOrganization.Nodes.Clear(); //创建根节点 TreeNode rootNode = new TreeNode(); rootNode.Text = departmentList.department[0].name; rootNode.Tag = departmentList.department[0].id; //rootNode.ImageUrl = "~/Image/Root.gif"; tvOrganization.Nodes.Add(rootNode); //创建其他节点 CreateChildNode(rootNode, tb); tvOrganization.ExpandAll(); } } }
/// <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"); }