<template>
	<view :style="'background-color: '+theme.bg+';color: '+theme.text" style="height: 100vh;">
		<myNav :title="i18n.mine.about.title"></myNav>
		<view class="">
			<view class="padding flex justify-between align-center" @click="gotoNext(item.path)"
				v-for="(item,index) in menuList">
				<view class="font-30">{{item.name}}</view>
				<view class="flex align-center">
					<text v-if="item.path=='gengxin'" class="font-26 nummedium-font">V{{version}}</text>
					<view class="cuIcon-right"></view>
				</view>
			</view>
			
			<!-- <view class="padding flex justify-between align-center" @click="clearCache">
				<view class="font-30 color-333333">清理缓存</view>
				<view class="flex align-center">
					<text class="font-26 nummedium-font color-333333 margin-r-10">{{fileSizeString}}</text>
				</view>
			</view> -->
			
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	import APPUpdate, {
		getCurrentNo
	} from '@/components/APPUpdate/js_sdk/appUpdate';
	// #endif
	export default {
		data() {
			return {
				fileSizeString: '0B',

				menuList: [{
						name: "关于我们",
						path: "/pages/my/aboutUs/women?title=about_us"
					}, {
						name: "用户条款",
						path: "/pages/my/aboutUs/women?title=user_item"
					}, {
						name: "隐私条约",
						path: "/pages/my/aboutUs/women?title=privacy_treaty"
					}, 
					// {
					// 	name: "官网",
					// 	path: "guanwang"
					// },
					// #ifdef APP-PLUS
					{
						name: "版本",
						path: "gengxin"
					},
					// #endif
				],
				GWaddress: '',

				version: "1.0.0" ,// 版本号
				theme:'',
			}
		},
		onLoad() {
			// #ifdef APP-PLUS
			getCurrentNo(res => {
				// 进页面获取当前APP版本号（用于页面显示）
				this.version = res.versionName;
			});
			// #endif
		},
		onShow() {
			if (uni.getStorageSync("theme") == "dark") {
				this.theme = this.$store.state.darkTheme
			}else{
				this.theme = this.$store.state.lightTheme
			}
			// this.formatSize()
			this.menuList[0].name = this.i18n.mine.about.title
			this.menuList[1].name = this.i18n.mine.about.list1
			this.menuList[2].name = this.i18n.mine.about.list2
			// this.menuList[3].name = this.i18n.mine.about.list3
			// #ifdef APP-PLUS
			this.menuList[3].name = this.i18n.mine.about.list4
			// #endif
		},
		computed: {
			i18n() {
				return this.$t('message');
			},
		},
		methods: {
			// 计算缓存大小
			formatSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0 B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + " B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + " KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + " MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + " GB";
					}
				});
			},
			clearCache() {
				let that = this;
				let os = plus.os.name;
				uni.showLoading({
					title: '清理中',
					mask:true
				})
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							// console.log('文件路径读取失败')
							uni.showToast({
								title: '文件路径读取失败',
								icon:'error',
								duration: 2000
							});
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}
			},
			gotoNext(url) {
				if (url == 'gengxin') {
					this.onAPPUpdate()
				} else if (url == 'guanwang') {
					this.getGW()
				} else {
					this.router.push(url)
				}
			},
			getGW() {
				this.post('/publics/get_common_data').then(res => {
					console.log(res)
					if (res.code == 1) {
						this.GWaddress = res.data.web_url
						if (res.data.web_url) {
							// #ifdef APP-PLUS
							plus.runtime.openURL(this.GWaddress)
							// #endif
							// #ifdef H5
							window.location.replace(this.GWaddress)
							// #endif
							// window.location.replace('http://www.baidu.com')
						} else {
							this.tos(this.i18n.cycle.nodata)
						}
					}
				})
			},
			// 检查APP是否有新版本
			onAPPUpdate() {
				// true 没有新版本的时候有提示，默认：false
				APPUpdate(true);
			},
		}
	}
</script>

<style>
</style>
