teenagePopup.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Vue from 'vue'
  2. import teenagePopUpWindow from '@/components/teenagePopUpWindow/teenagePopUpWindow.vue'
  3. const app = getApp();
  4. // 创建全局弹窗容器
  5. function createPopupContainer() {
  6. const container = document.createElement('div');
  7. container.id = 'teenage-popup-container';
  8. document.body.appendChild(container);
  9. return container;
  10. }
  11. export function createTeenagePopUpWindow(options = {}) {
  12. const { title, content, confirmText, imageSrc } = options;
  13. // 确保弹窗容器存在
  14. let container = document.getElementById('teenage-popup-container');
  15. if (!container) {
  16. container = createPopupContainer();
  17. }
  18. // 创建 Vue 实例
  19. const PopupConstructor = Vue.extend({
  20. render(h) {
  21. return h(teenagePopUpWindow, {
  22. props: {
  23. text: content || '截图至相册,微信扫一扫',
  24. confirmText: confirmText || '知道了',
  25. imageSrc: imageSrc || '../../static/me/qingshaonianmoshi.png'
  26. },
  27. on: {
  28. close: this.closePopup
  29. }
  30. });
  31. },
  32. methods: {
  33. closePopup() {
  34. this.$destroy();
  35. this.$el.remove();
  36. }
  37. }
  38. });
  39. // 创建并挂载实例
  40. const instance = new PopupConstructor().$mount(container);
  41. return {
  42. show: () => {
  43. // 在 uni-app 中使用 uni.createAnimation 或 $refs 来触发显示
  44. if (instance.$children && instance.$children[0] && instance.$children[0].open) {
  45. instance.$children[0].open();
  46. }
  47. },
  48. hide: () => {
  49. // 在 uni-app 中使用 uni.createAnimation 或 $refs 来触发隐藏
  50. if (instance.$children && instance.$children[0] && instance.$children[0].close) {
  51. instance.$children[0].close();
  52. }
  53. }
  54. };
  55. }
  56. // 全局定时弹窗方法
  57. export async function setupTeenagePopupTimer() {
  58. function showTeenagePopUpWindow() {
  59. const teenagePopup = createTeenagePopUpWindow({
  60. title: '青少年模式提醒',
  61. content: '为保护未成年人健康成长,建议合理控制使用时间',
  62. confirmText: '我知道了'
  63. });
  64. // 显示弹窗
  65. teenagePopup.show();
  66. }
  67. showTeenagePopUpWindow();
  68. }