tools.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { config } from "../config/config";
  2. import { book_item_data, chapter_item_data } from "../data/data";
  3. import { BookshelfData } from "../stores/bookshelfManager";
  4. import { UserStatus } from "../stores/userStatusManager";
  5. import { http } from "./http";
  6. import { log } from "./log";
  7. import { util } from "./util";
  8. export class tools {
  9. public static platform:string = config.Platform.H5
  10. public static initPlatform(){
  11. // #ifdef MP-WEIXIN
  12. tools.platform = config.Platform.WEIXIN
  13. // #endif
  14. // #ifdef MP-TOUTIAO
  15. tools.platform = config.Platform.TOUTIAO
  16. // #endif
  17. log.Debug("当前运行平台:",tools.platform)
  18. }
  19. public static getCurPlatform(){
  20. return tools.platform
  21. }
  22. // 进入书详情
  23. public static gotoBookdetails(book_id:number, cb:Function=null) {
  24. if(book_id) {
  25. uni.navigateTo({
  26. url:'/pages/bookdetails/bookdetails',
  27. success: (res) => {
  28. res.eventChannel.emit('data',{'book_id':book_id})
  29. cb && cb()
  30. }
  31. })
  32. // uni.navigateTo({
  33. // url:'/pages/bookdetails/bookdetails?book_id='+book_id,
  34. // success: () => {
  35. // cb && cb()
  36. // }
  37. // })
  38. } else {
  39. log.Error('书详情id错误')
  40. }
  41. }
  42. // 进入阅读
  43. public static gotoRead(book_data:book_item_data) {
  44. // console.log('book_data=',book_data)
  45. book_data.start_read_chapter_id = 1
  46. UserStatus().updateUserSelectBook(book_data)
  47. uni.navigateTo({
  48. url:'/pages/readbook/read'
  49. })
  50. }
  51. // 检查在书架
  52. public static checkOnBookshelf(book_id:number):boolean {
  53. return BookshelfData().checkBookOnBookshelf(book_id)
  54. }
  55. // 添加书架
  56. public static addBookshelf(book_data:book_item_data, cb:Function) {
  57. util.showLoading('加入书架...', true)
  58. util.hideLoading(500, ()=>{
  59. BookshelfData().addBook(book_data)
  60. cb && cb()
  61. })
  62. }
  63. // 删除书架
  64. public static deleteBookshelf(book_id_list:number[], cb:Function) {
  65. util.showLoading('移除书架...', true)
  66. util.hideLoading(500, ()=>{
  67. BookshelfData().deleteBook(book_id_list)
  68. cb && cb()
  69. })
  70. }
  71. public static getChapterList(chapter_path:string,cb:Function){
  72. let url = `${config.url_addr.Static}${chapter_path}`
  73. http.StaticRequest(url,(err,data)=>{
  74. if(err){
  75. return
  76. }
  77. cb(data)
  78. })
  79. }
  80. public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
  81. if(chapter_id){
  82. let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
  83. http.getStaticText(url,(err,data)=>{
  84. if(err){
  85. log.Error(err)
  86. return
  87. }
  88. // log.Debug("getCurChapterTxt",data)
  89. cb(tools.autoParagraph(data,emspWidth))
  90. // cb(data)
  91. })
  92. }
  93. }
  94. public static autoParagraph(text:string,emspWidth:number) {
  95. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  96. const emsp_html = `<span ${emspStyle}></span>`;
  97. const character = `<br><br>${emsp_html}`;
  98. return emsp_html+text.replace(/\n\s*/g,character)
  99. }
  100. public static autoParagraphTitle(text:string,emspWidth:number) {
  101. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  102. const emsp_html = `<span ${emspStyle}></span>`;
  103. return `<br><br>`+emsp_html+text
  104. }
  105. public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
  106. return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
  107. }
  108. public static getPreChapterData(index:number,list:chapter_item_data[]){
  109. let new_index = index -1
  110. if( new_index <0 ){
  111. return null
  112. }
  113. return list[new_index]
  114. }
  115. public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
  116. if(index<list.length&&index>=0){
  117. return list[index]
  118. }
  119. log.Error("getCurChapterData error ",index)
  120. return null
  121. }
  122. public static getNextChapterData(index:number,list:chapter_item_data[]){
  123. let new_index = index +1
  124. if( new_index >=list.length ){
  125. return null
  126. }
  127. return list[new_index]
  128. }
  129. public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
  130. return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
  131. }
  132. public static getFontColorByMode(mode:number){
  133. if(mode==config.read_config.readMode.Bright){
  134. return config.read_config.BrightFontColor
  135. }else{
  136. return config.read_config.DarkFontColor
  137. }
  138. }
  139. public static getDbColorByMode(mode:number){
  140. if(mode==config.read_config.readMode.Bright){
  141. return config.read_config.BrightDbColor
  142. }else{
  143. return config.read_config.DarkDbColor
  144. }
  145. }
  146. public static book_status_title(book_data:book_item_data){
  147. if(book_data.book_is_action==1){
  148. return `连载至 第${book_data.chapter_count}章`
  149. }else{
  150. return `已完结 共${book_data.chapter_count}章`
  151. }
  152. }
  153. }