share.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // #ifdef APP-PLUS
  2. // 复制
  3. function onCopy(item, shareInfo,callback) {
  4. let copyInfo = shareInfo.shareUrl || shareInfo.shareContent || shareInfo.shareImg;
  5. if (copyInfo) {
  6. uni.setClipboardData({
  7. data: copyInfo,
  8. success:() => {
  9. uni.showToast({
  10. title: "已复制到剪贴板",
  11. icon: "none"
  12. });
  13. callback && callback(item);
  14. }
  15. });
  16. }
  17. }
  18. // 更多
  19. function onMore(item, shareInfo,callback) {
  20. plus.share.sendWithSystem({
  21. type: "text",
  22. title: shareInfo.shareTitle || "",
  23. href: shareInfo.shareUrl || "",
  24. content: shareInfo.shareContent || "",
  25. },function(res){
  26. callback && callback(item);
  27. },function(err){
  28. console.log(err);
  29. });
  30. }
  31. // 分享
  32. function onShare(item, shareInfo,callback) {
  33. if (shareInfo.type == undefined) {
  34. shareInfo.type = item.type;
  35. }
  36. let shareObj = {
  37. provider: item.provider,
  38. type: shareInfo.type,
  39. success: (res) => {
  40. callback && callback(item);
  41. console.log("success:" + JSON.stringify(res));
  42. },
  43. fail: (err) => {
  44. console.log("分享失败,参数缺失 fail:" + JSON.stringify(err));
  45. }
  46. };
  47. if (shareInfo.shareTitle) {
  48. shareObj.title = shareInfo.shareTitle;
  49. }else if(item.provider == "qq"){
  50. uni.showToast({
  51. title: "缺失分享的标题",
  52. icon: "none"
  53. });
  54. return;
  55. }
  56. if(shareInfo.type == 0 || item.provider == "qq"){
  57. if (shareInfo.shareUrl) {
  58. shareObj.href = shareInfo.shareUrl;
  59. }else{
  60. uni.showToast({
  61. title: "缺失分享的地址",
  62. icon: "none"
  63. });
  64. return;
  65. }
  66. }
  67. if([0,1,3,4].includes(shareInfo.type)){
  68. if (shareInfo.shareContent) {
  69. shareObj.summary = shareInfo.shareContent;
  70. }else{
  71. uni.showToast({
  72. title: "缺失分享的描述",
  73. icon: "none"
  74. });
  75. return;
  76. }
  77. }
  78. if([0,2,5].includes(shareInfo.type)){
  79. if (shareInfo.shareImg) {
  80. shareObj.imageUrl = shareInfo.shareImg;
  81. }else{
  82. uni.showToast({
  83. title: "缺失分享的图片",
  84. icon: "none"
  85. });
  86. return;
  87. }
  88. }
  89. if([3,4].includes(shareInfo.type)){
  90. if (shareInfo.mediaUrl) {
  91. shareObj.mediaUrl = shareInfo.mediaUrl;
  92. }else{
  93. uni.showToast({
  94. title: "缺失分享的音视频地址",
  95. icon: "none"
  96. });
  97. return;
  98. }
  99. }
  100. if(shareInfo.type == 5){
  101. if (shareInfo.appId && shareInfo.appPath && shareInfo.appWebUrl) {
  102. shareObj.miniProgram = {
  103. id:shareInfo.appId,
  104. path:shareInfo.appPath,
  105. webUrl:shareInfo.appWebUrl,
  106. };
  107. if(shareInfo.appType){
  108. shareObj.miniProgram.type = shareInfo.appType;
  109. }
  110. }else{
  111. uni.showToast({
  112. title: "缺失分享小程序的参数",
  113. icon: "none"
  114. });
  115. return;
  116. }
  117. }
  118. if (item.scene) {
  119. shareObj.scene = item.scene;
  120. }
  121. uni.share(shareObj);
  122. }
  123. let otherShareList = [
  124. {
  125. icon: "/static/share/icon_copy.png",
  126. text: "复制",
  127. provider: "copy",
  128. onClick: onCopy
  129. },
  130. {
  131. icon: "/static/share/icon_more.png",
  132. text: "更多",
  133. provider: "more",
  134. onClick: onMore
  135. }
  136. ];
  137. let platformShareList = [];
  138. // 获取服务商支持的分享
  139. uni.getProvider({
  140. service: 'share',
  141. success: function (res) {
  142. if (res.provider.includes('sinaweibo')) {
  143. platformShareList = [{
  144. icon: "/static/share/icon_weibo.png",
  145. text: "新浪微博",
  146. onClick: onShare,
  147. provider: "sinaweibo",
  148. type: 0
  149. }].concat(platformShareList);
  150. }
  151. if (res.provider.includes('qq')) {
  152. platformShareList = [{
  153. icon: "/static/share/icon_qq.png",
  154. text: "QQ",
  155. onClick: onShare,
  156. provider: "qq",
  157. type: 1
  158. }].concat(platformShareList);
  159. }
  160. if (res.provider.includes('weixin')) {
  161. platformShareList = [{
  162. icon: "/static/share/icon_weixin.png",
  163. text: "微信好友",
  164. onClick: onShare,
  165. provider: "weixin",
  166. scene: "WXSceneSession",
  167. type: 0
  168. },
  169. {
  170. icon: "/static/share/icon_pengyouquan.png",
  171. text: "朋友圈",
  172. onClick: onShare,
  173. provider: "weixin",
  174. scene: "WXSenceTimeline",
  175. type: 0
  176. }].concat(platformShareList);
  177. }
  178. }
  179. });
  180. // 根据type类型过滤掉不支持的平台
  181. function platformFilter(data){
  182. if(data.type >= 0 && data.type <= 5){
  183. let platformList = [];
  184. let supportList = [
  185. ["weixin","sinaweibo"],
  186. ["weixin","sinaweibo","qq"],
  187. ["weixin","sinaweibo","qq"],
  188. ["weixin","qq"],
  189. ["weixin","sinaweibo"],
  190. ["weixin"],
  191. ];
  192. let currentSupport = supportList[data.type];
  193. platformShareList.forEach((item,index) => {
  194. if(currentSupport.includes(item.provider)){
  195. if(data.type == 5){
  196. if(item.scene == "WXSceneSession"){
  197. platformList.push(item);
  198. }
  199. } else {
  200. platformList.push(item);
  201. }
  202. }
  203. });
  204. return platformList.concat(otherShareList);
  205. }else{
  206. return platformShareList.concat(otherShareList);
  207. }
  208. }
  209. // 数据处理
  210. function dataFactory(shareInfo = {}) {
  211. let config = {
  212. ...shareInfo
  213. };
  214. config.shareUrl = shareInfo.shareUrl || "";
  215. config.shareContent = shareInfo.shareContent || "分享的描述";
  216. config.shareImg = shareInfo.shareImg || "分享的图片";
  217. return config;
  218. }
  219. export default function (shareInfo, callback) {
  220. shareInfo = dataFactory(shareInfo);
  221. // 以下为计算菜单的nview绘制布局,为固定算法,使用者无关关心
  222. let screenWidth = plus.screen.resolutionWidth
  223. //以360px宽度屏幕为例,上下左右边距及2排按钮边距留25像素,图标宽度55像素,同行图标间的间距在360宽的屏幕是30px,但需要动态计算,以此原则计算4列图标分别的left位置
  224. //图标下的按钮文字距离图标5像素,文字大小12像素
  225. //底部取消按钮高度固定为44px
  226. //TODO 未处理横屏和pad,这些情况6个图标应该一排即可
  227. let marginTop = 25, //上间距
  228. marginLeft = 25, //左间距
  229. iconWidth = 55, //图标宽宽
  230. iconHeight = 55, //图标高度
  231. icontextSpace = 10, //图标与文字间距
  232. textHeight = 12 //文字大小
  233. let left1 = marginLeft / 360 * screenWidth;
  234. let colNumber = Math.floor(screenWidth / (iconWidth + marginLeft));
  235. let i = (screenWidth - (iconWidth + marginLeft) * colNumber - marginLeft) / (colNumber + 1);
  236. let initMargin = marginLeft + i;
  237. let itemWidth = iconWidth + initMargin;
  238. let itemHeight = iconHeight + icontextSpace + textHeight + marginTop;
  239. let textTop = iconHeight + icontextSpace;
  240. let alphaBg = new plus.nativeObj.View("alphaBg", { //先创建遮罩层
  241. top: '0px',
  242. left: '0px',
  243. height: '100%',
  244. width: '100%',
  245. backgroundColor: 'rgba(0,0,0,0.5)'
  246. });
  247. alphaBg.addEventListener("click", function () { //处理遮罩层点击
  248. alphaBg.hide();
  249. shareMenu.hide();
  250. });
  251. let shareList = platformFilter(shareInfo);
  252. let shareMenu = new plus.nativeObj.View("shareMenu", { //创建底部图标菜单
  253. bottom: '0px',
  254. left: '0px',
  255. height: Math.ceil(shareList.length / colNumber) * itemHeight + marginTop + 44 + 1 + 'px',
  256. width: '100%',
  257. backgroundColor: 'rgb(255,255,255)'
  258. });
  259. //绘制底部图标菜单的内容
  260. shareMenu.draw([{
  261. tag: 'rect', //菜单顶部的分割灰线
  262. color: '#e7e7e7',
  263. position: {
  264. top: '0px',
  265. height: '1px'
  266. }
  267. },
  268. {
  269. tag: 'font',
  270. id: 'sharecancel', //底部取消按钮的文字
  271. text: '取消分享',
  272. textStyles: {
  273. size: '14px'
  274. },
  275. position: {
  276. bottom: '0px',
  277. height: '44px'
  278. }
  279. },
  280. {
  281. tag: 'rect', //底部取消按钮的顶部边线
  282. color: '#e7e7e7',
  283. position: {
  284. bottom: '45px',
  285. height: '1px'
  286. }
  287. }
  288. ]);
  289. shareList.map((v, k) => {
  290. let time = new Date().getTime();
  291. let row = Math.floor(k / colNumber);
  292. let col = k % colNumber;
  293. let item = [{
  294. src: v.icon,
  295. id: Math.random() * 1000 + time,
  296. tag: "img",
  297. position: {
  298. top: row * itemHeight + marginTop,
  299. left: col * itemWidth + initMargin,
  300. width: iconWidth,
  301. height: iconWidth
  302. }
  303. }, {
  304. text: v.text,
  305. id: Math.random() * 1000 + time,
  306. tag: "font",
  307. textStyles: {
  308. size: textHeight
  309. },
  310. position: {
  311. top: row * itemHeight + textTop,
  312. left: col * itemWidth + initMargin,
  313. width: iconWidth,
  314. height: iconWidth
  315. }
  316. }];
  317. shareMenu.draw(item);
  318. });
  319. shareMenu.addEventListener("click", function (e) { //处理底部图标菜单的点击事件,根据点击位置触发不同的逻辑
  320. if (e.screenY > plus.screen.resolutionHeight - 44) { //点击了底部取消按钮
  321. alphaBg.hide();
  322. shareMenu.hide();
  323. } else if (e.clientX < 5 || e.clientX > screenWidth - 5 || e.clientY < 5) {
  324. //屏幕左右边缘5像素及菜单顶部5像素不处理点击
  325. } else { //点击了图标按钮
  326. let x = e.clientX;
  327. let y = e.clientY;
  328. let colIdx = Math.floor(x / itemWidth);
  329. let rowIdx = Math.floor(y / itemHeight);
  330. let tapIndex = colIdx + rowIdx * colNumber;
  331. shareList[tapIndex].onClick(shareList[tapIndex], shareInfo,callback);
  332. }
  333. });
  334. alphaBg.show();
  335. shareMenu.show();
  336. return {
  337. alphaBg,
  338. shareMenu
  339. };
  340. };
  341. // #endif