tools.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. http.DynamicRequest(config.url_confg.Dynamic.user_login,opt, (err,data)=>{
  73. if(!err) {
  74. if(data.code==config.url_confg.StatesCode.SUCCESS){
  75. // console.log('登录成功=',data)
  76. UserData().updateUserData(data.content)
  77. uni.$emit(config.EVENT_TYPE.USER_LOGIN_SUCCESS)
  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. }
  122. // 获取用户信息
  123. public static getUserData():user_data {
  124. return UserData().getData()
  125. }
  126. // 获取用户是否VIP
  127. public static getUserIsVIP():boolean {
  128. return tools.getUserData().is_vip
  129. }
  130. // 获取请求书架参数
  131. public static getRequestBookshelfParameter(book_data:book_item_data) {
  132. let book_opt = {
  133. 'book_id':book_data.book_id,
  134. 'start_read_chapter_id':book_data.start_read_chapter_id}
  135. return book_opt
  136. }
  137. /*
  138. * 请求书架
  139. * books_shelf: json
  140. * stype: int 类型 1:获取 2:同步书架
  141. */
  142. public static requestBookShelf(book_list:book_item_data[], stype:number, successs_cb:Function, falil_cb:Function) {
  143. let books_shelf_string = ''
  144. if(book_list.length>0) {
  145. books_shelf_string = JSON.stringify(book_list)
  146. }
  147. let opt = {'books_shelf':books_shelf_string, 'stype':stype}
  148. http.DynamicRequest(config.url_confg.Dynamic.books_shelf, opt, (err,data)=>{
  149. // console.log('err=',err,'data=',data)
  150. if(!err) {
  151. if(data.code==config.url_confg.StatesCode.SUCCESS){
  152. successs_cb && successs_cb(data.content)
  153. } else {
  154. falil_cb && falil_cb()
  155. }
  156. } else {
  157. log.Error(err)
  158. falil_cb && falil_cb()
  159. }
  160. })
  161. }
  162. // 获取本地书架列表
  163. public static getLocalBookshelfList():book_item_data[] {
  164. return BookshelfManager.getBookList()
  165. }
  166. // 保存本地书架列表
  167. public static saveLocalBookshelfList() {
  168. BookshelfManager.saveLocalBookList()
  169. }
  170. // 检查在书架
  171. public static checkBookOnBookshelf(book_id:number, cb:Function) {
  172. BookshelfManager.checkBookOnBookshelf(book_id, (is_on:boolean)=>{
  173. cb && cb(is_on)
  174. })
  175. }
  176. // 添加书架
  177. public static addBookshelf(book_data:book_item_data, cb:Function=null) {
  178. util.showLoading('加载中...', true)
  179. util.hideLoading(500, ()=>{
  180. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  181. cb && cb()
  182. })
  183. // 本地添加书
  184. BookshelfManager.addBook(book_data)
  185. // 请求
  186. let data_list = []
  187. for (let i = 0; i < BookshelfManager.getBookList().length; i++) {
  188. let element = BookshelfManager.getBookList()[i]
  189. data_list.push(tools.getRequestBookshelfParameter(element))
  190. }
  191. tools.requestBookShelf(data_list,2,()=>{
  192. console.log('同步书架(加入)-成功')
  193. },null)
  194. }
  195. // 删除书架
  196. public static deleteBookshelf(book_id_list:number[], cb:Function=null) {
  197. util.showModal('删除?', `从书架删除这${book_id_list.length}本书?`, ()=>{
  198. util.showLoading('移除书架...', true)
  199. util.hideLoading(500, ()=>{
  200. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  201. cb && cb()
  202. })
  203. // 本地删除书
  204. BookshelfManager.deleteBook(book_id_list)
  205. // 请求
  206. let data_list = []
  207. for (let i = 0; i < BookshelfManager.getBookList().length; i++) {
  208. let element = BookshelfManager.getBookList()[i]
  209. data_list.push(tools.getRequestBookshelfParameter(element))
  210. }
  211. tools.requestBookShelf(data_list,2,()=>{
  212. console.log('同步书架(删除)-成功')
  213. },null)
  214. })
  215. }
  216. // 重置排序书架
  217. public static resetSortBookshelf(book_list:book_item_data[]) {
  218. BookshelfManager.resetData(book_list)
  219. }
  220. // 获取本地阅读历史列表
  221. public static getLocalReadHistoryList():book_item_data[] {
  222. return ReadHistoryManager.getBookList()
  223. }
  224. // 更新阅读历史
  225. public static updateReadHistory(book_data:book_item_data) {
  226. ReadHistoryManager.addBook(book_data, ()=>{
  227. uni.$emit(config.EVENT_TYPE.UPDATE_READHISTORY)
  228. })
  229. }
  230. // 重置排序阅读历史
  231. public static resetSortReadHistory(book_list:book_item_data[]) {
  232. ReadHistoryManager.resetData(book_list)
  233. }
  234. public static getChapterList(chapter_path:string,cb:Function){
  235. let url = `${config.url_addr.Static}${chapter_path}`
  236. http.StaticRequest(url,(err,data)=>{
  237. if(err){
  238. return
  239. }
  240. cb(data)
  241. })
  242. }
  243. public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
  244. if(chapter_id){
  245. let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
  246. http.getStaticText(url,(err,data)=>{
  247. if(err){
  248. log.Error(err)
  249. return
  250. }
  251. // log.Debug("getCurChapterTxt",data)
  252. cb(tools.autoParagraph(data,emspWidth))
  253. // cb(data)
  254. })
  255. }
  256. }
  257. public static autoParagraph(text:string,emspWidth:number) {
  258. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  259. const emsp_html = `<span ${emspStyle}></span>`;
  260. const character = `<br><br>${emsp_html}`;
  261. return emsp_html+text.replace(/\n\s*/g,character)
  262. }
  263. public static autoParagraphTitle(text:string,emspWidth:number) {
  264. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  265. const emsp_html = `<span ${emspStyle}></span>`;
  266. return `<br><br>`+emsp_html+text
  267. }
  268. public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
  269. return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
  270. }
  271. public static getPreChapterData(index:number,list:chapter_item_data[]){
  272. let new_index = index -1
  273. if( new_index <0 ){
  274. return null
  275. }
  276. return list[new_index]
  277. }
  278. public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
  279. if(index<list.length&&index>=0){
  280. return list[index]
  281. }
  282. log.Error("getCurChapterData error ",index)
  283. return null
  284. }
  285. public static getNextChapterData(index:number,list:chapter_item_data[]){
  286. let new_index = index +1
  287. if( new_index >=list.length ){
  288. return null
  289. }
  290. return list[new_index]
  291. }
  292. public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
  293. return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
  294. }
  295. public static getFontColorByMode(mode:number){
  296. if(mode==config.read_config.readMode.Bright){
  297. return config.read_config.BrightFontColor
  298. }else{
  299. return config.read_config.DarkFontColor
  300. }
  301. }
  302. public static getDbColorByMode(mode:number){
  303. if(mode==config.read_config.readMode.Bright){
  304. return config.read_config.BrightDbColor
  305. }else{
  306. return config.read_config.DarkDbColor
  307. }
  308. }
  309. public static book_status_title(book_data:book_item_data){
  310. if(book_data.book_is_action==1){
  311. return `连载至 第${book_data.chapter_count}章`
  312. }else{
  313. return `已完结 共${book_data.chapter_count}章`
  314. }
  315. }
  316. public static check_book_chapter_is_buy(book_id:number,chapter_id:number,cb:Function){
  317. http.DynamicRequest(config.url_confg.Dynamic.get_user_chapter_ids,{'book_id':book_id},(err,d)=>{
  318. if(d.code==config.url_confg.StatesCode.SUCCESS){
  319. cb(d.content.includes(chapter_id))
  320. }else{
  321. cb(false)
  322. }
  323. })
  324. }
  325. public static getCurBuyType(){
  326. let type = config.pay_type.NEI_BU;
  327. // switch (tools.getCurPlatform()){
  328. // case config.Platform.WEIXIN:
  329. // type = config.pay_type.WEI_XIN
  330. // break;
  331. // case config.Platform.TOUTIAO:
  332. // type = config.pay_type.DOU_YIN
  333. // break;
  334. // default:
  335. // break;
  336. // }
  337. return type
  338. }
  339. }