// 创建数据库表 create table TBSF_Conmmunicate_WXUser ( ID int identity(1,1) primary key, Staff_ID varchar(10), OpenId varchar(50), SessionKey varchar(50), UnionId varchar(50), IsValid bit, ) // SqlHelper数据库辅助类来自于CommunicationOperateDBUtility,可以自己编写 using System.Data; using System.Text; using CommunicationOperateDBUtility; namespace WXERP.Services.CommunicationOperateDAL { /// <summary> /// 微信信息 /// </summary> public class WXInforDeal { private SqlHelper sqlHelper = null; /// <summary> /// 初始化數據庫輔助對象 /// </summary> /// <param></param> public WXInforDeal(object con) { sqlHelper = new SqlHelper(con); } /// <summary> /// 獲取微信登陸用戶信息 /// </summary> /// <param>工號</param> /// <returns></returns> public DataSet GetLoginUserInfo(string staffIdList) { DataSet ds = new DataSet(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(" SELECT distinct OpenId FROM "); stringBuilder.Append(" TBSF_Conmmunicate_WXUser WHERE Staff_ID IN ("); stringBuilder.Append(staffIdList); stringBuilder.Append(")"); string strSql = stringBuilder.ToString(); sqlHelper.DBRunSql(strSql, ref ds); return ds; } } }
五、编写订阅消息基类模型
using System; using System.Data; using Newtonsoft.Json; using System.Collections.Generic; using WXERP.Services.CommunicationOperateDAL; namespace WXERP.Models { /// <summary> /// 訂閲消息請求模型 /// </summary> public class SubscribeMessageModel { /// <summary> /// 初始化審核訂閲消息 /// </summary> /// <param>數據庫事務</param> /// <param>下一個審核通知用戶工號</param> public SubscribeMessageModel(object dbTransOrCnn, string nextAuditStaffId) { WXInforDeal wxInfoDeal = new WXInforDeal(dbTransOrCnn); DataSet wxUserInfo = wxInfoDeal.GetLoginUserInfo(nextAuditStaffId); if (wxUserInfo != null && wxUserInfo.Tables.Count > 0 && wxUserInfo.Tables[0].Rows.Count > 0) { Touser = wxUserInfo.Tables[0].Rows[0]["OpenId"].ToString(); } } /// <summary> /// 消息接收者的openid /// </summary> [JsonProperty("touser")] public string Touser { get; set; } /// <summary> /// 消息模板ID /// </summary> [JsonProperty("template_id")] public string TemplateId { get; set; } /// <summary> /// 點擊模板卡片后的跳轉頁面,僅限本小程序内的頁面,支持帶參數(示例index?foo=bar),該字段不填則不跳轉 /// </summary> [JsonProperty("page")] public string Page { get; set; } /// <summary> /// 跳轉小程序類型:developer開發版、trial體驗版、formal正式版,默认为正式版 /// </summary> [JsonProperty("miniprogram_state")] public string MiniprogramState { get; set; } /// <summary> /// 進入小程序查看的語言類型,支持zh_CN(簡體中文)、en_US(英文)、zh_HK(繁體中文)、zh_TW(繁體中文),默認為zh_CN /// </summary> [JsonProperty("lang")] public string Lang { get; set; } /// <summary> /// 模板内容 /// </summary> [JsonProperty("data")] public Dictionary<string, DataValue> Data { get; set; } } /// <summary> /// 模板内容關鍵字 /// </summary> public class DataValue { /// <summary> /// 訂閲消息參數值 /// </summary> [JsonProperty("value")] public string Value { get; set; } } /// <summary> /// 小程序訂閲消息響應模型 /// </summary> public class SubscribeMsgResponseModel { /// <summary> /// 錯誤代碼 /// </summary> public int Errcode { get; set; } /// <summary> /// 錯誤信息 /// </summary> public string Errmsg { get; set; } } /// <summary> /// 小程序獲取token響應模型 /// </summary> public class AccessTokenResponseModel { /// <summary> /// 小程序訪問token /// </summary> public string Access_token { get; set; } /// <summary> /// Token過期時間,單位秒 /// </summary> public int Expires_id { get; set; } /// <summary> /// Token創建時間 /// </summary> public DateTime Create_time { get; set; } /// <summary> /// 刷新以後的Token /// </summary> public string Refresh_token { get; set; } /// <summary> /// 小程序用戶唯一標識,如果用戶未關注公衆號,訪問公衆號網頁也會產生 /// </summary> public string Openid { get; set; } /// <summary> /// 用戶授權的作用域,使用逗號分隔 /// </summary> public string Scope { get; set; } } }