native.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import * as clipboard from 'clipboard-polyfill/text';
  2. export default function ({ $userAgent, $wx }, i) {
  3. // const before = () => {
  4. // if ($userAgent.isApp) {
  5. // return;
  6. // }
  7. // // else if ($userAgent.isMiniProgram) {
  8. // // throw new Error('小程序环境不支持');
  9. // // }
  10. // throw new Error('非App环境');
  11. // };
  12. i('native', {
  13. async share({ title, desc, link, imgUrl }) {
  14. // before();
  15. if ($userAgent.isSzx) {
  16. if ($userAgent.isIos) {
  17. await window.webkit.messageHandlers.share.postMessage({
  18. title,
  19. content: desc,
  20. gotoUrl: link,
  21. shareImg: imgUrl,
  22. });
  23. return;
  24. }
  25. if ($userAgent.isAndroid) {
  26. await window.native.share(title, desc, link, imgUrl);
  27. return;
  28. }
  29. return;
  30. }
  31. if ($userAgent.isSzxBrowser) {
  32. await this.setShareInfo({ title, desc, link, imgUrl });
  33. await window.webkit.messageHandlers.openShare.postMessage({});
  34. }
  35. },
  36. async setShareInfo({ title, desc, link, imgUrl, path }) {
  37. if ($userAgent.isSzxBrowser) {
  38. // before();
  39. await window.webkit.messageHandlers.setShareInfo.postMessage({
  40. title,
  41. // content,
  42. gotoUrl: link,
  43. shareImg: imgUrl,
  44. });
  45. return;
  46. }
  47. if ($userAgent.isMiniProgram) {
  48. await $wx?.miniProgram?.postMessage({
  49. data: {
  50. action: 'updateAppMessageShareData',
  51. params: {
  52. title,
  53. path,
  54. imageUrl: imgUrl,
  55. },
  56. },
  57. });
  58. }
  59. // if ($userAgent.isIos) {
  60. // } else {
  61. // window.native.setShareUrl(url);
  62. // }
  63. },
  64. clipboard: {
  65. /**
  66. *
  67. * @param {string} text
  68. * @returns {Promise<any>}
  69. */
  70. async writeText(text) {
  71. if ($userAgent.isSzx && $userAgent.isAndroid) {
  72. return await window.native.copyToClipboard(text);
  73. }
  74. return await clipboard.writeText(text);
  75. },
  76. },
  77. });
  78. }