native.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // import * as clipboard from 'clipboard-polyfill/text';
  2. import copy from 'copy-to-clipboard';
  3. console.log("🚀 ~ file: native.js ~ line 3 ~ copy", copy)
  4. export default function ({ $userAgent, $wx }, i) {
  5. // const before = () => {
  6. // if ($userAgent.isApp) {
  7. // return;
  8. // }
  9. // // else if ($userAgent.isMiniProgram) {
  10. // // throw new Error('小程序环境不支持');
  11. // // }
  12. // throw new Error('非App环境');
  13. // };
  14. i('native', {
  15. async share({ title, desc, link, imgUrl }) {
  16. // before();
  17. // 普通分享
  18. if ($userAgent.isSzx) {
  19. if ($userAgent.isIos) {
  20. await window.webkit.messageHandlers.share.postMessage({
  21. title,
  22. content: desc,
  23. gotoUrl: link,
  24. shareImg: imgUrl,
  25. });
  26. return;
  27. }
  28. if ($userAgent.isAndroid) {
  29. await window.native.share(title, desc, link, imgUrl, '28_分享活动');
  30. return;
  31. }
  32. return;
  33. }
  34. if ($userAgent.isSzxBrowser) {
  35. await this.setShareInfo({ title, desc, link, imgUrl });
  36. await window.webkit.messageHandlers.openShare.postMessage({});
  37. }
  38. },
  39. async eventShare({ title, desc, link, imgUrl }) {
  40. // before();
  41. // 分享朋友圈
  42. if ($userAgent.isSzx) {
  43. if ($userAgent.isIos) {
  44. await window.webkit.messageHandlers.share.postMessage({
  45. title,
  46. content: desc,
  47. gotoUrl: link,
  48. shareImg: imgUrl,
  49. });
  50. return;
  51. }
  52. if ($userAgent.isAndroid) {
  53. await window.native.showEventShare(link, title, desc, imgUrl);
  54. return;
  55. }
  56. return;
  57. }
  58. if ($userAgent.isSzxBrowser) {
  59. await this.setShareInfo({ title, desc, link, imgUrl });
  60. await window.webkit.messageHandlers.openShare.postMessage({});
  61. }
  62. },
  63. async setShareInfo({ title, desc, link, imgUrl, path }) {
  64. if ($userAgent.isSzxBrowser) {
  65. // before();
  66. await window.webkit.messageHandlers.setShareInfo.postMessage({
  67. title,
  68. // content,
  69. gotoUrl: link,
  70. shareImg: imgUrl,
  71. });
  72. return;
  73. }
  74. if ($userAgent.isMiniProgram) {
  75. await $wx?.miniProgram?.postMessage({
  76. data: {
  77. action: 'updateAppMessageShareData',
  78. params: {
  79. title,
  80. path,
  81. imageUrl: imgUrl,
  82. },
  83. },
  84. });
  85. }
  86. // if ($userAgent.isIos) {
  87. // } else {
  88. // window.native.setShareUrl(url);
  89. // }
  90. },
  91. clipboard: {
  92. /**
  93. *
  94. * @param {string} text
  95. * @returns {Promise<any>}
  96. */
  97. async writeText(text) {
  98. if ($userAgent.isSzx && $userAgent.isAndroid) {
  99. return await window.native.copyToClipboard(text);
  100. }
  101. return await copy(text, {
  102. format: 'text/plain',
  103. });
  104. // return await clipboard.writeText(text);
  105. },
  106. },
  107. });
  108. }