import request from '@/helpers/request' import { Button, closeToast, Icon, Image, showFailToast, showLoadingToast, showSuccessToast, showToast } from 'vant' import { defineComponent, onMounted, reactive } from 'vue' import { useRoute, useRouter } from 'vue-router' import styles from './index.module.less' import { postMessage, promisefiyPostMessage } from '@/helpers/native-message' import html2canvas from 'html2canvas' import { state as baseState } from '@/state' import OQrcode from '@/components/o-qrcode' import iconImage from './images/icon-image.png' import iconWeichat from './images/icon-weichat.png' import teacherTopBg from './images/teacher-top_bg.png' import manageTopBg from './images/manage-top_bg.png' import orchestraTopBg from './images/orchestra-top_bg.png' import defaultLogo from '@/common/images/logo@2x.png' import OHeader from '@/components/o-header' import { browser } from '@/helpers/utils' export default defineComponent({ name: 'save-share-image', setup() { const route = useRoute() const router = useRouter() const state = reactive({ type: route.query.type as any, paramValue: 2, schoolName: '', schoolId: '', url: null as any, schoolLogo: '', loading: false }) const imgs = reactive({ saveLoading: false, image: null as any, shareLoading: false }) const onSaveImg = async () => { // 判断是否在保存中... if (imgs.saveLoading) { return } imgs.saveLoading = true // 判断是否已经生成图片 if (imgs.image) { saveImg() } else { const container: any = document.getElementById(`preview-container`) html2canvas(container, { allowTaint: true, useCORS: true, backgroundColor: null }) .then(async (canvas) => { const url = canvas.toDataURL('image/png') imgs.image = url saveImg() }) .catch(() => { closeToast() imgs.saveLoading = false }) } } const onShare = () => { if (imgs.shareLoading) { return } imgs.shareLoading = true if (imgs.image) { openShare() } else { const container: any = document.getElementById(`preview-container`) html2canvas(container, { allowTaint: true, useCORS: true, backgroundColor: null }) .then(async (canvas) => { const url = canvas.toDataURL('image/png') imgs.image = url openShare() }) .catch(() => { closeToast() imgs.shareLoading = false }) } } const openShare = () => { const image = imgs.image setTimeout(() => { imgs.shareLoading = false }, 100) if (image) { postMessage( { api: 'shareTripartite', content: { title: '', desc: '', image, video: '', type: 'image', // button: ['copy'] shareType: 'wechat' } }, (res: any) => { if (res && res.content) { showToast(res.content.message || (res.content.status ? '分享成功' : '分享失败')) } } ) } } const saveImg = async () => { showLoadingToast({ message: '图片生成中...', forbidClick: true }) setTimeout(() => { imgs.saveLoading = false }, 100) const res = await promisefiyPostMessage({ api: 'savePicture', content: { base64: imgs.image } }) if (res?.content?.status === 'success') { showSuccessToast('保存成功') } else { showFailToast('保存失败') } } // 获取当前用户所在的学校 const getDetail = async () => { try { const schoolId = (baseState.user.data.schoolInfos || []) .map((item) => { return item.id }) .join(',') const res = await request.get(`/api-school/school/detail/${schoolId}`, {}) state.schoolName = res.data.name state.schoolId = res.data.id state.schoolLogo = res.data.logo ? res.data.logo + '@base@tag=imgScale&w=570?t=' + +new Date() : '' // 生成二维码 if (state.type === 'teacher') { state.url = location.origin + '/orchestra-school/#/companion-teacher-register?id=' + res.data.id + '&name=' + res.data.name + '&t=' + +new Date() } else if (state.type === 'manage') { state.url = location.origin + '/orchestra-school/#/manage-teacher-register?id=' + res.data.id + '&name=' + res.data.name + '&t=' + +new Date() } } catch { // } } // 获取当前用户所在的学校 const getOrchestraDetail = async () => { try { const res = await request.get('/api-school/orchestra/detail/' + route.query.id) state.schoolName = res.data.name state.schoolId = res.data.id state.schoolLogo = res.data.schoolLogo + '@base@tag=imgScale&w=570?t=' + +new Date() // 生成二维码 state.url = window.location.origin + '/orchestra-student/#/preApply?id=' + route.query.id } catch { // } } onMounted(async () => { if (state.type === 'teacher') { document.title = '乐团伴学指导注册' } else if (state.type === 'manage') { document.title = '乐团管理老师注册' } else if (state.type === 'orchestra') { document.title = '乐团报名' } try { const { data } = await request.get('/api-school/open/paramConfig/queryByParamName', { requestType: 'form', params: { paramName: 'qr_code_expire_hours' } }) state.paramValue = data.paramValue } catch { // } if (state.type === 'orchestra') { getOrchestraDetail() } else { getDetail() } }) const onBack = () => { if (browser().isApp) { postMessage({ api: 'goBack' }) } else { router.back() } } return () => (
{{ content: () => (
) }}
{state.type === 'teacher' && } {state.type === 'manage' && } {state.type === 'orchestra' && }
{state.type !== 'orchestra' ? ( <>
{state.schoolName}
邀请您成为 {state.type === 'teacher' && '乐团伴学指导'} {state.type === 'manage' && '乐团管理老师'}
) : ( <>
乐团报名
{state.schoolName}
)}
扫描上方二维码完成资料填写
{state.type !== 'orchestra' && (
二维码将在{state.paramValue}小时后失效,请及时登记
)}
{!state.loading && (
{state.type === 'teacher' && } {state.type === 'manage' && } {state.type === 'orchestra' && }
{state.type !== 'orchestra' ? ( <>
{state.schoolName}
邀请您成为 {state.type === 'teacher' && '乐团伴学指导'} {state.type === 'manage' && '乐团管理老师'}
) : ( <>
乐团报名
{state.schoolName}
)}
扫描上方二维码完成资料填写
{state.type !== 'orchestra' && (
二维码将在{state.paramValue}小时后失效,请及时登记
)}
)}
) } })