native.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if ($userAgent.isSzx) {
  18. if ($userAgent.isIos) {
  19. await window.webkit.messageHandlers.share.postMessage({
  20. title,
  21. content: desc,
  22. gotoUrl: link,
  23. shareImg: imgUrl,
  24. });
  25. return;
  26. }
  27. if ($userAgent.isAndroid) {
  28. await window.native.share(title, desc, link, imgUrl);
  29. return;
  30. }
  31. return;
  32. }
  33. if ($userAgent.isSzxBrowser) {
  34. await this.setShareInfo({ title, desc, link, imgUrl });
  35. await window.webkit.messageHandlers.openShare.postMessage({});
  36. }
  37. },
  38. async setShareInfo({ title, desc, link, imgUrl, path }) {
  39. if ($userAgent.isSzxBrowser) {
  40. // before();
  41. await window.webkit.messageHandlers.setShareInfo.postMessage({
  42. title,
  43. // content,
  44. gotoUrl: link,
  45. shareImg: imgUrl,
  46. });
  47. return;
  48. }
  49. if ($userAgent.isMiniProgram) {
  50. await $wx?.miniProgram?.postMessage({
  51. data: {
  52. action: 'updateAppMessageShareData',
  53. params: {
  54. title,
  55. path,
  56. imageUrl: imgUrl,
  57. },
  58. },
  59. });
  60. }
  61. // if ($userAgent.isIos) {
  62. // } else {
  63. // window.native.setShareUrl(url);
  64. // }
  65. },
  66. clipboard: {
  67. /**
  68. *
  69. * @param {string} text
  70. * @returns {Promise<any>}
  71. */
  72. async writeText(text) {
  73. if ($userAgent.isSzx && $userAgent.isAndroid) {
  74. return await window.native.copyToClipboard(text);
  75. }
  76. return await copy(text, {
  77. format: 'text/plain',
  78. });
  79. // return await clipboard.writeText(text);
  80. },
  81. },
  82. });
  83. }