|
|
@@ -24,7 +24,7 @@
|
|
|
</view>
|
|
|
<view class="medicine-actions">
|
|
|
<button class="action-btn edit-btn" @click="editMedicine(medicine)">编辑</button>
|
|
|
- <button class="action-btn delete-btn" @click="deleteMedicine(medicine.id)">删除</button>
|
|
|
+ <button class="action-btn delete-btn" @click="deleteMedicineHandler(medicine.id)">删除</button>
|
|
|
</view>
|
|
|
</view>
|
|
|
</view>
|
|
|
@@ -89,27 +89,24 @@
|
|
|
<script setup lang="ts">
|
|
|
import { ref, computed, onMounted } from 'vue'
|
|
|
import CustomNav from '@/components/custom-nav.vue'
|
|
|
+import {
|
|
|
+ getMedicineList,
|
|
|
+ createMedicine,
|
|
|
+ updateMedicine,
|
|
|
+ deleteMedicine
|
|
|
+} from '@/api/medicine'
|
|
|
|
|
|
interface Medicine {
|
|
|
id: string
|
|
|
name: string
|
|
|
pinyinFirstLetters: string
|
|
|
barcode?: string // 添加条形码字段,可选
|
|
|
+ createTime?: string
|
|
|
+ updateTime?: string
|
|
|
}
|
|
|
|
|
|
// 药品列表
|
|
|
-const medicines = ref<Medicine[]>([
|
|
|
- { id: '1', name: '阿司匹林', pinyinFirstLetters: 'asp', barcode: '6920224840072' },
|
|
|
- { id: '2', name: '硝苯地平', pinyinFirstLetters: 'xbd', barcode: '6920224840089' },
|
|
|
- { id: '3', name: '盐酸二甲双胍片', pinyinFirstLetters: 'ysejsg', barcode: '6920224840096' },
|
|
|
- { id: '4', name: '格列美脲', pinyinFirstLetters: 'glm', barcode: '6920224840102' },
|
|
|
- { id: '5', name: '瑞格列奈', pinyinFirstLetters: 'rgl', barcode: '6920224840119' },
|
|
|
- { id: '6', name: '胰岛素', pinyinFirstLetters: 'yds', barcode: '6920224840126' },
|
|
|
- { id: '7', name: '美托洛尔', pinyinFirstLetters: 'mtl', barcode: '6920224840133' },
|
|
|
- { id: '8', name: '氨氯地平', pinyinFirstLetters: 'ald', barcode: '6920224840140' },
|
|
|
- { id: '9', name: '辛伐他汀', pinyinFirstLetters: 'xfd', barcode: '6920224840157' },
|
|
|
- { id: '10', name: '阿托伐他汀', pinyinFirstLetters: 'atf', barcode: '6920224840164' }
|
|
|
-])
|
|
|
+const medicines = ref<Medicine[]>([])
|
|
|
|
|
|
// 搜索关键词
|
|
|
const searchKeyword = ref('')
|
|
|
@@ -125,6 +122,13 @@ const currentMedicine = ref<Medicine>({
|
|
|
pinyinFirstLetters: ''
|
|
|
})
|
|
|
|
|
|
+// 分页参数
|
|
|
+const pagination = ref({
|
|
|
+ pageNum: 1,
|
|
|
+ pageSize: 10,
|
|
|
+ total: 0
|
|
|
+})
|
|
|
+
|
|
|
// 过滤后的药品列表
|
|
|
const filteredMedicines = computed(() => {
|
|
|
if (!searchKeyword.value.trim()) {
|
|
|
@@ -140,7 +144,7 @@ const filteredMedicines = computed(() => {
|
|
|
|
|
|
// 处理搜索
|
|
|
const handleSearch = () => {
|
|
|
- // 实际逻辑在 computed 属性中处理
|
|
|
+ loadMedicineList()
|
|
|
}
|
|
|
|
|
|
// 显示添加模态框
|
|
|
@@ -163,18 +167,30 @@ const editMedicine = (medicine: Medicine) => {
|
|
|
}
|
|
|
|
|
|
// 删除药品
|
|
|
-const deleteMedicine = (id: string) => {
|
|
|
+const deleteMedicineHandler = (id: string) => {
|
|
|
uni.showModal({
|
|
|
title: '确认删除',
|
|
|
content: '确定要删除这个药品吗?',
|
|
|
- success: (res) => {
|
|
|
+ success: async (res) => {
|
|
|
if (res.confirm) {
|
|
|
- const index = medicines.value.findIndex(m => m.id === id)
|
|
|
- if (index !== -1) {
|
|
|
- medicines.value.splice(index, 1)
|
|
|
+ try {
|
|
|
+ const result: any = await deleteMedicine(id)
|
|
|
+ if (result?.data?.code === 200) {
|
|
|
+ uni.showToast({
|
|
|
+ title: '删除成功',
|
|
|
+ icon: 'success'
|
|
|
+ })
|
|
|
+ loadMedicineList()
|
|
|
+ } else {
|
|
|
+ uni.showToast({
|
|
|
+ title: result?.data?.message || '删除失败',
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
uni.showToast({
|
|
|
- title: '删除成功',
|
|
|
- icon: 'success'
|
|
|
+ title: '删除失败',
|
|
|
+ icon: 'none'
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -183,7 +199,7 @@ const deleteMedicine = (id: string) => {
|
|
|
}
|
|
|
|
|
|
// 保存药品(添加或更新)
|
|
|
-const saveMedicine = () => {
|
|
|
+const saveMedicine = async () => {
|
|
|
if (!currentMedicine.value.name.trim()) {
|
|
|
uni.showToast({
|
|
|
title: '请输入药品名称',
|
|
|
@@ -200,32 +216,36 @@ const saveMedicine = () => {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if (isEditing.value) {
|
|
|
- // 更新药品
|
|
|
- const index = medicines.value.findIndex(m => m.id === currentMedicine.value.id)
|
|
|
- if (index !== -1) {
|
|
|
- medicines.value[index] = { ...currentMedicine.value }
|
|
|
+ try {
|
|
|
+ let result: any;
|
|
|
+ if (isEditing.value) {
|
|
|
+ // 更新药品
|
|
|
+ result = await updateMedicine(currentMedicine.value.id, currentMedicine.value)
|
|
|
+ } else {
|
|
|
+ // 添加药品
|
|
|
+ const { id, ...createData } = currentMedicine.value
|
|
|
+ result = await createMedicine(createData)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result?.data?.code === 200) {
|
|
|
uni.showToast({
|
|
|
- title: '更新成功',
|
|
|
+ title: isEditing.value ? '更新成功' : '添加成功',
|
|
|
icon: 'success'
|
|
|
})
|
|
|
+ closeModal()
|
|
|
+ loadMedicineList()
|
|
|
+ } else {
|
|
|
+ uni.showToast({
|
|
|
+ title: result?.data?.message || (isEditing.value ? '更新失败' : '添加失败'),
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
}
|
|
|
- } else {
|
|
|
- // 添加药品
|
|
|
- const newMedicine: Medicine = {
|
|
|
- id: Date.now().toString(),
|
|
|
- name: currentMedicine.value.name.trim(),
|
|
|
- pinyinFirstLetters: currentMedicine.value.pinyinFirstLetters?.trim().toLowerCase(),
|
|
|
- barcode: currentMedicine.value.barcode?.trim()
|
|
|
- }
|
|
|
- medicines.value.push(newMedicine)
|
|
|
+ } catch (error) {
|
|
|
uni.showToast({
|
|
|
- title: '添加成功',
|
|
|
- icon: 'success'
|
|
|
+ title: isEditing.value ? '更新失败' : '添加失败',
|
|
|
+ icon: 'none'
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
- closeModal()
|
|
|
}
|
|
|
|
|
|
// 关闭模态框
|
|
|
@@ -263,9 +283,36 @@ const scanBarcode = () => {
|
|
|
// #endif
|
|
|
}
|
|
|
|
|
|
+// 加载药品列表
|
|
|
+const loadMedicineList = async () => {
|
|
|
+ try {
|
|
|
+ const params = {
|
|
|
+ pageNum: pagination.value.pageNum,
|
|
|
+ pageSize: pagination.value.pageSize,
|
|
|
+ keyword: searchKeyword.value.trim() || undefined
|
|
|
+ }
|
|
|
+
|
|
|
+ const result: any = await getMedicineList(params)
|
|
|
+ if (result?.data?.code === 200) {
|
|
|
+ medicines.value = result.data.data.records
|
|
|
+ pagination.value.total = result.data.data.total
|
|
|
+ } else {
|
|
|
+ uni.showToast({
|
|
|
+ title: '获取药品列表失败',
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ uni.showToast({
|
|
|
+ title: '获取药品列表失败',
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 页面加载时
|
|
|
onMounted(() => {
|
|
|
- // 可以在这里从服务器获取药品列表数据
|
|
|
+ loadMedicineList()
|
|
|
})
|
|
|
</script>
|
|
|
|