<script>
import md5 from '@/common/md5.js';
export default {
  globalData: {
    //shareUrl: "https://heater.xazhima.com/", 测试地址
	shareUrl:"https://h.xayuanju.com/",
    globalTimestamp: Date.now().toString(),
    secret: "Heater_weichat_app_zhima",
    selectedIndex:0,
	isSider:false,
	user_id: '',
	open_id:'',
	//open_id:"oX46e5ImwFndnH442HW6Yt7W9Mck",//测试使用 替换正确appId后默认为空
	user_status:'',
	user_name:'',
	user_phone:'',
	user_headUrl:'',
	globalAuth:false,
	isAuth : false,
	session_key: '',
	contact_name:'',
	company_name:'',
	company_logo:'',
	isAndroid:false,
	nowTime:'',
	statusObj:{
			  '0':'未缴费',
			  '1':'已缴费',
			  '2':'空置已缴费',
			  '3':'空置转全额'
	},
	statusColor:{
			  '0':'#D9001B',
			  '1':'#70B603',
			  '2':'#4B7902',
			  '3':'#BFBF00',
	}
  },
  onLaunch: function () {
    console.log("App Launch");
	// let equType = uni.getSystemInfoSync().platform;
	//  switch(equType){
	//     case 'android':
	//        console.log('运行Android上')
	// 		setTimeout(()=>{
	// 			getApp().globalData.isAndroid = true 
	// 		},100)
	//        break;
	//     case 'ios':
	//        console.log('运行iOS上');
	//        break;
	//     default:
	//        console.log('运行在开发者工具上')
	//        break;
	// }
	this.loginLoad();
  },
  onShow: function () {
    console.log("App Show");
    uni.hideTabBar({})
  },
  onHide: function () {
    console.log("App Hide");
  },
  methods:{
	  getNowTime(){
			let time = new Date(Date.now());
		    const y = time.getFullYear();
		    const m =
		      time.getMonth() + 1 < 10
		        ? "0" + (time.getMonth() + 1)
		        : time.getMonth() + 1;
		    const d = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
		    // const h = time.getHours()
		    // const mm = time.getMinutes();
		    // const s = time.getSeconds();
		    return y + "-" + m + "-" + d;
	  },
	  loginLoad(){
	  	let that = this;
	  	uni.login({
	  		success(res) {
	  			that.loginRequest(res.code)
	  		}
	  	})
	  },
	  makeApiUrl(method,source,action){
		  let timestamp = Date.now().toString();
		  let md5Sign = md5("method="+method+"&timestamp="+timestamp+"&secret="+getApp().globalData.secret);
		  let url = getApp().globalData.shareUrl+'api/api.php'+'?method='+method+'&source='+source+'&action='+action+'&timestamp='+timestamp +'&sign='+md5Sign;
		  return url;
	  },
	  MD5(str){
		let md5str = md5(str);
		return md5str.toUpperCase();  
	  },
	  randomStr(len){
		  let str = '';
		  let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		  for (let i = 0; i < len; i++){
			  str += chars.charAt(Math.random() * chars.length);
		  }
		  return str;
	  },
	  loginRequest(codeRes){
	  	let url = getApp().makeApiUrl("user",'user','login');
		uni.request({
	  		url:url,
	  		method: 'POST',
	  		header: {
	  			'content-type': 'application/x-www-form-urlencoded'
	  		},
	  		data: {
	  			code:codeRes
	  		},
	  		success: (res) => {
	  			console.log(res)
	  			if(res.data.code === 200){
	  				getApp().globalData.session_key = res.data.data.session_key;
	  				getApp().globalData.open_id = res.data.data.openid;
					getApp().globalData.user_status = res.data.data.status;
					getApp().globalData.user_phone = res.data.data.phone;
					getApp().globalData.nowTime = this.getNowTime();
					if (res.data.data.phone === ''){
						//必须有手机号才算登录成功，系统才可用。
						getApp().globalData.isAuth = false;
					}
					else{
						getApp().globalData.isAuth = true;
					}
					uni.setStorageSync('openId', res.data.data.openid);
				//	uni.hideLoading() 
	  			}
	  		},
	  		fail: () => {
	  			console.log("连接失败");
	  		}
	  	});
	  },
	  getPhoneNumber(e){
	  	 if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
	  	 console.log(e.detail);
	  	  getApp().phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key);
		  return true;
	  	} else { //
	  	  return false;
	  	}
	  },
	  phoneRequest(myIv,myEncryptedData,sKey){
	  	let url = getApp().makeApiUrl('user','user','phone');
		uni.request({
	  		url:url,
	  		method: 'POST',
	  		header: {
	  			'content-type': 'application/x-www-form-urlencoded'
	  		},
	  		data: {
	  			iv:myIv,
	  			sessionKey:sKey,
	  			encryptedData:myEncryptedData,
	  			openId:getApp().globalData.open_id
	  		},
	  		success: (res) => {
	  			console.log(res)
	  			if(res.data.code === 200){
	  				getApp().globalData.user_phone = res.data.data;
					if (res.data.data != ""){
						getApp().globalData.isAuth = true;
					}
	  			}
	  		},
	  		fail: () => {
	  			console.log("连接失败");
	  		}
	  	});
	  },
  }
};
</script>

<style lang="scss">
/*每个页面公共css */
@import url("/components/gaoyia-parse/parse.css");
@import '@/common/uni.css';
page {
	height: 100%;
}
.display-flex-start {
  display: flex;
  align-items: center;
}
.display-flex-end {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.display-between {
  display: flex;
  justify-content: space-between;
}
.display-around {
  display: flex;
  justify-content: space-around;
}
.display-between-column {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.display-around-column {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
.display-wrap {
  display: flex;
  flex-wrap: wrap;
}
.items-center {
  align-items: center;
}
// .uni-popup__wrapper.uni-custom.center .uni-popup__wrapper-box {
//   max-height: 65%!important;
// }
</style>
