JavaScript/页面引导:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 引言 == == 页面新功能引导 ==”
 
Kangkk​(留言 | 贡献)
 
第2行: 第2行:


== 页面新功能引导 ==
== 页面新功能引导 ==
{{hc|head=pop.js|output=<syntaxhighlight lang="javascript">
/**
* @description 绑定浏览器弹窗
* @author kangkk
*/
// import cookie from 'js-cookie';
import { POP_UP_PROJECT_DATA } from '../config/pop';
import { mustBeNumber } from './number';
/**
* @function checkParamWithPop
* @description 检查参数是否合法
* @param {string} _ -
* @returns {boolean}
*/
const checkParamWithPop = _ => {
  if (!POP_UP_PROJECT_DATA[_]) throw new Error(`不合法的参数${_},在 config/pop.js 文件中检查参数设置`);
};
/**
* @function getDataWithPop
* @description 获取弹窗参数
* @returns {object} 弹窗的值
*/
export const getDataWithPop = _ => {
  const data = localStorage.getItem('pop-up-window') || {};
  try {
    return typeof data === 'string'
      ? JSON.parse(data)
      : data;
  } catch {
    throw new Error(`pop-up-window 解析错误`);
  }
};
/**
* @function getTimeWithPop
* @description 获取弹窗参数
* @param {string} _ - [POP_UP_PROJECT_DATA] 弹窗类型
* @returns {string|undefined} 弹窗类型的时间戳
*/
export const getTimeWithPop = _ => {
  checkParamWithPop(_);
  return mustBeNumber(getDataWithPop()[_]);
};
/**
* @function setTimeWithPop
* @description 设置弹窗参数
* @param {string} _ - [POP_UP_PROJECT_DATA] 弹窗类型
*/
export const setTimeWithPop = _ => {
  checkParamWithPop(_);
  const data = getDataWithPop();
  data[_] = new Date().getTime();
  try {
    localStorage.setItem(
      'pop-up-window',
      JSON.stringify(data)
    );
  } catch {
    throw new Error(`设置 localStorage pop-up-window 错误`);
  }
};
/**
* @function switchModalWithPop
* @description 是否打开引导弹窗
* @param {string} _ - [POP_UP_PROJECT_DATA] 弹窗类型
* @returns {boolean}
*/
export const switchModalWithPop = _ => {
  const time = getTimeWithPop(_);
  if (time && time >= POP_UP_PROJECT_DATA[_]) return false;
  return true;
};
</syntaxhighlight>}}