tools.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 { ReadSetting } from "../stores/readSetting";
  6. import { ReadRecord } from "../stores/readRecordManager";
  7. import { UserData } from "../stores/userDataManager";
  8. import { UserStatus } from "../stores/userStatusManager";
  9. import { http } from "./http";
  10. import { log } from "./log";
  11. import { sdkUtil } from "./sdkUtil";
  12. import { util } from "./util";
  13. export class tools {
  14. public static platform:string = config.Platform.H5
  15. public static initPlatform(){
  16. // #ifdef MP-WEIXIN
  17. config.applet_id = config.applet_id_config.wx
  18. tools.platform = config.Platform.WEIXIN
  19. //微信初始化设置显示分享弹窗,右上角'···'才可以转发给朋友,复制链接
  20. wx.showShareMenu({
  21. withShareTicket: true,
  22. menus: ['shareAppMessage', 'shareTimeline']
  23. });
  24. // #endif
  25. // #ifdef MP-TOUTIAO
  26. config.applet_id = config.applet_id_config.dy
  27. tools.platform = config.Platform.TOUTIAO
  28. // #endif
  29. log.Debug("当前运行平台:",tools.platform)
  30. }
  31. // 获取当前平台
  32. public static getCurPlatform(){
  33. return tools.platform
  34. }
  35. // 获取平台标识
  36. public static getPlatformID():string {
  37. let id = 'browser'
  38. switch (tools.getCurPlatform()){
  39. case config.Platform.WEIXIN:
  40. id = 'wx'
  41. break;
  42. case config.Platform.TOUTIAO:
  43. id = 'douyin'
  44. break;
  45. default:
  46. break;
  47. }
  48. return id
  49. }
  50. // 请求获取用户open_id
  51. private static requestGetUserOpenId(cb:Function) {
  52. sdkUtil.login((res:any)=>{
  53. if(res!=null) {
  54. let url = ''
  55. switch (tools.getCurPlatform()){
  56. case config.Platform.TOUTIAO:
  57. url = config.url_confg.Dynamic.user_get_douyin_openid
  58. break;
  59. case config.Platform.WEIXIN:
  60. url = config.url_confg.Dynamic.user_get_wx_open_id
  61. break;
  62. default:
  63. break;
  64. }
  65. http.DynamicRequest(url,res,(err,data)=>{
  66. if(!err) {
  67. if(data.code==config.url_confg.StatesCode.SUCCESS){
  68. cb && cb(data.content.openid)
  69. }
  70. }
  71. })
  72. } else {
  73. cb && cb('browser_123')
  74. }
  75. })
  76. }
  77. // 登录
  78. public static requestLogin(cb:Function=null) {
  79. tools.requestGetUserOpenId((open_id:string)=>{
  80. let opt = {'open_id':open_id, 'platform':tools.getPlatformID()}
  81. http.DynamicRequest(config.url_confg.Dynamic.user_login,opt, (err,data)=>{
  82. if(!err) {
  83. if(data.code==config.url_confg.StatesCode.SUCCESS){
  84. console.log('登录成功=',data)
  85. UserData().updateUserData(data.content)
  86. uni.$emit(config.EVENT_TYPE.USER_LOGIN_SUCCESS)
  87. cb && cb()
  88. } else{
  89. console.log('登录失败=',data)
  90. }
  91. } else {
  92. log.Error(err)
  93. }
  94. })
  95. })
  96. }
  97. // 进入书详情
  98. public static value_gotoBookdetails_data = null
  99. public static gotoBookdetails(book_id:number,wx_book_id:string, cb:Function=null) {
  100. if(book_id) {
  101. tools.value_gotoBookdetails_data = {'book_id':book_id}
  102. if(tools.getCurPlatform()==config.Platform.WEIXIN){
  103. util.showLoading()
  104. let url = config.url_confg.Static.book_details(book_id)
  105. http.StaticRequest(url,(err=null,data=null)=>{
  106. util.hideLoading()
  107. if(!err&&data) {
  108. if(data.code==config.url_confg.StatesCode.SUCCESS){
  109. var book_data = new book_item_data()
  110. book_data = data.content
  111. console.log("book_data",book_data)
  112. UserStatus().updateUserSelectBook(book_data)
  113. wx.redirectTo({
  114. url: `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?bookId=${book_data.wx_book_id}`
  115. });
  116. }
  117. }
  118. })
  119. }else{
  120. uni.navigateTo({
  121. url:'/pagesA/bookdetails/bookdetails',
  122. success: (res) => {
  123. res.eventChannel.emit('data',{'book_id':book_id})
  124. cb && cb()
  125. }
  126. })
  127. }
  128. } else {
  129. log.Error('书详情id错误')
  130. }
  131. }
  132. // 进入查看更多
  133. public static value_gotoSeeMore_data = null
  134. public static gotoSeeMore(id:number) {
  135. if(id) {
  136. tools.value_gotoSeeMore_data = {'id':id}
  137. uni.navigateTo({
  138. url:'/pagesA/seeMore/seeMore'
  139. })
  140. } else {
  141. log.Error('查看更多id错误')
  142. }
  143. }
  144. // 进入webview
  145. public static value_gotoWebview = null
  146. public static gotoWebview(title:string, html:string) {
  147. if(html) {
  148. tools.value_gotoWebview = {'title':title,'html':html}
  149. uni.navigateTo({
  150. url:'/pagesA/webview/webview'
  151. })
  152. } else {
  153. log.Error('进入webview链接错误')
  154. }
  155. }
  156. // 进入阅读
  157. public static gotoRead(book_data:book_item_data, chapter_id:number=-1) {
  158. // console.log('book_data=',book_data)
  159. // 获取上次开始阅读章节id
  160. if(chapter_id==-1) {
  161. let read_record_data = ReadRecord().getReadRecordData(book_data.book_id)
  162. if(read_record_data) {
  163. book_data.start_read_chapter_id = read_record_data.chapter_id
  164. } else {
  165. book_data.start_read_chapter_id = 1
  166. }
  167. console.log('开始阅读-获取上次章节id=',book_data.start_read_chapter_id)
  168. } else {
  169. book_data.start_read_chapter_id = chapter_id
  170. console.log('开始阅读-指定章节id=',chapter_id)
  171. }
  172. // 更新用户选择书
  173. UserStatus().updateUserSelectBook(book_data)
  174. // 更新阅读历史
  175. tools.updateReadHistory(book_data)
  176. // 跳转
  177. uni.navigateTo({
  178. url:'/pagesA/readbook/read'
  179. })
  180. }
  181. // 退出阅读
  182. public static exitRead() {
  183. let book_data = UserStatus().getUserSelectBook()
  184. // 更新:书架,读到当前章节数据
  185. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF_CURRENT_READ_CHAPTER,book_data)
  186. }
  187. // 获取用户信息
  188. public static getUserData():user_data {
  189. return UserData().getData()
  190. }
  191. // 获取用户是否VIP
  192. public static getUserIsVIP():boolean {
  193. return tools.getUserData().is_vip
  194. }
  195. // 获取请求书架参数
  196. public static getRequestBookshelfParameter(book_data:book_item_data) {
  197. let book_opt = {
  198. 'book_id':book_data.book_id,
  199. 'start_read_chapter_id':book_data.start_read_chapter_id}
  200. return book_opt
  201. }
  202. /*
  203. * 请求书架
  204. * books_shelf: json
  205. * stype: int 类型 1:获取 2:同步书架
  206. */
  207. public static requestBookShelf(book_list:book_item_data[], stype:number, successs_cb:Function, falil_cb:Function) {
  208. let books_shelf_string = ''
  209. if(book_list.length>0) {
  210. books_shelf_string = JSON.stringify(book_list)
  211. }
  212. let opt = {'books_shelf':books_shelf_string, 'stype':stype}
  213. http.DynamicRequest(config.url_confg.Dynamic.books_shelf, opt, (err,data)=>{
  214. // console.log('err=',err,'data=',data)
  215. if(!err) {
  216. if(data.code==config.url_confg.StatesCode.SUCCESS){
  217. successs_cb && successs_cb(data.content)
  218. } else {
  219. falil_cb && falil_cb()
  220. }
  221. } else {
  222. log.Error(err)
  223. falil_cb && falil_cb()
  224. }
  225. })
  226. }
  227. // 获取本地书架列表
  228. public static getLocalBookshelfList():book_item_data[] {
  229. return BookshelfManager.getBookList()
  230. }
  231. // 保存本地书架列表
  232. public static saveLocalBookshelfList() {
  233. BookshelfManager.saveLocalBookList()
  234. }
  235. // 检查在书架(异步)
  236. public static checkBookOnBookshelf(book_id:number, cb:Function) {
  237. BookshelfManager.checkBookOnBookshelf(book_id, (is_on:boolean)=>{
  238. cb && cb(is_on)
  239. })
  240. }
  241. // 检查在书架(同步)
  242. public static syncCheckBookOnBookshelf(book_id:number):boolean {
  243. return BookshelfManager.syncCheckBookOnBookshelf(book_id)
  244. }
  245. // 添加书架
  246. public static addBookshelf(book_data:book_item_data, cb:Function=null) {
  247. util.showLoading('加载中...', true)
  248. // 获取阅读记录
  249. let record_data = ReadRecord().getReadRecordData(book_data.book_id)
  250. if(record_data) {
  251. book_data.start_read_chapter_id = record_data.chapter_id
  252. } else {
  253. if(!book_data.start_read_chapter_id) {
  254. book_data.start_read_chapter_id = 1
  255. }
  256. }
  257. // 本地添加书
  258. BookshelfManager.addBook(book_data)
  259. // 请求
  260. let data_list = []
  261. for (let i = 0; i < BookshelfManager.getBookList().length; i++) {
  262. let element = BookshelfManager.getBookList()[i]
  263. data_list.push(tools.getRequestBookshelfParameter(element))
  264. }
  265. tools.requestBookShelf(data_list,2,()=>{
  266. console.log('同步书架(加入)-成功')
  267. util.hideLoading()
  268. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  269. cb && cb()
  270. },()=>{
  271. util.hideLoading()
  272. })
  273. }
  274. // 删除书架
  275. public static deleteBookshelf(book_id_list:number[], cb:Function=null) {
  276. util.showModal('删除?', `从书架删除这${book_id_list.length}本书?`, ()=>{
  277. util.showLoading('移除书架...', true)
  278. // 本地删除书
  279. BookshelfManager.deleteBook(book_id_list)
  280. // 请求
  281. let data_list = []
  282. for (let i = 0; i < book_id_list.length; i++) {
  283. let book_id = book_id_list[i]
  284. let book_data = new book_item_data()
  285. book_data.book_id = book_id
  286. book_data.start_read_chapter_id = 1
  287. data_list.push(tools.getRequestBookshelfParameter(book_data))
  288. }
  289. // 本地阅读记录删除
  290. ReadRecord().deleteReadRecordData(book_id_list)
  291. // 请求
  292. tools.requestBookShelf(data_list,2,()=>{
  293. console.log('同步书架(删除)-成功')
  294. util.hideLoading()
  295. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  296. cb && cb()
  297. },()=>{
  298. util.hideLoading()
  299. })
  300. })
  301. }
  302. // 重置排序书架
  303. public static resetSortBookshelf(book_list:book_item_data[]) {
  304. BookshelfManager.resetData(book_list)
  305. }
  306. // 获取本地阅读历史列表
  307. public static getLocalReadHistoryList():book_item_data[] {
  308. return ReadHistoryManager.getBookList()
  309. }
  310. // 更新阅读历史
  311. public static updateReadHistory(book_data:book_item_data) {
  312. ReadHistoryManager.addBook(book_data, ()=>{
  313. uni.$emit(config.EVENT_TYPE.UPDATE_READHISTORY)
  314. })
  315. }
  316. // 删除阅读历史
  317. public static deleteReadHistory(book_id_list:number[]) {
  318. ReadHistoryManager.deleteBook(book_id_list)
  319. }
  320. // 重置排序阅读历史
  321. public static resetSortReadHistory(book_list:book_item_data[]) {
  322. ReadHistoryManager.resetData(book_list)
  323. }
  324. public static getChapterList(chapter_path:string,cb:Function){
  325. let url = `${config.url_addr.Static}${chapter_path}`
  326. http.StaticRequest(url,(err,data)=>{
  327. if(err){
  328. return
  329. }
  330. cb(data)
  331. })
  332. }
  333. public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
  334. if(chapter_id){
  335. let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
  336. http.getStaticText(url,(err,data)=>{
  337. if(err){
  338. log.Error(err)
  339. return
  340. }
  341. // log.Debug("getCurChapterTxt",data)
  342. cb(tools.autoParagraph(data,emspWidth))
  343. // cb(data)
  344. })
  345. }
  346. }
  347. public static autoParagraph(text:string,emspWidth:number) {
  348. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  349. const emsp_html = `<span ${emspStyle}></span>`;
  350. const character = `<br><br>${emsp_html}`;
  351. return emsp_html+text.replace(/\n\s*/g,character)
  352. }
  353. public static autoParagraphTitle(text:string,emspWidth:number) {
  354. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  355. const emsp_html = `<span ${emspStyle}></span>`;
  356. return `<br><br>`+emsp_html+text
  357. }
  358. public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
  359. return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
  360. }
  361. public static getPreChapterData(index:number,list:chapter_item_data[]){
  362. let new_index = index -1
  363. if( new_index <0 ){
  364. return null
  365. }
  366. return list[new_index]
  367. }
  368. public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
  369. if(index<list.length&&index>=0){
  370. return list[index]
  371. }
  372. log.Error("getCurChapterData error ",index)
  373. return null
  374. }
  375. public static getNextChapterData(index:number,list:chapter_item_data[]){
  376. let new_index = index +1
  377. if( new_index >=list.length ){
  378. return null
  379. }
  380. return list[new_index]
  381. }
  382. public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
  383. return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
  384. }
  385. public static getFontColorByMode(mode:number){
  386. if(mode==config.read_config.readMode.Bright){
  387. return config.read_config.BrightFontColor
  388. }else{
  389. return config.read_config.DarkFontColor
  390. }
  391. }
  392. public static getDbColorByMode(mode:number){
  393. if(mode==config.read_config.readMode.Bright){
  394. return config.read_config.BrightDbColor
  395. }else{
  396. return config.read_config.DarkDbColor
  397. }
  398. }
  399. public static book_status_title(book_data:book_item_data){
  400. if(book_data.book_is_action==1){
  401. return `连载至 第${book_data.chapter_count}章`
  402. }else{
  403. return `已完结 共${book_data.chapter_count}章`
  404. }
  405. }
  406. public static check_book_chapter_is_buy(book_id:number,chapter_id:number,cb:Function){
  407. http.DynamicRequest(config.url_confg.Dynamic.get_user_chapter_ids,{'book_id':book_id},(err,d)=>{
  408. if(d.code==config.url_confg.StatesCode.SUCCESS){
  409. cb(d.content.includes(chapter_id))
  410. }else{
  411. cb(false)
  412. }
  413. })
  414. }
  415. public static get_book_un_lock_list(book_id:number,cb:Function){
  416. http.DynamicRequest(config.url_confg.Dynamic.get_user_chapter_ids,{'book_id':book_id},(err,d)=>{
  417. if(d.code==config.url_confg.StatesCode.SUCCESS){
  418. cb(d.content)
  419. }else{
  420. cb(null)
  421. }
  422. })
  423. }
  424. public static getCurBuyType(){
  425. let type = config.pay_type.WEI_XIN;
  426. // switch (tools.getCurPlatform()){
  427. // case config.Platform.WEIXIN:
  428. // type = config.pay_type.WEI_XIN
  429. // break;
  430. // case config.Platform.TOUTIAO:
  431. // type = config.pay_type.DOU_YIN
  432. // break;
  433. // default:
  434. // break;
  435. // }
  436. return type
  437. }
  438. public static getBgResByIndex(index:number,select_index:number){
  439. if(ReadSetting().getReadSetting().readMode==config.read_config.readMode.Dark){
  440. return config.read_config.colorBgResList[index].off_res
  441. }
  442. if(index==select_index){
  443. return config.read_config.colorBgResList[index].on_res
  444. }
  445. return config.read_config.colorBgResList[index].off_res
  446. }
  447. // public static getWxBookId(book_id:number){
  448. // var temp = [
  449. // {"book_id": 1, "wx_book_id": "A1HcfuuvhA6S7Nqy3f97nTixyb"},
  450. // {"book_id": 2, "wx_book_id": "A1Hcfuwb1iw9PakEYJz8wGWN4V"},
  451. // {"book_id": 3, "wx_book_id": "A1HcfuuvKqNdTuMD9y9bTAinvh"},
  452. // {"book_id": 4, "wx_book_id": "A1HcfuyDVgAqSRAiaWsYRcGttu"},
  453. // {"book_id": 5, "wx_book_id": "A1Hcfuzo33e6KTd9VCpmFc23L9"},
  454. // {"book_id": 6, "wx_book_id": "A1Hcfuzp91oWFt7RAGoLFV2ZUq"},
  455. // {"book_id": 7, "wx_book_id": "A1HcfuzqEyxvCJbgqLmuFN35dX"},
  456. // {"book_id": 8, "wx_book_id": "A1Hcfv2QnMSB5M47k2j85MnE4m"},
  457. // {"book_id": 9, "wx_book_id": "A1Hcfv2RtKbb1mYPR6hh5EnkDT"},
  458. // {"book_id": 10, "wx_book_id": "A1Hcfv2SzHkzxC2f6AgG57oGN9"}
  459. // ];
  460. // for (var i = 0; i < temp.length; i++) {
  461. // if(temp[i].book_id==book_id){
  462. // return temp[i].wx_book_id
  463. // }
  464. // }
  465. // return null
  466. // }
  467. public static getBookIdByWxBookId(wx_book_id:string,cb){
  468. // var temp = [
  469. // {"book_id": 1, "wx_book_id": "A1HcfuuvhA6S7Nqy3f97nTixyb"},
  470. // {"book_id": 2, "wx_book_id": "A1Hcfuwb1iw9PakEYJz8wGWN4V"},
  471. // {"book_id": 3, "wx_book_id": "A1HcfuuvKqNdTuMD9y9bTAinvh"},
  472. // {"book_id": 4, "wx_book_id": "A1HcfuyDVgAqSRAiaWsYRcGttu"},
  473. // {"book_id": 5, "wx_book_id": "A1Hcfuzo33e6KTd9VCpmFc23L9"},
  474. // {"book_id": 6, "wx_book_id": "A1Hcfuzp91oWFt7RAGoLFV2ZUq"},
  475. // {"book_id": 7, "wx_book_id": "A1HcfuzqEyxvCJbgqLmuFN35dX"},
  476. // {"book_id": 8, "wx_book_id": "A1Hcfv2QnMSB5M47k2j85MnE4m"},
  477. // {"book_id": 9, "wx_book_id": "A1Hcfv2RtKbb1mYPR6hh5EnkDT"},
  478. // {"book_id": 10, "wx_book_id": "A1Hcfv2SzHkzxC2f6AgG57oGN9"}
  479. // ];
  480. // for (var i = 0; i < temp.length; i++) {
  481. // if(temp[i].wx_book_id==wx_book_id){
  482. // return temp[i].book_id
  483. // }
  484. // }
  485. // return 1
  486. let url = config.url_confg.Dynamic.get_wxbook_to_bookid
  487. http.DynamicRequest(url,{wx_book_id:wx_book_id},(err,data)=>{
  488. if(!err) {
  489. // console.log("wx_book_id",data,wx_book_id)
  490. if(data.code==config.url_confg.StatesCode.SUCCESS){
  491. cb && cb(data.content.id)
  492. }
  493. }
  494. })
  495. }
  496. // 下单(支付类型 1:抖音支付 2:微信支付 3:内部充值(内部测试))
  497. public static requestRechargeOrderBuy(pay_type:number, goods_id:number, cb:Function) {
  498. let opt = {'pay_type':pay_type,'goods_id':goods_id}
  499. http.DynamicRequest(config.url_confg.Dynamic.recharge.order_buy,opt,(err=null,data=null)=>{
  500. log.Debug('下单 data=',data,'err=',err)
  501. if(!err&&data) {
  502. if(data.code==config.url_confg.StatesCode.SUCCESS){
  503. let order_id = data.content.order_id
  504. cb && cb(order_id,data.content.wxpay_info)
  505. } else {
  506. cb && cb('',null)
  507. }
  508. } else {
  509. cb && cb('',null)
  510. }
  511. })
  512. }
  513. // 查询订单
  514. public static requestRechargeOrderInfo(order_id:string, cb:Function) {
  515. let opt = {'order_id':order_id}
  516. http.DynamicRequest(config.url_confg.Dynamic.recharge.order_info,opt,(err=null,data=null)=>{
  517. log.Debug('查询订单 data=',data,'err=',err)
  518. if(!err&&data) {
  519. if(data.code==config.url_confg.StatesCode.SUCCESS){
  520. let status = data.content.status //订单状态 1:支付中 2:已经支付
  521. cb && cb(status)
  522. } else {
  523. cb && cb(-1)
  524. }
  525. } else {
  526. cb && cb(-1)
  527. }
  528. })
  529. }
  530. }