182 lines
4.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ref, onMounted, reactive } from 'vue';
import inputSuppliesApis from '@/apis/inputSuppliesApi';
const { getMaterailTypes } = inputSuppliesApis;
export function useBasicInfo(set = {}) {
const loadFinish = ref(false);
const searchCondition = Object.assign(
{
dataType: '',
moduleType: '',
},
set
);
/* ------ data ------ */
// #region
const materialTypes = reactive({
1: [{ value: '0', label: '全部' }],
2: [{ value: '0', label: '全部' }],
3: [{ value: '0', label: '全部' }],
4: [{ value: '0', label: '全部' }],
5: [{ value: '0', label: '全部' }],
});
/* 不包含顶级 */
const materialTwoLevel = reactive({});
const goodsUnitOptions = reactive([
{ value: '1', label: 'ml/瓶' },
{ value: '2', label: 'mg/盒' },
{ value: '3', label: 'kg/袋' },
]);
const useDosageUnit = reactive([
{ value: '1', label: '/亩' },
{ value: '2', label: '/平方米' },
{ value: '3', label: '/株' },
]);
// #endregion
/* ------ ------ */
// #region
/* 获取物资类型 */
async function getmaterialType(_set = {}) {
let params = Object.assign(searchCondition, _set);
let res = await getMaterailTypes(params);
if (res && res?.code == 200) {
res.data.forEach((item) => {
const { moduleType, children } = item;
materialTypes[moduleType].push(...handleTypes(children));
handleTwoLevel();
});
// console.log('materialTypes --- ', materialTypes);
// console.log('materialTwoLevel --- ', materialTwoLevel);
}
loadFinish.value = true;
let t = setInterval(() => {
loadFinish.value = false;
clearInterval(t);
}, 1000);
}
/* 将所有的数据处理成value label格式方便其他地方指直接使用 */
function handleTypes(arr) {
arr.forEach((item) => {
item.value = item.id;
item.label = item.dataName;
if (item.children.length > 0) {
item.children = handleTypes(item.children);
}
});
return arr;
}
/* 处理二级及以下的联动数据 */
function handleTwoLevel() {
for (let key in materialTypes) {
materialTwoLevel[key] = {};
materialTypes[key].forEach((v) => {
if (v.value != 0) {
materialTwoLevel[key][v.dataType] = v.children;
}
});
}
}
/* 获取标签的名字 */
function targetName(arr, ids, _name) {
let name = '';
if (!arr || !arr.length || ids.length < 1) {
return name;
}
let _ids = JSON.parse(JSON.stringify(ids));
if (typeof ids == 'string') {
let obj = arr.find((v) => v.value == _ids) || { value: '', label: '', children: [] };
name = obj.label;
} else {
let obj = arr.find((v) => v.value == _ids[0]) || { value: '', label: '', children: [] };
name = _name + (_name ? '/' : '') + obj.label;
_ids.splice(0, 1);
if (_ids.length > 0) {
name = targetName(obj.children, _ids, name);
}
}
return name;
}
function filterTypes(_set = {}) {
let filterType = Object.assign(
{
dataType: '1',
moduleType: '1',
},
_set
);
let all = materialTypes[filterType.moduleType];
let _arr = [];
all.forEach((item) => {
if (filterType.dataType == item.dataType) {
_arr = item.children;
}
});
return _arr;
}
const _timer = ref(null);
function defaultGet(fc = () => {}) {
_timer.value = setTimeout(() => {
clearTimeout(_timer.value);
if (loadFinish.value) {
fc();
} else {
defaultGet(fc);
}
}, 200);
}
/* t 1规格 2用量 */
function handleNumUnit(_info = {}) {
let text = '';
if (!_info.type || !_info.type < -1) return text;
const info = Object.assign(
{
num1: '',
unit1: '',
num2: '',
unit2: '',
type: -2,
},
_info
);
if (info.type == -1) {
let u1 = (goodsUnitOptions.find((_v) => _v.value == info.unit1) || { label: '' }).label;
let u2 = (useDosageUnit.find((_v) => _v.value == info.unit2) || { label: '' }).label;
text = `${info.num2}${u1.split('/')[1]}${u2}`;
} else if (info.type == 1) {
text = `${info.num1}${(goodsUnitOptions.find((_v) => _v.value == info.unit1) || { label: '' }).label}`;
} else if (info.type == 2) {
text = `${info.num2}${(useDosageUnit.find((_v) => _v.value == info.unit2) || { label: '' }).label}`;
}
return text;
}
function handleShowName(text = '') {
if (!text || !text.includes('|')) return false;
let names = JSON.parse(text.split('|')[1]);
let _t = '';
names.forEach((v, i) => {
_t += (i == 0 ? '' : '') + v;
});
return _t;
}
// #endregion
onMounted(getmaterialType);
return {
defaultGet,
loadFinish,
materialTypes,
materialTwoLevel,
goodsUnitOptions,
useDosageUnit,
getmaterialType,
targetName,
filterTypes,
handleShowName,
handleNumUnit,
};
}