No Description

index.ts 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { TOOL_ACTION_TYPE } from '@components/comment/common/ItemToolBar';
  2. export type AuthType = 'guest'|'user'|'owner'|'unknow'
  3. /**
  4. * HTML 编码
  5. * 将 < > 等字符串进行编码
  6. * @param {string} str 文本
  7. */
  8. export function htmlEncode(str: string) {
  9. if (!str) return "";
  10. return str.replace(/[<>]/gim, function(i) {
  11. return "&#" + i.charCodeAt(0) + ";";
  12. });
  13. }
  14. export const getUserType = (currentUser: any, targetUserId: number): AuthType => {
  15. if (!currentUser) return 'guest';
  16. if (currentUser.id === targetUserId) return 'owner';
  17. if (currentUser.id) return 'user';
  18. return 'unknow';
  19. }
  20. export const getActionAuth = (userType: AuthType, actionType: TOOL_ACTION_TYPE) => {
  21. if (userType === 'guest') {
  22. return [
  23. TOOL_ACTION_TYPE.FAVOR
  24. ].includes(actionType);
  25. }
  26. if (userType === 'owner') {
  27. return [
  28. TOOL_ACTION_TYPE.FAVOR,
  29. TOOL_ACTION_TYPE.REPLY,
  30. TOOL_ACTION_TYPE.EDIT,
  31. TOOL_ACTION_TYPE.DELETE,
  32. ].includes(actionType);
  33. }
  34. if (userType === 'user') {
  35. return [
  36. TOOL_ACTION_TYPE.FAVOR,
  37. TOOL_ACTION_TYPE.REPLY,
  38. ].includes(actionType);
  39. }
  40. }
  41. export function isUrl(inputString: string) {
  42. // 需完整匹配
  43. const regexp = /^((http(s)?:)?\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
  44. var res = inputString.match(regexp);
  45. if (res === null) return false;
  46. else return true;
  47. }