/// <summary> /// 查找未开启模板消息的用户用户,帮其开通模板消息 /// </summary> public void openNewTemplateMsg() { //查找所有用户未开启的模板消息 --专业版的基础版不开放模板消息,因此增加过滤条件 (xcxtemplate.Type != 22 or xcxappaccountrelation.versionId != 3) string findNotOpenTemplateMsgSql = $@"select templatemsg.Id as templateMsgId,xcxtemplate.type, xcxappaccountrelation.appId,templatemsg.titileId,templatemsg.colNums,templatemsg.tmgType from xcxappaccountrelation inner join xcxtemplate on xcxappaccountrelation.TId = xcxtemplate.Id and (xcxtemplate.Type != 22 or xcxappaccountrelation.versionId != 3) inner join templatemsg on xcxtemplate.Type = templatemsg.Ttypeid and templatemsg.state = 1 left join templatemsg_user on xcxappaccountrelation.AppId = templatemsg_user.AppId and templatemsg_user.TmId = templatemsg.Id where IFNULL(xcxappaccountrelation.appid,'') != '' AND templatemsg_user.Id is NULL limit 0,1000 "; //效率太慢了,1次1000条吧 TemplateMsg_User newUserMsg; string errorMsg = string.Empty; using (MySql.Data.MySqlClient.MySqlDataReader dr = SqlMySql.ExecuteDataReader(connName, System.Data.CommandType.Text, findNotOpenTemplateMsgSql, null)) { if (dr != null) { while (dr.Read()) { if (dr["templateMsgId"] == DBNull.Value || dr["appId"] == DBNull.Value || dr["titileId"] == DBNull.Value || dr["colNums"] == DBNull.Value || dr["type"] == DBNull.Value || dr["tmgType"] == DBNull.Value) { continue; } //帮未开通模板消息的客户开通模板消息 addResultModel _addResult = MsnModelHelper.addMsnToMy(Convert.ToString(dr["appId"]), Convert.ToString(dr["titileId"]), Convert.ToString(dr["colNums"])?.Split(','), ref errorMsg); if (_addResult != null && !string.IsNullOrWhiteSpace(_addResult.template_id)) { newUserMsg = new TemplateMsg_User(); newUserMsg.AppId = dr["appId"].ToString(); newUserMsg.TmId = Convert.ToInt32(dr["templateMsgId"]); newUserMsg.Ttypeid = Convert.ToInt32(dr["type"]); newUserMsg.ColNums = dr["colNums"].ToString(); newUserMsg.TitleId = dr["titileId"].ToString(); newUserMsg.State = 1; //启用 newUserMsg.CreateDate = DateTime.Now; newUserMsg.TemplateId = _addResult.template_id; //微信公众号内的模板Id newUserMsg.TmgType = Convert.ToInt32(dr["tmgType"]); TemplateMsg_UserBLL.SingleModel.Add(newUserMsg); } } } } }
/// <summary> /// 组合模板并添加至帐号下的个人模板库 /// </summary> /// <param name="AppId"></param> /// <param name="offset"></param> /// <param name="keyword_id_list">[3,4,5]</param> /// <param name="msg"></param> /// <returns></returns> public static addResultModel addMsnToMy(string AppId, string id, string[] keyword_id_list, ref string msg) { addResultModel model = new addResultModel(); try { string token = ""; if (!GetToken(AppId, ref token)) { model.errcode = -1; model.errmsg = token; msg = token; return(model); } string returnData = ""; //表示新增 string postData = Newtonsoft.Json.JsonConvert.SerializeObject(new { id = id, keyword_id_list = keyword_id_list }); returnData = DoPostJson("https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=" + token, postData); if (string.IsNullOrEmpty(returnData)) { model.errcode = -1; model.errmsg = "组合模板并添加至帐号下的个人模板库失败"; return(model); } model = !string.IsNullOrEmpty(returnData) ? SerializeHelper.DesFromJson <addResultModel>(returnData) : new addResultModel(); } catch (Exception ex) { log4net.LogHelper.WriteError(typeof(MsnModelHelper), ex); } return(model); }