<template>
	<view class="pic">
		<!-- 头部功能组件 -->
		<cu-loadding :show="show" />
		<!-- header -->
		<cu-custom isBack bgColor="bg-gradual-blue"><view slot="content">海报</view></cu-custom>
		<!-- section -->
		<!-- 富文本 -->
		<rich-text :nodes="value.describe"></rich-text>
		<view class="bg-white text-gray text-center padding-bottom">
			<view v-if="value.name" class="padding-lr-sm padding-tb">{{value.name}},邀请你购买</view>
			<view class="padding-lr-sm padding-bottom">{{value.title}}</view>
			<view style="width: 400rpx;height: 400rpx;margin: 0 auto;">
				<image @click="previewImage(value.wxCode)" style="width: 100%;height: 100%;" mode="aspectFit" :src="value.wxCode" />
			</view>
			<view class="padding-lr-sm">点击保存</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			value:"",
			show: true
		};
	},
	onLoad(options) {
		this.initRequest();
	},
	methods: {
		// 报错
		showErr(content) {
			content = JSON.stringify(content) === '{}' ? '未知错误' : content;
			uni.showModal({
				content,
				showCancel: false,
				success: res => console.log(res)
			});
		},
		async initRequest() {
			try {
				var res = await this.$global.request({
					url: '/customer/activity/poster/',
					method: 'get',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						Authorization: uni.getStorageSync('token')
					},
					data: {
						appid: this.$store.state.userInfo.appid,
						activity_id: this.$store.state.userInfo.activity_id
					}
				});
				res.data.describe=this.$global.richTextChange(res.data.describe);
				res.data.wxCode=this.$global.options.REQUESTURL+res.data.wxCode; 
				res.data.name=this.$store.state.userInfo.name;
				this.value=res.data;
			} catch (err) {
				this.showErr(err);
			} finally {
				this.show = false;
			}
		},
	    // 识别二维码
		previewImage(img){
			uni.previewImage({current:img,urls:[img]});
			this.savefunc(img)
		},
		// 保存图片
		savefunc(img){
			uni.downloadFile({
				url:img,
				success:(res)=>{
					if(res.statusCode===200){
						uni.saveImageToPhotosAlbum({
								filePath:res.tempFilePath,
								success(res){
									uni.showToast({
										icon:"none",
										title:"保存成功"
									})
								},
								fail(err){
									uni.showToast({
										icon:"none",
										title:"保存失败"
									})
								}
							})
						
					}
				}
			})
		}
	}
};
</script>

<style></style>
