VUE/JS讀取APK的包名

見上圖,兩種方式獲取包名,1.選擇apk,點擊讀取實現;2.直接輸入包名(你知道包名的情況線下),點擊添加。

我們來實現通過1方法選擇APK方式實現

直接上代碼,組件html

 <p style="margin-left: 230px;" id="BappInstallCSX">
                            <p style="margin-left: 230px;display:none;" id="BPappInstall1">
                                白名單應用:<input type="file" style="width: 200px; height: 30px;" accept=".apk" id="BfileappInstallC"  οnchange="BappSelect()"/>
                                           <input type="button" value="讀取" style="margin-top: -10px;" onclick="BappSelect()"/>
                            </p>

                            <p style="margin-left: 230px;display:none;" id="BPappInstall2">
                                應用包名:<input type="text" style="width: 150px; height: 15px;" id="BinputappInstallC"/>
                                         <input type="button" value="添加" style="margin-top: -10px;" onclick="addBPappInstall()"/>
                                         <input type="button" value="刪除" style="margin-top: -10px;" onclick="deleBPappInstall()"/>
                            </p>

                              <table style="margin-left: 230px;align-self: auto; text-align: center;display:none;"class="appInstallCtable" id="BappInstallCtableid">
                                <thead style="background-color:#acabac;border-style:solid;border-width:1px;" class="appInstallCthead">
                                  <tr class="appInstallCheader">
                                     <th> </th>
                                     <th>apk文件名 </th>
                                     <th>包名 </th>
                                     <th>應用名 </th>
                                  </tr>
                                </thead>
                                <tbody id="BappInstallCtbody" class="appInstallCtbody" style="background-color: #FCFDFD;color: #414C5E;border-top: 1px solid #D6DBDF;border-bottom: 1px solid #D6DBDF;">
                                  <!-- js中動態拼接-->
                                </tbody>
                              </table>
                        </p>

VUE的實現:

data() {
  return {

      newpagesBAPPINSTALL: {},
      pagesBAPPINSTALLs: [],
      pagesBAPPINSTALLlist:[],//獲取發送給後端的配置      

  }
}

methods: {


 BappSelect1(){//選擇文件按鈕,判斷是否是apk
              var objFile = document.getElementById('BfileappInstallC').files[0];//獲取文件內容對象
              var objStr = /\.(apk)$/; //文件類型正則驗證
              if(!objStr.test(objFile.name)){
                this.$alert('請選擇正確的APK!', '警告', {
                        confirmButtonText: '確定',
                        // callback: action => {
                        //     this.$message({
                        //         type: 'info',
                        //         message: `action: ${ action }`
                        //     });
                        // }
                  });
                return false;
              } 
       },
       BappSelect(){ //點擊添加按鈕
              let AppInfoParser = require('../../../../build/app-info-parser-master/dist/app-info-parser.js');
              const files = document.getElementById('BfileappInstallC').files;
              console.log("aaa"+JSON.stringify(files))
              const parser = new AppInfoParser(files[0]);
              console.log("bbb"+ JSON.stringify(parser))
              parser.parse().then(result => {
                console.log('app info ----> ', result);
                console.log('包名:', result.package);
                console.log('BfileappInstallC buffer ----> ', parser.file);
                console.log("APK名稱"+parser.file.name);

                let APKname = parser.file.name; //APK文件名
                let PACKname = result.package;//包名
                let APPname = " ";//應用名
                let APKList = {"newpagesBAPPINSTALL":APKname,"inputUSERBAPPINSTALLC":PACKname,"apkUSERBAPPINSTALLC":APPname}
                this.pagesBAPPINSTALLs.push(APKList);//添加顯示出來之用
                this.pagesBAPPINSTALLlist.push(PACKname);//收集數據發送給後端
                
                // BPappInstallpp.push(APKname);
                // BPappInstallpp.push(PACKname);
                // BPappInstallpp.push(APPname);

              }).catch(err => {
                this.$alert('無法獲取apk信息,請檢查!', '警告', {
                        confirmButtonText: '確定',
                        // callback: action => {
                        //     this.$message({
                        //         type: 'info',
                        //         message: `action: ${ action }`
                        //     });
                        // }
                  });
                console.log('無法獲取apk信息 ----> ', err)
              })
       },
       addBPappInstall(){
           if (this.newpagesBAPPINSTALL.inputUSERBAPPINSTALLC == "" || this.newpagesBAPPINSTALL.inputUSERBAPPINSTALLC == null){
               this.$alert('包名不能爲空!', '警告', {
                        confirmButtonText: '確定',
                        // callback: action => {
                        //     this.$message({
                        //         type: 'info',
                        //         message: `action: ${ action }`
                        //     });
                        // }
                  });
           }else{
               this.pagesBAPPINSTALLs.push(this.newpagesBAPPINSTALL);//把獲取的APK放到表格中展示
               this.pagesBAPPINSTALLlist.push(this.newpagesBAPPINSTALL.inputUSERBAPPINSTALLC);//獲取的參數要轉給後臺
               this.newpagesBAPPINSTALL = {inputUSERBAPPINSTALLC:""};//置空,否則重複添加按鈕內容重複
           }
       },
}

 

擴展:

我們選擇APK文件的時候可以先判斷一下。JQ/JS寫法:

 $(document).ready(function(){//監聽選擇文件--判斷選的是不是APK
    $("#BfileappInstallC").change(function(evt){
      var objFile = document.getElementById('BfileappInstallC').files[0];//獲取文件內容對象
      var objStr = /\.(apk)$/; //文件類型正則驗證
      if(!objStr.test(objFile.name)){
        alert("請選擇正確的APK");
        return false;
      }
    });
    $("#HfileappInstallC").change(function(evt){
      var objFile = document.getElementById('HfileappInstallC').files[0];//獲取文件內容對象
      console.log("aaa"+ objFile);
      var objStr = /\.(apk)$/; //文件類型正則驗證
      if(!objStr.test(objFile.name)){
        alert("請選擇正確的APK");
        return false;
      }

    });
   });

選擇文件後,直接點擊讀取按鈕,然後就會獲取APK信息,我這裏只取APK包名。

 function BappSelect () {//讀取APK包名的方法
        const files = document.getElementById('BfileappInstallC').files;
        console.log("aaa"+JSON.stringify(files) )
        const parser = new AppInfoParser(files[0]);
        console.log("bbb"+ JSON.stringify(parser))
        parser.parse().then(result => {
          console.log('app info ----> ', result);
          console.log('包名:', result.package);
          console.log('BfileappInstallC buffer ----> ', parser.file);
          console.log("APK名稱"+parser.file.name);
          // document.getElementById('BinputappInstallC').value = result.package;

          var APKname = parser.file.name; //APK文件名
          var PACKname = result.package;//包名
          var APPname = " ";//應用名
          var str = "";
          var checkbox='<input type="checkbox" name="appInstallCcheckId" class="appInstallCcheckbox" id="appInstallCcekall" >';
          str = "<tr><td>" + checkbox +"</td><td>"+APKname+"</td><td>"+PACKname+"</td><td>"+APPname+"</td></tr>";
          BPappInstallpp.push(APKname);
          BPappInstallpp.push(PACKname);
          BPappInstallpp.push(APPname);
          if(APKname == "" || APKname == null){
             alert("包名不能爲空!!!");
          }else{
            $(".appInstallCtbody").append(str);
          }

        }).catch(err => {
          alert("無法獲取apk信息,請檢查!");
          console.log('無法獲取apk信息 ----> ', err)
        })
  }

//添加按鈕方法
function addBPappInstall(){ //添加APK包名到輸入框

     var APKname = ""; //APK文件名
     var PACKname = document.getElementById("BinputappInstallC").value;//包名
     var APPname = "";//應用名
     var str = "";
     var checkbox='<input type="checkbox" name="appInstallCcheckId" class="appInstallCcheckbox" id="appInstallCcekall" >';
     str = "<tr><td>" + checkbox +"</td><td>"+APKname+"</td><td>"+PACKname+"</td><td>"+APPname+"</td></tr>";
      BPappInstallpp.push(APKname);
      BPappInstallpp.push(PACKname);
      BPappInstallpp.push(APPname);

     if(PACKname == "" || PACKname == null || PACKname == " "){
       alert("白名單不能爲空!!!");
     }else{
       $(".appInstallCtbody").append(str);
     }
     $("#BinputappInstallC").val("");
  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章