123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // import * as clipboard from 'clipboard-polyfill/text';
- import copy from 'copy-to-clipboard';
- console.log("🚀 ~ file: native.js ~ line 3 ~ copy", copy)
- export default function ({ $userAgent, $wx }, i) {
- // const before = () => {
- // if ($userAgent.isApp) {
- // return;
- // }
- // // else if ($userAgent.isMiniProgram) {
- // // throw new Error('小程序环境不支持');
- // // }
- // throw new Error('非App环境');
- // };
- i('native', {
- async share({ title, desc, link, imgUrl }) {
- // before();
- // 普通分享
- if ($userAgent.isSzx) {
- if ($userAgent.isIos) {
- await window.webkit.messageHandlers.share.postMessage({
- title,
- content: desc,
- gotoUrl: link,
- shareImg: imgUrl,
- });
- return;
- }
- if ($userAgent.isAndroid) {
- await window.native.share(title, desc, link, imgUrl, '28_分享活动');
- return;
- }
- return;
- }
- if ($userAgent.isSzxBrowser) {
- await this.setShareInfo({ title, desc, link, imgUrl });
- await window.webkit.messageHandlers.openShare.postMessage({});
- }
- },
- async eventShare({ title, desc, link, imgUrl }) {
- // before();
- // 分享朋友圈
- if ($userAgent.isSzx) {
- if ($userAgent.isIos) {
- await window.webkit.messageHandlers.share.postMessage({
- title,
- content: desc,
- gotoUrl: link,
- shareImg: imgUrl,
- });
- return;
- }
- if ($userAgent.isAndroid) {
- await window.native.showEventShare(link, title, desc, imgUrl);
- return;
- }
- return;
- }
- if ($userAgent.isSzxBrowser) {
- await this.setShareInfo({ title, desc, link, imgUrl });
- await window.webkit.messageHandlers.openShare.postMessage({});
- }
- },
- async setShareInfo({ title, desc, link, imgUrl, path }) {
- if ($userAgent.isSzxBrowser) {
- // before();
- await window.webkit.messageHandlers.setShareInfo.postMessage({
- title,
- // content,
- gotoUrl: link,
- shareImg: imgUrl,
- });
- return;
- }
- if ($userAgent.isMiniProgram) {
- await $wx?.miniProgram?.postMessage({
- data: {
- action: 'updateAppMessageShareData',
- params: {
- title,
- path,
- imageUrl: imgUrl,
- },
- },
- });
- }
- // if ($userAgent.isIos) {
- // } else {
- // window.native.setShareUrl(url);
- // }
- },
- clipboard: {
- /**
- *
- * @param {string} text
- * @returns {Promise<any>}
- */
- async writeText(text) {
- if ($userAgent.isSzx && $userAgent.isAndroid) {
- return await window.native.copyToClipboard(text);
- }
- return await copy(text, {
- format: 'text/plain',
- });
- // return await clipboard.writeText(text);
- },
- },
- });
- }
|