private void btnDelete_Click(object sender, EventArgs e) { if (this.dgAgentWechatAccount.CurrentRow==null) { MessageBox.Show("请选择一行记录"); return; } String userId= this.dgAgentWechatAccount.CurrentRow.Cells[6].Value.ToString(); if (!String.IsNullOrEmpty(userId)) { WechatAction wechatAction = new Wechat.WechatAction(); HttpResult result = wechatAction.deleteUserFromWechat(userId, Settings.Default.Wechat_Secret); if (result.StatusCode == System.Net.HttpStatusCode.OK) { AgentWechatAccountDao agentWechatAccountDao = new AgentWechatAccountDao(); agentWechatAccountDao.Delete(userId); this.prepareGrid(this.txtKeyword.Text.Trim(), this.cboType.Text.Trim(),this.checkUnsync.Checked); } } }
/// <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"); }