public async Task <IActionResult> OnPostAsync(int id = 0) { IsEdit = id > 0; MpAccount mpAccount = null; if (IsEdit) { mpAccount = await _mpAccountService.GetObjectAsync(z => z.Id == id); if (mpAccount == null) { return(RenderError("公众号信息不存在!")); } _mpAccountService.Mapper.Map(MpAccountDto, mpAccount); } else { mpAccount = new MpAccount(MpAccountDto); } await _mpAccountService.SaveObjectAsync(mpAccount); //重新进行公众号注册 await AccessTokenContainer.RegisterAsync(mpAccount.AppId, mpAccount.AppSecret, $"{mpAccount.Name}-{mpAccount.Id}"); //立即获取 AccessToken await AccessTokenContainer.GetAccessTokenAsync(mpAccount.AppId, true); //return RedirectToPage("./Edit", new { id = mpAccount.Id, uid = Uid }); return(Ok(new { id = mpAccount.Id, uid = Uid })); }
public async Task <IActionResult> OnPostAjaxAsync([FromBody] MpAccountDto mpAccountDto) { MpAccount mpAccount = null; if (mpAccountDto.Id > 0) { mpAccount = await _mpAccountService.GetObjectAsync(z => z.Id == mpAccountDto.Id); if (mpAccount == null) { return(RenderError("公众号信息不存在!")); } mpAccountDto.AddTime = mpAccount.AddTime; _mpAccountService.Mapper.Map(mpAccountDto, mpAccount); mpAccount.LastUpdateTime = DateTime.Now; } else { mpAccount = new MpAccount(mpAccountDto); } await _mpAccountService.SaveObjectAsync(mpAccount); //重新进行公众号注册 await AccessTokenContainer.RegisterAsync(mpAccount.AppId, mpAccount.AppSecret, $"{mpAccount.Name}-{mpAccount.Id}"); //立即获取 AccessToken await AccessTokenContainer.GetAccessTokenAsync(mpAccount.AppId, true); //return RedirectToPage("./Edit", new { id = mpAccount.Id, uid = Uid }); return(Ok(new { id = mpAccount.Id, uid = Uid })); }
public CustomMessageHandler(Stream inputStream, PostModel postModel, MpAccount ac, System.Data.Entity.DbContext etis, int maxRecordCount = 0) : base(inputStream, postModel, maxRecordCount) { //这里设置仅用于测试,实际开发可以在外部更全局的地方设置, //比如MessageHandler<MessageContext>.GlobalWeixinContext.ExpireMinutes = 3。 WeixinContext.ExpireMinutes = 3; account = ac; entities = etis; int.TryParse(msgcachetime, out cachesecond); }
private async Task ReFreshFans(MpAccount model) { var mpId = model.Id; #region 初始化 var sb = new StringBuilder(); int stoptick = 0, stopcount = 100; var fanssavestr = @" if not exists(select * from MpFans where MpID='{0}' and OpenID='{1}') insert into MpFans ([MpID],[OpenID],[NickName],[Sex],[Language],[City],[Province],[Country],[HeadImgUrl],[SubscribeTime],[UnionID],[WxGroupID],[IsFans],[UpdateTime],CreationTime,IsDeleted) values ({0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{12}','{13}',getdate(),getdate(),0) else update MpFans SET [MpID]='{0}',[OpenID]='{1}',[NickName]='{2}',[Sex]='{3}',[Language]='{4}',[City]='{5}',[Province]='{6}',[Country]='{7}',[HeadImgUrl]='{8}',[SubscribeTime]='{9}',[UnionID]='{10}',[WxGroupID]='{12}',[IsFans]='{13}',[UpdateTime]=getdate(),IsDeleted=0 WHERE MpID='{0}' and OpenID='{1}' "; #endregion #region 更新用户 #region 更新前10000个用户 OpenIdResultJson fansopenids = null; string _accesstoken = ""; try { _accesstoken = (await StaticObjects.GetAccessToken(model.TaskAccessToken)).access_token; fansopenids = UserApi.Get(_accesstoken, ""); } catch (Exception e) { _accesstoken = (await StaticObjects.GetAccessToken(model.TaskAccessToken, 1)).access_token; //LogWriter.Error(string.Format("获取MpID为{0}的openid报错,错误信息:{1}", mpId, ex)); fansopenids = UserApi.Get(_accesstoken, ""); } DateTime now = DateTime.Now; if (fansopenids.count > 0) { foreach (var id in fansopenids.data.openid) { UserInfoJson wxinfo = null; try { wxinfo = UserApi.Info((await StaticObjects.GetAccessToken(model.TaskAccessToken)).access_token, id); } catch { //LogWriter.Error(string.Format("获取MpID为{0},openid为{1}的用户信息报错,错误信息:{2}", mpId.ToString(), id, ex)); wxinfo = UserApi.Info((await StaticObjects.GetAccessToken(model.TaskAccessToken, 1)).access_token, id); } sb.AppendFormat(fanssavestr, mpId, id, DbTool.ToSqlParamString(wxinfo.nickname), wxinfo.sex, DbTool.ToSqlParamString(wxinfo.language), DbTool.ToSqlParamString(wxinfo.city), DbTool.ToSqlParamString(wxinfo.province), DbTool.ToSqlParamString(wxinfo.country), DbTool.ToSqlParamString(wxinfo.headimgurl), DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time), DbTool.ToSqlParamString(wxinfo.unionid), DbTool.ToSqlParamString(wxinfo.remark), wxinfo.groupid, wxinfo.subscribe); stoptick++; if (stoptick % stopcount == 0 || stoptick == fansopenids.count) { if (sb.Length > 0) { StaticObjects.db.ExcuteNonQuery(sb.ToString(), null, false); } sb.Clear(); } } } #endregion #region while循环更新后续所有用户 while (!string.IsNullOrEmpty(fansopenids.next_openid)) { try { fansopenids = UserApi.Get((await StaticObjects.GetAccessToken(model.TaskAccessToken)).access_token, fansopenids.next_openid); } catch { //LogWriter.Error(string.Format("获取MpID为{0}的openid报错,nextopenid为{1},错误信息:{2}", mpId, fansopenids.next_openid, ex)); fansopenids = UserApi.Get((await StaticObjects.GetAccessToken(model.TaskAccessToken, 1)).access_token, fansopenids.next_openid); } if (fansopenids.count > 0) { foreach (var id in fansopenids.data.openid) { UserInfoJson wxinfo = null; try { wxinfo = UserApi.Info((await StaticObjects.GetAccessToken(model.TaskAccessToken)).access_token, id); } catch { //LogWriter.Error(string.Format("获取MpID为{0},openid为{1}的用户信息报错,错误信息:{2}", mpId, id, ex)); wxinfo = UserApi.Info((await StaticObjects.GetAccessToken(model.TaskAccessToken, 1)).access_token, id); } sb.AppendFormat(fanssavestr, mpId, id, DbTool.ToSqlParamString(wxinfo.nickname), wxinfo.sex, DbTool.ToSqlParamString(wxinfo.language), DbTool.ToSqlParamString(wxinfo.city), DbTool.ToSqlParamString(wxinfo.province), DbTool.ToSqlParamString(wxinfo.country), DbTool.ToSqlParamString(wxinfo.headimgurl), DateTimeHelper.GetDateTimeFromXml(wxinfo.subscribe_time), DbTool.ToSqlParamString(wxinfo.unionid), DbTool.ToSqlParamString(wxinfo.remark), wxinfo.groupid, wxinfo.subscribe); stoptick++; if (stoptick % stopcount == 0 || stoptick == fansopenids.count) { if (sb.Length > 0) { StaticObjects.db.ExcuteNonQuery(sb.ToString(), null, false); } sb.Clear(); } } } } #endregion //没更新到的用户设为取消关注 sb.AppendFormat("update MpFans set IsFans='0' where MpID='{0}' and UpdateTime<'{1}'", mpId, now.AddDays(-1)); #endregion if (sb.Length > 0) { StaticObjects.db.ExcuteNonQuery(sb.ToString(), null, false); } //LogWriter.Info(string.Format("更新数量{0},更新完毕", stoptick)); }