teenagePopup.js 2.6 KB

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