mixin.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var isOnLaunch = true;
  2. export default {
  3. data() {
  4. return {}
  5. },
  6. onLoad() {
  7. // 将vk实例挂载到app.globalData上,方便在非Vue页面自身函数中调用vk实例(因为获取不到this)
  8. let app = getApp({ allowDefault: true });
  9. if (app && app.globalData && !app.globalData.vk) {
  10. app.globalData.vk = this.vk;
  11. }
  12. if (this.vk) {
  13. const url = this.vk.pubfn.getCurrentPageRoute();
  14. // 检测是否可以分享(小程序专属)
  15. this.vk.navigate.checkAllowShare({ url });
  16. // 检测是否需要登录,只有首次启动的页面才需要检测,其他页面通过 vk.navigateTo 跳转前会自动判断。
  17. if (isOnLaunch && !this.vk.checkToken() && getCurrentPages().length == 1) {
  18. isOnLaunch = false; // 重新标记为非首次页面
  19. this.vk.pubfn.checkLogin({ url, isOnLaunch: true }); // 检测是否需要登录
  20. }
  21. }
  22. },
  23. created() {
  24. // 将vk实例挂载到app.globalData上,方便在非Vue页面自身函数中调用vk实例(因为获取不到this)
  25. let app = getApp({ allowDefault: true });
  26. if (app && app.globalData && !app.globalData.vk) {
  27. app.globalData.vk = this.vk;
  28. }
  29. },
  30. methods: {
  31. $getData(data, key, defaultValue) {
  32. let { vk } = this;
  33. return vk.pubfn.getData(data, key, defaultValue);
  34. }
  35. }
  36. }