[转载][c#]asp.net开发微信公众平台(3)微信消息封装及反射赋值 - 沫尘 - 博客园

[转载]asp.net开发微信公众平台(3)微信消息封装及反射赋值 – 沫尘 – 博客园.

上一篇已经搭建好整体框架,实现了入口的验证, 验证通过后就交给LookMsgType方法处理,LookMsgType方法主要是对微信发来的不同的消息进行分解,不同的类型交给业务逻辑层不同的方法处理, 对不同类型的消息判断,可以用if,也可以用switch 一般来说超过5个的if用switch会更好, 这里贴出LookMsgType方法:

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">LookMsgType</span><span class="pun">(</span><span class="kwd">string</span><span class="pln"> msgType</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
 
</span><span class="com">#region 判断消息类型</span><span class="pln">
</span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="pln">msgType</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"text"</span><span class="pun">:</span><span class="pln">
</span><span class="typ">RText</span><span class="pln"> mText </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RText</span><span class="pun">();</span><span class="pln">
mText </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mText</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoText</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln"> mText</span><span class="pun">);</span><span class="com">//文本消息</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"image"</span><span class="pun">:</span><span class="pln">
</span><span class="typ">RImg</span><span class="pln"> mImg </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RImg</span><span class="pun">();</span><span class="pln">
mImg </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mImg</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoImg</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mImg</span><span class="pun">);</span><span class="com">//图片</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"voice"</span><span class="pun">:</span><span class="pln"> </span><span class="com">//声音</span><span class="pln">
</span><span class="typ">RVoice</span><span class="pln"> mVoice </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RVoice</span><span class="pun">();</span><span class="pln">
mVoice </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mVoice</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoVoice</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mVoice</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
 
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"video"</span><span class="pun">:</span><span class="com">//视频</span><span class="pln">
</span><span class="typ">RVideo</span><span class="pln"> mVideo </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RVideo</span><span class="pun">();</span><span class="pln">
mVideo </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mVideo</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoVideo</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln"> mVideo</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
 
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"location"</span><span class="pun">:</span><span class="com">//地理位置</span><span class="pln">
</span><span class="typ">RLocation</span><span class="pln"> mLocation </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RLocation</span><span class="pun">();</span><span class="pln">
mLocation </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mLocation</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoLocation</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mLocation</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"link"</span><span class="pun">:</span><span class="com">//链接</span><span class="pln">
</span><span class="typ">RLink</span><span class="pln"> mLink </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RLink</span><span class="pun">();</span><span class="pln">
mLink </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mLink</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoLink</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mLink</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="com">#region 事件</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"event"</span><span class="pun">:</span><span class="pln">
 
</span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">ReadModel</span><span class="pun">(</span><span class="str">"Event"</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">))</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"subscribe"</span><span class="pun">:</span><span class="pln">
 
</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">ReadModel</span><span class="pun">(</span><span class="str">"EventKey"</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">).</span><span class="typ">IndexOf</span><span class="pun">(</span><span class="str">"qrscene_"</span><span class="pun">)</span><span class="pln"> </span><span class="pun">&</span><span class="pln">gt</span><span class="pun">;=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
</span><span class="typ">RCodeNotSub</span><span class="pln"> mNotSub </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RCodeNotSub</span><span class="pun">();</span><span class="pln">
mNotSub </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mNotSub</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoCodeNotSub</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mNotSub</span><span class="pun">);</span><span class="com">//未关注的新用户,扫描带参数的二维码关注</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">else</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
</span><span class="typ">RSub</span><span class="pln"> mSub </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RSub</span><span class="pun">();</span><span class="pln">
mSub </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mSub</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoSub</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mSub</span><span class="pun">);</span><span class="com">//普通关注</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"unsubscribe"</span><span class="pun">:</span><span class="pln">
</span><span class="typ">RUnsub</span><span class="pln"> mUnSub </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RUnsub</span><span class="pln"> </span><span class="pun">();</span><span class="pln">
mUnSub </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mUnSub</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoUnSub</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mUnSub</span><span class="pun">);</span><span class="com">//取消关注</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
 
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"SCAN"</span><span class="pun">:</span><span class="pln">
</span><span class="typ">RCodeSub</span><span class="pln"> mCodeSub </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RCodeSub</span><span class="pun">();</span><span class="pln">
mCodeSub </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mCodeSub</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoCodeSub</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln">mCodeSub</span><span class="pun">);</span><span class="com">//已经关注的用户扫描带参数的二维码</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"LOCATION"</span><span class="pun">:</span><span class="com">//用户上报地理位置</span><span class="pln">
 
</span><span class="typ">RSubLocation</span><span class="pln"> mSubLoc </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RSubLocation</span><span class="pun">();</span><span class="pln">
mSubLoc </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mSubLoc</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
 
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoSubLocation</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln"> mSubLoc</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"CLICK"</span><span class="pun">:</span><span class="com">//自定义菜单点击</span><span class="pln">
 
</span><span class="typ">RMenuClick</span><span class="pln"> mMenuClk </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RMenuClick</span><span class="pun">();</span><span class="pln">
mMenuClk </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mMenuClk</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoMenuClick</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln"> mMenuClk</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">case</span><span class="pln"> </span><span class="str">"VIEW"</span><span class="pun">:</span><span class="com">//自定义菜单跳转事件</span><span class="pln">
 
</span><span class="typ">RMenuView</span><span class="pln"> mMenuVw </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">RMenuView</span><span class="pun">();</span><span class="pln">
mMenuVw </span><span class="pun">=</span><span class="pln"> </span><span class="typ">ReadXml</span><span class="pun">.</span><span class="typ">GetModel</span><span class="pun">(</span><span class="pln">mMenuVw</span><span class="pun">,</span><span class="pln"> xmlModel</span><span class="pun">);</span><span class="pln">
</span><span class="typ">BLLWei</span><span class="pun">.</span><span class="typ">DoMenuView</span><span class="pun">(</span><span class="pln">dbHome</span><span class="pun">,</span><span class="pln"> mMenuVw</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="pun">};</span><span class="pln">
</span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
</span><span class="com">#endregion</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="com">#endregion</span><span class="pln">
</span><span class="pun">}</span>

外层switch判断msgtype,   在event类型时,再次switch判断具体的事件类型(关注、取消关注、自定义菜单事件等),  至此所有的微信发来的消息都有处理了,在上面代码中用到消息模型以及ReadXml.GetModel方法给模型赋值, 赋值之后传递给业务逻辑层对应的方法处理,  下面写出消息封装和给模型赋值的方法。

1、消息封装:

对所有微信发来的消息进行封装, 在datamodel中建一个Receive文件夹和一个send文件夹,在其中分别建立对应消息的类,完成之后,完整的datamodel类库如下图:

 

举例

—–接收消息:

文本消息RText.cs

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">RText</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">ToUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 开发者微信号</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">FromUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 用户号(OpenID)</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> </span><span class="typ">CreateTime</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 创建时间</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">MsgType</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="com">//消息类型</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">Content</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//内容</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> </span><span class="typ">MsgId</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//消息ID</span><span class="pln">
 
    </span><span class="pun">}</span>

自定义菜单点击RMenuClick.cs

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">RMenuClick</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">ToUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 开发者微信号</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">FromUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 用户号(OpenID)</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> </span><span class="typ">CreateTime</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 创建时间</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">MsgType</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="com">//消息类型</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">Event</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//事件类型</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">EventKey</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//事件key</span><span class="pln">
         
    </span><span class="pun">}</span>

其他也都类似,不一一列举。

—–发送消息

发送文本消息SText.cs

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SText</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
 
 
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">ToUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 用户号(OpenID)</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">FromUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 开发者微信号</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> </span><span class="typ">CreateTime</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 创建时间</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">MsgType</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"text"</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="com">//消息类型</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">Content</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//内容</span><span class="pln">
 
 
    </span><span class="pun">}</span><span class="pln">
 
</span><span class="typ">SText</span>

发送图文消息SNews.cs

<span class="kwd">namespace</span><span class="pln"> </span><span class="typ">DataModel</span><span class="pun">.</span><span class="typ">Send</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">SNews</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">ToUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 用户号(OpenID)</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">FromUserName</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 开发者微信号</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">long</span><span class="pln"> </span><span class="typ">CreateTime</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">// 创建时间</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">MsgType</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"news"</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="com">//消息类型</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> </span><span class="typ">ArticleCount</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//图文个数</span><span class="pln">
 
        </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">List</span><span class="pun"><</span><span class="typ">ArticlesModel</span><span class="pun">></span><span class="pln"> </span><span class="typ">Articles</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//图文列表</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ArticlesModel</span><span class="pln"> </span><span class="com">//默认第一条大图显示</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">Title</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//标题</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">Description</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//描述</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">PicUrl</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//图片链接  </span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">Url</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">get</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="com">//点击之后跳转的链接</span><span class="pln">
 
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
 
</span><span class="pun">发送图文消息</span>

在发送图文消息中,因为回复给微信的图文消息中,具体的图文内容是多条(最多可以10条),所以单独会有ArticlesModel。 后面文章会写出图文消息的发送。

2、通过反射给model赋值

在上篇文章写的入口处,已经有了解析xml的方法,现在封装了消息,通常的做法,是每次用到对应的model就手动写代码赋值, 而我这里LookMsgType方法中所有给消息赋值时全用的ReadXml.GetModel这同一个方法, 这里用的就是反射,方法如下:

<span class="com">/// <summary></span><span class="pln">
        </span><span class="com">/// 通过反射给接收消息model赋值</span><span class="pln">
        </span><span class="com">/// </summary></span><span class="pln">
        </span><span class="com">/// <typeparam name="T"></typeparam></span><span class="pln">
        </span><span class="com">/// <param name="model"></param></span><span class="pln">
        </span><span class="com">/// <returns></returns></span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> T </span><span class="typ">GetModel</span><span class="pun"><</span><span class="pln">T</span><span class="pun">>(</span><span class="pln">T model</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Dictionary</span><span class="pun"><</span><span class="kwd">string</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">></span><span class="pln"> xmlModel</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">where</span><span class="pln"> T </span><span class="pun">:</span><span class="pln"> </span><span class="kwd">class</span><span class="pln">
        </span><span class="pun">{</span><span class="pln">
            </span><span class="kwd">var</span><span class="pln"> m </span><span class="pun">=</span><span class="pln"> model</span><span class="pun">.</span><span class="typ">GetType</span><span class="pun">();</span><span class="pln">
            </span><span class="kwd">foreach</span><span class="pln"> </span><span class="pun">(</span><span class="typ">PropertyInfo</span><span class="pln"> p </span><span class="kwd">in</span><span class="pln"> m</span><span class="pun">.</span><span class="typ">GetProperties</span><span class="pun">())</span><span class="pln">
            </span><span class="pun">{</span><span class="pln">
                </span><span class="kwd">string</span><span class="pln"> name </span><span class="pun">=</span><span class="pln"> p</span><span class="pun">.</span><span class="typ">Name</span><span class="pun">;</span><span class="pln">
                </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">xmlModel</span><span class="pun">.</span><span class="typ">Keys</span><span class="pun">.</span><span class="typ">Contains</span><span class="pun">(</span><span class="pln">name</span><span class="pun">))</span><span class="pln">
                </span><span class="pun">{</span><span class="pln">
                    </span><span class="kwd">string</span><span class="pln"> value</span><span class="pun">=</span><span class="pln">xmlModel</span><span class="pun">.</span><span class="typ">Where</span><span class="pun">(</span><span class="pln">x </span><span class="pun">=></span><span class="pln"> x</span><span class="pun">.</span><span class="typ">Key</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> name</span><span class="pun">).</span><span class="typ">FirstOrDefault</span><span class="pun">().</span><span class="typ">Value</span><span class="pun">;</span><span class="pln">
                    p</span><span class="pun">.</span><span class="typ">SetValue</span><span class="pun">(</span><span class="pln">model</span><span class="pun">,</span><span class="pln">
                    </span><span class="kwd">string</span><span class="pun">.</span><span class="typ">IsNullOrEmpty</span><span class="pun">(</span><span class="pln">value</span><span class="pun">)</span><span class="pln"> </span><span class="pun">?</span><span class="pln"> </span><span class="kwd">null</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">Convert</span><span class="pun">.</span><span class="typ">ChangeType</span><span class="pun">(</span><span class="pln">value</span><span class="pun">,</span><span class="pln"> p</span><span class="pun">.</span><span class="typ">PropertyType</span><span class="pun">),</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">);</span><span class="pln">
                </span><span class="pun">}</span><span class="pln">
            </span><span class="pun">}</span><span class="pln">
            </span><span class="kwd">return</span><span class="pln"> model</span><span class="pun">;</span><span class="pln">
        </span><span class="pun">}</span>

T model 就是要使用的消息类, xmlmodel是在入口处传递进来的解析的微信发来的xml信息, 这样,就不需要每次手动写代码赋值了。

好了,此篇实现了lookmsgtype方法, 实现了消息封装和反射赋值, 接下去就是到了业务逻辑层中的处理和具体实现了..

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

登录

注册