<template>
    <div class="page-cell">
        <co-header title="门户">
            <a @click="goBack()" slot="left">
                <co-button icon="back"></co-button>
            </a>
        </co-header>
        <div class="wrapper">
            <co-cell @click.native="getAppKey()" title="获取AppKey"></co-cell>
            <co-cell @click.native="getHtmlRootDir()" title="获取获取轻应用的手机SD卡根目录"></co-cell>
            <co-cell @click.native="getPageParams()" title="获取页面参数"></co-cell>
            <co-cell @click.native="getLoginInfo()" title="获取登陆信息(同步)"></co-cell>
            <co-cell @click.native="getKNDLoginInfo()" title="获取登陆信息(异步)"></co-cell>
            <!--<co-cell @click.native="getBusinessInfo()" title="获取代办业务信息(异步)"></co-cell>-->
            <co-cell @click.native="requestProxy()" title="通过原生网络框架，发起异步网络请求。" label="默认附有业务字段信息"></co-cell>
            <co-cell @click.native="download()" title="下载附件"></co-cell>
            <co-cell @click.native="upload()" title="上传附件"></co-cell>
            <!--<co-cell @click.native="addNotification()" title="在当前界面添加事件监听"></co-cell>-->
            <!--<co-cell @click.native="sendNotification()" title="发送某个广播通知类型的消息"></co-cell>-->
            <co-cell @click.native="openFile()" title="打开附件"></co-cell>
            <co-cell @click.native="setFunBadge()" title="设置工作区旗标数"></co-cell>
            <co-cell @click.native="setStatusBarStyle()" title="设置状态栏样式" label="设置为白色"></co-cell>
            <co-cell @click.native="refresh()" title="刷新工作区信息"></co-cell>
            <co-cell @click.native="choiceFile()" title="选择本地文件，支持多选"></co-cell>
            <co-cell @click.native="choiceUser()" title="选人功能"></co-cell>
        </div>
    </div>
</template>
<style>
</style>
<script>
  export default {
    data() {
      return {
        nativeList: []
      };
    },
    methods: {
      goBack: function() {
        history.go(-1);
      },
      // 获取getAppKey
      getAppKey: function() {
        alert(corMPortal.getAppKey());
      },
      // 获取获取轻应用的手机SD卡根目录
      getHtmlRootDir: function() {
        alert(corMPortal.getHtmlRootDir());
      },
      // 获取页面参数
      getPageParams: function() {
        alert(corMPortal.getPageParams());
      },
      // 获取登陆信息(同步)
      getLoginInfo: function() {
        alert(corMPortal.getLoginInfo());
      },
      // 获取登陆信息(异步)
      getKNDLoginInfo: function() {
        corMPortal.getKNDLoginInfo(function(error, data) {
          if (error === 0) {
            alert(JSON.stringify(data));
          } else {
            alert('error');
          }
        });
      },
      // 通过原生网络框架，发起异步网络请求，默认附有业务字段信息。
      requestProxy: function() {
        let params = {
          url: 'https://news.baidu.com/news?tn=bdapibaiyue&t=newchosenlist',
          method: 'post',
          data: {
            mid: 'EAFAA319E74A53774BD52C36F1B3526A%3AFG%3D1',
            cuid: '',
            ln: '18',
            wf: '0',
            action: '1',
            down: '1',
            display_time: '0',
            withtoppic: '1',
            orientation: '1',
            from: 'news_webapp',
            pd: 'webapp',
            os: 'iphone',
            nids: '',
            remote_device_type: '1',
            os_type: '1',
            screen_size_width: '320',
            screen_size_height: '537'
          },
          isForm: true
        };
        corMPortal.requestProxy(params, function(error, result) {
          if (error === 0) {
            alert(JSON.stringify(result.data.data.news));
          } else {
            alert('error');
          }
        });
      },
      // 通过原生网络框架下载附件
      download: function() {
        // 入参
        let params = {
          url: 'https://www.baidu.com/img/bd_logo1.png?where=super',
          filename: 'log.png'
        };
        // 执行
        corMPortal.download(params, function(error, data) {
          if (error === 0) {
            if (data.data.reqCode === 0) {
              // 下载完成
              alert(corJS.jsonToStr(data.data));
            } else if (data.data.reqCode === 2) {
              console.log(data);
              // 下载中
              console.log((data.data.progress) + '%');
            }
          } else {
            alert('error');
          }
        });
      },
      // 通过原生网络框架，上传附件。
      upload: function() {
        // 参数
        let jwt = corMPortal.getLoginInfo().jwt;  // mpm上传需要jwt作为登录验证
        let params = {
          url: 'http://test.coracle.com:10129/mpm/upload-file', // 可切换成其他mpm环境测试
          headers: {
            'X-Authorization': jwt
          },
          contentName: 'files',
          data: {
            file: {
              fPath: '/storage/emulated/0/Android/data/com.cor.xsimple.test/cache/temp/1533539568811.png',
              fType: 'png',
              fName: '1533539568971.png'
            }
          }
        };
        // 执行
        corMPortal.upload(params, function(error, data) {
          if (error === 0) {
            if (data.data.reqCode === 0) {

              // 上传完成
              alert(JSON.stringify(data.data.file));
            } else if (data.data.reqCode === 2) {
              // 上传中
              console.log((data.data.progress) + '%');
            }
          } else {
            alert('error');
          }
        });
      },
      // 打开附件
      openFile: function() {
        // 入参
        let params = {
          path: 'https://www.baidu.com/img/bd_logo1.png?where=super',
          suffix: 'png',
          type: 'IMAGE'
        };
        corMPortal.openFile(params);
      },
      // 设置工作区旗标数
      setFunBadge: function() {
        let params = {
          funkey: 'task',  // 对应轻应用的funkey
          badgeNum: 10
        };
        corMPortal.setFunBadge(params);
      },
      // 设置状态栏样式
      setStatusBarStyle: function() {
        let params = {
          style: 'black'
        };
        corMPortal.setStatusBarStyle(params);
      },
      // 刷新通知或工作区
      refresh: function() {
        let params = {
          type: 'workspace'
        };
        corMPortal.refresh(params);
      },
      // 利用原生UI控件选择本地文件，支持多选
      choiceFile: function() {
        let params = {
          maxNum: 6
        };
        corMPortal.choiceFile(params, function(error, data) {
          if (error === 0) {
            alert(JSON.stringify(data.data));
          } else {
            alert('error');
          }
        });
      },
      // 选择人员
      choiceUser: function() {
        // 入参
        let params = {
          isCheckBox: true
        };
        corMPortal.choiceUser(params, function(error, data) {
          if (error === 0) {
            alert(JSON.stringify(data.data));
          } else {
            alert('error');
          }
        });
      }
    }
  };
</script>

