share_sinaweibo.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <meta name="MobileOptimized" content="320"/>
  8. <title>微博分享</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var shares=null;
  12. var sweibo=null;
  13. // H5 plus事件处理
  14. function plusReady(){
  15. updateSerivces();
  16. }
  17. if(window.plus){
  18. plusReady();
  19. }else{
  20. document.addEventListener('plusready', plusReady, false);
  21. }
  22. /**
  23. * 更新分享服务
  24. */
  25. function updateSerivces(){
  26. plus.share.getServices(function(s){
  27. shares={};
  28. for(var i in s){
  29. var t=s[i];
  30. shares[t.id]=t;
  31. }
  32. sweibo=shares['sinaweibo'];
  33. }, function(e){
  34. outSet('获取分享服务列表失败:'+e.message);
  35. });
  36. }
  37. // 分享文本
  38. function shareText(){
  39. var msg={type:'text'};
  40. if(!text_content.value){
  41. plus.nativeUI.alert('请输入要分享的内容!');
  42. return;
  43. }
  44. msg.content=text_content.value;
  45. sweibo?share(sweibo, msg):plus.nativeUI.alert('当前环境不支持新浪微博分享操作!');
  46. }
  47. // 分享图片
  48. function shareImage(){
  49. var msg={type:'image'};
  50. if(!image_picture.realUrl){
  51. plus.nativeUI.alert('请选择要分享的图片!');
  52. return;
  53. }
  54. msg.pictures=[image_picture.realUrl];
  55. msg.content='我在使用HBuilder分享图片'; //可选
  56. sweibo?share(sweibo, msg):plus.nativeUI.alert('当前环境不支持新浪微博分享操作!');
  57. }
  58. // 分享网页
  59. function shareWeb(){
  60. var msg={type:'web'};
  61. if(!web_href.value){
  62. plus.nativeUI.alert('请输入分享网页的链接地址!');
  63. web_href.focus();
  64. return;
  65. }
  66. msg.href=web_href.value;
  67. if(!web_content.value){
  68. plus.nativeUI.alert('请输入分享网页的描述!');
  69. web_content.focus();
  70. return;
  71. }
  72. msg.content=web_content.value;
  73. sweibo?share(sweibo, msg):plus.nativeUI.alert('当前环境不支持新浪微博分享操作!');
  74. }
  75. // 分享视频
  76. function shareVideo(){
  77. var msg={type:'video',thumbs:['_www/logo.png']};
  78. if(!video_media.value){
  79. plus.nativeUI.alert('请输入分享视频的链接地址!');
  80. video_media.focus();
  81. return;
  82. }
  83. msg.media=video_media.value;
  84. msg.content='我在使用HBuilder分享视频'; //可选
  85. sweibo?share(sweibo, msg):plus.nativeUI.alert('当前环境不支持新浪微博分享操作!');
  86. }
  87. // 分享
  88. function share(srv, msg, button){
  89. outSet('分享操作:');
  90. if(!srv){
  91. outLine('无效的分享服务!');
  92. return;
  93. }
  94. button&&(msg.extra=button.extra);
  95. // 发送分享
  96. if(srv.authenticated){
  97. outLine('---已授权---');
  98. doShare(srv, msg);
  99. }else{
  100. outLine('---未授权---');
  101. srv.authorize(function(){
  102. doShare(srv, msg);
  103. }, function(e){
  104. outLine('认证授权失败:'+JSON.stringify(e));
  105. });
  106. }
  107. }
  108. // 发送分享
  109. function doShare(srv, msg){
  110. outLine(JSON.stringify(msg));
  111. srv.send(msg, function(){
  112. outLine('分享到"'+srv.description+'"成功!');
  113. }, function(e){
  114. outLine('分享到"'+srv.description+'"失败: '+JSON.stringify(e));
  115. });
  116. }
  117. // 解除授权
  118. function cancelAuth(){
  119. outSet('解除授权:');
  120. if(sweibo){
  121. if(sweibo.authenticated){
  122. outLine('取消"'+sweibo.description+'"');
  123. }
  124. sweibo.forbid();
  125. }else{
  126. outLine('当前环境不支持QQ分享操作!');
  127. }
  128. }
  129. // 拍照添加图片分享
  130. function imageCameraPicture(){
  131. outSet('拍照添加分享图片:');
  132. var cmr=plus.camera.getCamera();
  133. cmr.captureImage(function(p){
  134. plus.io.resolveLocalFileSystemURL(p,function(entry){
  135. image_picture.src=entry.toLocalURL();
  136. image_picture.realUrl=p;
  137. outLine('拍照图片:'+image_picture.realUrl);
  138. },function(e){
  139. outLine('读取拍照文件错误:'+e.message);
  140. } );
  141. },function(e){
  142. outLine('拍照失败:'+e.message);
  143. });
  144. }
  145. // 从相册添加图片分享
  146. function imageGalleryPicture(){
  147. outSet('从相册添加分享图片:');
  148. plus.gallery.pick(function(p){
  149. // 从相册返回的路径不需要转换可以直接使用
  150. image_picture.src=p;
  151. image_picture.realUrl=image_picture.src;
  152. outLine('选择图片:'+image_picture.realUrl);
  153. });
  154. }
  155. // 使用Logo图片分享
  156. function imageLogoPicture(){
  157. outSet('使用Logo分享图片:');
  158. var url='_www/logo.png';
  159. plus.io.resolveLocalFileSystemURL(url, function(entry){
  160. image_picture.src=entry.toLocalURL();
  161. image_picture.realUrl=url;
  162. }, function(e){
  163. outLine('读取Logo文件错误:'+e.message);
  164. });
  165. }
  166. // 拍摄视频
  167. function videoCameraCapture(){
  168. outSet('录像分享视频:');
  169. var cmr = plus.camera.getCamera();
  170. cmr.startVideoCapture(function(p){
  171. outLine('录像成功:'+p);
  172. video_media.value=p;
  173. }, function(e){
  174. outLine('录像失败:'+JSON.stringify(e));
  175. }, {filename:'_doc/camera/'});
  176. }
  177. // 选择视频
  178. function imageGallerySelect(){
  179. outSet('从相册选择分享视频:');
  180. plus.gallery.pick(function(p){
  181. video_media.value=p;
  182. outLine('选择视频成功:'+p);
  183. }, function(e){
  184. outLine('选择视频失败:'+JSON.stringify(e));
  185. },{filter:'video'});
  186. }
  187. </script>
  188. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  189. <style type="text/css">
  190. .sharecontent{
  191. width:80%;
  192. -webkit-user-select:text;
  193. border: 1px solid #6C6C6C;
  194. -webkit-border-radius: 2px;
  195. border-radius: 2px;
  196. }
  197. .sharepicture{
  198. width:30%;
  199. }
  200. .share_input{
  201. width:90%;
  202. -webkit-user-select:text;
  203. border: 1px solid #6C6C6C;
  204. -webkit-border-radius: 2px;
  205. border-radius: 2px;
  206. margin-bottom: .5em;
  207. }
  208. </style>
  209. </head>
  210. <body>
  211. <br/>
  212. <p class="heading">分享文本:</p>
  213. <textarea id="text_content" class="sharecontent" rows="3">我正在使用HBuilder开发移动应用,赶紧跟我一起来体验!</textarea>
  214. <div class="button" onclick="shareText()">分享文本</div>
  215. <hr color="#EEE"/><br/><br/>
  216. <p class="heading">分享图片:</p>
  217. <table style="width:100%;">
  218. <tbody>
  219. <tr>
  220. <td style="width:30%"><div class="button button-select" onclick="imageCameraPicture()">拍照</div></td>
  221. <td style="width:30%"><div class="button button-select" onclick="imageGalleryPicture()">相册选取</div></td>
  222. <td style="width:30%"><div class="button button-select" onclick="imageLogoPicture()">使用logo图</div></td>
  223. </tr>
  224. </tbody>
  225. </table>
  226. <img id="image_picture" class="sharepicture" src="../img/add.png"/>
  227. <br/>
  228. <p class="des">分享图片可设置文本内容(可选)及缩略图(可选)。未安装微博客户端时Android平台图片限定大小为1M以内,iOS平台则不支持分享图片。</p>
  229. <div class="button" onclick="shareImage()">分享图片</div>
  230. <hr color="#EEE"/><br/><br/>
  231. <p class="heading">分享网页:</p>
  232. <table style="width:100%;">
  233. <tbody>
  234. <tr>
  235. <td style="width:20%;text-align:right;font-size:12px;">网页地址</td>
  236. <td style="width:65%">
  237. <input id="web_href" class="share_input" type="url" value="http://www.dcloud.io/" placeholder="请输入要分享的网页地址"/>
  238. </td>
  239. </tr>
  240. <tr>
  241. <td style="width:20%;text-align:right;font-size:12px;">网页描述</td>
  242. <td style="width:65%">
  243. <input id="web_content" class="share_input" type="url" value="我正在使用HBuilder+HTML5开发移动应用,赶紧跟我一起来体验!" placeholder="请输入要分享的网页描述"/>
  244. </td>
  245. </tr>
  246. </tbody>
  247. </table>
  248. <p class="des">分享网页链接在内容之后,如果要将链接放到内容中可直接在内容中添加链接。</p>
  249. <div class="button" onclick="shareWeb()">分享网页</div>
  250. <hr color="#EEE"/><br/><br/>
  251. <p class="heading">分享视频:</p>
  252. <table style="width:100%;">
  253. <tbody>
  254. <tr>
  255. <td style="width:40%"><div class="button button-select" onclick="videoCameraCapture()">拍摄</div></td>
  256. <td style="width:40%"><div class="button button-select" onclick="imageGallerySelect()">相册选取</div></td>
  257. </tr>
  258. </tbody>
  259. </table>
  260. <input id="video_media" class="share_input" style="width:90%" type="url" value="" placeholder="选择要分享的视频"/>
  261. <p class="des">分享视频可设置文本内容(可选)及缩略图(可选)。未安装微博客户端时Android&amp;iOS平台则不支持分享视频。</p>
  262. <div class="button" onclick="shareVideo()">分享视频</div>
  263. <hr color="#EEE"/>
  264. <div id="outpos"/>
  265. <div id="output">
  266. 新浪微博分享支持分享文本、网页、图片、视频(本地地址)等。
  267. </div>
  268. </body>
  269. </html>