define(['zepto','medtap'],function($,medtap){
	var my = {
		wechatId:medtap.getRequest('wechatId'),
		init:function(){
			my.getToken();
		},
		getToken: function() {
			localStorage.wxLungToken = '';
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/auth/token',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					wechatId: my.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var token = res.content.token;
						localStorage.setItem('wxLungToken', token);
						my.checkUser();
					} else {
		
					}
				},
				error: function(res) {
					console.log(res.resultDesc);
				}
			})
		},
		checkUser: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/user/getWechatUser',
				type: 'GET',
				async: false,
				data: {
					wechatId: my.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					var info = res.content.wechatUser;
					if (info.hasOwnProperty('userId')) {
						my.getUserDetail();
					} else {
						window.location.replace('../login/login.html?wechatId=' + my.wechatId + '&route=my');
					}
				}
			})
		},
		getUserDetail:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/getUserDetail',
				type:'GET',
				async:false,
				data:{
					
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var userInfo = res.content.userInfo;
						/* $('#username').val(userInfo.username);
						$('#sex').val(userInfo.sex == 'M' ? '男' : '女');
						$('#mobile').val(userInfo.mobile);
						$('#tel').val(userInfo.telephone || '');
						$('#wechat').val(userInfo.wechatNumber || '');
						$('#email').val(userInfo.email || '');
						if(userInfo.hasOwnProperty('healthType')){
							if(userInfo.healthType.child == 'HE1'){
								$('#healthType').val('健康的咨询者');
							}else if(userInfo.healthType.child == 'HE2'){
								$('#healthType').val('患者家属');
							}else if(userInfo.healthType.child == 'HE3'){
								$('#healthType').val('患者本人');
							}
						} */
						if (!userInfo.hasOwnProperty('healthType') || !userInfo['username'] || !userInfo['sex'] || !userInfo['telephone']) {
							window.location.replace('../login/pages/basicUserInfo.html?wechatId=' + my.wechatId  + '&operationType=12&route=my');
						} else {
							$('#username').val(userInfo.username);
							$('#sex').val(userInfo.sex == 'M' ? '男' : '女');
							$('#mobile').val(userInfo.mobile);
							$('#tel').val(userInfo.telephone || '');
							$('#wechat').val(userInfo.wechatNumber || '');
							$('#email').val(userInfo.email || '');
							if(userInfo.hasOwnProperty('healthType')){
								if(userInfo.healthType.child == 'HE1'){
									$('#healthType').val('健康的咨询者');
								}else if(userInfo.healthType.child == 'HE2'){
									$('#healthType').val('患者家属');
								}else if(userInfo.healthType.child == 'HE3'){
									$('#healthType').val('患者本人');
								}
							}
						}
	
					}
				}
			})
		}
	}
	my.init();
})