| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- import {
- SendEvent,
- } from '/utils/util.js'
- // import {
- // getChapterList
- // } from '../../api/api.js'
- // app.js
- // 引入阅读器插件
- const novelPlugin = requirePlugin('novel-plugin')
- import {userLogin, getChapterUnlockStatus, getNovelDetail, addBrowsingHistory} from './api/api'
- import {apple} from './config/config'
- App({
- onLaunch(options) {
- console.log('场景值:', options, options.scene);
- this.globalData.from = options.query.from || ''
- novelPlugin.setLoggerConfig({
- info: false,
- debug: false,
- log: false,
- warn: false,
- error: true,
- })
- // 监听进入插件页事件
- novelPlugin.onPageLoad(onNovelPluginLoad)
- // 展示本地存储能力
- // const logs = wx.getStorageSync('logs') || []
- // logs.unshift(Date.now())
- // wx.setStorageSync('logs', logs);
- wx.setStorageSync('scene', options.scene)// 储存场景值
- },
- onShow: function (opt) {
- console.log('onShow-小程序进入前台', opt)
- SendEvent('customAppletsShowEvent', {}, this)
- },
- // 登录方法
- login(promotionid) {
- return new Promise((resolve, reject) => {
- // 检查登录状态
- if (this.checkLoginStatus()) {
- resolve();
- return;
- }
- wx.login({
- success: res => {
- userLogin({
- "appid": apple.appid,
- "channel": 1,
- "srcType": "0",
- "srcAppId": "",
- "srcId": "",
- "cusId": "",
- "inviter": "",
- "promotionId": promotionid,
- "inviterPromotionId": "",
- "system": "pc",
- "type": "inner",
- "inner": 1,
- code: res.code,
- }).then(res => {
- console.log(res);
- wx.setStorageSync('accessToken', res.accessToken);
- this.globalData.isLogin = true;
- this.globalData.userCode = res.userCode;
- this.globalData.openId = res.openId;
- this.globalData.unionId = res.unionId;
- console.log('登录成功', this.globalData)
- SendEvent('login', {})
- resolve(res);
- }).catch(err => {
- this.globalData.isLogin = false;
- reject(err);
- })
- },
- fail: err => {
- this.globalData.isLogin = false;
- reject(err);
- }
- })
- })
- },
- // 自动登录
- async autoLogin(promotionid) {
- try {
- // 检查是否已经登录
- const token = wx.getStorageSync('accessToken');
- if (token) {
- // 已经登录
- console.log('已经登录')
- return;
- }
- // 未登录,执行登录
- const res = this.login(promotionid);
- } catch (error) {
- console.error('登录失败:', error);
- }
- },
- // 检查登录状态
- checkLoginStatus() {
- const token = wx.getStorageSync('accessToken');
- return !!token && this.globalData.isLogin;
- },
- globalData: {
- userInfo: null,
- isLogin: false,
- novelManager: {},
- uuid: '',
- isShowDialog: false,
- novelId: '',
- unlockChapterNum: 1,
- bookmarkAdNum: 3,
- openId: '',
- unionId: '',
- isEnterVideoDetail: false, // 是否进入过视频详情页
- ads_config: {}, // 广告策略配置
- userCode: '',
- from: ''
- }
- })
- // 插件初始化回调
- async function onNovelPluginLoad(data) {
- // data.id - 阅读器实例 id,每个插件页对应一个阅读器实例
- const novelManager = novelPlugin.getNovelManager(data.id)
- getApp().globalData.novelManager = novelManager
- novelManager.setFullScreenComponentStatus({
- show: true,
- });
- let pluginInfo = novelManager.getPluginInfo();
- let innerBookId = pluginInfo.query?.innerBookId;
- let unlockStatus = await getChapterUnlockStatus({
- novelId: innerBookId
- });
- let bookDetail = await getNovelDetail(innerBookId);
- getApp().globalData.novelId = bookDetail.id
- console.log("bookDetail", bookDetail);
- if (unlockStatus && Array.isArray(unlockStatus.status)) {
- const contents = unlockStatus.status.map((lockState, index) => {
- return {
- index: index,
- status: lockState,
- };
- });
- novelManager.setContents({
- contents: contents,
- });
- novelManager.setChargeWay({
- globalConfig: {
- mode: 1,
- buttonText: '解锁',
- showButton: true,
- tip: '看完广告之后可继续阅读',
- }
- })
- let chapterConfigs = []
- for (let index = 0; index < bookDetail.latestChapterNo; index++) {
- if ((index + 1) % getApp().globalData.bookmarkAdNum === 0) {
- chapterConfigs.push({
- chapterIndex: index,
- blocks: [
- {
- type: 3,
- unitId: apple.ads.bookmarkAdId
- }
- ]
- })
- }
- }
- novelManager.setAdBlock({
- chapterConfigs: chapterConfigs
- })
- }
- function startRead(res) {
- console.log('开始阅读', res);
- updateBrowsingHistory(res);
- }
- function updateBrowsingHistory(res) {
- let params = {
- novelAuthor: bookDetail.author,
- novelCover: bookDetail.cover,
- novelId: bookDetail.id,
- novelTitle: bookDetail.title,
- status: bookDetail.status
- }
- addBrowsingHistory(params);
- }
- // 监听用户行为事件
- novelManager.onUserTriggerEvent(res => {
- const {
- event_id
- } = res;
- console.log('用户行为:', event_id, res);
- // 根据不同的事件类型处理
- switch (event_id) {
- case 'start_read': // 开始阅读
- console.log('开始阅读章节:', res.chapter_id);
- SendEvent('start_read', {
- page: '开始阅读'
- })
- break;
- case 'click_startread': // 点击开始阅读
- SendEvent('click_startread', {
- page: '点击开始阅读'
- })
- startRead(res);
- break;
- case 'leave_readpage': // 离开阅读页
- SendEvent('leave_readpage', {
- page: '离开阅读页'
- })
- console.log('阅读时长:', res.read_time);
- break;
- case 'change_chapter': // 切换章节
- SendEvent('change_chapter', {
- page: '切换到章节'
- })
- console.log('切换到章节:', res.chapter_id);
- break;
- case 'get_chapter': // 获取章节数据
- SendEvent('get_chapter', {
- page: '获取章节数据'
- })
- console.log("get_chapter", res);
- console.log('章节状态:', res.pay_status);
- break;
- default:
- break;
- }
- })
- }
|