<template>
	<view class="pages">
		<view class="myinfo-wrap">
			<view class="myinfo">
				<view class="myinfo-photo">
					<image class="myinfo-photo-img" :src="user.icon" mode=""></image>
				</view>
				<view class="myinfo-text">
					<view class="myinfo-text-name">{{user.nickname || '登录/注册'}}</view>
					<view class="myinfo-text-identity"><!-- {{company.type}} --><text v-if="company.compName">供应商 | {{company.compName}}</text></view>
				</view>
			</view>		
			<view class="myinfo-focus">
				<view class="myinfo-focus-item myinfo-focus-myfocus">
					<navigator url="/pages/usercenter/myfocus/myfocus">
						<view class="number">{{pre.want||0}}</view>
					</navigator>
					<view class="text">我有意向的</view>
				</view>
				<view class="myinfo-focus-item myinfo-focus-focusme">
					<navigator url="/pages/usercenter/focusme/focusme">
						<view class="number">{{pre.own||0}}</view>
					</navigator>
					<view class="text">有意向我的</view>
				</view>
			</view>
		</view>		
		<!-- myinfo-wrap end -->
		<view class="wrap">
			<navigator url="/pages/usercenter/mypublish/mypublish">
				<view class="select-bar">
					<svg class="icon select-bar-icon" aria-hidden="true">
						<use xlink:href="#iconwodedingdan"></use>
					</svg>
					<view class="select-bar-text">我的信息发布</view>
					<view class="select-bar-arrow"></view>
				</view>
			</navigator>
			<navigator url="/pages/usercenter/authentication/authentication" v-if="!company.compName">
				<view class="select-bar">
					<svg class="icon select-bar-icon" aria-hidden="true">
						<use xlink:href="#iconwodetuandui"></use>
					</svg>
					<view class="select-bar-text">企业认证</view>
					<view class="select-bar-arrow"></view>
				</view>
			</navigator>
			<navigator url="/pages/usercenter/feedback/feedback">
				<view class="select-bar">
					<svg class="icon select-bar-icon" aria-hidden="true">
						<use xlink:href="#iconbangzhu"></use>
					</svg>
					<view class="select-bar-text">帮助反馈</view>
					<view class="select-bar-arrow"></view>
				</view>
			</navigator>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	export default {
		data() {
			return {
				params:{
					token:'',
					tokenhead:'',
				},
				info:'',
				name:'',
				usertype:'',
				usercompany:'',
				myfocus:0,
				focusme:1,
				pre:[],
				user:[],
				company:[],
				wxuser:[],
				thispage:'',
				
			}
		},
		onShow() {
			this.thispage = `/pages/usercenter/usercenter`;
			let serf = this;
			let locationLocaturl = window.location.search;
			function getUrlParams(url) {
				if (decodeURIComponent(url).indexOf('?') > -1) {
					var result = [];
					var urlParamsArr = decodeURIComponent(url).split('?');
					urlParamsArr.shift();
					let newUrlParamsArr = urlParamsArr.join('&').split('&');
					for (var i = 0; i < newUrlParamsArr.length; i++) {
						var paramKey = newUrlParamsArr[i].split('=')[0];
						var paramValue = newUrlParamsArr[i].split('=')[1];
						result.push({
							key: paramKey,
							value: paramValue
						});
					}
					return result;
				} else {
					console.log('该URL中不含参数');
				}
			};
			
			let locationArr = getUrlParams(locationLocaturl);
			if(!locationArr){
				let loginurl = `${this.config.apiBaseurl}/wechat/h5/authorize?returnUrl=/pages/usercenter/usercenter`
				window.location.replace(loginurl);
				return
			};
			let itemtoken = locationArr.find(function(locationArr) {
				return locationArr.key === 'token';
			});
			let itemtokenHead = locationArr.find(function(locationArr) {
				return locationArr.key === 'tokenHead';
			});
			if (itemtoken) {
				this.params.token = itemtoken.value;				
			}
			if (itemtokenHead) {
				this.params.tokenhead = itemtokenHead.value;				
			}
			if(this.params.tokenhead&&this.params.token){
				let thetoken = this.params.tokenhead+this.params.token;
				this.$api.http.get(this.config.apiBaseurl+'/wechat/h5/info', {header: {Authorization:thetoken}}).then(res => {					
					let userInfo = res.data;
					this.info = userInfo;
					this.pre = this.info.data.pre || '';
					this.user = this.info.data.user || '';
					this.company = this.info.data.company || '';
					this.wxuser = this.info.data.wxUsers;
					console.log('this.info',JSON.parse(JSON.stringify(this.info)));
					userInfo.accessToken = this.params.token;
					userInfo.tokenhead = this.params.tokenhead;
					this.setLogin(userInfo);
				}).catch(err => {
					if(err.data.message =='暂未登录或token已经过期'){
						let loginurl = `${this.config.apiBaseurl}/wechat/h5/authorize?returnUrl=${this.thispage}`
						console.log('loginurl',loginurl);
						window.location.replace(loginurl);
						return
					}
					console.log(err)
				});
			};
			//获取会员信息
			let thetoken = this.params.tokenhead + this.params.token;
			// this.$api.http.get(this.config.apiBaseurl+'/sso/info',{header: {Authorization:thetoken}}).then(res => {				
			// 	this.userinfo = res.data.data;
			// 	console.log('this.userinfo',JSON.parse(JSON.stringify(this.userinfo)));
			// }).catch(err => {			
			// });
		},
		onLoad() {
			
		},
		methods: {
			...mapMutations(['setLogin']),

		},
		// computed:{
		// 	...mapState(['hasLogin','userInfo'])
		// }
	}
</script>

<style scoped>
	@import url("./usercenter.css");
</style>