app.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {
  2. SendEvent,
  3. } from '/utils/util.js'
  4. // import {
  5. // getChapterList
  6. // } from '../../api/api.js'
  7. // app.js
  8. // 引入阅读器插件
  9. const novelPlugin = requirePlugin('novel-plugin')
  10. import {userLogin, getChapterUnlockStatus, getNovelDetail, addBrowsingHistory} from './api/api'
  11. import {apple} from './config/config'
  12. App({
  13. onLaunch(options) {
  14. console.log('场景值:', options, options.scene);
  15. novelPlugin.setLoggerConfig({
  16. info: false,
  17. debug: false,
  18. log: false,
  19. warn: false,
  20. error: true,
  21. })
  22. // 监听进入插件页事件
  23. novelPlugin.onPageLoad(onNovelPluginLoad)
  24. // 展示本地存储能力
  25. // const logs = wx.getStorageSync('logs') || []
  26. // logs.unshift(Date.now())
  27. // wx.setStorageSync('logs', logs);
  28. wx.setStorageSync('scene', options.scene)// 储存场景值
  29. },
  30. // 登录方法
  31. login(promotionid) {
  32. return new Promise((resolve, reject) => {
  33. // 检查登录状态
  34. if (this.checkLoginStatus()) {
  35. resolve();
  36. return;
  37. }
  38. wx.login({
  39. success: res => {
  40. userLogin({
  41. "appid": apple.appid,
  42. "channel": 1,
  43. "srcType": "0",
  44. "srcAppId": "",
  45. "srcId": "",
  46. "cusId": "",
  47. "inviter": "",
  48. "promotionId": promotionid,
  49. "inviterPromotionId": "",
  50. "system": "pc",
  51. "type": "inner",
  52. "inner": 1,
  53. code: res.code,
  54. }).then(res => {
  55. console.log(res);
  56. wx.setStorageSync('accessToken', res.accessToken);
  57. this.globalData.isLogin = true;
  58. this.globalData.userCode = res.userCode;
  59. this.globalData.openId = res.openId;
  60. this.globalData.unionId = res.unionId;
  61. console.log('登录成功', this.globalData)
  62. SendEvent('login', {})
  63. resolve(res);
  64. }).catch(err => {
  65. this.globalData.isLogin = false;
  66. reject(err);
  67. })
  68. },
  69. fail: err => {
  70. this.globalData.isLogin = false;
  71. reject(err);
  72. }
  73. })
  74. })
  75. },
  76. // 自动登录
  77. async autoLogin(promotionid) {
  78. try {
  79. // 检查是否已经登录
  80. const token = wx.getStorageSync('accessToken');
  81. if (token) {
  82. // 已经登录
  83. console.log('已经登录')
  84. return;
  85. }
  86. // 未登录,执行登录
  87. const res = this.login(promotionid);
  88. } catch (error) {
  89. console.error('登录失败:', error);
  90. }
  91. },
  92. // 检查登录状态
  93. checkLoginStatus() {
  94. const token = wx.getStorageSync('accessToken');
  95. return !!token && this.globalData.isLogin;
  96. },
  97. globalData: {
  98. userInfo: null,
  99. isLogin: false,
  100. novelManager: {},
  101. uuid: '',
  102. isShowDialog: false,
  103. novelId: '',
  104. unlockChapterNum: 1,
  105. bookmarkAdNum: 3,
  106. openId: '',
  107. unionId: '',
  108. isEnterVideoDetail: false // 是否进入过视频详情页
  109. }
  110. })
  111. // 插件初始化回调
  112. async function onNovelPluginLoad(data) {
  113. // data.id - 阅读器实例 id,每个插件页对应一个阅读器实例
  114. const novelManager = novelPlugin.getNovelManager(data.id)
  115. getApp().globalData.novelManager = novelManager
  116. novelManager.setFullScreenComponentStatus({
  117. show: true,
  118. });
  119. let pluginInfo = novelManager.getPluginInfo();
  120. let innerBookId = pluginInfo.query?.innerBookId;
  121. let unlockStatus = await getChapterUnlockStatus({
  122. novelId: innerBookId
  123. });
  124. let bookDetail = await getNovelDetail(innerBookId);
  125. getApp().globalData.novelId = bookDetail.id
  126. console.log("bookDetail", bookDetail);
  127. if (unlockStatus && Array.isArray(unlockStatus.status)) {
  128. const contents = unlockStatus.status.map((lockState, index) => {
  129. return {
  130. index: index,
  131. status: lockState,
  132. };
  133. });
  134. novelManager.setContents({
  135. contents: contents,
  136. });
  137. novelManager.setChargeWay({
  138. globalConfig: {
  139. mode: 1,
  140. buttonText: '解锁',
  141. showButton: true,
  142. tip: '看完广告之后可继续阅读',
  143. }
  144. })
  145. let chapterConfigs = []
  146. for (let index = 0; index < bookDetail.latestChapterNo; index++) {
  147. if ((index + 1) % getApp().globalData.bookmarkAdNum === 0) {
  148. chapterConfigs.push({
  149. chapterIndex: index,
  150. blocks: [
  151. {
  152. type: 3,
  153. unitId: apple.ads.bookmarkAdId
  154. }
  155. ]
  156. })
  157. }
  158. }
  159. novelManager.setAdBlock({
  160. chapterConfigs: chapterConfigs
  161. })
  162. }
  163. function startRead(res) {
  164. console.log('开始阅读', res);
  165. updateBrowsingHistory(res);
  166. }
  167. function updateBrowsingHistory(res) {
  168. let params = {
  169. novelAuthor: bookDetail.author,
  170. novelCover: bookDetail.cover,
  171. novelId: bookDetail.id,
  172. novelTitle: bookDetail.title,
  173. status: bookDetail.status
  174. }
  175. addBrowsingHistory(params);
  176. }
  177. // 监听用户行为事件
  178. novelManager.onUserTriggerEvent(res => {
  179. const {
  180. event_id
  181. } = res;
  182. console.log('用户行为:', event_id, res);
  183. // 根据不同的事件类型处理
  184. switch (event_id) {
  185. case 'start_read': // 开始阅读
  186. console.log('开始阅读章节:', res.chapter_id);
  187. SendEvent('start_read', {
  188. page: '开始阅读'
  189. })
  190. break;
  191. case 'click_startread': // 点击开始阅读
  192. SendEvent('click_startread', {
  193. page: '点击开始阅读'
  194. })
  195. startRead(res);
  196. break;
  197. case 'leave_readpage': // 离开阅读页
  198. SendEvent('leave_readpage', {
  199. page: '离开阅读页'
  200. })
  201. console.log('阅读时长:', res.read_time);
  202. break;
  203. case 'change_chapter': // 切换章节
  204. SendEvent('change_chapter', {
  205. page: '切换到章节'
  206. })
  207. console.log('切换到章节:', res.chapter_id);
  208. break;
  209. case 'get_chapter': // 获取章节数据
  210. SendEvent('get_chapter', {
  211. page: '获取章节数据'
  212. })
  213. console.log("get_chapter", res);
  214. console.log('章节状态:', res.pay_status);
  215. break;
  216. default:
  217. break;
  218. }
  219. })
  220. }