OWX使用说明
项目设置
应用设置
通过UCS添加具体项目添加项目信息
Name(应用名称)
ApplicationPath(消息回调地址,OWX抛过来的消息接收的地址如:http://localhost/XXXX/rest/wxmessage/handle)
ApplicationCallBack(登入回调地址,OWX登入后需要具体项目登入地址如:http://dev.nbeport.com/XXXX/home/LoginCallBack)
WeixinAppId(微信ID)
WeixinAppSecret(微信密钥)
WeixinClientSecret(响应微信的Token验证信息)
具体项目微信设置
微信回调地址统一以http://dev.nbeport.com/owx/rest/message/{applicationId 具体在上面应用中设定的ID}
Token需与上面设置的WeixinAppSecret一致
设置JS接口安全域名,授权回调页面域名
在OWX中设置首页登入模版
//权限过滤器 套用V2模版 不需做修改
[WeixinAuthorizeV2]
//定义要设置的应用首页
public ActionResult Index_V2(string id)
{
var url = Request.QueryString["returnUrl"];
//如果有回调URL则进行回调,没有回调则默认进入对应应用首页 这里的URL为GOX短连接唯一ID
if (!String.IsNullOrEmpty(url))
{
string shortCode = url;
var rs = GoxClient.GetShortUrl(shortCode);
return Content(string.Format("<script>window.location.href='" + rs.LongUrl + "'; </script>"));
}
ViewData["ApplicationId"] = Session["applicationId"] == null ? "default" : Session["applicationId"].ToString();
ViewData["Openid"] = Session["openId"] == null ? "default" : Session["openId"].ToString();
return View();
}
注:后期将通过UCS菜单进行控制
具体应用中的设置
这里以XXXX应用为例子
- HomeController设置
//HomeController
public class HomeController : Controller
{
public HomeController()
{
}
//获取Authentication
private IAuthenticationManager authentication
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
//获取应用认证ID
private string GetOauthType
{
get
{
return System.Configuration.ConfigurationManager.AppSettings["oauth:authType"];
}
}
[WeixinAuthorize]
public ActionResult Index()
{
//获取用户信息并放入VIWDATA
var result = authentication.AuthenticateAsync(GetOauthType).Result;
ViewData["UserId"] = result.Identity.Claims.Where(x => x.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value;
ViewData["OpenId"] = result.Identity.Claims.Where(x => x.Type == ClaimTypes.Name).FirstOrDefault().Value;
ViewData["ApplicationId"] = Purple.ConfigHelper.GetConfigString("oauth:clientId");
return View();
}
/// <summary>
/// 授权回调函数,跟上面应用中设立的参数要一致
/// </summary>
/// <param name="userId">用户ID</param>
/// <param name="openId">微信ID</param>
/// <param name="returnUrl">回调短连接ID</param>
public ActionResult LoginCallBack(string userId,string openId,string returnUrl)
{
//有信息才会登入不然返回owx进行再授权
if(!String.IsNullOrEmpty(userId))
{
//sigin
this.authentication.SignOut(GetOauthType);
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
claims.Add(new Claim(ClaimTypes.Name, openId));
var id = new ClaimsIdentity(claims, GetOauthType);
this.authentication.SignIn(id);
}
return Redirect(Purple.ConfigHelper.GetConfigString("address:owx") + "/weixin/Index_V2?id=" + Purple.ConfigHelper.GetConfigString("oauth:clientId")+"&ReturnUrl="+returnUrl);
}
}
- 登入授权过滤器
///<summary>
/// 认证信息
/// </summary>
public class WeixinAuthorize : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return base.AuthorizeCore(httpContext);
}
//验证不通过直接跳到OWX进行授权
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
var url = filterContext.HttpContext.Request.Params["id"];
filterContext.HttpContext.Response.Redirect(Purple.ConfigHelper.GetConfigString("address:owx") + "/WxAccountV2/UnifyLogin?id=" + Purple.ConfigHelper.GetConfigString("oauth:clientId") + "&returnUrl=" + url);
}
}
- 设置参数必须加入下列项目
<add key="address:owx" value="http://XXXXX/owx" />
<add key="address:oauthwx" value="http://XXXX/cas/rest/AccountWx" />
消息处理设置
在前面应用中已经将消息处理接口设置,在具体应用中只要设置具体回调接口
- 消息处理接口
//定义回调方法
public HttpResponseMessage MessageHandle(WxMessageReceive model)
{
//要返回的XML
string RETURNXML = "";
//初始化信息
TextMessage initModel = new TextMessage()
{
ToUserName = model.FromUserName,
FromUserName = model.ToUserName,
CreateTime = HwxHelper.ConvertDateTimeInt(DateTime.Now),
MsgType = "text",
Content = "敬请期待!"
};
RETURNXML = HwxHelper.GetPostXml<TextMessage>(initModel);
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(RETURNXML, Encoding.UTF8);
return response;
}
- Purple.top.Hwx.Model提供所有关于微信被动消息的模型
使用时需先安装Purple.top.Hwx nuget包
1. WxMessageReceive--接受模型
public class WxMessageReceive
{
#region 基础信息
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName { get; set; }
/// <summary>
/// 发送方帐号
/// </summary>
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string MsgType { get; set; }
/// <summary>
/// 消息ID 用于非EVENT消息
/// </summary>
public long MsgId { get; set; }
/// <summary>
/// 图片消息媒体id,可以调用多媒体文件下载接口拉取数据
/// </summary>
public string MediaId { get; set; }
#endregion
#region 文本消息
/// <summary>
/// 文本消息内容
/// </summary>
public string Content { get; set; }
#endregion
#region 图片消息
/// <summary>
/// 图片链接
/// </summary>
public string PicUrl { get; set; }
#endregion
#region 语音消息
/// <summary>
/// 语音格式
/// </summary>
public string Format { get; set; }
#endregion
#region 视频/小视频 消息
/// <summary>
/// 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据
/// </summary>
public string ThumbMediaId { get; set; }
#endregion
#region 地理位置消息
/// <summary>
/// 地理位置维度
/// </summary>
public decimal Location_X { get; set; }
/// <summary>
/// 地理位置经度
/// </summary>
public decimal Location_Y { get; set; }
/// <summary>
/// 地图缩放大小
/// </summary>
public int Scale { get; set; }
/// <summary>
/// 地理位置信息
/// </summary>
public string Label { get; set; }
#endregion
#region 链接消息
/// <summary>
/// 消息标题
/// </summary>
public string Title { get; set; }
/// <summary>
/// 消息描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 消息链接
/// </summary>
public string Url { get; set; }
#endregion
#region event
/// <summary>
/// 事件类型
/// </summary>
public string Event { get; set; }
/// <summary>
/// 事件Key值
/// </summary>
public string EventKey { get; set; }
#region 扫描带参数二维码事件
/// <summary>
/// 二维码的ticket,可用来换取二维码图片
/// </summary>
public string Ticket { get; set; }
#endregion
#region 上报地理位置事件
/// <summary>
/// 地理位置纬度
/// </summary>
public decimal Latitude { get; set; }
/// <summary>
/// 地理位置经度
/// </summary>
public decimal Longitude { get; set; }
/// <summary>
/// 地理位置精度
/// </summary>
public decimal Precision { get; set; }
#endregion
#endregion
}
2. TextMessage--文本消息
public partial class TextMessage
{
/// <summary>
/// 接收方帐号
/// </summary>
public partial class TextMessage
{
[XmlIgnore]
public string ToUserName { get; set; }
/// <summary>
/// 开发者微信号
/// </summary>
[XmlIgnore]
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
[XmlIgnore]
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
[XmlIgnore]
public string MsgType { get; set; }
/// <summary>
/// 回复的内容
/// </summary>
[XmlIgnore]
public string Content { get; set; }
}
3. ImgMessage--图片消息
public partial class ImgMessage
{
/// <summary>
/// 接收方帐号
/// </summary>
[XmlIgnore]
public string ToUserName { get; set; }
/// <summary>
/// 开发者微信号
/// </summary>
[XmlIgnore]
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
[XmlIgnore]
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
[XmlIgnore]
public string MsgType { get; set; }
/// <summary>
/// 图片信息
/// </summary>
[XmlElement("Image")]
public Image Image { get; set; }
}
/// <summary>
/// 图片信息
/// </summary>
public partial class Image
{
/// <summary>
/// 素材ID
/// </summary>
[XmlIgnore]
public string MediaId { get; set; }
}
4. VoiceMessage--声音消息
public partial class VoiceMessage
{
/// <summary>
/// 接收方帐号
/// </summary>
[XmlIgnore]
public string ToUserName { get; set; }
/// <summary>
/// 开发者微信号
/// </summary>
[XmlIgnore]
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
[XmlIgnore]
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
[XmlIgnore]
public string MsgType { get; set; }
}
5. VideoMessage--视频消息
public partial class VideoMessage
{
/// <summary>
/// 接收方帐号
/// </summary>
[XmlIgnore]
public string ToUserName { get; set; }
/// <summary>
/// 开发者微信号
/// </summary>
[XmlIgnore]
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
[XmlIgnore]
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
[XmlIgnore]
public string MsgType { get; set; }
}
public partial class Video
{
/// <summary>
/// 素材ID
/// </summary>
[XmlIgnore]
public string MediaId { get; set; }
/// <summary>
/// 标题
/// </summary>
[XmlIgnore]
public string Title { get; set; }
/// <summary>
/// 描述
/// </summary>
[XmlIgnore]
public string Description { get; set; }
}
6. MusicMessage--音乐消息
public partial class MusicMessage
{
/// <summary>
/// 接收方帐号
/// </summary>
[XmlIgnore]
public string ToUserName { get; set; }
/// <summary>
/// 开发者微信号
/// </summary>
[XmlIgnore]
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
[XmlIgnore]
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
[XmlIgnore]
public string MsgType { get; set; }
}
/// <summary>
/// 音乐消息
/// </summary>
public partial class Music
{
/// <summary>
/// 标题
/// </summary>
[XmlIgnore]
public string Title { get; set; }
/// <summary>
/// 描述
/// </summary>
[XmlIgnore]
public string Description { get; set; }
/// <summary>
/// 音乐链接
/// </summary>
[XmlIgnore]
public string MusicUrl { get; set; }
/// <summary>
/// 高品质音乐链接
/// </summary>
[XmlIgnore]
public string HQMusicUrl { get; set; }
/// <summary>
/// 缩略图媒体ID
/// </summary>
[XmlIgnore]
public string ThumbMediaId { get; set; }
}
7. ArticleMessage--图文消息
public partial class ArticleMessage
{
/// <summary>
/// 接收方帐号
/// </summary>
[XmlIgnore]
public string ToUserName { get; set; }
/// <summary>
/// 开发者微信号
/// </summary>
[XmlIgnore]
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间
/// </summary>
[XmlIgnore]
public int CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
[XmlIgnore]
public string MsgType { get; set; }
/// <summary>
/// 图文消息条数
/// </summary>
[XmlIgnore]
public int ArticleCount { get; set; }
}
/// <summary>
/// 文章消息
/// </summary>
public partial class Article
{
/// <summary>
/// 标题
/// </summary>
[XmlIgnore]
public string Title { get; set; }
/// <summary>
/// 描述
/// </summary>
[XmlIgnore]
public string Description { get; set; }
/// <summary>
/// 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
/// </summary>
[XmlIgnore]
public string PicUrl { get; set; }
/// <summary>
/// 点击图文消息跳转链接
/// </summary>
[XmlIgnore]
public string Url { get; set; }
}
-
Purple.top.Hwx.Helper提供将所有上述模型转换成XML文档
HwxHelper.GetPostXml()将上述模型转换成微信接收的模型
-
主动消息发送
method:httppost /rest/message/{applicationId}/sendtemplate postJsonModel:
/// <summary>
/// 模版发送模型
/// </summary>
public class TempalteSendModel
{
//对应的用户ID
public string touser { get; set; }
//模版ID
public string template_id { get; set; }
//模版消息点击URL
public string url { get; set; }
//要发送的数据模型
public object data { get; set; }
}
二维码扫码设置
未完待续
图片上传
未完待续