开发日志:企业微信自定义工作台
效果图:

登录成功之后调用:
WxHelper.set_workbench_template();
WxHelper.SetWorkbench("0", "0", "0");
/// <summary>
/// 设置应用在工作台展示的模版
/// https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_template?access_token=ACCESS_TOKEN
/// </summary>
public static string set_workbench_template() {
string strtoken = GetSQLAccessToken();
string formatString = "https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_template?access_token="+strtoken;
StringBuilder builder = new StringBuilder();
builder.AppendLine("{ ");
builder.AppendLine(" \"agentid\":"+AppConfig.wxAgentId+", ");
builder.AppendLine(" \"type\":\"keydata\", ");
builder.AppendLine(" \"image\":{ ");
builder.AppendLine(" \"url\":\"http://www.xxxxx.com/Content/Login/images/logo.png\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com\" ");
builder.AppendLine(" }, ");
builder.AppendLine(" \"keydata\":{ ");
builder.AppendLine(" \"items\":[ ");
builder.AppendLine(" { ");
builder.AppendLine(" \"key\":\"待办工作\", ");
builder.AppendLine(" \"data\":\"0\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com/SystemManage/FlowWork/DaiBanLiuCheng\" ");
builder.AppendLine(" }, ");
builder.AppendLine(" { ");
builder.AppendLine(" \"key\":\"在办工作\", ");
builder.AppendLine(" \"data\":\"0\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com/SystemManage/FlowWork/WoJingBanDe\" ");
builder.AppendLine(" }, ");
builder.AppendLine(" { ");
builder.AppendLine(" \"key\":\"办结工作\", ");
builder.AppendLine(" \"data\":\"0\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com/SystemManage/FlowWork/WoFaQiDe\" ");
builder.AppendLine(" } ");
builder.AppendLine(" ] ");
builder.AppendLine(" },");
builder.AppendLine(" \"replace_user_data\":true ");
builder.AppendLine("} ");
string retString = HttpRequest(formatString, builder.ToString());
return retString;
}
/// <summary>
/// 设置应用在用户工作台展示的数据
/// </summary>
public static string SetWorkbench(string num1, string num2, string num3) {
string strtoken = GetSQLAccessToken();
string formatString = "https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_data?access_token="+strtoken;
StringBuilder builder = new StringBuilder();
builder.AppendLine("{ ");
builder.AppendLine(" \"agentid\":"+AppConfig.wxAgentId+", ");
builder.AppendLine(" \"userid\":\"13320163493\", ");
builder.AppendLine(" \"type\":\"keydata\", ");
builder.AppendLine(" \"keydata\":{ ");
builder.AppendLine(" \"items\":[ ");
builder.AppendLine(" { ");
builder.AppendLine(" \"key\":\"待办工作\", ");
builder.AppendLine(" \"data\":\""+num1+"\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com/SystemManage/FlowWork/DaiBanLiuCheng\" ");
builder.AppendLine(" }, ");
builder.AppendLine(" { ");
builder.AppendLine(" \"key\":\"在办工作\", ");
builder.AppendLine(" \"data\":\""+num2+"\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com/SystemManage/FlowWork/WoJingBanDe\" ");
builder.AppendLine(" }, ");
builder.AppendLine(" { ");
builder.AppendLine(" \"key\":\"办结工作\", ");
builder.AppendLine(" \"data\":\""+num3+"\", ");
builder.AppendLine(" \"jump_url\":\"http://www.xxxxx.com/SystemManage/FlowWork/WoFaQiDe\" ");
builder.AppendLine(" } ");
builder.AppendLine(" ] ");
builder.AppendLine(" } ");
builder.AppendLine("} ");
string retString = HttpRequest(formatString, builder.ToString());
return retString;
}
public static string HttpRequest(string url, string param, string method = "POST") {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method=method;
request.ContentType="application/x-www-form-urlencoded";
request.Accept="*/*";
request.Timeout=15000;
request.AllowAutoRedirect=false;
StreamWriter requestStream = null;
WebResponse response = null;
string retString = null;
try {
requestStream=new StreamWriter(request.GetRequestStream());
requestStream.Write(param);
requestStream.Close();
response=request.GetResponse();
if(response!=null) {
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
retString=reader.ReadToEnd();
reader.Close();
}
} catch(Exception ex) {
retString="error:"+ex.Message;
} finally {
request=null;
requestStream=null;
response=null;
}
StringBuilder builder = new StringBuilder();
builder.AppendLine("");
builder.AppendLine("url:"+url);
builder.AppendLine("param:"+param);
builder.AppendLine("retString:"+retString);
Logger.LogFile(builder.ToString(), "weixin");
return retString;
}
/// <summary>
/// 获取微信凭证access_token的接口
/// </summary>
/// 将token值存放在数据库中,110分钟过期,每次生成token值之前从数据库中取出最老的未过期token,
/// 如果数据库中没有符合要求的token,重新生成
/// <returns>返回string类型的access_token</returns>
public static string GetSQLAccessToken()
{
Access_token res = new Access_token();
var sql = @"SELECT TOP 1 TokenValue FROM Token WHERE type='kqweixin_token' and GETDATE()<ExpireTime ORDER BY ExpireTime DESC ";
res.access_token = new DBHelper().ExecuteScalar<string>(sql);
if (res.access_token == null)
{
string formatString = String.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", AppConfig.appID, AppConfig.appsecret);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(formatString);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
if (retString.IndexOf("7200") > 0)
{
Access_token token = new Access_token();
token = ParseFromJson<Access_token>(retString);
res.access_token = token.access_token;
res.expires_in = token.expires_in;
sql = @"insert into Token (TokenValue,ExpireTime,Type) VALUES ('" + res.access_token + "', CONVERT(NVARCHAR(100), DATEADD(MINUTE, 110, GETDATE()), 20),'kqweixin_token')";
var aa = new DBHelper().GetExcuteNonQuery(sql);
}
}
return res.access_token;
}
/// <summary>
/// 将JSON对象转换为Model
/// </summary>
public static T ParseFromJson<T>(string szJson) {
T obj = Activator.CreateInstance<T>();
using(MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson))) {
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
/// <summary>
/// 微信Access_token模型
/// </summary>
/// <returns></returns>
public class Access_token {
string _access_token;
string _expires_in;
/// <summary>
/// 获取到的凭证
/// </summary>
public string access_token {
get { return _access_token; }
set { _access_token=value; }
}
/// <summary>
/// 凭证有效时间,单位:秒
/// </summary>
public string expires_in {
get { return _expires_in; }
set { _expires_in=value; }
}
}
CREATE TABLE [dbo].[Token](
[Gid] [INT] IDENTITY(1,1) NOT NULL,
[TokenValue] [NVARCHAR](255) NULL,
[ExpireTime] [NVARCHAR](255) NULL,
[Type] [NVARCHAR](255) NULL,
CONSTRAINT [PK_Token] PRIMARY KEY CLUSTERED
(
[Gid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
开发日志:企业微信实现扫码登录(WEB): https://www.cnblogs.com/luomingui/p/17749840.html
开发日志:企业微信自定义工作台:https://www.cnblogs.com/luomingui/p/18985097
开发日志:企业微信消息提醒和接收消息:https://www.cnblogs.com/luomingui/p/18985125

浙公网安备 33010602011771号