Explorar o código

完成千分符功能

yuanmingze hai 1 mes
pai
achega
64dadf47e2

+ 1 - 0
src/auto-imports.d.ts

@@ -16,6 +16,7 @@ declare global {
   const defineComponent: typeof import('vue').defineComponent
   const defineStore: typeof import('pinia').defineStore
   const effectScope: typeof import('vue').effectScope
+  const formatThousands: typeof import('@/utils/formatThousands').formatThousands
   const getActivePinia: typeof import('pinia').getActivePinia
   const getCurrentInstance: typeof import('vue').getCurrentInstance
   const getCurrentScope: typeof import('vue').getCurrentScope

+ 2 - 0
src/main.ts

@@ -5,6 +5,7 @@ import { setAuthHandlers } from '@/services/request'
 
 import { setupStore } from '@/stores'
 import { useUserStoreWithOut } from '@/stores/modules/user'
+import { formatThousands } from '@/utils/formatThousands'
 
 import FormField from '@/components/FormField/index.vue'
 
@@ -18,6 +19,7 @@ import App from './App.vue'
 export function createApp() {
   const app = createSSRApp(App)
   app.component('FormField', FormField)
+  app.config.globalProperties.formatThousands = formatThousands
 
   setupStore(app)
   return {

+ 1 - 6
src/pages-mine/points-rule/index.vue

@@ -51,7 +51,7 @@
                   />
                 </view>
 
-                <text class="score-text">+{{ formatScore(item.score) }}积分</text>
+                <text class="score-text">+{{ formatThousands(item.score) || '0' }}积分</text>
               </view>
             </view>
           </template>
@@ -219,11 +219,6 @@ const openDesc = (task: TaskRuleItem) => {
   descVisible.value = true
 }
 
-const formatScore = (score: TaskRuleItem['score']) => {
-  const value = Number(score)
-
-  return Number.isFinite(value) ? value : score || 0
-}
 </script>
 
 <style lang="scss" scoped>

+ 1 - 5
src/pages-task/completed-record/index.vue

@@ -85,7 +85,7 @@
               class="record-card__score"
               :class="{ 'record-card__score--disabled': isRejectedRecord(item) }"
             >
-              +{{ formatAmount(item.score) }}积分
+              +{{ formatThousands(item.score) }}积分
             </view>
           </view>
         </view>
@@ -307,10 +307,6 @@ const formatDate = (value: string | null) => {
   return date.isValid() ? date.format('YYYY-MM-DD') : '-'
 }
 
-const formatAmount = (value: string | number | null) => {
-  return value === null || value === undefined || value === '' ? '-' : value
-}
-
 onReachBottom(loadMoreRecordList)
 
 onLoad(() => {

+ 4 - 11
src/pages-task/points-package-record/index.vue

@@ -115,10 +115,10 @@
             <view v-if="isSettledPackage(item)" class="record-card__settle">
               <view class="record-card__settle-item">结算状态:已结算</view>
               <view class="record-card__settle-item"
-                >税后收入:{{ formatAmount(item.userRealAmount) }}</view
+                >税后收入:{{ formatThousands(item.userRealAmount) }}</view
               >
               <view class="record-card__settle-item"
-                >已交税金:{{ formatAmount(item.actualTax) }}</view
+                >已交税金:{{ formatThousands(item.actualTax) }}</view
               >
             </view>
 
@@ -500,11 +500,11 @@ const getProgressPercentage = (item: ScorePackageRecordItem) => {
  */
 const getProgressText = (item: ScorePackageRecordItem) => {
   if (isScorePackage(item)) {
-    return `${formatAmount(item.finishScore)}/${formatAmount(item.score)}`
+    return `${formatThousands(item.finishScore) || '-'}/${formatThousands(item.score) || '-'}`
   }
 
   const finishedNum = String(item.taskAddFlag) === '1' ? item.taskFinishNum : item.taskNum2
-  return `${formatAmount(finishedNum)}/${formatAmount(item.taskNum2)}`
+  return `${formatThousands(finishedNum) || '-'}/${formatThousands(item.taskNum2) || '-'}`
 }
 
 /**
@@ -517,13 +517,6 @@ const formatDate = (value: Nullable<string>) => {
   return date.isValid() ? date.format('YYYY-MM-DD') : '-'
 }
 
-/**
- * 格式化金额 / 数值
- */
-const formatAmount = (value: Nullable<string | number>) => {
-  return value === null || value === undefined || value === '' ? '-' : value
-}
-
 /**
  * 获取错误文案
  */

+ 1 - 1
src/pages-task/task-form/composables/useTaskFormAction.ts

@@ -359,7 +359,7 @@ export const useTaskFormAction = ({
         return false
       }
 
-      showMessage(`提交成功,若审核通过可获得${score}积分`, SUCCESS_TOAST_DURATION)
+      showMessage(`提交成功,若审核通过可获得${formatThousands(score)}积分`, SUCCESS_TOAST_DURATION)
     }
 
     if (disposed) return false

+ 1 - 1
src/pages-task/task-package/list.vue

@@ -86,7 +86,7 @@
 
           <view class="task-card__meta">
             <text>需求人数:{{ item.userGetNum }}/{{ item.userNum }}</text>
-            <text>预计积分值:{{ item.score }}分</text>
+            <text>预计积分值:{{ formatThousands(item.score) }}分</text>
             <text v-if="item.taskNum">任务数:{{ item.taskNum }}个</text>
           </view>
 

+ 7 - 0
src/types/formatThousands.d.ts

@@ -0,0 +1,7 @@
+export {}
+
+declare module 'vue' {
+  interface ComponentCustomProperties {
+    formatThousands: typeof import('@/utils/formatThousands').formatThousands
+  }
+}

+ 22 - 0
src/utils/formatThousands.ts

@@ -0,0 +1,22 @@
+/**
+ * 给数字添加千分位分隔符,保留原有小数位,不做四舍五入。
+ * 空值返回空字符串,非数字内容原样返回。
+ */
+export function formatThousands(value: string | number | null | undefined): string {
+  if (value === null || value === undefined) return ''
+
+  const text = String(value).trim()
+  if (!text) return ''
+
+  // 已格式化的数字可以重复调用;不改写逗号位置不合法的内容。
+  if (text.includes(',') && !/^[+-]?\d{1,3}(?:,\d{3})+(?:\.\d+)?$/.test(text)) {
+    return text
+  }
+
+  const normalized = text.replace(/,/g, '')
+  const match = /^([+-]?)(\d+)(\.\d+)?$/.exec(normalized)
+  if (!match) return text
+
+  const [, sign, integer, fraction = ''] = match
+  return `${sign}${integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}${fraction}`
+}

+ 1 - 1
vite.config.ts

@@ -17,7 +17,7 @@ export default defineConfig(async ({ mode }) => {
       tsconfigPaths(),
       UniKuRoot(),
       AutoImport({
-        imports: ['vue', 'pinia'],
+        imports: ['vue', 'pinia', { '@/utils/formatThousands': ['formatThousands'] }],
         dts: 'src/auto-imports.d.ts',
       }),
       Components({