tools.ts 17 KB

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