MUI:app在线更新功能实现

mui是一个非常接近于原生APP的前端开发框架,很多朋友通过该框架开发了很多手机app,这个框架,目前菠菜园也处于一个学习阶段,所以很多关于MUI的分享更多的是来源于网络。

当然不是为了装逼才写在这里,更多的是自己在用到相关功能的时候可以很方便的找到,所以菠菜园与其说是给大家分享内容的博客,倒不如说是波波在IT行业的成长历程。

mui前端开发框架

参考资料:http://ask.dcloud.net.cn/article/182

HTML部分代码:

  1. <div class="mui-content">
  2.      <div class="mui-scroll">
  3.           <div class="login-img">
  4.                <img src="images/icon.png" width="20%" />
  5.                <p id="version">当前版本:</p>
  6.           </div>
  7.     </div>
  8.     <ul class="mui-table-view check" id="check">
  9.         <li class="mui-table-view-cell">
  10.             <div class="updateProDiv">更新进度:
  11.                  <progress value="" max="" id="proDownFile"></progress>
  12.                  <span class="present"></span>
  13.             </div>
  14.             <a href="#" id="update" class="mui-nacigate-right">检查更新</a>
  15.         </li>
  16.     </ul>
  17. </div>

JS部分代码:

  1. <script src="js/jquery-1.8.3.min.js"></script>
  2. <script>
  3.         var wgtVer=null;
  4.         function plusReady(){ // 获取本地应用资源版本号 
  5.             plus.runtime.getProperty(plus.runtime.appid,function(inf){
  6.                 wgtVer=inf.version;
  7.                 version.innerHTML = '当前应用版本:'+wgtVer;
  8.             });
  9.         }
  10.         if(window.plus){
  11.             plusReady();
  12.         }else{
  13.             document.addEventListener('plusready',plusReady,false);
  14.         }
  15.         var ver;
  16.         //休眠方法 
  17.         function sleep(numberMillis) {
  18.             var now = new Date();
  19.             var exitTime = now.getTime() + numberMillis;
  20.             while (true) {
  21.                 now = new Date();
  22.                 if (now.getTime() > exitTime)
  23.                     return;
  24.             }
  25.         }
  26.         mui('.mui-table-view-cell').on('click', '#update', function() {
  27.             plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  28.                 ver = inf.version;
  29.                 console.log("当前应用版本:" + ver);
  30.                 var url= app.baseurl+'index.php/api/other/version';
  31.                 var client;
  32.                 if(mui.os.ios) {client='ios';}
  33.                 else{client='android';}
  34.                 mui.ajax(url,{
  35.                     data:{
  36.                         version: ver,
  37.                         client:client
  38.                     },
  39.                     dataType:'json',
  40.                     type:'POST',
  41.                     timeout:10000,
  42.                     success:function(data){
  43.                         if(data.status==1){
  44.                             var btnArray = ['是', '否'];
  45.                             mui.confirm('最新version是:' + data.version+',是否更新', '发现最新版本', btnArray, function(z) {
  46.                                 if (z.index == 0) {
  47.                                     console.log('确定');
  48.                                     $('.updateProDiv').css('display', 'block');
  49.                                     $('#update').css('display', 'none');
  50.                                     var dtask = plus.downloader.createDownload(data.url, {}, function(d, status) {
  51.                                         if (status == 200) {
  52.                                             clearInterval(i);
  53.                                             $('.persent').html("100%");
  54.                                             plus.nativeUI.toast("正在准备环境,请稍后!");
  55.                                             sleep(1000);
  56.                                             var path = d.filename;//_downloads yijietong.apk 
  57.                                             console.log(d.filename);
  58.                                             $('#update').css('display', 'block');
  59.                                             $('.updateProDiv').css('display', 'none');
  60.                                             plus.runtime.install(path); // 安装下载的apk文件 
  61.                                         } else {
  62.                                             alert('Download failed:' + status);
  63.                                         }
  64.                                     });
  65.                                     dtask.start();
  66.                                     var i = setInterval(function() {
  67.                                         var totalSize = dtask.totalSize;
  68.                                         var downloadedSize = dtask.downloadedSize;
  69.                                         $('#proDownFile').attr('value', downloadedSize);
  70.                                         $('#proDownFile').attr('max', totalSize);
  71.                                         console.log(dtask.downloadedSize);
  72.                                         console.log(dtask.totalSize);
  73.                                     }, 100); //1000为1秒钟 
  74.                                 } else {
  75.                                     console.log('不确定');
  76.                                     return;
  77.                                 }
  78.                             });
  79.                         }
  80.                         else{
  81.                             alert(data.message);
  82.                         }
  83.                     },
  84.                     error: function(xhr, type, errerThrown) {
  85.                         mui.toast('网络异常,请稍候再试');
  86.                     }
  87.                 });
  88.             });
  89.         });
  90.     </script>

服务器对接部分代码:

  1. public function version(){
  2.            $data = $_POST;
  3.            $m_version=M('versions');
  4.            $ret=$m_version->find();
  5.            if($ret['version']==$data['version']){
  6.                    $this->ajaxReturn(array('status'=>0,'message'=>'当前版本已经是最新版!'));
  7.            }
  8.            else
  9.            {
  10.                $ret_data['status']=1;
  11.                $ret_data['version']=$ret['version'];
  12.                if($data['client']=='android'){
  13.                    $ret_data['url']=sp_get_asset_upload_path('apk/yijietong.apk',true);
  14.                }
  15.                else{ $ret_data['url']=sp_get_asset_upload_path('apk/yijietong.apk',true);}
  16.                $this->ajaxReturn($ret_data);
  17.            }
  18.        }

以上内容来源于网络,仅供大家学习交流,敬请关注菠菜园其他文章。

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!