<template>
	<view class="container">
		<button @click="handleLogin">登录获取code</button>
		<button @click="handleToken">获取access_token</button>
		<button @click="handleUserInfo">获取用户信息</button>
		<button @click="handleClick">订阅</button>
		<button @click="handleClick2">设置</button>
		<button @click="handleGo">跳转</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 订单进度模版id
			templateOrder: 'ERU1ZY9IqwNkDxWyFJvo5VSE7ua-wey3SqhZgjqLDtU',
			// 小程序Id
			appid: 'wx2fc3f45732fae5d3',
			// 小程序密钥
			screct: '7eee4a49a4470a77f9222995e8511547',
			access_token: '',
			code: ''
		}
	},
	onLoad() {
		uni.switchTab({
			url: '/pages/home/home'
		})
	},
	methods: {
		handleGo() {
			uni.navigateTo({
				url: '/pages/shouquan/shouquan'
			})
		},
		handleLogin() {
			uni.login({
				provider: 'weixin', //使用微信登录
				success: (loginRes) => {
					console.log(loginRes)
					this.code = loginRes.code
				}
			})
		},
		handleToken() {
			uni.request({
				url: 'https://api.weixin.qq.com/sns/jscode2session',
				data: {
					grant_type: 'authorization_code',
					appid: this.appid,
					appsecret: this.screct,
					js_code: this.code
				},
				success: (res) => {
					console.log(res)
					if (res.statusCode === 200) {
						// this.access_token = res.data.
					}
				}
			})
		},

		handleUserInfo() {
			uni.getUserProfile({
				desc: '用于完善用户资料',
				lang: 'zh_CN',
				success: (res) => {
					console.log('res', res)
				}
			})
		},
		// 订阅方法
		handleClick() {
			if (uni.requestSubscribeMessage) {
				uni.requestSubscribeMessage({
					tmplIds: [this.templateOrder],
					success: (res) => {
						console.log(1)
						console.log(res)
						for (let key in res) {
							console.log(key)
							if (res[key] === 'accept') {
								uni.showToast({
									title: '订阅成功',
									icon: 'success',
									mask: true
								})
							} else if (res[key] === 'reject') {
								uni.showToast({
									title: '订阅失败',
									icon: 'none',
									mask: true
								})
							}
						}
					},
					fail: (res) => {
						console.log(2)
						console.log(res)
					}
				})
			} else {
				uni.showToast({
					title: '请更新您的微信版本来获取订阅消息功能',
					icon: 'none'
				})
			}
		},
		// 设置方法
		handleClick2() {
			// 获取用户订阅消息设置
			uni.getSetting({
				withSubscriptions: true,
				success: (res) => {
					console.log(res)
					console.log(res.subscriptionsSetting)
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	height: 100vh;
	background-color: #fff;
}
</style>