app.js 7.7 KB

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