share.vue 8.0 KB
Newer Older
H
hulinNeil 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<template>
	<view>
		<page-head :title="title"></page-head>
		<view class="page-body">
			<view class="page-section-title">分享内容</view>
			<view class="page-section">
				<view class="textarea-wrp">
					<textarea class="textarea" v-model="shareText" />
				</view>
			</view>
			<view class="page-section-title">分享图片:</view>
			<view class="page-section">
				<view class="uni-uploader-body">
					<view class="uni-uploader__input-box" v-if="!image" @tap="chooseImage"></view>
					<image class="uni-uploader__img" v-if="image" :src="image"></image>
				</view>
			</view>
H
hulinNeil 已提交
18
			<!-- #ifdef APP-PLUS -->
X
update  
xiaoyucoding 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
			<view class="page-section-title">分享类型:</view>
			<view class="page-section">
				<view class="uni-uploader-body">
					<radio-group @change="radioChange">
						<label class="radio">
							<radio value="1" checked="true"/>文字
						</label>
						<label class="radio">
							<radio value="2" />图片
						</label>
						<label class="radio">
							<radio value="0" />图文
						</label>
						<label class="radio">
							<radio value="5" />小程序
						</label>
					</radio-group>
				</view>
			</view>
H
hulinNeil 已提交
38 39
			<view class="btn-area" v-if="providerList.length > 0">
				<block v-for="(value,index) in providerList" :key="index">
X
update  
xiaoyucoding 已提交
40
					<button type="primary" v-if="value" :disabled="shareType === 5 && value.name !== '分享到微信好友'" @tap="share(value)">{{value.name}}</button>
H
hulinNeil 已提交
41 42
				</block>
			</view>
H
hulinNeil 已提交
43 44 45 46 47 48
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<view class="btn-area">
				<button type="primary" open-type="share">分享</button>
			</view>
			<!-- #endif -->
H
hulinNeil 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
		</view>

	</view>
</template>
<script>
	import pageHead from '../../../components/page-head.vue'

	export default {
		data() {
			return {
				title: 'share',
				shareText: 'uni-app可以同时发布成原生App、微信小程序,邀请你一起体验!',
				href:"https://uniapp.dcloud.io",
				image: '',
X
update  
xiaoyucoding 已提交
63
				shareType:1,
H
hulinNeil 已提交
64 65 66
				providerList: []
			}
		},
H
hulinNeil 已提交
67 68 69 70 71 72 73
		onShareAppMessage() {
			return {
				title: this.shareText ? this.shareText : "欢迎体验uni-app",
				path: '/pages/component/component',
				imageUrl:this.image ? this.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
			}
		},
H
hulinNeil 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		onUnload:function(){
			this.shareText='uni-app可以同时发布成原生App、微信小程序,邀请你一起体验!',
			this.href = "https://uniapp.dcloud.io",
			this.image='';
		},
		onLoad: function () {
			uni.getProvider({
				service: "share",
				success: (e) => {
					console.log("success", e);
					let data = []
					for (let i = 0; i < e.provider.length; i++) {
						switch (e.provider[i]) {
							case 'weixin':
								data.push({
									name: '分享到微信好友',
									id: 'weixin',
									sort:0
								})
								data.push({
									name: '分享到微信朋友圈',
									id: 'weixin',
									type:'WXSenceTimeline',
									sort:1
								})
								break;
							case 'sinaweibo':
								data.push({
									name: '分享到新浪微博',
									id: 'sinaweibo',
									sort:2
								})
								break;
							case 'qq':
								data.push({
									name: '分享到QQ',
									id: 'qq',
									sort:3
								})
								break;
							default:
								break;
						}
					}
					this.providerList = data.sort((x,y) => {
						return x.sort - y.sort
					});
				},
				fail: (e) => {
					console.log("获取登录通道失败", e);
					uni.showModal({
						content:"获取登录通道失败",
						showCancel:false
					})
				}
			});
		},
		methods: {
			async share(e) {
X
update  
xiaoyucoding 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
				console.log("分享通道:"+ e.id +"; 分享类型:" + this.shareType);
				
				if(!this.shareText && (this.shareType === 1 || this.shareType === 0)){
					uni.showModal({
						content:"分享内容不能为空",
						showCancel:false
					})
					return;
				}
				
				if(!this.image && (this.shareType === 2 || this.shareType === 0)){
					uni.showModal({
						content:"分享图片不能为空",
						showCancel:false
					})
					return;
				}
				
H
hulinNeil 已提交
151 152 153
				let shareOPtions = {
					provider: e.id,
					scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : "WXSceneSession", //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏     
X
update  
xiaoyucoding 已提交
154
					type: this.shareType,
H
hulinNeil 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
					success: (e) => {
						console.log("success", e);
						uni.showModal({
							content: "分享成功",
							showCancel:false
						})
					},
					fail: (e) => {
						console.log("fail", e)
						uni.showModal({
							content: e.errMsg,
							showCancel:false
						})
					},
					complete:function(){
						console.log("分享操作结束!")
					}
				}
X
update  
xiaoyucoding 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
				
				switch (this.shareType){
					case 0:
						shareOPtions.summary = this.shareText;
						shareOPtions.imageUrl = this.image;
						shareOPtions.title = "欢迎体验uniapp";
						shareOPtions.href = "http://uniapp.dcloud.io";
						break;
					case 1:
						shareOPtions.summary = this.shareText;
						break;
					case 2:
						shareOPtions.imageUrl = this.image;
						break;
					case 5:
						shareOPtions.imageUrl = this.image ? this.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
						shareOPtions.title = "欢迎体验uniapp";
						shareOPtions.miniProgram = {
							id:"gh_33446d7f7a26",
							path:"/pages/component/component",
							webUrl:"http://uniapp.dcloud.io",
							type:0
						};
						break;
					default:
						break;
H
hulinNeil 已提交
199
				}
X
update  
xiaoyucoding 已提交
200 201
				
				if(shareOPtions.type === 0 && plus.os.name === 'iOS'){//如果是图文分享,且是ios平台,则压缩图片 
H
hulinNeil 已提交
202 203
					shareOPtions.imageUrl = await this.compress();
				}
X
update  
xiaoyucoding 已提交
204 205 206 207
				if(shareOPtions.type === 1 && shareOPtions.provider === 'qq'){//如果是分享文字到qq,则必须加上href和title
					shareOPtions.href = "http://uniapp.dcloud.io";
					shareOPtions.title = "欢迎体验uniapp";
				}
H
hulinNeil 已提交
208 209
				uni.share(shareOPtions);
			},
X
update  
xiaoyucoding 已提交
210 211 212 213
			radioChange(e){
				console.log("type:" + e.detail.value);
				this.shareType = parseInt(e.detail.value);
			},
H
hulinNeil 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					sizeType: ['compressed', 'original'],
					success: (res) => {
						this.image = res.tempFilePaths[0];
					}
				})
			},
			compress(){//压缩图片 图文分享要求分享图片大小不能超过20Kb
				console.log("开始压缩");
				let img = this.image;
				return new Promise((res) => {
					var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
					console.log('after' + localPath);
					// 压缩size
					plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
						entry.file((file) => {// 可通过entry对象操作图片 
							console.log("getFile:" + JSON.stringify(file));
							if(file.size > 20480) {// 压缩后size 大于20Kb
								plus.zip.compressImage({
									src: img,
									dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
									width: '10%',
									height: '10%',
									quality: 1,
									overwrite: true
								}, (event) => {
									console.log('success zip****' + event.size);
									let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
									res(newImg);
								}, function(error) {
									uni.showModal({
										content:"分享图片太大,需要请重新选择图片!",
										showCancel:false
									})
								});
							}
						});
					}, (e) => {
						console.log("Resolve file URL failed: " + e.message);
						uni.showModal({
							content:"分享图片太大,需要请重新选择图片!",
							showCancel:false
						})
					});
				})
			}
		},
		components: {
			pageHead
		}
	}
</script>

<style>
	@import "../../../common/uni.css";

	.textarea-wrp {
		padding: 0 20px;
	}
	.page-section{
		margin-bottom: 20px;
	}
	.textarea {
		border: 2px solid #D8D8D8;
		padding: 10px;
X
update  
xiaoyucoding 已提交
282
		height: 90px;
H
hulinNeil 已提交
283 284 285 286 287 288 289 290 291 292 293
		width: 690px;
	}
	.uni-input{
		border: 2px solid #D8D8D8;
		padding: 0 10px;
		width: 690px;
	}
	.uni-uploader-body {
		display: flex;
		justify-content: center;
	}
X
update  
xiaoyucoding 已提交
294 295 296 297 298 299 300
	radio-group{
		box-sizing: border-box;
		width: 100%;
		padding: 0 30px;
		display: flex;
		justify-content: space-between;
	}
H
hulinNeil 已提交
301
</style>