<script>
	export default {
		onLaunch() {
			if(!uni.getStorageSync("userinfo")){
				this.wxLogin();
			}
			
			// 缓存没有头像时,获取用户头像
			uni.getStorage({
				key: 'avatar',
				fail: res => {
					uni.getUserInfo({
						provider:'weixin',
						success: res => {
							uni.setStorageSync('avatar', res.userInfo.avatarUrl)
						}
					})
				}
			})
		},
		
		methods: {
			wxLogin(){
				uni.login({
					provider: 'weixin',
					success: res => {
						if(res.code) {
							console.log(res.code)
							this.getOpenId(res.code)
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			
			async getOpenId(code) {
				const { data:res } = await this.$http('/getOpenId',{code:code})
				if(res.code === 0){
					console.log(res)
					uni.setStorageSync("sessionKey", res.sessionKey)
					uni.setStorageSync("openId", res.openId);
				}
			}
		}
	}
</script>

<style lang="scss">
	@import "@/static/iconfont.css";
	@import "@/uni_modules/uview-ui/index.scss";
	body {
		word-break: break-all;
		// word-wrap: break-word;
		
		.i-spot {
			margin: 2px;
			width: 16px;
			height: 16px;
			border-radius: 50%;
			background: radial-gradient(#0f0, hsl(90, 99%, 99%));
		}
		.warning {
			background: radial-gradient(hsl(33, 88%, 55%), hsl(60, 99%, 99%));
		}
		.danger {
			background: radial-gradient(#f00, hsl(0, 99%, 99%));
		}
		.info {
			background: radial-gradient(#999, hsl(0, 0%, 88%));
		}
		
		.i-pin {
			position: absolute;
			top: 1vw;
			right: 4vw;
			font-size: 8vw;
			color: hsl(200, 88%, 55%);
			text-shadow: 1px 0 1px #369;
		}
	}
</style>
