tools.ts 14 KB

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