mine.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // pages/mine/mine.js
  2. import { apple } from "../../config/config";
  3. import { SendEvent } from "../../utils/util";
  4. let interstitialAd = null;
  5. const app = getApp();
  6. Page({
  7. data: {
  8. userInfo: {},
  9. userCode: "", // 用户id
  10. },
  11. onLoad: function (options) {
  12. this.autoLogin();
  13. },
  14. onShow() {},
  15. onTabItemTap(item) {
  16. setTimeout(() => {
  17. this.interstitalPlayFn();
  18. }, 1000);
  19. console.log(item.index);
  20. },
  21. // 自动登录
  22. async autoLogin() {
  23. try {
  24. // 检查是否已经登录
  25. if (app.globalData.accessToken) {
  26. this.setData({
  27. isLoggedIn: true,
  28. userCode: app.globalData.userCode,
  29. });
  30. // 已经登录
  31. console.log("已经登录");
  32. // 页面首次加载时的处理
  33. this.interstitalLoad();
  34. return;
  35. }
  36. // 未登录,执行登录
  37. const res = await app.login().then((res) => {
  38. console.log("用户ID:", app.globalData.userCode);
  39. this.setData({
  40. isLoggedIn: true,
  41. userCode: app.globalData.userCode,
  42. });
  43. console.log("重新登录", res);
  44. this.interstitalLoad();
  45. });
  46. } catch (error) {
  47. console.error("登录失败:", error);
  48. }
  49. },
  50. // 插屏广告加载
  51. interstitalLoad() {
  52. let adId = apple.ads.insertAdId;
  53. interstitialAd = wx.createInterstitialAd({
  54. adUnitId: adId,
  55. });
  56. interstitialAd.onLoad(() => {
  57. console.log("我的页插屏广告onLoad");
  58. });
  59. interstitialAd.onError((err) => {
  60. console.log("我的页插屏广告onError", err);
  61. });
  62. interstitialAd.onClose(() => {
  63. console.log("我的页插屏广告onClose");
  64. });
  65. },
  66. // 插屏广告展示
  67. interstitalPlayFn() {
  68. if (interstitialAd) {
  69. interstitialAd.show().catch((err) => {
  70. console.error("我的页插屏显错误", err);
  71. });
  72. }
  73. },
  74. // onTabItemTap(item) {
  75. // console.log(item.index)
  76. // },
  77. goToReadingHistory() {
  78. SendEvent("mine_reading_history", { uuid: app.globalData.uuid });
  79. wx.switchTab({
  80. url: "/pages/bookshelf/bookshelf",
  81. });
  82. },
  83. });