Files
kangyuan-mobile/pages/home/share.vue
T

550 lines
13 KiB
Vue

<template>
<s-layout title="邀请好友">
<view class="share_page_bg" id="captureBox">
<view class="slogan">
<view class="text" v-for="(t, idx) in sloganLines" :key="idx">{{ t }}</view>
</view>
<view class="share_card">
<view class="con_center">
<view class="center m-b-28">
<view class="con_name">邀好友来购物</view>
<view class="sub_title m-t-16">长按保存二维码或复制邀请码分享</view>
</view>
<view class="flex-center">
<view class="code flex-center code_wrap">
<view class="img">
<Qrcode
ref="qrcodeRef"
:size="300"
@generated="onQrGenerated"
@longpress="saveServiceQrcode"
/>
</view>
</view>
</view>
<view class="center m-t-32 m-b-24 label_text">您的推荐码</view>
<view class="invite_code_bar">
<view class="code_val">{{ inviteinfo?.inviteCode }}</view>
<view class="copy_btn" @click="copy(inviteinfo?.inviteCode)">
<view class="icon-copy"></view>
<text class="btn_text">复制</text>
</view>
</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>
</s-layout>
</template>
<script setup>
import Qrcode from './components/Qrcode';
import { getCurrentInstance, nextTick, ref } from 'vue';
import * as api from '@/sheep/api/home/index';
const instance = getCurrentInstance();
const proxy = instance?.proxy;
const sloganLines = ref(['优品赢四海', '购福赴新途']);
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 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) * 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((rect.height || 1) * ratioY)),
};
const sideExpand = Math.max(0, Math.floor(30 * ratioX));
const topExpand = Math.max(0, Math.floor(60 * ratioY));
const bottomExpand = Math.max(0, Math.floor(20 * ratioY));
clip.top = Math.max(0, clip.top - topExpand);
clip.left = Math.max(0, clip.left - sideExpand);
clip.width = clip.width + sideExpand * 2;
clip.height = clip.height + topExpand + 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();
await sleep(450);
uni.hideLoading();
const { localPath: shareImage } = await drawCurrentWebviewToFile();
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>
:deep(page) {
background: #fff6f7;
}
.share_page_bg {
position: relative;
min-height: 100vh;
padding: 330rpx 40rpx 0rpx;
box-sizing: border-box;
background: #fff6f7;
}
.share_page_bg::before {
content: '';
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
background-image: linear-gradient(
to bottom,
rgba(255, 246, 247, 0) 0%,
rgba(255, 246, 247, 0) 35%,
rgba(255, 246, 247, 1) 85%,
rgba(255, 246, 247, 1) 100%
),
url('/static/home/jinzhongzi_bg.png');
background-repeat: no-repeat, no-repeat;
background-position: center top, center -80rpx;
background-size: 100% 100%, 100% auto;
pointer-events: none;
z-index: 0;
}
.slogan {
position: absolute;
top: 140rpx;
left: 50rpx;
right: 40rpx;
text-align: left;
color: #fff;
font-size: 40rpx;
font-weight: 700;
letter-spacing: 2rpx;
z-index: 1;
.text + .text {
margin-top: 10rpx;
}
}
.share_card {
position: relative;
z-index: 1;
width: 100%;
border-radius: 32rpx;
background: rgba(255, 255, 255, 0.92);
box-shadow: 0 18rpx 56rpx rgba(0, 0, 0, 0.12);
overflow: hidden;
}
.con_center {
padding: 60rpx 40rpx 44rpx;
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: #333;
}
.code {
width: 380rpx;
height: 380rpx;
border: 4rpx solid rgba(255, 111, 145, 0.28);
border-radius: 32rpx;
background: rgba(255, 255, 255, 0.6);
}
.code_wrap {
backdrop-filter: blur(10px);
}
.img {
width: 300rpx;
height: 300rpx;
border-radius: 18rpx;
overflow: hidden;
background: #fff;
}
.invite_code_bar {
display: flex;
align-items: center;
background: transparent;
border-radius: 20rpx;
overflow: visible;
height: 120rpx;
margin: 0;
gap: 12rpx;
}
.code_val {
flex: 1;
color: #fff;
font-size: 56rpx;
font-weight: 700;
letter-spacing: 4rpx;
text-align: center;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(to right, #ffb4a2, #ff6f91);
border-top-left-radius: 20rpx;
border-bottom-left-radius: 20rpx;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.copy_btn {
width: 140rpx;
height: 120rpx;
background: #ff7a8a;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #fff;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
border-top-right-radius: 20rpx;
border-bottom-right-radius: 20rpx;
}
.btn_text {
color: #fff;
font-size: 22rpx;
font-weight: 500;
margin-top: 10rpx;
}
.icon-copy {
width: 24rpx;
height: 24rpx;
border: 1.3333rpx solid currentColor;
border-radius: 5.3333rpx;
position: relative;
margin-bottom: 5.3333rpx;
box-sizing: border-box;
}
.icon-copy::before {
content: '';
position: absolute;
left: -8rpx;
top: 8rpx;
width: 24rpx;
height: 24rpx;
border: 1.3333rpx solid currentColor;
border-radius: 5.3333rpx;
box-sizing: border-box;
opacity: 0.7;
}
.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>