<template>
	<view>
		<cu-custom bgColor="bg-gradual-blueness" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">设置</block>
		</cu-custom>
		<view class="cu-list menu">
			<view class="cu-item arrow">
				<navigator class="content" hover-class="none" url="/pages/login/forget/forget-reset" open-type="redirect">
					<text class="text-semi">修改密码</text>
				</navigator>
			</view>
<!--			<view class="cu-item arrow">
				<navigator class="content" hover-class="none" url="/pages/my/face/index" open-type="redirect">
					<text class="text-semi">更新人脸信息</text>
				</navigator>
			</view>-->
<!--			<view class="cu-item arrow">
				<navigator class="content" hover-class="none" url="/pages/my/updateSignature/index" open-type="redirect">
					<text class="text-semi">更新签字信息</text>
				</navigator>
			</view>-->
			<view class="cu-item arrow" @click="plusReady()">
				<view class="content">
					<text class="text-semi">检查更新</text>
				</view>
			</view>
			<view class="cu-item margin-top text-center" @click="unlogin()">
				<view class="content">
					<text class="text-semi">退出当前账户</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
  import { decryptAes } from "@/common/aesUtil";
	import {
		basePath,
    loginUser,
		setloginUserId,
		setCorpinfoId,
		setloginUser,
		setDeptId,
		setIsRest,
    baseImgPath,
    setVersion,
    setPremission
	} from '@/common/tool.js';
	export default {
		data() {
			return {
        wgtVer: '',
        version:'',
        brand:'other'
			}
		},
		methods: {
      // 获取当前版本号
      plusReady() {
        var that = this;

        // 获取本地应用资源版本号
        plus.runtime.getProperty(plus.runtime.appid, function (inf) {
          that.wgtVer = inf.version;  //获取当前版本号
          setVersion(that.wgtVer);
          that.version = plus.runtime.version;
          uni.getSystemInfo({

            success(res) {
              that.brand = res.brand.toUpperCase();
              that.checkPlatform();
              console.log(that.brand) //手机牌子
              console.log(res.model) //手机型号
              console.log(res.screenWidth) //屏幕宽度
              console.log(res.screenHeight) //屏幕高度
            }
          });
        });
      },
      checkPlatform() {
        var _this=this;
        uni.getSystemInfo({
          success: (res) => {
            _this.checkUpdate(res.platform);
          }
        })
      },
      // 自动更新
      checkUpdate(platform){
        var that=this;
        uni.request({
          url: basePath + '/app/versionmanager/getVersion',  //获取最新版本号
          method: 'POST',
          data: {
            FILETYPE:platform,
            CORPINFO_ID:loginUser.CORPINFO_ID,
            USER_ID:loginUser.USER_ID,
          },
          success: res => {
            res.data = decryptAes(res.data);
            console.log("发布版本：" + res.data.pd.VERSION + " -> " + Number(res.data.pd.VERSION.replace(/\./g, "")))
            console.log("运行版本：" + that.wgtVer + " -> " + Number(that.wgtVer.replace(/\./g, "")))
            console.log(Number(res.data.pd.VERSION.replace(/\./g, "")) > Number(that.wgtVer.replace(/\./g, "")))
            if(Number(res.data.pd.VERSION.replace(/\./g, "")) > Number(that.wgtVer.replace(/\./g, ""))){
              if(platform=="android"){
                plus.nativeUI.confirm( "请保持应用最新版本，否则将无法使用。", function(e){
                  if(e.index == 0){
                    var url2 = encodeURI(baseImgPath + res.data.pd.FILEURL);
                    plus.runtime.openURL(url2, function(res) {});
                    setTimeout(function() {
                      plus.runtime.quit();
                    }, 2000);
                  } else {
                    plus.runtime.quit();
                  }
                }, "下载更新", ["下载"] );
              }else{
                plus.nativeUI.confirm( "请保持应用最新版本，否则将无法使用。", function(e){
                  if(e.index == 0){
                    var url='itms-apps://itunes.apple.com/cn/app/id1632339513?mt=8';// 应用在appstore的地址
                    plus.runtime.openURL(url);
                    setTimeout(function() {
                      const threadClass = plus.ios.importClass("NSThread");
                      const mainThread = plus.ios.invoke(threadClass, "mainThread");
                      plus.ios.invoke(mainThread, "exit");
                    }, 2000);
                  }
                }, "下载更新", ["下载"] );
              }
            } else {
              uni.showToast({
                icon: 'none',
                title: "当前已是最新版本，当前版本号：" + that.wgtVer,
                duration: 2000
              });
            }
          },

          fail: (res) => {
            uni.showToast({
              icon: 'none',
              title: "服务器正在升级，请稍后再试。",
              duration: 2000
            });
          }
        });
      },
			unlogin(){
				uni.showModal({
					title: '提示',
					cancelText: '确认',
					confirmText: '取消',
					content: '确定退出当前账户？',
					success: (res) => {
						if (res.cancel) {
						  this.updatePushCid()
							setloginUserId(null);
							setCorpinfoId(null);
							setDeptId(null);
							setloginUser(null);
							setIsRest(null);
              setPremission(null)
							uni.removeStorage({key: 'USER'});
							uni.redirectTo({
								url: '/pages/login/home'
							});
						}
					}
				});
			},
      updatePushCid(){
        uni.request({
          url: basePath + '/app/user/updatePushCid',
          method: 'POST',
          dataType: 'json',
          header: {
            'Content-type':'application/x-www-form-urlencoded'
          },
          data: {
            USER_ID:loginUser.USER_ID,
            PUSH_CID:""
          }
        })
      }
		}
	}
</script>

<style>

</style>

