share.js 9.5 KB

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