tools.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. uni.$emit(config.EVENT_TYPE.USER_LOGIN_SUCCESS)
  79. cb && cb()
  80. }
  81. } else {
  82. log.Error(err)
  83. }
  84. })
  85. })
  86. }
  87. // 进入书详情
  88. public static gotoBookdetails(book_id:number, cb:Function=null) {
  89. if(book_id) {
  90. uni.navigateTo({
  91. url:'/pages/bookdetails/bookdetails',
  92. success: (res) => {
  93. res.eventChannel.emit('data',{'book_id':book_id})
  94. cb && cb()
  95. }
  96. })
  97. } else {
  98. log.Error('书详情id错误')
  99. }
  100. }
  101. // 进入阅读
  102. public static gotoRead(book_data:book_item_data) {
  103. // console.log('book_data=',book_data)
  104. console.log('start_read_chapter_id=',book_data.start_read_chapter_id)
  105. // 获取上次开始阅读章节id
  106. if(!book_data.start_read_chapter_id||book_data.start_read_chapter_id<1) {
  107. book_data.start_read_chapter_id = 1
  108. }
  109. // 更新用户选择书
  110. UserStatus().updateUserSelectBook(book_data)
  111. // 更新阅读历史
  112. tools.updateReadHistory(book_data)
  113. // 跳转
  114. uni.navigateTo({
  115. url:'/pages/readbook/read'
  116. })
  117. }
  118. // 退出阅读
  119. public static exitRead() {
  120. // let book_data = UserStatus().getUserSelectBook()
  121. // 更新:书架、阅读历史,读到当前章节数据
  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 getRequestBookshelfParameter(book_data:book_item_data) {
  133. let book_opt = {
  134. 'book_id':book_data.book_id,
  135. 'start_read_chapter_id':book_data.start_read_chapter_id}
  136. return book_opt
  137. }
  138. /*
  139. * 请求书架
  140. * books_shelf: json
  141. * stype: int 类型 1:获取 2:同步书架
  142. */
  143. public static requestBookShelf(book_list:book_item_data[], stype:number, successs_cb:Function, falil_cb:Function) {
  144. let books_shelf_string = ''
  145. if(book_list.length>0) {
  146. books_shelf_string = JSON.stringify(book_list)
  147. }
  148. let opt = {'books_shelf':books_shelf_string, 'stype':stype}
  149. http.DynamicRequest(config.url_confg.Dynamic.books_shelf, opt, (err,data)=>{
  150. // console.log('err=',err,'data=',data)
  151. if(!err) {
  152. if(data.code==config.url_confg.StatesCode.SUCCESS){
  153. successs_cb && successs_cb(data.content)
  154. } else {
  155. falil_cb && falil_cb()
  156. }
  157. } else {
  158. log.Error(err)
  159. falil_cb && falil_cb()
  160. }
  161. })
  162. }
  163. // 获取本地书架列表
  164. public static getLocalBookshelfList():book_item_data[] {
  165. return BookshelfManager.getBookList()
  166. }
  167. // 保存本地书架列表
  168. public static saveLocalBookshelfList() {
  169. BookshelfManager.saveLocalBookList()
  170. }
  171. // 检查在书架
  172. public static checkBookOnBookshelf(book_id:number, cb:Function) {
  173. BookshelfManager.checkBookOnBookshelf(book_id, (is_on:boolean)=>{
  174. cb && cb(is_on)
  175. })
  176. }
  177. // 添加书架
  178. public static addBookshelf(book_data:book_item_data, cb:Function=null) {
  179. util.showLoading('加载中...', true)
  180. util.hideLoading(500, ()=>{
  181. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  182. cb && cb()
  183. })
  184. // 本地添加书
  185. BookshelfManager.addBook(book_data)
  186. // 请求
  187. let data_list = []
  188. for (let i = 0; i < BookshelfManager.getBookList().length; i++) {
  189. let element = BookshelfManager.getBookList()[i]
  190. data_list.push(tools.getRequestBookshelfParameter(element))
  191. }
  192. tools.requestBookShelf(data_list,2,()=>{
  193. console.log('同步书架(加入)-成功')
  194. },null)
  195. }
  196. // 删除书架
  197. public static deleteBookshelf(book_id_list:number[], cb:Function=null) {
  198. util.showModal('删除?', `从书架删除这${book_id_list.length}本书?`, ()=>{
  199. util.showLoading('移除书架...', true)
  200. util.hideLoading(500, ()=>{
  201. uni.$emit(config.EVENT_TYPE.UPDATE_BOOKSHELF)
  202. cb && cb()
  203. })
  204. // 本地删除书
  205. BookshelfManager.deleteBook(book_id_list)
  206. // 请求
  207. let data_list = []
  208. for (let i = 0; i < BookshelfManager.getBookList().length; i++) {
  209. let element = BookshelfManager.getBookList()[i]
  210. data_list.push(tools.getRequestBookshelfParameter(element))
  211. }
  212. tools.requestBookShelf(data_list,2,()=>{
  213. console.log('同步书架(删除)-成功')
  214. },null)
  215. })
  216. }
  217. // 重置排序书架
  218. public static resetSortBookshelf(book_list:book_item_data[]) {
  219. BookshelfManager.resetData(book_list)
  220. }
  221. // 获取本地阅读历史列表
  222. public static getLocalReadHistoryList():book_item_data[] {
  223. return ReadHistoryManager.getBookList()
  224. }
  225. // 更新阅读历史
  226. public static updateReadHistory(book_data:book_item_data) {
  227. ReadHistoryManager.addBook(book_data, ()=>{
  228. uni.$emit(config.EVENT_TYPE.UPDATE_READHISTORY)
  229. })
  230. }
  231. // 重置排序阅读历史
  232. public static resetSortReadHistory(book_list:book_item_data[]) {
  233. ReadHistoryManager.resetData(book_list)
  234. }
  235. public static getChapterList(chapter_path:string,cb:Function){
  236. let url = `${config.url_addr.Static}${chapter_path}`
  237. http.StaticRequest(url,(err,data)=>{
  238. if(err){
  239. return
  240. }
  241. cb(data)
  242. })
  243. }
  244. public static getCurChapterTxt(base_path:string,chapter_id:number,emspWidth:number,cb:Function){
  245. if(chapter_id){
  246. let url = `${config.url_addr.Static}${base_path}${chapter_id}.txt`
  247. http.getStaticText(url,(err,data)=>{
  248. if(err){
  249. log.Error(err)
  250. return
  251. }
  252. // log.Debug("getCurChapterTxt",data)
  253. cb(tools.autoParagraph(data,emspWidth))
  254. // cb(data)
  255. })
  256. }
  257. }
  258. public static autoParagraph(text:string,emspWidth:number) {
  259. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  260. const emsp_html = `<span ${emspStyle}></span>`;
  261. const character = `<br><br>${emsp_html}`;
  262. return emsp_html+text.replace(/\n\s*/g,character)
  263. }
  264. public static autoParagraphTitle(text:string,emspWidth:number) {
  265. const emspStyle = `style="margin-left: ${emspWidth}px;"`;
  266. const emsp_html = `<span ${emspStyle}></span>`;
  267. return `<br><br>`+emsp_html+text
  268. }
  269. public static getChapter(cur_chapter_id:number,list:chapter_item_data[]){
  270. return list.find(chapter_data=>cur_chapter_id==chapter_data.id)
  271. }
  272. public static getPreChapterData(index:number,list:chapter_item_data[]){
  273. let new_index = index -1
  274. if( new_index <0 ){
  275. return null
  276. }
  277. return list[new_index]
  278. }
  279. public static getCurChapterData(index:number,list:chapter_item_data[]):chapter_item_data{
  280. if(index<list.length&&index>=0){
  281. return list[index]
  282. }
  283. log.Error("getCurChapterData error ",index)
  284. return null
  285. }
  286. public static getNextChapterData(index:number,list:chapter_item_data[]){
  287. let new_index = index +1
  288. if( new_index >=list.length ){
  289. return null
  290. }
  291. return list[new_index]
  292. }
  293. public static getCurChapterIndex(chapter_id:number,list:chapter_item_data[]){
  294. return list.findIndex(chapter_data=>chapter_id==chapter_data.id)
  295. }
  296. public static getFontColorByMode(mode:number){
  297. if(mode==config.read_config.readMode.Bright){
  298. return config.read_config.BrightFontColor
  299. }else{
  300. return config.read_config.DarkFontColor
  301. }
  302. }
  303. public static getDbColorByMode(mode:number){
  304. if(mode==config.read_config.readMode.Bright){
  305. return config.read_config.BrightDbColor
  306. }else{
  307. return config.read_config.DarkDbColor
  308. }
  309. }
  310. public static book_status_title(book_data:book_item_data){
  311. if(book_data.book_is_action==1){
  312. return `连载至 第${book_data.chapter_count}章`
  313. }else{
  314. return `已完结 共${book_data.chapter_count}章`
  315. }
  316. }
  317. public static check_book_chapter_is_buy(book_id:number,chapter_id:number,cb:Function){
  318. http.DynamicRequest(config.url_confg.Dynamic.get_user_chapter_ids,{'book_id':book_id},(err,d)=>{
  319. if(d.code==config.url_confg.StatesCode.SUCCESS){
  320. cb(d.content.includes(chapter_id))
  321. }else{
  322. cb(false)
  323. }
  324. })
  325. }
  326. public static getCurBuyType(){
  327. let type = config.pay_type.NEI_BU;
  328. // switch (tools.getCurPlatform()){
  329. // case config.Platform.WEIXIN:
  330. // type = config.pay_type.WEI_XIN
  331. // break;
  332. // case config.Platform.TOUTIAO:
  333. // type = config.pay_type.DOU_YIN
  334. // break;
  335. // default:
  336. // break;
  337. // }
  338. return type
  339. }
  340. }