<template>
	<view class="page">
		<button style="padding: 0;margin-left: 0; text-align: left;" class="nullBtn"  v-if='taskid == 487'>
			<image class="posterImg" src="https://ckbapi.quwanzhi.com/upload/img/ckb.png" mode="widthFix"></image>
		</button>
		<button style="padding: 0;margin-left: 0; text-align: left;" class="nullBtn" open-type="getPhoneNumber" v-else
			@getphonenumber="getPhoneNumber">
			<image class="posterImg" :src="poster.sUrl" mode="widthFix"></image>
		</button>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request';
	export default {
		data() {
			return {
				taskid: '',
				token: "test",
				task: null,
				poster: null,
				cid: '',
			}
		},
		onLoad(options) {
			let scene = options.scene;
			let taskId = options.id;
			let cid = options.cid;

			console.log('页面参数:', { scene, taskId, cid });

			// 小程序端解析 scene 参数
			// #ifdef MP-WEIXIN
			if (scene != undefined) {
				// 先对 scene 进行 URL 解码（处理 %2C 等编码字符）
				try {
					scene = decodeURIComponent(scene);
				} catch (e) {
					console.warn('scene 解码失败，使用原始值:', e);
				}
				
				// 如果 scene 包含逗号，分割成 [taskId, channelId]
				if (scene.includes(',')) {
					const parts = scene.split(',');
					this.taskid = parts[0] || '';
					this.cid = parts[1] || '0';
				} else {
					// 如果不包含逗号，只有 taskId，channelId 为 0
					this.taskid = scene;
					this.cid = '0';
				}
			} else if (taskId != undefined) {
				// 兼容旧参数：使用 options.id
				this.taskid = taskId;
				this.cid = cid || '0';
			} else {
				// 默认值
				this.taskid = 487;
				this.cid = '0';
			}
			// #endif

			// H5 端兼容旧参数
			// #ifdef H5
			if (taskId != undefined) {
				this.taskid = taskId;
				this.cid = cid || '0';
			} else if (scene != undefined) {
				// H5 也可能有 scene 参数（从分享链接等）
				// 先对 scene 进行 URL 解码
				try {
					scene = decodeURIComponent(scene);
				} catch (e) {
					console.warn('scene 解码失败，使用原始值:', e);
				}
				
				if (scene.includes(',')) {
					const parts = scene.split(',');
					this.taskid = parts[0] || '';
					this.cid = parts[1] || '0';
				} else {
					this.taskid = scene;
					this.cid = '0';
				}
			} else {
				this.taskid = 487;
				this.cid = '0';
			}
			// #endif

			console.log('解析结果:', { taskid: this.taskid, cid: this.cid });

			this.getPosterData()
		},
		methods: {
			getPosterData() {
				let that = this;
				uni.request({
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
						'token': that.token
					},
					url: that.$config.apiUrl2 + '/v1/frontend/business/poster/getone',
					data: {
						id: that.taskid,
					},
					success: (res) => {
						if (res.data.code == 200) {
						
							that.poster = res.data.data.poster
							that.task = res.data.data.task

							//修改标题
							uni.setNavigationBarTitle({
								title: ""
							});
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none',
								duration: 2000
							});
						}
					}
				});
			},
			onShareAppMessage() {
				return {
					title: '',
					path: '/pages/poster/index2?id=' + this.taskid,
					imageUrl: poster.sUrl // 可以省略不写，这样会默认截图当前页面作为分享图片
				};
			},
			// 获取用户手机号
			getPhoneNumber(res) {
				let that = this;
				if (res.detail.errMsg == "getPhoneNumber:ok") {
					uni.showLoading({
						title: '请求中...',
						mask: true
					})
					uni.request({
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息
							'token': that.token
						},
						url: that.$config.apiUrl2 + '/v1/frontend/business/poster/decryptphone',
						data: {
							id: that.taskid,
							cid: that.cid,
							code: res.detail.code
						},
						success: (res) => {
							uni.hideLoading();
							if (res.data.code == 200) {
								uni.showModal({
									title: '提示',
									content: that.task.sTip,
									success: function(res) {
										console.log(res)
									}
								})
							} else {
								uni.showToast({
									title: res.data.message,
									icon: 'none',
									duration: 2000
								});
							}
						}
					});

				}
			},

		}
	}
</script>

<style>
	.page {
		width: 100vw;
		height: 100vh;
	}

	.posterImg {
		width: 100%;
	}
</style>