<script>
	import {
		mapMutations
	} from 'vuex'
	import {
		version
	} from './package.json'
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';

	export default {
		onLaunch: function() {
			// #ifdef H5
			console.log(
				`%c hello uniapp %c v${version} `,
				'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff',
				'background:#007aff ;padding: 1px; border-radius: 0 3px 3px 0;  color: #fff; font-weight: bold;'
			)
			// #endif
			// 线上示例使用
			// console.log('%c uni-app官方团队诚邀优秀前端工程师加盟，一起打造更卓越的uni-app & uniCloud，欢迎投递简历到 hr2013@dcloud.io', 'color: red');
			console.log('App Launch');
			// #ifdef APP-PLUS
			// App平台检测升级，服务端代码是通过uniCloud的云函数实现的，详情可参考：https://ext.dcloud.net.cn/plugin?id=4542
			if (plus.runtime.appid !== 'HBuilder') { // 真机运行不需要检查更新，真机运行时appid固定为'HBuilder'，这是调试基座的appid
				checkUpdate()
			}

			// 一键登录预登陆，可以显著提高登录速度
			uni.preLogin({
				provider: 'univerify',
				success: (res) => {
					// 成功
					this.setUniverifyErrorMsg();
					console.log("preLogin success: ", res);
				},
				fail: (res) => {
					this.setUniverifyLogin(false);
					this.setUniverifyErrorMsg(res.errMsg);
					// 失败
					console.log("preLogin fail res: ", res);
				}
			})
			// #endif
			// #ifdef MP-WEIXIN
			let openid = uni.getStorageSync('openId');
			if (!openid) {
				this.wxLogin();
			} else {
				this.globalData.openId = openid;
				this.getUserInfo();
			}
			// #endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			openId: '',
			test: '',
			// baseUrl: "http://192.168.3.32:80",
			// baseUrl: "http://192.168.1.106:80",
			// baseUrl: "http://47.104.224.41:80",//测试环境
			// imgPrefix: "http://47.104.224.41:9000/etriphome/appresource/image/", //图片资源
			baseUrl: "https://api.etriphome.com/api",//测试环境
			imgPrefix: "https://api.etriphome.com:9000/etriphome/appresource/image/", //图片资源
		},
		methods: {
			...mapMutations(['setUniverifyErrorMsg', 'setUniverifyLogin']),
			//获取用户信息
			getUserInfo() {
				uni.request({
					url: this.globalData.baseUrl + '/weChatUser/getWeChatUser?openId=' + this.globalData.openId,
					method: 'GET',
					success: (res) => {
						// console.log(res)
						if (res.data.code == 200) {
							if (JSON.stringify(res.data.data) != '{}') {
								uni.setStorageSync('userInfo', JSON.stringify(res.data.data));
								this.doLogin(res.data.data.username, res.data.data.phone);
							}
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					}
				})
			},
			//登录
			doLogin(username, phone) {
				uni.showLoading({
					title: '登录中',
					mask: true
				});
				uni.request({
					url: this.globalData.baseUrl + '/blade-auth/getToken',
					method: 'POST',
					data: {
						openId: this.globalData.openId,
						phone: phone
					},
					header: {
						'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'
					},
					success: (res) => {
						if (res.data.code == 200) {
							uni.setStorageSync('token', res.data.data.access_token);
							setTimeout(() => {
								console.log('重新渲染')
								wx.reLaunch({
									url: '/pages/pages_zhentou/homePage/homePage'
								})
							}, 500)
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
						}
					},
					complete: () => {
						uni.hideLoading();
					}
				})
			},
			//微信登陆
			wxLogin() {
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: (event) => {
						const {
							code
						} = event;
						//客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
						uni.request({
							url: this.globalData.baseUrl + '/app/login', //仅为示例，并非真实接口地址。
							method: 'POST',
							data: {
								code: event.code
							},
							success: (res) => {
								if (res.data.code == 200) {
									this.globalData.openId = res.data.data.openid;
									uni.setStorageSync('openId', res.data.data.openid);
									if (res.data.data.userInfo) {
										uni.setStorageSync('userInfo', JSON.stringify(res.data.data.userInfo));
										if(res.data.data.userInfo.phone){
											this.doLogin(res.data.data.userInfo.username, res.data.data.userInfo.phone);
										}
									}
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
								}
							},
							fail: (err) => {
								console.log('wxlogin',err)
							}
						});
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
						console.log(err, '授权登录失败')
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-PLUS-NVUE */
	/* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
	@import './common/uni.css';
	// @import '@/static/customicons.css';

	/* H5 兼容 pc 所需 */
	/* #ifdef H5 */
	@media screen and (min-width: 768px) {
		body {
			overflow-y: scroll;
		}
	}

	/* 顶栏通栏样式 */
	/* .uni-top-window {
	    left: 0;
	    right: 0;
	} */

	uni-page-body {
		background-color: #F5F5F5 !important;
		min-height: 100% !important;
		height: auto !important;
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #fff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}

	/* #endif */

	/* 以下样式用于 hello uni-app 演示所需 */
	page {
		background-color: #efeff4;
		height: 100%;
		font-size: 28rpx;
		/* line-height: 1.8; */
	}

	.fix-pc-padding {
		padding: 0 50px;
	}

	.uni-header-logo {
		padding: 30rpx;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 10rpx;
	}

	.uni-header-image {
		width: 100px;
		height: 100px;
	}

	.uni-hello-text {
		color: #7A7E83;
	}

	.uni-hello-addfile {
		text-align: center;
		line-height: 300rpx;
		background: #FFF;
		padding: 50rpx;
		margin-top: 10px;
		font-size: 38rpx;
		color: #808080;
	}

	/* #endif*/
</style>