tools.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 { ReadHistory } from "../stores/readHistoryManager";
  5. import { UserData } from "../stores/userDataManager";
  6. import { UserStatus } from "../stores/userStatusManager";
  7. import { http } from "./http";
  8. import { log } from "./log";
  9. import { sdkUtil } from "./sdkUtil";
  10. import { util } from "./util";
  11. export class tools {
  12. public static platform:string = config.Platform.H5
  13. public static initPlatform(){
  14. // #ifdef MP-WEIXIN
  15. tools.platform = config.Platform.WEIXIN
  16. // #endif
  17. // #ifdef MP-TOUTIAO
  18. tools.platform = config.Platform.TOUTIAO
  19. // #endif
  20. log.Debug("当前运行平台:",tools.platform)
  21. }
  22. // 获取当前平台
  23. public static getCurPlatform(){
  24. return tools.platform
  25. }
  26. // 获取平台标识
  27. public static getPlatformID():string {
  28. let id = 'browser'
  29. switch (tools.getCurPlatform()){
  30. case config.Platform.WEIXIN:
  31. id = 'wx'
  32. break;
  33. case config.Platform.TOUTIAO:
  34. id = 'douyin'
  35. break;
  36. default:
  37. break;
  38. }
  39. return id
  40. }
  41. // 请求获取用户open_id
  42. private static requestGetUserOpenId(cb:Function) {
  43. sdkUtil.login((res:any)=>{
  44. if(res!=null) {
  45. let url = ''
  46. switch (tools.getCurPlatform()){
  47. case config.Platform.TOUTIAO:
  48. url = config.url_confg.Dynamic.user_get_douyin_openid
  49. break;
  50. case config.Platform.WEIXIN:
  51. url = config.url_confg.Dynamic.user_get_wx_open_id
  52. break;
  53. default:
  54. break;
  55. }
  56. http.DynamicRequest(url,res,(err,data)=>{
  57. if(!err) {
  58. if(data.code==config.url_confg.StatesCode.SUCCESS){
  59. cb && cb(data.content.openid)
  60. }
  61. }
  62. })
  63. } else {
  64. cb && cb('browser_123')
  65. }
  66. })
  67. }
  68. // 登录
  69. public static requestLogin(cb:Function) {
  70. tools.requestGetUserOpenId((open_id:string)=>{
  71. let opt = {'open_id':open_id, 'platform':tools.getPlatformID()}
  72. // console.log('参数=',opt)
  73. http.DynamicRequest(config.url_confg.Dynamic.user_login,opt, (err,data)=>{
  74. if(!err) {
  75. if(data.code==config.url_confg.StatesCode.SUCCESS){
  76. // console.log('登录成功=',data)
  77. UserData().updateUserData(data.content)
  78. cb && cb()
  79. }
  80. } else {
  81. log.Error(err)
  82. }
  83. })
  84. })
  85. }
  86. // 进入书详情
  87. public static gotoBookdetails(book_id:number, cb:Function=null) {
  88. if(book_id) {
  89. uni.navigateTo({
  90. url:'/pages/bookdetails/bookdetails',
  91. success: (res) => {
  92. res.eventChannel.emit('data',{'book_id':book_id})
  93. cb && cb()
  94. }
  95. })
  96. } else {
  97. log.Error('书详情id错误')
  98. }
  99. }
  100. // 进入阅读
  101. public static gotoRead(book_data:book_item_data) {
  102. // console.log('book_data=',book_data)
  103. console.log('start_read_chapter_id=',book_data.start_read_chapter_id)
  104. // book_data.start_read_chapter_id = 1
  105. UserStatus().updateUserSelectBook(book_data)
  106. tools.updateReadHistory(book_data)
  107. uni.navigateTo({
  108. url:'/pages/readbook/read'
  109. })
  110. }
  111. // 退出阅读
  112. public static exitRead() {
  113. // let book_data = UserStatus().getUserSelectBook()
  114. // 更新:书架、阅读历史,读到当前章节数据
  115. // UserStatus().getUserSelectBook().start_read_chapter_id =
  116. }
  117. // 获取本地书架列表
  118. public static getLocalBookshelfList():book_item_data[] {
  119. return BookshelfData().getBookList()
  120. }
  121. // 重置排序书架
  122. public static resetSortBookshelf(book_list:book_item_data[]) {
  123. BookshelfData().book_list = book_list
  124. BookshelfData().saveBookList()
  125. }
  126. // 检查在书架
  127. public static checkBookOnBookshelf(book_id:number, cb:Function) {
  128. BookshelfData().checkBookOnBookshelf(book_id, (is_on:boolean)=>{
  129. cb && cb(is_on)
  130. })
  131. }
  132. // 添加书架
  133. public static addBookshelf(book_data:book_item_data, cb:Function=null) {
  134. util.showLoading('加入书架...', true)
  135. util.hideLoading(500, ()=>{
  136. // 请求
  137. BookshelfData().addBook(book_data)
  138. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  139. cb && cb()
  140. })
  141. }
  142. // 获取本地阅读历史列表
  143. public static getLocalReadHistoryList():book_item_data[] {
  144. return ReadHistory().getBookList()
  145. }
  146. // 更新阅读历史
  147. public static updateReadHistory(book_data:book_item_data) {
  148. ReadHistory().addBook(book_data, ()=>{
  149. uni.$emit(config.EVENT_TYPE.UPDATE_READHISTORY)
  150. })
  151. }
  152. // 重置排序阅读历史
  153. public static resetSortReadHistory(book_list:book_item_data[]) {
  154. ReadHistory().book_list = book_list
  155. ReadHistory().saveBookList()
  156. }
  157. // 删除书架
  158. public static deleteBookshelf(book_id_list:number[], cb:Function=null) {
  159. util.showModal('删除?', `从书架删除这${book_id_list.length}本书?`, ()=>{
  160. util.showLoading('移除书架...', true)
  161. util.hideLoading(500, ()=>{
  162. // 请求
  163. BookshelfData().deleteBook(book_id_list)
  164. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  165. cb && cb()
  166. })
  167. })
  168. }
  169. public static getChapterList(chapter_path:string,cb:Function){
  170. let url = `${config.url_addr.Static}${chapter_path}`
  171. http.StaticRequest(url,(err,data)=>{
  172. if(err){
  173. return
  174. }
  175. cb(data)
  176. })
  177. }
  178. public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
  179. if(chapter_id){
  180. let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
  181. http.getStaticText(url,(err,data)=>{
  182. if(err){
  183. log.Error(err)
  184. return
  185. }
  186. // log.Debug("getCurChapterTxt",data)
  187. cb(tools.autoParagraph(data,emspWidth))
  188. // cb(data)
  189. })
  190. }
  191. }
  192. public static autoParagraph(text:string,emspWidth:number) {
  193. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  194. const emsp_html = `<span ${emspStyle}></span>`;
  195. const character = `<br><br>${emsp_html}`;
  196. return emsp_html+text.replace(/\n\s*/g,character)
  197. }
  198. public static autoParagraphTitle(text:string,emspWidth:number) {
  199. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  200. const emsp_html = `<span ${emspStyle}></span>`;
  201. return `<br><br>`+emsp_html+text
  202. }
  203. public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
  204. return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
  205. }
  206. public static getPreChapterData(index:number,list:chapter_item_data[]){
  207. let new_index = index -1
  208. if( new_index <0 ){
  209. return null
  210. }
  211. return list[new_index]
  212. }
  213. public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
  214. if(index<list.length&&index>=0){
  215. return list[index]
  216. }
  217. log.Error("getCurChapterData error ",index)
  218. return null
  219. }
  220. public static getNextChapterData(index:number,list:chapter_item_data[]){
  221. let new_index = index +1
  222. if( new_index >=list.length ){
  223. return null
  224. }
  225. return list[new_index]
  226. }
  227. public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
  228. return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
  229. }
  230. public static getFontColorByMode(mode:number){
  231. if(mode==config.read_config.readMode.Bright){
  232. return config.read_config.BrightFontColor
  233. }else{
  234. return config.read_config.DarkFontColor
  235. }
  236. }
  237. public static getDbColorByMode(mode:number){
  238. if(mode==config.read_config.readMode.Bright){
  239. return config.read_config.BrightDbColor
  240. }else{
  241. return config.read_config.DarkDbColor
  242. }
  243. }
  244. public static book_status_title(book_data:book_item_data){
  245. if(book_data.book_is_action==1){
  246. return `连载至 第${book_data.chapter_count}章`
  247. }else{
  248. return `已完结 共${book_data.chapter_count}章`
  249. }
  250. }
  251. }