Files
test-subject/src/stores/common.js
2025-10-14 18:04:20 +08:00

50 lines
1.6 KiB
JavaScript

import { defineStore } from 'pinia'
export const useCommonStore = defineStore('common', () => {
/* 提示类公共函数-后期替换为跳转应用或下载链接 */
const download = () => uni.showToast({ title: 'downloadapp', icon: 'none' })
const openapp = () => uni.showToast({ title: '打开APP', icon: 'none' })
// 点赞数一类的数量格式工具函数
const formatCount = n =>
n >= 1_000_000 ? Math.floor(n / 100_000) / 10 + 'm+' :
n >= 1_000 ? Math.floor(n / 100) / 10 + 'k+' : n;
/* 富文本 评论@高亮 */
/**
* 把单个 atUsers 数组转成富文本节点
* @param {string[]} atUsers
* @returns {Array} rich-text nodes
*/
function atUsersToNodes(atUsers = []) {
return atUsers.map(name => ({
type: 'node',
name: 'span',
attrs: { style: 'color:#0969DA;', 'data-name': name },
children: [{ type: 'text', text: `@${name} ` }]
}))
}
/* 日期处理函数 */
/**
* 格式化日期,如果年份是当年则不显示年份
* @param {string|Date} date - 日期字符串或Date对象
* @returns {string} 格式化后的日期字符串
*/
function formatDate(date) {
const inputDate = new Date(date);
const currentYear = new Date().getFullYear();
const inputYear = inputDate.getFullYear();
if (inputYear === currentYear) {
// 当年:显示月份和日期
return `${inputDate.getMonth() + 1}-${inputDate.getDate()}`;
} else {
// 非当年:显示完整年月日
return `${inputYear}-${inputDate.getMonth() + 1}-${inputDate.getDate()}`;
}
}
return { download, openapp, formatCount, atUsersToNodes, formatDate }
})