tools.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { config } from "../config/config";
  2. import { book_item_data, chapter_item_data, user_data } from "../data/data";
  3. import { BookshelfManager } from "../stores/bookshelfManager";
  4. import { ReadHistoryManager } 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. // 获取上次开始阅读章节id
  105. if(!book_data.start_read_chapter_id||book_data.start_read_chapter_id<1) {
  106. book_data.start_read_chapter_id = 1
  107. }
  108. // 更新用户选择书
  109. UserStatus().updateUserSelectBook(book_data)
  110. // 更新阅读历史
  111. tools.updateReadHistory(book_data)
  112. // 跳转
  113. uni.navigateTo({
  114. url:'/pages/readbook/read'
  115. })
  116. }
  117. // 退出阅读
  118. public static exitRead() {
  119. // let book_data = UserStatus().getUserSelectBook()
  120. // 更新:书架、阅读历史,读到当前章节数据
  121. // UserStatus().getUserSelectBook().start_read_chapter_id =
  122. }
  123. // 获取用户信息
  124. public static getUserData():user_data {
  125. return UserData().getData()
  126. }
  127. // 获取用户是否VIP
  128. public static getUserIsVIP():boolean {
  129. return tools.getUserData().is_vip
  130. }
  131. // 获取本地书架列表
  132. public static getLocalBookshelfList():book_item_data[] {
  133. return BookshelfManager.getBookList()
  134. }
  135. // 检查在书架
  136. public static checkBookOnBookshelf(book_id:number, cb:Function) {
  137. BookshelfManager.checkBookOnBookshelf(book_id, (is_on:boolean)=>{
  138. cb && cb(is_on)
  139. })
  140. }
  141. // 添加书架
  142. public static addBookshelf(book_data:book_item_data, cb:Function=null) {
  143. util.showLoading('加入书架...', true)
  144. util.hideLoading(500, ()=>{
  145. // 请求
  146. BookshelfManager.addBook(book_data)
  147. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  148. cb && cb()
  149. })
  150. }
  151. // 删除书架
  152. public static deleteBookshelf(book_id_list:number[], cb:Function=null) {
  153. util.showModal('删除?', `从书架删除这${book_id_list.length}本书?`, ()=>{
  154. util.showLoading('移除书架...', true)
  155. util.hideLoading(500, ()=>{
  156. // 请求
  157. BookshelfManager.deleteBook(book_id_list)
  158. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  159. cb && cb()
  160. })
  161. })
  162. }
  163. // 重置排序书架
  164. public static resetSortBookshelf(book_list:book_item_data[]) {
  165. BookshelfManager.resetData(book_list)
  166. }
  167. // 获取本地阅读历史列表
  168. public static getLocalReadHistoryList():book_item_data[] {
  169. return ReadHistoryManager.getBookList()
  170. }
  171. // 更新阅读历史
  172. public static updateReadHistory(book_data:book_item_data) {
  173. ReadHistoryManager.addBook(book_data, ()=>{
  174. uni.$emit(config.EVENT_TYPE.UPDATE_READHISTORY)
  175. })
  176. }
  177. public static getChapterList(chapter_path:string,cb:Function){
  178. let url = `${config.url_addr.Static}${chapter_path}`
  179. http.StaticRequest(url,(err,data)=>{
  180. if(err){
  181. return
  182. }
  183. cb(data)
  184. })
  185. }
  186. public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
  187. if(chapter_id){
  188. let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
  189. http.getStaticText(url,(err,data)=>{
  190. if(err){
  191. log.Error(err)
  192. return
  193. }
  194. // log.Debug("getCurChapterTxt",data)
  195. cb(tools.autoParagraph(data,emspWidth))
  196. // cb(data)
  197. })
  198. }
  199. }
  200. public static autoParagraph(text:string,emspWidth:number) {
  201. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  202. const emsp_html = `<span ${emspStyle}></span>`;
  203. const character = `<br><br>${emsp_html}`;
  204. return emsp_html+text.replace(/\n\s*/g,character)
  205. }
  206. public static autoParagraphTitle(text:string,emspWidth:number) {
  207. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  208. const emsp_html = `<span ${emspStyle}></span>`;
  209. return `<br><br>`+emsp_html+text
  210. }
  211. public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
  212. return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
  213. }
  214. public static getPreChapterData(index:number,list:chapter_item_data[]){
  215. let new_index = index -1
  216. if( new_index <0 ){
  217. return null
  218. }
  219. return list[new_index]
  220. }
  221. public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
  222. if(index<list.length&&index>=0){
  223. return list[index]
  224. }
  225. log.Error("getCurChapterData error ",index)
  226. return null
  227. }
  228. public static getNextChapterData(index:number,list:chapter_item_data[]){
  229. let new_index = index +1
  230. if( new_index >=list.length ){
  231. return null
  232. }
  233. return list[new_index]
  234. }
  235. public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
  236. return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
  237. }
  238. public static getFontColorByMode(mode:number){
  239. if(mode==config.read_config.readMode.Bright){
  240. return config.read_config.BrightFontColor
  241. }else{
  242. return config.read_config.DarkFontColor
  243. }
  244. }
  245. public static getDbColorByMode(mode:number){
  246. if(mode==config.read_config.readMode.Bright){
  247. return config.read_config.BrightDbColor
  248. }else{
  249. return config.read_config.DarkDbColor
  250. }
  251. }
  252. public static book_status_title(book_data:book_item_data){
  253. if(book_data.book_is_action==1){
  254. return `连载至 第${book_data.chapter_count}章`
  255. }else{
  256. return `已完结 共${book_data.chapter_count}章`
  257. }
  258. }
  259. }