Skip to content

Commit

Permalink
feat: 🎸 add douyin miniprogram support
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingyun2010 committed Jan 5, 2023
1 parent 3bdb14f commit 35c3f70
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const Mini = {
window.jd.miniProgram.navigateBack();
}

if (window && window.tt && window.tt.miniProgram && window.tt.miniProgram.navigateBack) {
key++;
window.tt.miniProgram.navigateBack();
}

if (refreshPreWebview) {
Mini.postMessage({ type: 'refresh_pre_webview' });
}
Expand Down Expand Up @@ -62,6 +67,10 @@ const Mini = {
window.jd.miniProgram.navigateTo({ url });
}

if (window && window.tt && window.tt.miniProgram && window.tt.miniProgram.navigateTo) {
window.tt.miniProgram.navigateTo({ url });
}

const utm = getUtm();
Mini.postMessage({ type: 'update_utm', payload: utm });
},
Expand All @@ -86,6 +95,10 @@ const Mini = {
window.jd.miniProgram.switchTab({ url });
}

if (window && window.tt && window.tt.miniProgram && window.tt.miniProgram.switchTab) {
window.tt.miniProgram.switchTab({ url });
}

const utm = getUtm();
Mini.postMessage({ type: 'update_utm', payload: utm });
},
Expand All @@ -105,6 +118,10 @@ const Mini = {
if (window && window.jd && window.jd.miniProgram && window.jd.miniProgram.postMessage) {
window.jd.miniProgram.postMessage({ data });
}

if (window && window.tt && window.tt.miniProgram && window.tt.miniProgram.postMessage) {
window.tt.miniProgram.postMessage({ data });
}
},

/**
Expand All @@ -127,6 +144,10 @@ const Mini = {
window.jd.miniProgram.redirectTo({ url });
}

if (window && window.tt && window.tt.miniProgram && window.tt.miniProgram.redirectTo) {
window.tt.miniProgram.redirectTo({ url });
}

const utm = getUtm();
Mini.postMessage({ type: 'update_utm', payload: utm });
},
Expand All @@ -151,6 +172,10 @@ const Mini = {
window.jd.miniProgram.reLaunch({ url });
}

if (window && window.tt && window.tt.miniProgram && window.tt.miniProgram.reLaunch) {
window.tt.miniProgram.reLaunch({ url });
}

const utm = getUtm();
Mini.postMessage({ type: 'update_utm', payload: utm });
},
Expand Down
20 changes: 20 additions & 0 deletions lib/miniprogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export const initMini = () => {
const isWx = /MicroMessenger/gi.test(ua);
const isAlipay = /AlipayClient/gi.test(ua);
const isSwan = /swan/gi.test(ua);
const isTT = /toutiaomicroapp/gi.test(ua);

if (isAlipay) {
document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>');
}
Expand All @@ -20,6 +22,16 @@ export const initMini = () => {
'<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"' + '>' + '<' + '/' + 'script>',
);
}

if (isTT) {
document.writeln(
'<script src="https://lf1-cdn-tos.bytegoofy.com/goofy/developer/jssdk/jssdk-1.1.0.js"' +
'>' +
'<' +
'/' +
'script>',
);
}
};

interface MiniEnv {
Expand All @@ -28,6 +40,7 @@ interface MiniEnv {
isSwan?: boolean;
isJD?: boolean;
isMiniProgram?: boolean;
isTT?: boolean;
}

export const getMiniProgramEnv = (): Promise<MiniEnv> => {
Expand Down Expand Up @@ -64,6 +77,13 @@ export const getMiniProgramEnv = (): Promise<MiniEnv> => {
isMiniProgram: res.smartprogram,
});
});
} else if (window.tt && window.tt.miniProgram.getEnv) {
window.tt.miniProgram.getEnv((res: any) => {
resolve({
isTT: res.miniprogram,
isMiniProgram: res.miniprogram,
});
});
} else {
resolve({
isMiniProgram: false,
Expand Down
2 changes: 2 additions & 0 deletions types/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Window {
atzuche: any;
NativeJsBridge: any;
_cblock_: any;
tt: any;
}

interface Window {
Expand All @@ -30,4 +31,5 @@ interface Window {
isMiniProgram: boolean;
isiOS: boolean;
isAndroid: boolean;
isTT: boolean;
}

0 comments on commit 35c3f70

Please sign in to comment.