<!--
 * @Author: euzhi
 * @Description: 帮助使用
-->
<template>
	<view class="appCotent">
		<div class="hlep">
			<!-- <u-cell-group>
        <u-cell title="设置消息订阅" :isLink="true" @click="openMsg()"></u-cell>
      </u-cell-group> -->
			<image v-if="image" :src="image" />
			<image v-else src="@/static/my/help.png" />
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image: '',
				//是否订阅消息
				is_authorization: uni.getStorageSync('is_authorization'),
			}
		},
		methods: {
			getHelpInfo() {
				this.$http.request({
					url: '/app/user/help',
					method: 'get',
					data: {}
				}).then(res => {
					if (res.imgPath != '') {
						this.image = res.imgPath
					}
				})
			},
			//获取消息订阅
			openMsg() {
				var that = this;
				let nc_templateid = that.nc_templateid;
				// 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
				wx.getSetting({
					withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
					success(res) {
						if (that.is_authorization) {

						} else {
							if (res.subscriptionsSetting[nc_templateid] == "reject") {
								//用户点击了“总是保持以上，不再询问”
								wx.showModal({
									title: "温馨提示",
									content: "检测到您没有开启订阅消息的权限，是否去设置？",
									success: function(modal) {
										if (modal.confirm) {
											// 点击肯定
											wx.openSetting({
												withSubscriptions: true,
											});
											that.is_authorization = true;
											uni.setStorageSync("is_authorization", true);
										} else {
											uni.showToast({
												title: "您已拒绝订阅消息授权，无法接收通知",
												icon: "error",
											});
											that.is_authorization = false;
										}
									},
								});
							} else {
								// 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
								// var templateid = that.setting.templateid.map(item => item.tempid)；
								let tmpid = [nc_templateid];
								uni.requestSubscribeMessage({
									tmplIds: tmpid,
									success(res) {
										if (res[tmpid[0]] == "accept") {
											uni.showToast({
												title: "订阅成功",
												icon: "none",
											});
											that.is_authorization = true;
											uni.setStorageSync("is_authorization", true);
										} else {
											uni.showToast({
												title: "您拒绝订阅消息授权，无法接收通知",
												icon: "none",
											});
											that.is_authorization = false;
										}
									},
									fail: (res) => {
										wx.showModal({
											title: "温馨提示",
											content: "检测到您没有开启订阅消息的权限，是否去设置？",
											success: function(modal) {
												if (modal.confirm) {
													// 点击肯定
													wx.openSetting({
														withSubscriptions: true,
													});
													that.is_authorization = true;
													uni.setStorageSync("is_authorization",
														true);
												} else {
													uni.showToast({
														title: "您已拒绝订阅消息授权，无法接收通知",
														icon: "error",
													});
													that.is_authorization = false;
												}
											},
										});
									},
								});
							}
						}
					},
				});
			},
			//改变消息订阅
			changeMessages() {
				wx.openSetting({
					withSubscriptions: true,
				});
				// if(!this.is_authorization){
				// 	wx.openSetting({
				// 		withSubscriptions: true,
				// 	});
				// 	this.is_authorization = false;
				// 	uni.setStorageSync("is_authorization", false);
				// }else{
				// 	wx.openSetting({
				// 		withSubscriptions: true,
				// 	});
				// 	this.is_authorization = false;
				// 	uni.setStorageSync("is_authorization", false);
				// }

			},
		},
		onShow() {
			this.getHelpInfo()
			//this.openMsg()
		},
	}
</script>

<style lang="scss" scoped>
	.hlep {
		padding: 0rpx 0;
		height: 3260rpx;

		image {
			height: 100%;
			width: 100%;
		}
	}
</style>
