settings.vue 6.6 KB
<template>
	<view>

		<view class="data-item">

			<text>弹窗开关</text>

			<switch checked @change="switchChange"></switch>

		</view>



		<view v-show="isApp" class="data-item" @click="clearCache()">

			<text>清理缓存</text>

			<view>
				<text style="margin-right: 30rpx;">{{currentSize}}</text>
				<image style="width: 28rpx; height: 28rpx;" src="../../static/arrow_right.png"></image>
			</view>


		</view>

		<view class="data-item" @click="goToShare()">

			<text>好友分享</text>

			<image style="width: 28rpx; height: 28rpx;" src="../../static/arrow_right.png"></image>

		</view>

		<view class="data-item" @click="goToStop()">

			<text>店主认证资料</text>

			<image style="width: 28rpx; height: 28rpx;" src="../../static/arrow_right.png"></image>

		</view>

		<view v-show="isApp" class="data-item" @click="showUpdateVersion()">

			<text>版本检测和更新</text>

			<view>
				<text style="font-size: 27rpx; margin-right: 15rpx;">{{this.updateInfoStr}}</text>
				<image style="width: 28rpx; height: 28rpx;" src="../../static/arrow_right.png"></image>
			</view>


		</view>

		<button style="background-color: white; margin: 35rpx; color: #469bf4;" @click="bindLogout()">
			{{ this.hasLogin ? "退出登录" : '登陆/注册' }}</button>

	</view>
</template>

<script>
	import {
		default as http
	} from "@/utils/http.js";
	import {
		updateUseModal,
		compare
	} from '@/utils/check_update.js';
	import {
		default as app_config
	} from '@/utils/app.js';
	import loginHelper from "../../utils/loginHelper.js"
	export default {
		data() {
			return {
				app_config,
				hasLogin: false,
				updateInfoStr: "已是最新版本",
				versionData: null,
				versionName: app_config.app_info.AppName,
				currentSize: "",
				isApp: false
			}
		},
		onShow() {
			this.hasLogin = loginHelper.hasLogin()
			console.log("onShow");
			 // #ifdef APP
			this.updateVersion();
			this.getStorageSize()
			this.isApp = true;
			// #endif

		},
		methods: {
			goToStop() {
				if(this.hasLogin) {
					let tempData = {
						url: `${app_config.server_url.app_host}eshop/center/adv?zq`
					}
					uni.navigateTo({
						url: `/pages/notitle_webview/notitle_webview?info=${encodeURIComponent(JSON.stringify(tempData))}`
					})
				} else {
					uni.navigateTo({
						url: '/pages/login_pwd/login_pwd',
					});
				}
				
			},
			goToShare() {
				
			},
			clearCache() {

				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					console.log(files, len)
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
									that.files = []
									uni.showToast({
										title: '清除成功',
										duration: 2000
									});
									that.getStorageSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '清除成功',
							duration: 2000
						});
						that.getStorageSize();
					});
				}
			},
			// 获取本地缓存大小
			getStorageSize() {
				// #ifdef APP
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.currentSize = "0B";
					} else if (sizeCache < 1024) {
						that.currentSize = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.currentSize = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.currentSize = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.currentSize = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
				// #endif

			},
			bindLogout() {
				{
					loginHelper.logout();
					uni.reLaunch({
						url: '/pages/mine/mine'
					})
				}
			},
			onLogout() {
				if (!loginHelper.hasLogin()) {
					uni.navigateTo({
						url: '/pages/login_pwd/login_pwd',
					});
				} else {
					uni.navigateTo({
						url: "/pages/logout/logout",
					});
				}
			},
			showUpdateVersion() {
				if (this.versionData) {
					updateUseModal({
						title: "更新",
						contents: this.versionData.Version_Content || '',
						is_mandatory: false,
						url: this.versionData.Package_Url,
						platform: 'android',
						type: "pkg" // 安装包类型
					});
				}
			},
			async updateVersion() {
				let appInfo = app_config.app_info;
				// #ifdef APP-PLUS
				let query_data = `query{
								update_app_version(App_Code:${JSON.stringify(appInfo.Version)}) {
								  App_Code
								  App_Name
								  Sys_Code
								  Sys_Name
								  Version_Code
								  Version_Type
								  Version_Content
								  Package_Url
								  Package_Weight
								  Version_Num
								  Update_Way
								  Download_Way
								  Update_Time
								  Publish_Time
								  Sys_Version
								  Promoter_Id
								}
							  }`;
				console.info(query_data);
				let result = await http.gql({
					query: query_data
				})

				console.info(result);
				//处理返回请求
				if (result && result.data && result.data.update_app_version) {
					if (result.data.update_app_version.Error) {
						uni.showToast({
							icon: "none",
							title: result.data.update_app_version.Error,
						});
					} else {
						this.versionData = result.data.update_app_version;
						if (this.versionData && this.versionData.Package_Url) {
							this.updateInfoStr = "更新版本";
						} else {
							this.updateInfoStr = "已是最新版本";
						}
					}
				}
				// #endif
				// #ifdef MP-WEIXIN
				this.versionName = app_config.app_info.AppName
				this.updateInfoStr = appInfo.Version
				// #endif
			},
			switchChange(e) {

			}
		}
	}
</script>

<style>
	.data-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 110rpx;
		background-color: white;
		margin-top: 3rpx;
		margin-bottom: 3rpx;
		padding-left: 30rpx;
		padding-right: 30rpx;
	}
</style>