service.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*!
  2. * =====================================================
  3. * User:HZC
  4. * State:服务
  5. *
  6. *
  7. * 获取所有课程 courseCode课程,sReuslt为回调方法名
  8. *
  9. function queryAllCourse(length, callback) {
  10. sDb.transaction(function(tx) {
  11. var sql = "select id,code,name,status,progress from category where length(code)=?";
  12. tx.executeSql(sql, [length], function(tx, rs) {
  13. var myRows = [];
  14. if (rs.rows.length > 0) {
  15. for (var i = 0; i < rs.rows.length; i++) {
  16. var row = rs.rows.item(i);
  17. myRows.push({
  18. code: row.code,
  19. });
  20. }
  21. }
  22. callback(myRows);
  23. }, onError);
  24. });
  25. }
  26. * =====================================================
  27. */
  28. var sDb = openDatabase('bookStoreDb', '', 'bookStore Db', 5 * 1000 * 1000);
  29. /**
  30. * 错误信息
  31. * @param {Object} tx
  32. * @param {Object} error
  33. */
  34. function onError(tx, error) {
  35. mui.toast('The db operation mistake : HzcError -- ' + error.message);
  36. console.error('The db operation mistake : HzcError -- ' + error.message);
  37. }
  38. /**
  39. * 正确信息
  40. * @param {Object} tx
  41. * @param {Object} rs
  42. */
  43. function onSucc(tx, rs) {
  44. console.log("onSuccess操作成功");
  45. }
  46. /**
  47. * 获取
  48. * @param {Object} categoryId
  49. */
  50. function getBooksByCategoryId(categoryId, callback) {
  51. sDb.transaction(function(tx) {
  52. var sql = "select id,name,img_path,html_name,dic_category_id,json,desc,is_download from dic_book where status = 1 and dic_category_id = ?";
  53. tx.executeSql(sql, [categoryId], function(tx, rs) {
  54. var myRows = [];
  55. if (rs.rows.length > 0) {
  56. for (var i = 0; i < rs.rows.length; i++) {
  57. var row = rs.rows.item(i);
  58. myRows.push({
  59. id: row.id,
  60. name: row.name,
  61. imgPath: row.img_path,
  62. htmlName: row.html_name,
  63. dicCategoryId: row.dic_category_id,
  64. jsonList: row.json,
  65. desc: row.desc,
  66. isDownload: row.is_download
  67. });
  68. }
  69. }
  70. callback(myRows);
  71. }, onError);
  72. });
  73. }
  74. /**
  75. * 获取我的书
  76. * @param {Object} start:开始数
  77. * @param {Object} callback
  78. */
  79. function getMyBooks(start, callback) {
  80. sDb.transaction(function(tx) {
  81. var sql = "select id,name,img_path,html_name,dic_category_id,json,desc,is_download from dic_book where status = 1 limit ?,5";
  82. tx.executeSql(sql, [start], function(tx, rs) {
  83. var myRows = [];
  84. if (rs.rows.length > 0) {
  85. for (var i = 0; i < rs.rows.length; i++) {
  86. var row = rs.rows.item(i);
  87. myRows.push({
  88. id: row.id,
  89. name: row.name,
  90. imgPath: row.img_path,
  91. htmlName: row.html_name,
  92. dicCategoryId: row.dic_category_id,
  93. jsonList: row.json,
  94. desc: row.desc,
  95. isDownload: row.is_download
  96. });
  97. }
  98. }
  99. callback(myRows);
  100. }, onError);
  101. });
  102. }
  103. /**
  104. * 同步后台数据库中的books
  105. * @param {Object} data
  106. * @param {Object} callback
  107. */
  108. function synDicBook(data, callback) {
  109. if (data.length > 0) {
  110. getLastBook(function(lastBook) {
  111. if (!lastBook) {
  112. lastBook.id = -1;
  113. }
  114. for (var i = 0; i < data.length; i++) {
  115. var book = data[i];
  116. if (book.id != lastBook.id) {
  117. saveBook(book, function() {
  118. console.log('保存书成功。');
  119. });
  120. }
  121. }
  122. });
  123. }
  124. }
  125. /**
  126. * 保存书
  127. * @param {Object} book
  128. * @param {Object} callback
  129. */
  130. function saveBook(book, callback) {
  131. sDb.transaction(function(tx) {
  132. var sql = "insert into dic_book (id,name,img_path,html_name,dic_category_id,json,desc,status) values (?,?,?,?,?,?,?,?)";
  133. tx.executeSql(sql, [book.id, book.name, book.imgPath, book.htmlName, book.dicCategoryId, book.jsonList, book.desc, book.status],
  134. callback, function(tx, error) {
  135. var msg = error.message.toString();
  136. if (msg.indexOf('unique') > 0) {
  137. updateBookStatus(book.id, 1, callback);
  138. }
  139. });
  140. });
  141. }
  142. /**
  143. * 获取最后一本书的id
  144. * @param {Object} callback
  145. */
  146. function getLastBook(callback) {
  147. sDb.transaction(function(tx) {
  148. var sql = 'select id from dic_book order by id desc limit 0,1';
  149. tx.executeSql(sql, [start], function(tx, rs) {
  150. var myRows = {};
  151. if (rs.rows.length > 0) {
  152. myRows.id = rs.rows[0].id;
  153. }
  154. callback(myRows);
  155. }, onError)
  156. });
  157. }
  158. /**
  159. * 更新书下载状态
  160. * @param {Object} bookId :id
  161. * @param {Object} status :1:已下载,2:未下载
  162. * @param {Object} callback
  163. */
  164. function updateDownloadStatus(bookId, status, callback) {
  165. sDb.transaction(function(tx) {
  166. var sql = 'update dic_book set is_download = ? where id = ?';
  167. tx.executeSql(sql, [status, bookId], function(tx, rs) {
  168. callback('true');
  169. }, onError);
  170. });
  171. }
  172. /**
  173. * 把所有已经下载的书的id保存在localstorage中
  174. * @param {Object} callback
  175. */
  176. function saveBookIds(callback) {
  177. sDb.transaction(function(tx) {
  178. var sql = 'select id from dic_book where status = 1';
  179. tx.executeSql(sql, [], function(tx, rs) {
  180. var ids = '';
  181. if (rs.rows.length > 0) {
  182. for (var i = 0; i < rs.rows.length; i++) {
  183. var bookId = rs.rows.item(i).id;
  184. ids = bookId + ',' + ids;
  185. }
  186. }
  187. saveBookIdsInStorage(ids);
  188. callback;
  189. }, onError);
  190. });
  191. }
  192. /**
  193. * 更新书的状态
  194. * @param {Object} id
  195. * @param {Object} status : 0:不可用,1:可用
  196. */
  197. function updateBookStatus(id, status, callback) {
  198. sDb.transaction(function(tx) {
  199. var sql = 'update dic_book set status = ? where id = ?';
  200. tx.executeSql(sql, [status, id], callback, onError)
  201. })
  202. }
  203. /**
  204. * 删除图书
  205. * @param {Object} id
  206. * @param {Object} callback
  207. */
  208. function deleteBooks(id, callback) {
  209. sDb.transaction(function(tx) {
  210. var sql = 'delete from dic_book where id = ?';
  211. tx.executeSql(sql, [id], callback, onError);
  212. });
  213. }