123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import { TOOL_ACTION_TYPE } from '@components/comment/common/ItemToolBar';
- export type AuthType = 'guest'|'user'|'owner'|'unknow'
-
- /**
- * HTML 编码
- * 将 < > 等字符串进行编码
- * @param {string} str 文本
- */
- export function htmlEncode(str: string) {
- if (!str) return "";
- return str.replace(/[<>]/gim, function(i) {
- return "&#" + i.charCodeAt(0) + ";";
- });
- }
-
- export const getUserType = (currentUser: any, targetUserId: number): AuthType => {
- if (!currentUser) return 'guest';
- if (currentUser.id === targetUserId) return 'owner';
- if (currentUser.id) return 'user';
- return 'unknow';
- }
-
- export const getActionAuth = (userType: AuthType, actionType: TOOL_ACTION_TYPE) => {
- if (userType === 'guest') {
- return [
- TOOL_ACTION_TYPE.FAVOR
- ].includes(actionType);
- }
- if (userType === 'owner') {
- return [
- TOOL_ACTION_TYPE.FAVOR,
- TOOL_ACTION_TYPE.REPLY,
- TOOL_ACTION_TYPE.EDIT,
- TOOL_ACTION_TYPE.DELETE,
- ].includes(actionType);
- }
- if (userType === 'user') {
- return [
- TOOL_ACTION_TYPE.FAVOR,
- TOOL_ACTION_TYPE.REPLY,
- ].includes(actionType);
- }
- }
-
- export function isUrl(inputString: string) {
- // 需完整匹配
- const regexp = /^((http(s)?:)?\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
- var res = inputString.match(regexp);
- if (res === null) return false;
- else return true;
- }
|