591 lines
15 KiB
Vue
591 lines
15 KiB
Vue
<template>
|
||
<s-layout :navbar="navBar" title="邀请好友" navPreset="float">
|
||
<view class="share_page_bg" id="captureBox" :style="{ marginTop: `-${navHeight}px` }">
|
||
<view class="slogan">
|
||
<image class="slogan_img" src="/static/share_bg.png" mode="widthFix" />
|
||
</view>
|
||
|
||
<view class="share_card">
|
||
<view class="center m-t-22 m-b-14 label_text">我的邀请码</view>
|
||
<view class="invite_code_bar">
|
||
<view class="code_val">{{ inviteinfo?.inviteCode }}</view>
|
||
<view class="copy_btn" @click="copy(inviteinfo?.inviteCode)">
|
||
<image class="copy-icon-img" src="/static/icon/copy_1.png" mode="aspectFit" />
|
||
</view>
|
||
</view>
|
||
<view class="con_center">
|
||
<view class="flex-center">
|
||
<view class="code flex-center code_wrap">
|
||
<view class="img">
|
||
<Qrcode
|
||
ref="qrcodeRef"
|
||
:size="320"
|
||
@generated="onQrGenerated"
|
||
@longpress="saveServiceQrcode"
|
||
/>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<view class="qr_tip">扫描二维码,注册并登录APP</view>
|
||
|
||
<!-- <view class="btns">
|
||
<view class="square-btn-outline" @click="saveImg">
|
||
<view class="icon-down"></view>
|
||
保存邀请图片至相册
|
||
</view>
|
||
<view class="square-btn" @click="openShare">
|
||
<view class="icon-link"></view>
|
||
分享给好友
|
||
</view>
|
||
</view> -->
|
||
|
||
<uni-popup ref="popup" type="share">
|
||
<uni-popup-share title="分享到" @select="doShare"></uni-popup-share>
|
||
</uni-popup>
|
||
</view>
|
||
</view>
|
||
|
||
<view class="save_btn" @click="saveImg">保存到相册</view>
|
||
</view>
|
||
</s-layout>
|
||
</template>
|
||
|
||
<script setup>
|
||
import Qrcode from './components/Qrcode';
|
||
import { getCurrentInstance, nextTick, ref } from 'vue';
|
||
import * as api from '@/sheep/api/home/index';
|
||
import { onPageScroll } from '@dcloudio/uni-app';
|
||
|
||
const instance = getCurrentInstance();
|
||
const proxy = instance?.proxy;
|
||
|
||
const sysInfoForNav = uni.getSystemInfoSync();
|
||
const navHeight = ref((sysInfoForNav?.statusBarHeight || 0) + 44);
|
||
const navBar = ref('inner');
|
||
onPageScroll((e) => {
|
||
if ((e?.scrollTop || 0) > 20) {
|
||
navBar.value = 'normal';
|
||
} else {
|
||
navBar.value = 'inner';
|
||
}
|
||
});
|
||
|
||
const inviteinfo = ref({});
|
||
const qrCodeUrl = ref('');
|
||
const qrcodeRef = ref(null);
|
||
const popup = ref(null);
|
||
|
||
const generateQRCode = (text) => {
|
||
nextTick(() => {
|
||
qrcodeRef.value?.createQRcode(text);
|
||
});
|
||
};
|
||
|
||
const getinviteinfo = () => {
|
||
api.inviteinfo().then((res) => {
|
||
inviteinfo.value = res?.data || {};
|
||
if (inviteinfo.value?.inviteUrl) generateQRCode(inviteinfo.value.inviteUrl);
|
||
});
|
||
};
|
||
|
||
const getCaptureBoxRect = () =>
|
||
new Promise((resolve, reject) => {
|
||
uni
|
||
.createSelectorQuery()
|
||
.in(proxy)
|
||
.select('#captureBox')
|
||
.boundingClientRect((rect) => {
|
||
if (!rect) {
|
||
reject(new Error('captureBox not found'));
|
||
return;
|
||
}
|
||
resolve(rect);
|
||
})
|
||
.exec();
|
||
});
|
||
|
||
const saveToAlbum = (filePath) =>
|
||
new Promise((resolve, reject) => {
|
||
uni.saveImageToPhotosAlbum({
|
||
filePath,
|
||
success: () => resolve(true),
|
||
fail: (e) => reject(e),
|
||
});
|
||
});
|
||
|
||
const toFileUrlIfNeeded = (p) => (p.startsWith('file://') ? p : `file://${p}`);
|
||
|
||
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||
|
||
let isSavingPoster = false;
|
||
|
||
const showSaveSuccessToast = () => {
|
||
uni.showToast({
|
||
title: '保存成功',
|
||
icon: 'success',
|
||
duration: 500,
|
||
});
|
||
setTimeout(() => uni.hideToast(), 500);
|
||
};
|
||
|
||
const drawCurrentWebviewToFile = async (params) => {
|
||
if (typeof plus === 'undefined' || !plus.webview || !plus.nativeObj) {
|
||
throw new Error('plus is not available');
|
||
}
|
||
|
||
const webview = plus.webview.getTopWebview?.() || plus.webview.currentWebview();
|
||
const bitmap = new plus.nativeObj.Bitmap(`share_bitmap_${Date.now()}`);
|
||
|
||
await new Promise((resolve, reject) => {
|
||
webview.draw(
|
||
bitmap,
|
||
() => resolve(true),
|
||
(e) => reject(e),
|
||
{ check: true },
|
||
);
|
||
});
|
||
|
||
const fileUrl = `_doc/share_capture_${Date.now()}.jpg`;
|
||
await new Promise((resolve, reject) => {
|
||
bitmap.save(
|
||
fileUrl,
|
||
params?.clip
|
||
? { overwrite: true, format: 'jpg', quality: 90, clip: params.clip }
|
||
: { overwrite: true, format: 'jpg', quality: 90 },
|
||
() => resolve(true),
|
||
(e) => reject(e),
|
||
);
|
||
});
|
||
bitmap.clear();
|
||
|
||
const localPath = plus.io.convertLocalFileSystemURL(fileUrl);
|
||
return {
|
||
localPath,
|
||
filePath: toFileUrlIfNeeded(localPath),
|
||
};
|
||
};
|
||
|
||
const savePoster = async () => {
|
||
// #ifdef APP-PLUS
|
||
if (isSavingPoster) return;
|
||
isSavingPoster = true;
|
||
try {
|
||
uni.showLoading({ title: '生成图片...' });
|
||
await nextTick();
|
||
for (let i = 0; i < 8; i++) {
|
||
if (qrCodeUrl.value) break;
|
||
await sleep(200);
|
||
}
|
||
|
||
const rect = await getCaptureBoxRect();
|
||
const sysInfo = uni.getSystemInfoSync();
|
||
const excludeTopPx = (sysInfo?.statusBarHeight || 0) + 44;
|
||
const ratioX =
|
||
typeof plus !== 'undefined' && plus.screen && plus.screen.resolutionWidth
|
||
? plus.screen.resolutionWidth / sysInfo.windowWidth
|
||
: sysInfo.pixelRatio || 1;
|
||
const ratioY =
|
||
typeof plus !== 'undefined' && plus.screen && plus.screen.resolutionHeight
|
||
? plus.screen.resolutionHeight / sysInfo.windowHeight
|
||
: sysInfo.pixelRatio || 1;
|
||
const maxWidth = Math.max(1, Math.floor(sysInfo.windowWidth * ratioX));
|
||
const maxHeight = Math.max(1, Math.floor(sysInfo.windowHeight * ratioY));
|
||
|
||
const clip = {
|
||
top: Math.max(0, Math.floor(((rect.top || 0) + excludeTopPx) * ratioY)),
|
||
left: Math.max(0, Math.floor((rect.left || 0) * ratioX)),
|
||
width: Math.max(1, Math.floor((rect.width || 1) * ratioX)),
|
||
height: Math.max(1, Math.floor(Math.max(1, (rect.height || 1) - excludeTopPx) * ratioY)),
|
||
};
|
||
|
||
const sideExpand = Math.max(0, Math.floor(30 * ratioX));
|
||
const bottomExpand = Math.max(0, Math.floor(20 * ratioY));
|
||
|
||
clip.left = Math.max(0, clip.left - sideExpand);
|
||
clip.width = clip.width + sideExpand * 2;
|
||
clip.height = clip.height + bottomExpand;
|
||
|
||
if (clip.left >= maxWidth) clip.left = maxWidth - 1;
|
||
if (clip.top >= maxHeight) clip.top = maxHeight - 1;
|
||
if (clip.left + clip.width > maxWidth) clip.width = maxWidth - clip.left;
|
||
if (clip.top + clip.height > maxHeight) clip.height = maxHeight - clip.top;
|
||
|
||
const { localPath, filePath } = await drawCurrentWebviewToFile({ clip });
|
||
try {
|
||
await saveToAlbum(localPath);
|
||
} catch (e) {
|
||
await saveToAlbum(filePath);
|
||
}
|
||
|
||
showSaveSuccessToast();
|
||
} catch (e) {
|
||
const msg =
|
||
(typeof e === 'string' && e) ||
|
||
e?.errMsg ||
|
||
e?.message ||
|
||
(e ? JSON.stringify(e) : 'unknown error');
|
||
uni.showModal({
|
||
title: '保存失败',
|
||
content:
|
||
msg.includes('authorize') || msg.includes('Permission')
|
||
? `${msg}\n\n请到系统设置开启相册权限后重试`
|
||
: msg,
|
||
showCancel: false,
|
||
});
|
||
} finally {
|
||
isSavingPoster = false;
|
||
uni.hideLoading();
|
||
}
|
||
// #endif
|
||
|
||
// #ifndef APP-PLUS
|
||
uni.showToast({ title: '仅App支持整页保存', icon: 'none' });
|
||
// #endif
|
||
};
|
||
|
||
const saveImg = async () => {
|
||
await savePoster();
|
||
};
|
||
|
||
const saveServiceQrcode = async () => {
|
||
await savePoster();
|
||
};
|
||
|
||
const onQrGenerated = (path) => {
|
||
qrCodeUrl.value = path;
|
||
};
|
||
|
||
const copy = (text) => {
|
||
const val = text || inviteinfo.value?.inviteCode || '';
|
||
if (!val) return;
|
||
uni.setClipboardData({
|
||
data: val,
|
||
success: () => {
|
||
uni.showToast({ icon: 'none', title: '复制成功' });
|
||
},
|
||
});
|
||
};
|
||
|
||
const openShare = () => {
|
||
popup.value?.open?.();
|
||
};
|
||
|
||
const mapShareProvider = (name) => {
|
||
if (!name) return '';
|
||
const m = {
|
||
wx: 'weixin',
|
||
ali: 'alipay',
|
||
qq: 'qq',
|
||
sina: 'sinaweibo',
|
||
};
|
||
return m[name] || '';
|
||
};
|
||
|
||
const doShare = async (res) => {
|
||
const provider = mapShareProvider(res?.item?.name);
|
||
if (!provider) {
|
||
uni.showToast({ title: '当前渠道不支持', icon: 'none' });
|
||
return;
|
||
}
|
||
|
||
// #ifdef APP-PLUS
|
||
try {
|
||
uni.showLoading({ title: '生成分享图片...' });
|
||
await nextTick();
|
||
for (let i = 0; i < 8; i++) {
|
||
if (qrCodeUrl.value) break;
|
||
await sleep(200);
|
||
}
|
||
const rect = await getCaptureBoxRect();
|
||
const sysInfo = uni.getSystemInfoSync();
|
||
const excludeTopPx = (sysInfo?.statusBarHeight || 0) + 44;
|
||
const ratioX =
|
||
typeof plus !== 'undefined' && plus.screen && plus.screen.resolutionWidth
|
||
? plus.screen.resolutionWidth / sysInfo.windowWidth
|
||
: sysInfo.pixelRatio || 1;
|
||
const ratioY =
|
||
typeof plus !== 'undefined' && plus.screen && plus.screen.resolutionHeight
|
||
? plus.screen.resolutionHeight / sysInfo.windowHeight
|
||
: sysInfo.pixelRatio || 1;
|
||
const maxWidth = Math.max(1, Math.floor(sysInfo.windowWidth * ratioX));
|
||
const maxHeight = Math.max(1, Math.floor(sysInfo.windowHeight * ratioY));
|
||
const clip = {
|
||
top: Math.max(0, Math.floor(((rect.top || 0) + excludeTopPx) * ratioY)),
|
||
left: Math.max(0, Math.floor((rect.left || 0) * ratioX)),
|
||
width: Math.max(1, Math.floor((rect.width || 1) * ratioX)),
|
||
height: Math.max(1, Math.floor(Math.max(1, (rect.height || 1) - excludeTopPx) * ratioY)),
|
||
};
|
||
const sideExpand = Math.max(0, Math.floor(30 * ratioX));
|
||
const bottomExpand = Math.max(0, Math.floor(20 * ratioY));
|
||
clip.left = Math.max(0, clip.left - sideExpand);
|
||
clip.width = clip.width + sideExpand * 2;
|
||
clip.height = clip.height + bottomExpand;
|
||
if (clip.left >= maxWidth) clip.left = maxWidth - 1;
|
||
if (clip.top >= maxHeight) clip.top = maxHeight - 1;
|
||
if (clip.left + clip.width > maxWidth) clip.width = maxWidth - clip.left;
|
||
if (clip.top + clip.height > maxHeight) clip.height = maxHeight - clip.top;
|
||
uni.hideLoading();
|
||
const { localPath: shareImage } = await drawCurrentWebviewToFile({ clip });
|
||
uni.share({
|
||
provider,
|
||
type: 1,
|
||
imageUrl: shareImage,
|
||
success: () => uni.showToast({ title: '分享成功' }),
|
||
fail: () => uni.showToast({ title: '分享失败', icon: 'none' }),
|
||
});
|
||
} catch (error) {
|
||
uni.hideLoading();
|
||
uni.showToast({ title: '截图失败', icon: 'none' });
|
||
}
|
||
// #endif
|
||
};
|
||
|
||
getinviteinfo();
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.share_page_bg {
|
||
position: relative;
|
||
min-height: 100vh;
|
||
padding: 570rpx 40rpx 80rpx;
|
||
box-sizing: border-box;
|
||
// background: #0b53d8;
|
||
}
|
||
|
||
.share_page_bg::before {
|
||
content: '';
|
||
position: absolute;
|
||
left: 0;
|
||
top: 0;
|
||
right: 0;
|
||
bottom: 0;
|
||
background-image: url('/static/home/seedBg.png');
|
||
background-repeat: no-repeat;
|
||
background-position: center top;
|
||
background-size: cover;
|
||
pointer-events: none;
|
||
z-index: -1;
|
||
}
|
||
|
||
.slogan {
|
||
position: absolute;
|
||
top: 0rpx;
|
||
left: 0;
|
||
right: 0;
|
||
z-index: 0;
|
||
// padding: 0 40rpx;
|
||
box-sizing: border-box;
|
||
overflow: hidden;
|
||
border-radius: 0 0 30rpx 30rpx;
|
||
}
|
||
|
||
.slogan_img {
|
||
width: 100%;
|
||
display: block;
|
||
}
|
||
|
||
.share_card {
|
||
position: relative;
|
||
z-index: 0;
|
||
width: 100%;
|
||
border-radius: 28rpx;
|
||
background: rgba(255, 255, 255, 0.96);
|
||
box-shadow: 0 16rpx 40rpx rgba(0, 0, 0, 0.18);
|
||
overflow: hidden;
|
||
}
|
||
|
||
.con_center {
|
||
padding: 5rpx 40rpx 24rpx;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.con_name {
|
||
font-size: 40rpx;
|
||
font-weight: 700;
|
||
color: #1f2d23;
|
||
}
|
||
|
||
.sub_title {
|
||
font-size: 28rpx;
|
||
color: #5d6b61;
|
||
}
|
||
|
||
.label_text {
|
||
font-size: 26rpx;
|
||
font-weight: 600;
|
||
color: #1a73ff;
|
||
}
|
||
|
||
.code {
|
||
width: 360rpx;
|
||
height: 360rpx;
|
||
border-radius: 22rpx;
|
||
background: #fff;
|
||
}
|
||
|
||
.code_wrap {
|
||
// box-shadow: 0 10rpx 28rpx rgba(31, 63, 132, 0.14);
|
||
}
|
||
|
||
.img {
|
||
width: 320rpx;
|
||
height: 320rpx;
|
||
// border-radius: 18rpx;
|
||
overflow: hidden;
|
||
background: #fff;
|
||
}
|
||
|
||
.invite_code_bar {
|
||
width: 80%;
|
||
margin: 0 auto;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
background: #f2f3f5;
|
||
border-radius: 18rpx;
|
||
overflow: hidden;
|
||
height: 90rpx;
|
||
}
|
||
|
||
.code_val {
|
||
// flex: 1;
|
||
color: #111;
|
||
font-size: 56rpx;
|
||
font-weight: 700;
|
||
letter-spacing: 4rpx;
|
||
text-align: center;
|
||
height: 100%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background: transparent;
|
||
}
|
||
|
||
.copy_btn {
|
||
width: 80rpx;
|
||
height: 112rpx;
|
||
background: transparent;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
color: #1a73ff;
|
||
}
|
||
|
||
.icon-copy {
|
||
width: 28rpx;
|
||
height: 28rpx;
|
||
border: 2rpx solid currentColor;
|
||
border-radius: 6rpx;
|
||
position: relative;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.icon-copy::before {
|
||
content: '';
|
||
position: absolute;
|
||
left: -10rpx;
|
||
top: 10rpx;
|
||
width: 28rpx;
|
||
height: 28rpx;
|
||
border: 2rpx solid currentColor;
|
||
border-radius: 6rpx;
|
||
box-sizing: border-box;
|
||
opacity: 0.7;
|
||
}
|
||
|
||
.copy-icon-img {
|
||
width: 44rpx;
|
||
height: 44rpx;
|
||
display: block;
|
||
}
|
||
|
||
.qr_tip {
|
||
margin-top: 0rpx;
|
||
text-align: center;
|
||
font-size: 22rpx;
|
||
color: #9aa0a6;
|
||
}
|
||
|
||
.save_btn {
|
||
margin-top: 32rpx;
|
||
width: 100%;
|
||
height: 104rpx;
|
||
border-radius: 18rpx;
|
||
color: #fff;
|
||
font-size: 34rpx;
|
||
font-weight: 600;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background: linear-gradient(135deg, var(--primary-grad-start) 0%, var(--primary-grad-end) 100%);
|
||
box-shadow: 0 18rpx 40rpx var(--primary-shadow);
|
||
}
|
||
|
||
.btns {
|
||
margin-top: 32rpx;
|
||
}
|
||
|
||
.square-btn-outline {
|
||
width: 100%;
|
||
padding: 30rpx 0;
|
||
border-radius: 12rpx;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
background: transparent;
|
||
color: #2b5f3d;
|
||
border: 2rpx solid #2b5f3d;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.square-btn {
|
||
margin-top: 20rpx;
|
||
width: 100%;
|
||
padding: 30rpx 0;
|
||
border-radius: 12rpx;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: center;
|
||
background: linear-gradient(90deg, #2f7a4a, #55a96d);
|
||
color: #fff;
|
||
}
|
||
|
||
.icon-down {
|
||
width: 0;
|
||
height: 0;
|
||
border-left: 12rpx solid transparent;
|
||
border-right: 12rpx solid transparent;
|
||
border-top: 16rpx solid currentColor;
|
||
margin-right: 14rpx;
|
||
}
|
||
|
||
.icon-link {
|
||
width: 30rpx;
|
||
height: 30rpx;
|
||
position: relative;
|
||
margin-right: 14rpx;
|
||
}
|
||
|
||
.icon-link::before,
|
||
.icon-link::after {
|
||
content: '';
|
||
position: absolute;
|
||
width: 18rpx;
|
||
height: 12rpx;
|
||
border: 4rpx solid rgba(255, 255, 255, 0.95);
|
||
border-radius: 18rpx;
|
||
top: 8rpx;
|
||
}
|
||
|
||
.icon-link::before {
|
||
left: 0;
|
||
transform: rotate(-18deg);
|
||
}
|
||
|
||
.icon-link::after {
|
||
right: 0;
|
||
transform: rotate(18deg);
|
||
}
|
||
</style>
|