64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, reactive } from 'vue'
|
|
import api from '../api/client'
|
|
|
|
export const useCalculatorStore = defineStore('calculator', () => {
|
|
const file = ref(null)
|
|
const materialId = ref(null)
|
|
const settings = reactive({
|
|
infill_percent: 30,
|
|
layer_height_mm: 0.2,
|
|
quantity: 1,
|
|
post_processing: [],
|
|
})
|
|
const result = ref(null)
|
|
const loading = ref(false)
|
|
const error = ref(null)
|
|
const uploadProgress = ref(0)
|
|
|
|
async function calculate() {
|
|
if (!file.value || !materialId.value) return
|
|
|
|
loading.value = true
|
|
error.value = null
|
|
uploadProgress.value = 0
|
|
|
|
const formData = new FormData()
|
|
formData.append('file', file.value)
|
|
formData.append('material_id', materialId.value)
|
|
formData.append('infill_percent', settings.infill_percent)
|
|
formData.append('layer_height_mm', settings.layer_height_mm)
|
|
formData.append('quantity', settings.quantity)
|
|
formData.append('post_processing', settings.post_processing.join(','))
|
|
|
|
try {
|
|
const { data } = await api.post('/calculate', formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
onUploadProgress: (e) => {
|
|
uploadProgress.value = Math.round((e.loaded / e.total) * 100)
|
|
},
|
|
})
|
|
result.value = data
|
|
} catch (e) {
|
|
error.value = e.response?.data?.detail || 'Ошибка расчёта'
|
|
result.value = null
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
function reset() {
|
|
file.value = null
|
|
materialId.value = null
|
|
settings.infill_percent = 30
|
|
settings.layer_height_mm = 0.2
|
|
settings.quantity = 1
|
|
settings.post_processing = []
|
|
result.value = null
|
|
error.value = null
|
|
uploadProgress.value = 0
|
|
}
|
|
|
|
return { file, materialId, settings, result, loading, error, uploadProgress, calculate, reset }
|
|
})
|