你提供的这段代码是 Vue 2 选项式写法的组件逻辑,包含了采购相关的所有方法和状态。要直接放进你之前给的 Vue 3 <script setup> 主页面里,需要做一些转换和调整。
方案1:直接放进主页面 <script setup> 并转换为组合式 API
我帮你把这段代码转换成组合式写法,方便直接放进 <script setup>,并且和你之前的代码无缝集成。
转换后的组合式写法示例(放在 <script setup> 中)
import { ref, computed, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import uniq from 'lodash/uniq'
import { multiplied, plus } from '@/utils/bignumber'
import { Modal, message } from 'ant-design-vue'
import { apiPurchase } from '@/api/daodi'
// 传入响应式的商品列表和选中id数组
export function usePurchase(prodListRef, selectedRowKeysRef) {
const router = useRouter()
const selectAll = ref(false)
const mVisible = ref(false)
const selectedGoods = computed(() => selectedRowKeysRef.value.length || 0)
const selectedGoodsAmount = computed(() => {
const selectedProd = prodListRef.value.filter(prod => selectedRowKeysRef.value.includes(prod.id))
if (selectedProd.length === 0) return 0
const priceStrategies = {
1: (prev, cur) => {
const curAmount = multiplied(cur.salesPrice, cur.purchaseQuantity || 0)
return plus(prev, curAmount)
},
2: (prev, cur) => {
const { purchaseQuantity, wholesalePrices } = cur
const adaptiveWholesalePrice = wholesalePrices.find(item => {
const purchaseQuantityGTQuantityMin = +purchaseQuantity >= +item.quantityMin
const purchaseQuantityLTQuantityMax = +purchaseQuantity <= +item.quantityMax || !item.quantityMax
return purchaseQuantityGTQuantityMin && purchaseQuantityLTQuantityMax
})
const curAmount = multiplied(adaptiveWholesalePrice?.price || 0, purchaseQuantity || 0)
return plus(prev, curAmount)
}
}
return selectedProd.reduce((prev, cur) => priceStrategies[cur.priceType](prev, cur), 0)
})
function onSelectChange({ id }, val) {
const idx = selectedRowKeysRef.value.indexOf(id)
if (val && idx === -1) selectedRowKeysRef.value.push(id)
if (!val && idx > -1) selectedRowKeysRef.value.splice(idx, 1)
}
function clearSelectedRowKeys() {
selectedRowKeysRef.value = []
}
function handleSelectAll(checked) {
if (!checked) {
selectAll.value = false
clearSelectedRowKeys()
return
}
const availableProd = prodListRef.value.filter(e => !e.purchaseDisabled)
const withoutPurchaseQtyProd = availableProd.filter(e => !e.purchaseQuantity)
if (withoutPurchaseQtyProd.length !== 0) {
withoutPurchaseQtyProd.forEach(prod => {
prod.purchaseQuantityValid = false
})
return message.error('请输入采购数量')
}
selectAll.value = true
const prodIdList = availableProd.map(prod => prod.id)
selectedRowKeysRef.value = uniq([...selectedRowKeysRef.value, ...prodIdList])
}
function handlePurchaseQuantityInput(id, val) {
const prod = prodListRef.value.find(prod => prod.id === id)
if (prod) prod.purchaseQuantity = val
}
function beforeCreateValid(purchaseGoods) {
let valid = true
purchaseGoods.forEach(item => {
if (!item.purchaseQuantity) {
item.purchaseQuantityValid = false
valid = false
}
})
return valid ? '' : '请输入采购数量'
}
function handelPurchase(typeName) {
if (selectedRowKeysRef.value.length === 0) return message.error('请选择摘牌产品')
const purchaseGoods = prodListRef.value.filter(prod => selectedRowKeysRef.value.includes(prod.id))
const errMsg = beforeCreateValid(purchaseGoods)
if (errMsg) {
message.error(errMsg)
return
}
const baoGongArr = purchaseGoods.filter(item => item.isBaoGong === true)
if (typeName == 'goodsDelist') {
setGoodsDelistRecord(baoGongArr)
setApiPurchase(purchaseGoods)
return
}
if (baoGongArr.length === 1) {
checkGoodsDelistRecord(baoGongArr)
} else if (baoGongArr.length > 1) {
message.error('不可同时摘牌多个保供产品,请重新选择')
return false
} else {
getConfirmPurchase(purchaseGoods)
}
}
function getConfirmPurchase(purchaseGoods) {
Modal.confirm({
title: '确认摘牌?',
content: '点击确认摘牌后,系统将创建购销合同',
okText: '确认摘牌',
cancelText: '再想想',
onOk: async () => {
const data = {
bizType: 0,
goodsList: purchaseGoods.map(e => ({
goodsId: e.id,
delistedQuantity: e.purchaseQuantity
}))
}
const result = await apiPurchase(data)
const { code, msg, data: contractId } = result
if (+code !== 0) {
message.error(msg)
} else {
goToTcmSaleContract(contractId)
}
}
})
}
async function setApiPurchase(purchaseGoods) {
const data = {
bizType: 0,
goodsList: purchaseGoods.map(e => ({
goodsId: e.id,
delistedQuantity: e.purchaseQuantity
}))
}
const result = await apiPurchase(data)
const { code, msg, data: contractId } = result
if (+code !== 0) {
message.error(msg)
} else {
goToTcmSaleContract(contractId)
}
}
async function setGoodsDelistRecord(baoGongArr) {
// 这里你需要自己实现请求逻辑,示例:
// await fetch('/tcme/goodsDelistRecord/save', { method: 'POST', body: JSON.stringify(...) })
}
async function checkGoodsDelistRecord(baoGongArr) {
// 这里你需要自己实现请求逻辑,示例:
// const res = await fetch(...)
// if (res.code === 0) mVisible.value = true
// else message.error(res.msg)
}
function handleCancel() {
mVisible.value = false
}
function goToTcmSaleContract(id) {
Modal.confirm({
title: '已创建购销合同,请前往编辑买家信息',
content: '与挂牌方确认交易信息后可以进行线上签订,双方签订完成后即可下单交易。',
okText: '立即编辑',
cancelText: '稍后编辑',
onOk() {
if (id) {
router.push({ path: '/buyer/tcm-sale-contract/contract-edit', query: { id, isEdit: true } })
} else {
router.push({ path: 'buyer/tcm-sale-contract/index', query: { activeTab: 0 } })
}
},
onCancel() {
Modal.confirm({
class: 'daodi-mall__text--bold',
content: '您可前往【我的工作台】-【合同管理】-【采购合同】起草中列表中,进行编辑。',
okText: '我知道了'
})
}
})
}
return {
selectAll,
mVisible,
selectedGoods,
selectedGoodsAmount,
handelPurchase,
onSelectChange,
clearSelectedRowKeys,
handleSelectAll,
handlePurchaseQuantityInput,
handleCancel
}
}