app.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // app.js
  2. // 引入阅读器插件
  3. const novelPlugin = requirePlugin('novel-plugin')
  4. import { userLogin,getChapterUnlockStatus,getNovelDetail,addBrowsingHistory } from './api/api'
  5. import {apple} from './config/config'
  6. App({
  7. onLaunch(options) {
  8. console.log('场景值:',options.scene);
  9. novelPlugin.setLoggerConfig({
  10. info: true,
  11. debug: true,
  12. log: true,
  13. warn: true,
  14. error: true,
  15. })
  16. // 监听进入插件页事件
  17. novelPlugin.onPageLoad(onNovelPluginLoad)
  18. // 展示本地存储能力
  19. const logs = wx.getStorageSync('logs') || []
  20. logs.unshift(Date.now())
  21. wx.setStorageSync('logs', logs)
  22. },
  23. // 登录方法
  24. login() {
  25. return new Promise((resolve, reject) => {
  26. // 检查登录状态
  27. if (this.checkLoginStatus()) {
  28. resolve();
  29. return;
  30. }
  31. wx.login({
  32. success: res => {
  33. userLogin({
  34. "appid": apple.appid,
  35. "channel": 1,
  36. "srcType": "0",
  37. "srcAppId": "",
  38. "srcId": "",
  39. "cusId": "",
  40. "inviter": "",
  41. "promotionId": "",
  42. "inviterPromotionId": "",
  43. "system": "pc",
  44. "type": "inner",
  45. "inner": 1,
  46. code: res.code,
  47. }).then(res => {
  48. console.log(res);
  49. wx.setStorageSync('accessToken', res.accessToken);
  50. this.globalData.isLogin = true;
  51. resolve(res);
  52. }).catch(err => {
  53. this.globalData.isLogin = false;
  54. reject(err);
  55. })
  56. },
  57. fail: err => {
  58. this.globalData.isLogin = false;
  59. reject(err);
  60. }
  61. })
  62. })
  63. },
  64. // 检查登录状态
  65. checkLoginStatus() {
  66. const token = wx.getStorageSync('accessToken');
  67. return !!token && this.globalData.isLogin;
  68. },
  69. globalData: {
  70. userInfo: null,
  71. isLogin: false,
  72. novelManager: {},
  73. uuid: '',
  74. isShowDialog: false
  75. }
  76. })
  77. // 插件初始化回调
  78. async function onNovelPluginLoad(data) {
  79. // data.id - 阅读器实例 id,每个插件页对应一个阅读器实例
  80. const novelManager = novelPlugin.getNovelManager(data.id)
  81. getApp().globalData.novelManager = novelManager
  82. novelManager.setFullScreenComponentStatus({
  83. show: true,
  84. })
  85. let pluginInfo = novelManager.getPluginInfo();
  86. let innerBookId = pluginInfo.query?.innerBookId;
  87. console.log("innerBookId",innerBookId);
  88. let unlockStatus = await getChapterUnlockStatus({
  89. novelId: innerBookId
  90. });
  91. let bookDetail = await getNovelDetail(innerBookId);
  92. console.log("bookDetail",bookDetail);
  93. // 设置目录状态(根据unlockStatus数据设置章节状态)
  94. if (unlockStatus && Array.isArray(unlockStatus.status)) {
  95. // 将unlockStatus.status数组转换为目录状态格式
  96. const contents = unlockStatus.status.map((lockState, index) => {
  97. return {
  98. index: index, // 章节索引
  99. status: lockState,
  100. };
  101. });
  102. console.log("contents",contents);
  103. // 设置目录状态
  104. novelManager.setContents({
  105. contents: contents,
  106. });
  107. }
  108. function startRead(res) {
  109. console.log('开始阅读',res);
  110. updateBrowsingHistory(res);
  111. }
  112. function updateBrowsingHistory(res) {
  113. let params = {
  114. novelAuthor: bookDetail.author,
  115. novelCover: bookDetail.cover,
  116. novelId: bookDetail.id,
  117. novelTitle: bookDetail.title,
  118. status: bookDetail.status
  119. }
  120. addBrowsingHistory(params);
  121. }
  122. // 监听用户行为事件
  123. novelManager.onUserTriggerEvent(res => {
  124. const { event_id } = res;
  125. console.log('用户行为:', event_id, res);
  126. // 根据不同的事件类型处理
  127. switch(event_id) {
  128. case 'start_read': // 开始阅读
  129. console.log('开始阅读章节:', res.chapter_id);
  130. break;
  131. case 'click_startread': // 点击开始阅读
  132. startRead(res);
  133. break;
  134. case 'leave_readpage': // 离开阅读页
  135. console.log('阅读时长:', res.read_time);
  136. break;
  137. case 'change_chapter': // 切换章节
  138. console.log('切换到章节:', res.chapter_id);
  139. break;
  140. case 'get_chapter': // 获取章节数据
  141. console.log("get_chapter",res);
  142. console.log('章节状态:', res.pay_status);
  143. break;
  144. default:
  145. break;
  146. }
  147. })
  148. }