<template>
	<view style="height:calc(100vh - 96px)">
		<view style="padding: 20rpx;">
			<view style="width: 100%;height: 20vh;border-radius: 10rpx;box-shadow: 0 0px 20px #b1b1b1;background-color: #fff;">
				<view style="display: flex;padding: 60rpx 40rpx 0;">
					<u-avatar src="/static/logo.png"></u-avatar>
					<view style="margin-left: 40rpx;;">
						<view style="font-size: 34rpx;font-weight: bold">
							{{userInfo.nickName}}
						</view>
						<view style="font-size: 30rpx;margin-top: 10rpx;">
							{{userInfo.dept.deptName}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="margin-top: 40rpx;background-color: #fff;">
			<u-cell-group>
				<u-cell icon="reload" title="检查更新" :value="version" isLink :iconStyle="{color:'#409EFF'}" @click="needUpdateApp(true)"></u-cell>
				<!-- <view style="height: 40rpx;background-color: #f5f5f5;"></view> -->
				<u-cell icon="arrow-rightward" title="退出登录" isLink :iconStyle="{color:'#409EFF'}" @click="out"></u-cell>
			</u-cell-group>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:uni.getStorageSync('userInfo'),
				version:null
			}
		},
		onLoad(){
			this.needUpdateApp(false)
		},
		methods: {
			out(){
				uni.showModal({
					title:"提示",
					content:"是否要退出",
					success(res){
						if(res.confirm){
							uni.clearStorageSync()
							uni.reLaunch({
								url:"/pages/login/login"
							})
						}
					}
				})
			},
			//升级
			needUpdateApp(i) {
				var _this = this;
				plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
					if(i==true){
						uni.showLoading({
							title:"检查中...",
							mask:true
						})
						uni.$u.http.post('/app/version/advance/'+wgtinfo.versionCode).then(res => {
							if(res.code==200){
								if(res.data != undefined){
									uni.hideLoading()
									uni.showModal({
										title:"提示",
										content:"有新版本是否进行更新",
										success(qwe){
											if(qwe.confirm){
												_this.upgradeApp(getApp().globalData.updateUrl+res.data.url)
											}
										}
									})
								}else{
									uni.hideLoading()
									uni.showToast({
										title:"当前为最新版本",
										icon:"none"
									})
								}
							}
						}).catch(res=>{
							uni.hideLoading()
						})
					}else{
						_this.version = wgtinfo.version
					}
				})
			},
			upgradeApp(url) {
				uni.showLoading({
					title: '更新中……'
				});
				uni.downloadFile({
					url: url,
					success: downloadResult => {
						if (downloadResult.statusCode === 200) {
							try {
								var filePath = downloadResult.tempFilePath;
								var ext = filePath.substring(
									filePath.lastIndexOf('.') + 1
								);
								plus.runtime.install(
									downloadResult.tempFilePath, {},
									function() {
										uni.hideLoading()
										plus.runtime.restart();
									},
									function(e) {
										uni.hideLoading()
										uni.showToast({
											title:"更新失败",
											icon:"none"
										})
									}
								);
							} catch (e) {
								uni.hideLoading()
							}
						}
					},
					fail: res => {
						uni.hideLoading()
						uni.showToast({
							title:"下载失败",
							icon:'none'
						})
					}
				});
			}
		}
	}
</script>

<style>

</style>
