123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import { config } from "../config/config";
- import { book_item_data, chapter_item_data } from "../data/data";
- import { BookshelfData } from "../stores/bookshelfManager";
- import { UserStatus } from "../stores/userStatusManager";
- import { http } from "./http";
- import { log } from "./log";
- import { util } from "./util";
- export class tools {
- public static platform:string = config.Platform.H5
- public static initPlatform(){
- // #ifdef MP-WEIXIN
- tools.platform = config.Platform.WEIXIN
- // #endif
-
- // #ifdef MP-TOUTIAO
- tools.platform = config.Platform.TOUTIAO
- // #endif
- log.Debug("当前运行平台:",tools.platform)
- }
- public static getCurPlatform(){
- return tools.platform
- }
-
- // 进入书详情
- public static gotoBookdetails(book_id:number, cb:Function=null) {
- if(book_id) {
- uni.navigateTo({
- url:'/pages/bookdetails/bookdetails',
- success: (res) => {
- res.eventChannel.emit('data',{'book_id':book_id})
- cb && cb()
- }
- })
- // uni.navigateTo({
- // url:'/pages/bookdetails/bookdetails?book_id='+book_id,
- // success: () => {
- // cb && cb()
- // }
- // })
- } else {
- log.Error('书详情id错误')
- }
- }
-
- // 进入阅读
- public static gotoRead(book_data:book_item_data) {
- // console.log('book_data=',book_data)
- book_data.start_read_chapter_id = 1
- UserStatus().updateUserSelectBook(book_data)
- uni.navigateTo({
- url:'/pages/readbook/read'
- })
- }
-
- // 检查在书架
- public static checkOnBookshelf(book_id:number):boolean {
- return BookshelfData().checkBookOnBookshelf(book_id)
- }
-
- // 添加书架
- public static addBookshelf(book_data:book_item_data, cb:Function) {
- util.showLoading('加入书架...', true)
- util.hideLoading(500, ()=>{
- BookshelfData().addBook(book_data)
- cb && cb()
- })
- }
-
- // 删除书架
- public static deleteBookshelf(book_id_list:number[], cb:Function) {
- util.showLoading('移除书架...', true)
- util.hideLoading(500, ()=>{
- BookshelfData().deleteBook(book_id_list)
- cb && cb()
- })
- }
-
- public static getChapterList(chapter_path:string,cb:Function){
- let url = `${config.url_addr.Static}${chapter_path}`
- http.StaticRequest(url,(err,data)=>{
- if(err){
- return
- }
- cb(data)
- })
- }
-
- public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
- if(chapter_id){
- let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
- http.getStaticText(url,(err,data)=>{
- if(err){
- log.Error(err)
- return
- }
- // log.Debug("getCurChapterTxt",data)
- cb(tools.autoParagraph(data,emspWidth))
- // cb(data)
- })
- }
- }
-
-
- public static autoParagraph(text:string,emspWidth:number) {
- const emspStyle = `style="margin-left: ${emspWidth}px;"`;
- const emsp_html = `<span ${emspStyle}></span>`;
- const character = `<br><br>${emsp_html}`;
- return emsp_html+text.replace(/\n\s*/g,character)
- }
-
- public static autoParagraphTitle(text:string,emspWidth:number) {
- const emspStyle = `style="margin-left: ${emspWidth}px;"`;
- const emsp_html = `<span ${emspStyle}></span>`;
- return `<br><br>`+emsp_html+text
- }
-
- public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
- return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
- }
-
- public static getPreChapterData(index:number,list:chapter_item_data[]){
- let new_index = index -1
- if( new_index <0 ){
- return null
- }
- return list[new_index]
- }
-
- public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
- if(index<list.length&&index>=0){
- return list[index]
- }
- log.Error("getCurChapterData error ",index)
- return null
- }
-
-
- public static getNextChapterData(index:number,list:chapter_item_data[]){
- let new_index = index +1
- if( new_index >=list.length ){
- return null
- }
- return list[new_index]
- }
-
- public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
- return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
- }
-
- public static getFontColorByMode(mode:number){
- if(mode==config.read_config.readMode.Bright){
- return config.read_config.BrightFontColor
- }else{
- return config.read_config.DarkFontColor
- }
- }
-
- public static getDbColorByMode(mode:number){
- if(mode==config.read_config.readMode.Bright){
- return config.read_config.BrightDbColor
- }else{
- return config.read_config.DarkDbColor
- }
- }
-
- public static book_status_title(book_data:book_item_data){
- if(book_data.book_is_action==1){
- return `连载至 第${book_data.chapter_count}章`
- }else{
- return `已完结 共${book_data.chapter_count}章`
- }
- }
- }
|