uniapp 分享例子做个记录

<template>

<view class="content">

<view class="invite-wrapper" v-if="inviteUrl">

<uni-qrcode class="invite-qrcode" ref="qrcode" cid="invite-qr-canvas" :text="inviteUrl" :margin="10" makeOnLoad

@makeComplete="qrcodeComplete" />

<view class="share-btn">

<button type="primary" @click="shareLink">分享链接</button>

<button type="primary" @click="shareImage">分享图片</button>

</view>

<view class="share-btn">

<button type="primary" @click="toInvitedUser">我邀请的用户</button>

</view>

<uni-popup id="popupShare" ref="popupShare" type="share">

<uni-popup-share title="分享到" :shareType="shareType" @select="select"></uni-popup-share>

</uni-popup>

</view>

</view>

</template>

<script>

import {

mapState

} from 'vuex'

export default {

data() {

return {

inviteUrl: '',

qrcodeImagePath: '',

qrcodePath: '',

shareType: ''

}

},

onLoad() {

uni.showLoading({

title: '加载中...'

})

uniCloud.callFunction({

name: 'user-center',

data: {

action: 'getInviteCode'

},

success: (res) => {

console.log(res);

if (res.result.code === 0) {

// 这里请修改为真实的邀请页面url

this.inviteUrl = 'https://login.tpl.dcloud.net.cn/#/pages/invite-reg/invite-reg?invite_code=' + res.result.myInviteCode

} else {

uni.showModal({

content: '获取用户邀请码失败:' + res.result.msg,

showCancel: false

})

}

},

fail: (err) => {

uni.showModal({

content: '获取用户邀请码失败,请稍后再试',

showCancel: false

})

},

complete() {

uni.hideLoading()

}

})

},

methods: {

copyInviteUrl() {

uni.setClipboardData({

data: this.inviteUrl

})

},

qrcodeComplete(path) {

this.qrcodeImagePath = path

},

shareLink() {

this.shareType = 'link'

this.$refs.popupShare.open()

},

shareImage() {

this.shareType = 'image'

this.$refs.popupShare.open()

},

select({

item

}) {

const shareTitle = '登录模板',

shareSummary = 'DCloud邀请您试用登录模板'

let params = {}

switch (`{this.shareType}_{item.name}`) {

case 'link_weixin':

params = {

type: 1,

summary: this.inviteUrl,

scene: 'WXSceneSession'

}

break;

case 'link_qq':

params = {

type: 1,

title: shareTitle,

summary: shareSummary,

href: this.inviteUrl

}

break;

case 'link_more':

uni.setClipboardData({

data: this.inviteUrl

})

return

case 'image_weixin':

params = {

type: 2,

imageUrl: this.qrcodeImagePath,

scene: 'WXSceneSession'

}

break;

case 'image_qq':

params = {

type: 2,

imageUrl: this.qrcodeImagePath

}

break;

case 'image_more':

uni.shareWithSystem({

type: 'image',

imageUrl: this.qrcodeImagePath

})

return

default:

break;

}

console.log(params);

uni.share({

provider: item.name,

...params,

success() {

uni.showModal({

content: '分享成功',

showCancel: false

})

},

fail(err) {

uni.showModal({

content: '分享失败:' + err.errMsg,

showCancel: false

})

}

})

},

toInvitedUser() {

uni.navigateTo({

url: '/pages/invited-user/invited-user'

})

}

}

}

</script>

<style>

.invite-wrapper {

display: flex;

padding: 20px;

flex-direction: column;

align-items: center;

}

.invite-qrcode {

border-radius: 10px;

box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 10px 1px;

overflow: hidden;

}

.share-btn {

display: flex;

flex-direction: row;

padding: 20px 0px 10px 0px;

}

.share-btn button {

flex: 1;

margin: 0px 10px;

border-radius: 40px;

}

</style>

相关推荐
清风细雨_林木木5 小时前
uni-app 和 uni-app x 的区别
uni-app
iOS阿玮5 小时前
期待iOS开发者加入,共同抵制“苹果税”反垄断招募令!
uni-app·app·apple
普通网友5 小时前
支持二次开发的代练App源码:订单管理、代练监控、安全护航功能齐全,一站式解决代练护航平台源码(PHP+ Uni-app)
安全·uni-app·php
蜕变菜鸟5 小时前
uview使用u-popup组件当开启遮罩层禁止下层页面滚动。uniapp弹框禁止页面上下滚动。
uni-app
吴传逞2 天前
记一次uniapp微信小程序开发scss变量失效的问题
微信小程序·uni-app·scss
2501_915921432 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008892 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
!win !2 天前
uni-app项目Tabbar实现切换icon动效
小程序·uni-app
xw52 天前
uni-app项目Tabbar实现切换icon动效
前端·uni-app
2501_916007472 天前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview