在我们的生活中,分享无处不在。在各个社区都有提供api过来,供开发人员使用。各大社区的API眼花缭乱,新浪和腾讯的特多。有兴趣就找点时间去摸索下。对于分享社区代码,很多都是js的。今天特意了解整理了下as3的。
先看下边的效果(各个社区按钮在左下角):
新版本的点击这里:http://blog1.vini123.com/?p=1552
在整理的过程中,发现腾讯空间,人人网提供的分享代码说明比较明晰。其他的都是找呀找,分析呀分析的。其中豆瓣网的还不能带图片分享。对于多图片同时分享功能,还不够完善。在所有的分享中,觉得Qzone的最好。
找到各个社区的分享代码提取网页,找到各个参数,了解其意义。然后以get的方式,将分享网站以及代码链接起来。然后用navigateToURL打开窗口就可以了。这里还要注意一点,如果参数有中文字符的,请用encodeURI将字符串编码为UTF-8转义序列。否则,分享出来的内容会有意想不到的东东。(会从你的html标签里边抽取有用信息)
这里,将每个社区的url都分开了。但是,url所获取的信息内容整理在一个ShareContent.as中。这里有链接的网站,分享的标题,分享理由,分享摘要,分享的图片等。改动ShareContent.as中相关的内容,
分享的内容也会跟着改变。
至于鼠标右键功能,ContextMenu是final不能被继承。只有去复合来使用了。 下边贴出完整的代码:
文档类:ShareToCommunity.as
package { import com.vini123.utils.share.ShareButton; import com.vini123.utils.RightMenu; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; [SWF(width ="720",height="450",frameRate= "24",backgroundColor="#000000")] public class ShareToCommunity extends Sprite { private var bg:Bg; private var shareButton:ShareButton; public function ShareToCommunity() { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; bg = new Bg(); addChild(bg); shareButton = new ShareButton(); addChild(shareButton); stage.addEventListener(Event.RESIZE,resizeHandler); resizeHandler(); var rightMenu:RightMenu = new RightMenu(); this.contextMenu = rightMenu.contextMenu; } private function resizeHandler(e:Event = null):void { shareButton.x = 6; shareButton.y = stage.stageHeight - 36; bg.width = stage.stageWidth; bg.height = stage.stageHeight; } } }
package com.vini123.utils.share { public class ShareContent { private static var _url:String = "http://viniapp.sinaapp.com"; //链接 private static var _title:String = "电子杂志制作工具"; //分享标题 private static var _desc:String = "美的事物是永恒的喜悦。如果笑靥如花,如果风情万种,如果那般决绝。那么来吧,分享起来!";//分享理由 private static var _summary:String ="君生我未生,我生君已老。隔了百年的光阴,万里的迢递。浮世肮脏,人心险恶。割裂了生和死,到哪里去寻找那一袭纯白如羽的华衣和那莲花般的素颜。"; //分享摘要 private static var _site:String = "http://viniapp.sinaapp.com";//分享来源 private static var _pics:Array = ["http://viniapp.sinaapp.com/page/01.jpg", "http://viniapp.sinaapp.com/page/02.jpg", "http://viniapp.sinaapp.com/page/03.jpg"]; public static function get url():String { return encodeURI(_url); } public static function get title():String { return encodeURI(_title); } public static function get desc():String { return encodeURI(_desc); } public static function get summary():String { return encodeURI(_summary); } public static function get site():String { return encodeURI(_site); } public static function get pics():Array { return _pics; } } }
package com.vini123.utils.share { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.net.navigateToURL; public class ShareButton extends Sprite { private var qzoneUrl:String; private var sinaUrl:String; private var renUrl:String; private var tqqUrl:String; private var kaixinUrl:String; private var doubanUrl:String; public function ShareButton() { init(); } private function init():void { qzoneIconButton.buttonMode = true; (qzoneIconButton as MovieClip).type = "qzoneIconButton"; qzoneIconButton.addEventListener(MouseEvent.ROLL_OVER,overHandler); qzoneIconButton.addEventListener(MouseEvent.ROLL_OUT,outHandler); qzoneIconButton.addEventListener(MouseEvent.CLICK,clickHandler); sinaIconButton.buttonMode = true; (sinaIconButton as MovieClip).type = "sinaIconButton"; sinaIconButton.addEventListener(MouseEvent.ROLL_OVER,overHandler); sinaIconButton.addEventListener(MouseEvent.ROLL_OUT,outHandler); sinaIconButton.addEventListener(MouseEvent.CLICK,clickHandler); renIconButton.buttonMode = true; (renIconButton as MovieClip).type = "renIconButton"; renIconButton.addEventListener(MouseEvent.ROLL_OVER,overHandler); renIconButton.addEventListener(MouseEvent.ROLL_OUT,outHandler); renIconButton.addEventListener(MouseEvent.CLICK,clickHandler); tqqIconButton.buttonMode = true; (tqqIconButton as MovieClip).type = "tqqIconButton"; tqqIconButton.addEventListener(MouseEvent.ROLL_OVER,overHandler); tqqIconButton.addEventListener(MouseEvent.ROLL_OUT,outHandler); tqqIconButton.addEventListener(MouseEvent.CLICK,clickHandler); kaixinIconButton.buttonMode = true; (kaixinIconButton as MovieClip).type = "kaixinIconButton"; kaixinIconButton.addEventListener(MouseEvent.ROLL_OVER,overHandler); kaixinIconButton.addEventListener(MouseEvent.ROLL_OUT,outHandler); kaixinIconButton.addEventListener(MouseEvent.CLICK,clickHandler); doubanIconButton.buttonMode = true; (doubanIconButton as MovieClip).type = "doubanIconButton"; doubanIconButton.addEventListener(MouseEvent.ROLL_OVER,overHandler); doubanIconButton.addEventListener(MouseEvent.ROLL_OUT,outHandler); doubanIconButton.addEventListener(MouseEvent.CLICK,clickHandler); } private function clickHandler(e:MouseEvent):void { var type:String = e.currentTarget.type; switch(type) { case "qzoneIconButton": navigateToURL(new URLRequest(Qzone.url)); break; case "sinaIconButton": navigateToURL(new URLRequest(Sina.url)); break; case "renIconButton": navigateToURL(new URLRequest(Renren.url)); break; case "tqqIconButton": navigateToURL(new URLRequest(Tqq.url)); break; case "kaixinIconButton": navigateToURL(new URLRequest(Kaixin.url)); break; case "doubanIconButton": navigateToURL(new URLRequest(Douban.url)); break; default: break; } } private function overHandler(e:MouseEvent):void { e.target.gotoAndPlay('over'); } private function outHandler(e:MouseEvent):void { e.target.gotoAndPlay('out'); } } }
package com.vini123.utils.share { /** * * @author Vini123 * 腾讯API:http://wiki.open.qq.com/wiki/API%E5%AF%BC%E8%88%AA%E5%9B%BE * 腾讯微博API http://wiki.open.qq.com/wiki/%E8%85%BE%E8%AE%AF%E5%BC%80%E6%94%BE%E5%B9%B3%E5%8F%B0%E5%BE%AE%E5%8D%9AOpenAPI%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E * 腾讯空间分享代码:http://connect.qq.com/intro/share/ */ public class Qzone { private static var _url:String; /** * * @param url 链接的地址 * @param desc 分享的理由 * @param summary 分享摘要 * @param title 分享的标题 * @param site 分享的来源网站 * @param pics 分享的图片集合。多张图片用"|"分开 */ public static function get url():String { if(_url ==null || _url =="") { _url = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + ShareContent.url + "&desc=" + ShareContent.desc + "&showcount=1&summary=" + ShareContent.summary + "&title="+ ShareContent.title + "&site="+ ShareContent.site +"&pics=" + ShareContent.pics[0] +"|" + ShareContent.pics[1] +"|" + ShareContent.pics[2]; } return _url; } } }
package com.vini123.utils.share { /** * * @author Vini123 * 新浪微博API:http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI#.E8.AF.9D.E9.A2.98 * 可参考: http://open.weibo.com/sharebutton http://open.weibo.com/wiki/2/statuses/upload_url_text */ public class Sina { private static var _url:String; /** * * @param url 链接的地址 * @param title 分享的理由 * @param pic 分享的图片 */ public static function get url():String { if(_url ==null || _url =="") { _url = "http://service.weibo.com/share/share.php?url=" + ShareContent.url + "&type=button&ralateUid=1764700305&language=zh_cn&default_text=" + ShareContent.desc + "&title="+ ShareContent.summary + "&source="+ ShareContent.site +"&pic=" + ShareContent.pics[0]; } return _url; } } }
package com.vini123.utils.share { /** * * @author Vini123 * 人人网API:http://wiki.dev.renren.com/wiki/API2 * 人人网分享代码:http://dev.renren.com/website/?widget=rrshare&content=use */ public class Renren { private static var _url:String; /** * @param resourceUrl 链接的地址 * @param title 分享的标题 * @param srcUrl 分享的来源网站 * @param description 分享详细描述 * @param pic 分享的图片。 //多张图片同事分享又该如何 */ public static function get url():String { if(_url ==null || _url =="") { _url = "http://widget.renren.com/dialog/share?resourceUrl=" + ShareContent.url + "&title="+ ShareContent.title + "&srcUrl="+ ShareContent.site + "&description=" + ShareContent.summary + "&pic=" + ShareContent.pics[0]; } return _url; } } }
package com.vini123.utils.share { /** * * @author Vini123 * 腾讯API:http://wiki.open.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91add_share#1.2.09.E6.A0.BC.E5.BC.8F * 腾讯微博分享代码:http://dev.t.qq.com/websites/qshare/ */ public class Tqq { private static var _url:String; /** * * @param url 链接的地址 * @param summary 分享摘要 * @param pic 分享的图片 */ public static function get url():String { if(_url ==null || _url =="") { _url = "http://share.v.t.qq.com/index.php?c=share&a=index&url=" + ShareContent.url + "&summary=" + ShareContent.summary + "&site="+ ShareContent.site +"&pic=" + ShareContent.pics[0]; } return _url; } } }
package com.vini123.utils.share { /** * * @author Vini123 * 开心网API http://open.kaixin001.com/document.php?type=records */ public class Kaixin { private static var _url:String; /** * * @param url 链接的地址 * @param content 分享摘要 * @param starid 公共主页id * @param aid 显示分享来源 * @param showcount 显示分享数 * @param style 显示样式 11这个可以跳过人人网ico图标链接 * @param pic 分享的图片,多个使用半角逗号 */ public static function get url():String { if(_url ==null || _url =="") { _url = "http://www.kaixin001.com/rest/records.php?url=" + ShareContent.url + "&content=" + ShareContent.summary + "&showcount=1&style=11"+ "&pic=" + ShareContent.pics[0] + "," + ShareContent.pics[1] + ","+ ShareContent.pics[2]; } return _url; } } }
package com.vini123.utils.share { /** * * @author Vini123 * 豆瓣网添加分享代码 http://www.douban.com/service/sharebutton */ public class Douban { private static var _url:String; /** * * @param url 链接的地址 * @param title 分享标题 * @param desc 分享的理由 */ public static function get url():String { if(_url ==null || _url =="") { _url = "http://www.douban.com/share/service?url=" + ShareContent.url + "&title=" + ShareContent.desc + "&comment="+ ShareContent.summary + "&image=" + ShareContent.pics[0]; } return _url; } } }
package com.vini123.utils { import flash.ui.ContextMenuItem; import flash.ui.ContextMenu; import flash.events.ContextMenuEvent; import flash.system.System; import flash.net.navigateToURL; import flash.net.URLRequest; public class RightMenu { private var _contextMenu:ContextMenu; public function RightMenu () { var menuItem1:ContextMenuItem = new ContextMenuItem("联系QQ:165525137"); var menuItem2:ContextMenuItem = new ContextMenuItem("林林杂志"); var menuItem3:ContextMenuItem = new ContextMenuItem("林林博客"); menuItem1.addEventListener (ContextMenuEvent.MENU_ITEM_SELECT,toQQ); menuItem2.addEventListener (ContextMenuEvent.MENU_ITEM_SELECT,toMagazine); menuItem3.addEventListener (ContextMenuEvent.MENU_ITEM_SELECT,toBlog); _contextMenu = new ContextMenu(); _contextMenu .customItems.push (menuItem1); _contextMenu .customItems.push (menuItem2); _contextMenu .customItems.push (menuItem3); _contextMenu .hideBuiltInItems (); } public function get contextMenu():ContextMenu { return _contextMenu; } private function toQQ (e:ContextMenuEvent) { navigateToURL (new URLRequest("tencent://message/?uin=165525137&Site=孤独&Menu=yes"), "_blank"); } private function toMagazine (e:ContextMenuEvent) { navigateToURL (new URLRequest("http://viniapp.sinaapp.com"),"_blank"); } private function toBlog (e:ContextMenuEvent) { navigateToURL (new URLRequest("http://blog1.vini123.com"),"_blank"); } private function copyUrl (e:ContextMenuEvent) { var txt:String = "http://www.vini123.com/demo/record.swf"; System.setClipboard (txt); } } }