book-list.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>图书列表</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
  7. <meta name="apple-mobile-web-app-capable" content="yes">
  8. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  9. <!--标准mui.css-->
  10. <link rel="stylesheet" href="../css/mui.min.css">
  11. <style>
  12. .title {
  13. margin: 20px 15px 10px;
  14. color: #6d6d72;
  15. font-size: 15px;
  16. }
  17. body {
  18. background-color: #efeff4;
  19. }
  20. .my-height {
  21. top: -20px;
  22. position: relative;
  23. right: -45px;
  24. height: 1px;
  25. color: brown;
  26. }
  27. @-webkit-keyframes btnRotate {
  28. 0% {
  29. -webkit-transform: rotateZ(0deg);
  30. }
  31. 100% {
  32. -webkit-transform: rotateZ(360deg);
  33. }
  34. }
  35. .donghua {
  36. -webkit-animation: btnRotate 1.5s linear infinite;
  37. }
  38. .mui-table-view-cell>a:not(.mui-btn){
  39. white-space: normal !important;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <header class="mui-bar mui-bar-nav">
  45. <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
  46. <h1 id="title-id" class="mui-title"></h1>
  47. </header>
  48. <div class="mui-content">
  49. <ul class="mui-table-view mui-table-view-chevron" id="books-id">
  50. <li class="mui-table-view-cell mui-media">
  51. <div style="text-align: center;margin-right: -60px;">加载中...</div>
  52. </li>
  53. </ul>
  54. </div>
  55. </body>
  56. <script id="book-temp-id" type="text/html">
  57. <li class="mui-table-view-cell mui-media">
  58. <a data-id='{id}' data-name='{name}' data-html-dist='{html_dist}' data-html-name='{html_name}' data-img-path='{img_path}' data-category-id='{dic_category_id}' data-json='{json}' data-desc='{desc}' data-is-download='{isDownload}' download-start='false'>
  59. <img id="img-{id}" class="mui-media-object mui-pull-left">
  60. <div class="mui-media-body">
  61. {name}
  62. <p class='mui-ellipsis'>{desc}</p>
  63. </div>
  64. <div id='download-progress-{id}' class="mui-pull-right my-height" style="display: none;">0%</div>
  65. <div id='download-{id}' class="mui-icon mui-icon-pulldown mui-pull-right my-height" style="display: {display};"></div>
  66. </a>
  67. </li>
  68. </script>
  69. <!--
  70. 作者:HZC
  71. 时间:2015-08-26
  72. 描述:图书列表
  73. -->
  74. <script type="text/javascript" src="../js/jquery.min.js" type="text/javascript" charset="UTF-8"></script>
  75. <script type="text/javascript" src="http://182.92.109.194:8080/rpc/helper.js" type="text/javascript" charset="UTF-8"></script>
  76. <script src="../js/mui.min.js" type="text/javascript" charset="utf-8"></script>
  77. <script src="../js/routes.js" type="text/javascript" charset="utf-8"></script>
  78. <script src="../js/app.js" type="text/javascript" charset="utf-8"></script>
  79. <script src="../js/db/service.js" type="text/javascript" charset="utf-8"></script>
  80. <script src="../js/hzc.rpc.js" type="text/javascript" charset="utf-8"></script>
  81. <script type="text/javascript">
  82. mui.init();
  83. mui.plusReady(function() {
  84. var self = plus.webview.currentWebview();
  85. var title = self.title;
  86. window.categoryId = self.categoryId;
  87. updateTitle(title);
  88. initDom();
  89. openBook();
  90. });
  91. /**
  92. * 初始化dom
  93. */
  94. function initDom() {
  95. listPdf(window.categoryId, function(data) {
  96. if (data.length > 0) {
  97. var htmlStr = '';
  98. for (var i = 0; i < data.length; i++) {
  99. var book = data[i];
  100. var myBooks = getBookIdsFromStorage();
  101. if (myBooks && myBooks.toString().indexOf(book.id) > -1) {
  102. book.display = 'none';
  103. book.isDownload = '1';
  104. } else {
  105. book.isDownload = '0';
  106. }
  107. var tempHtml = $id('book-temp-id').innerHTML;
  108. htmlStr += render(tempHtml, book);
  109. $id('books-id').innerHTML = htmlStr;
  110. book.imgPath = Routes.urls.book.bookUrl + book.img_path;
  111. setImg('img-' + book.id, book.imgPath);
  112. }
  113. } else {
  114. $id('books-id').innerHTML = '<li class="mui-table-view-cell mui-media"><div style="text-align: center;margin-right: -60px;">没有相关图书</div></li>';
  115. }
  116. });
  117. }
  118. /**
  119. * 更新标题
  120. * @param {Object} title
  121. */
  122. function updateTitle(title) {
  123. document.getElementById("title-id").innerHTML = title;
  124. }
  125. /**
  126. * 打开书
  127. */
  128. function openBook() {
  129. mui('#books-id').on('tap', 'a', function() {
  130. var bookSelf = this;
  131. //是否已经下载
  132. var isDownload = bookSelf.getAttribute('data-is-download');
  133. //书id
  134. var bookId = bookSelf.getAttribute('data-id');
  135. //html的名字
  136. var htmlName = bookSelf.getAttribute('data-html-name');
  137. var htmlDist = bookSelf.getAttribute('data-html-dist');
  138. //服务器中书的下载地址
  139. var loadUrl = Routes.urls.book.downloadBook + htmlDist + '/' + htmlName;
  140. // console.log(loadUrl)
  141. //没有下载,则提示下载,并开始下载,下载成功更新数据库中的下载状态
  142. if (isDownload == '0') {
  143. if (bookSelf.getAttribute('download-start') == 'true') {
  144. mui.toast('正在下载');
  145. return;
  146. }
  147. window.__downloading = true;
  148. mui.toast('开始下载');
  149. // setDownloadImage('img-' + bookId);
  150. // $id('img-' + bookId).classList.add('donghua');
  151. $id('download-' + bookId).style.display = 'none';
  152. $id('download-progress-'+bookId).style.display = 'block';
  153. bookSelf.setAttribute('download-start', 'true');
  154. //开始下载图书
  155. downloadSource(loadUrl, function() {
  156. var bookName = bookSelf.getAttribute('data-name');
  157. var dicCategoryId = bookSelf.getAttribute('data-category-id');
  158. var imgPath = bookSelf.getAttribute('data-img-path');
  159. var jsonList = bookSelf.getAttribute('data-json');
  160. var desc = bookSelf.getAttribute('data-desc');
  161. var newBook = {};
  162. newBook.id = bookId;
  163. //id,name,img_path,pdf_path,html_dist,html_name,dic_category_id,json,desc,status(0:无效,1:有效),is_download(0:没有下载,1:已经下载)
  164. newBook.name = bookName;
  165. newBook.imgPath = imgPath;
  166. newBook.desc = desc;
  167. newBook.status = 1;
  168. newBook.htmlName = htmlName;
  169. newBook.jsonList = jsonList;
  170. var jl = jsonList.split(',');
  171. var jll = jl.length;
  172. var progress = 1;
  173. for (var i = 0; i < jll; i++) {
  174. if (i == 0) continue;
  175. var suburl = jl[i];
  176. suburl = Routes.urls.book.getBooks + htmlDist + '/' + suburl;
  177. downloadSource(suburl, function() {
  178. progress += 1;
  179. var jindu = (progress / jll) * 100;
  180. $id('download-progress-'+bookId).innerHTML = parseInt(jindu)+'%';
  181. // console.log(jindu + '---------------------------------------------------')
  182. if (progress == jll) {
  183. saveBook(newBook, function() {
  184. window.__downloading = false;
  185. mui.toast(bookName + ',下载成功');
  186. // setImg('img-' + bookId, imgPath);
  187. // $id('img-' + bookId).classList.remove('donghua');
  188. bookSelf.setAttribute('data-is-download', '1');
  189. console.log('数据库更新成功');
  190. saveBookIds();
  191. $id('download-progress-'+bookId).style.display = 'none';
  192. });
  193. }
  194. }, function() {
  195. window.__downloading = false;
  196. mui.toast('下载失败');
  197. });
  198. }
  199. }, function() {
  200. mui.toast('下载失败');
  201. $id('download-' + bookId).style.display = 'block';
  202. bookSelf.setAttribute('download-start', 'false');
  203. });
  204. } else {
  205. var bookPath = DOWNLOADPATH + htmlName;
  206. var absoluteBookPath = convertToAbsoluteURL(bookPath);
  207. //已下载
  208. mui.openWindow({
  209. id: 'book-id',
  210. url: 'book.html',
  211. extras: {
  212. href: absoluteBookPath
  213. },
  214. show: {
  215. duration: 200
  216. }
  217. });
  218. }
  219. });
  220. }
  221. var oldBack = mui.back;
  222. mui.back = function() {
  223. //plus.webview.getWebviewById('tab-webview-main-id').evalJS("mui.plusReady(function(){plus.screen.lockOrientation('portrait');})")
  224. //plus.webview.getWebviewById('tab-webview-main-id').reload();
  225. if (window.__downloading) {
  226. var current = plus.webview.currentWebview();
  227. current.hide('auto');
  228. } else {
  229. //plus.webview.getWebviewById('tab-webview-subpage-first.html').reload();
  230. plus.webview.currentWebview().opener().evalJS("document.getElementById('books-id').innerHTML = '';window.start = 0;window.loadBooks(1);");
  231. oldBack();
  232. }
  233. }
  234. </script>
  235. </html>