Browse Source

自定义方案编辑删除修改新增

liyuliangjiazai 5 months ago
parent
commit
e415c452a1

+ 217 - 8
code/ajyApp/pages/device/acupointEdit/acupointEdit.vue

@@ -6,12 +6,14 @@
 			<view class="nav-back" @click="goBack">
 				<image src="/static/public/back.png" mode="aspectFill" class="back-icon"></image>
 			</view>
-			<text class="nav-title">穴位定位</text>
+			<text class="nav-title">{{pageMode === 'edit' ? '编辑方案' : '新增方案'}}</text>
 			<view class="nav-right">
-				<text class="nav-count">{{acupointList.length}}穴位</text>
+				<text class="nav-save-btn" @click="savePlan">保存</text>
 			</view>
 		</view>
 
+
+
 		<!-- 人体模型 + 穴位标注 -->
 		<scroll-view class="body-scroll" :scroll-y="!isLandscape" :show-scrollbar="false" :scroll-top="scrollTop" @scroll="onScroll" scroll-with-animation>
 			<view class="body-content">
@@ -33,7 +35,8 @@
 						:key="point.id"
 						class="acupoint-dot"
 						:class="{
-							'acupoint-active': selectedIndex === index
+							'acupoint-active': selectedIndex === index,
+							'acupoint-edited': selectedIndex !== index && (point.customSide || point._inPlan)
 						}"
 						:style="{
 							left: point.screenX + 'px',
@@ -60,11 +63,12 @@
 						class="list-item"
 						:class="{
 							'list-item-active': selectedIndex === index,
+							'list-item-edited': selectedIndex !== index && (point.customSide || point._inPlan),
 							'list-item-full': point.isSingle
 						}"
 						@click="onListItemTap(index)"
 					>
-						<view class="item-index" :class="{'item-index-active': selectedIndex === index}">
+						<view class="item-index" :class="{'item-index-active': selectedIndex === index || point.customSide || point._inPlan}">
 							<text class="index-text">{{index + 1}}</text>
 						</view>
 						<text class="item-name">{{point.acupointName}}</text>
@@ -150,13 +154,37 @@
 				<text class="loading-text">加载中...</text>
 			</view>
 		</view>
+
+		<!-- 新增方案名称弹窗 -->
+		<view class="popup-mask" v-if="showNamePopup" @click="showNamePopup = false">
+			<view class="popup-content" @click.stop>
+				<view class="popup-header">
+					<text class="popup-title">保存方案</text>
+				</view>
+				<view class="popup-body">
+					<view class="form-group">
+						<text class="form-label">方案名称</text>
+						<input class="name-popup-input" v-model="planName" placeholder="请输入方案名称" maxlength="100" focus />
+					</view>
+				</view>
+				<view class="popup-footer">
+					<view class="btn btn-cancel" @click="showNamePopup = false">取消</view>
+					<view class="btn btn-confirm" @click="confirmSavePlan">确认保存</view>
+				</view>
+			</view>
+		</view>
+
+		<ay-toast ref="ayToast" />
 	</view>
 </template>
 
 <script>
 import { getBackAcupoints } from '@/utils/api/acupoint.js'
+import { createCustomPlan, updateCustomPlan, getCustomPlanDetail } from '@/utils/api/care.js'
+import ayToast from '@/components/ay-toast/ay-toast.nvue'
 
 export default {
+	components: { ayToast },
 	data() {
 		return {
 			isLandscape: false,
@@ -168,7 +196,7 @@ export default {
 			// SVG viewBox: 0 0 300 400, 一体式实心剪影
 			containerWidth: 345,
 			containerHeight: 460,
-			// 大椎穴(0,0)在容器中的位置
+			// 大椚穴(0,0)在容器中的位置
 			// 颈根/斜方肌起点(C7)在SVG中 y=78 -> 按比例放大1.15倍
 			originX: 172.5,
 			originY: 89.7,
@@ -188,7 +216,15 @@ export default {
 			methodOptions: ['温和灸', '雀啄灸', '回旋灸'],
 			
 			scrollTop: 0,
-			oldScrollTop: 0
+			oldScrollTop: 0,
+			
+			// 方案相关
+			pageMode: 'create', // 'create' | 'edit'
+			planId: null,
+			planName: '',
+			planData: null, // 编辑模式下的原始方案数据
+			saving: false,
+			showNamePopup: false
 		}
 	},
 	computed: {
@@ -220,6 +256,8 @@ export default {
 		const sysInfo = uni.getSystemInfoSync()
 		this.statusBarHeight = sysInfo.statusBarHeight || 44
 		this.profileId = decodeURIComponent(options.profileId || '')
+		this.pageMode = options.mode || 'create'
+		this.planId = options.planId || null
 		this.checkOrientation(sysInfo)
 		this.loadAcupoints()
 	},
@@ -292,7 +330,7 @@ export default {
 				}
 			} catch (e) {
 				console.error('加载穴位数据失败:', e)
-				uni.showToast({ title: '穴位数据加载失败', icon: 'none' })
+				this.$refs.ayToast.error('穴位数据加载失败')
 				this.acupointList = []
 			} finally {
 				this.loading = false
@@ -303,6 +341,10 @@ export default {
 					}
 				})
 			}
+			// 穴位数据加载完成后,编辑模式下加载方案详情并回填
+			if (this.pageMode === 'edit' && this.planId && this.acupointList.length > 0) {
+				this.loadPlanDetail()
+			}
 		},
 
 		/** 点击穴位点 */
@@ -368,7 +410,7 @@ export default {
 		saveEdit() {
 			if (this.selectedIndex >= 0) {
 				const point = this.acupointList[this.selectedIndex]
-				// 更新穴位列表中的对应数据(使用Vue的响应式需要特殊处理,但这里我们直接改属性,若无法触发视图更新可用$set或splice,但简单修改通常有效)
+				// 更新穴位列表中的对应数据
 				point.customSide = this.editForm.side
 				point.method = this.editForm.method
 				point.temperature = this.editForm.temperature
@@ -377,6 +419,117 @@ export default {
 			this.closePopup()
 		},
 
+		/** 加载方案详情(编辑模式) */
+		async loadPlanDetail() {
+			try {
+				const res = await getCustomPlanDetail(this.planId)
+				if (res) {
+					this.planData = res
+					this.planName = res.name || ''
+					// 穴位列表已加载完成,直接回填方案步骤参数
+					if (res.steps && res.steps.length > 0) {
+						this.applyStepsToAcupoints(res.steps)
+					}
+				}
+			} catch (e) {
+				console.error('加载方案详情失败:', e)
+				this.$refs.ayToast.error('加载方案失败')
+			}
+		},
+
+		/** 将方案步骤参数回填到穴位列表 */
+		applyStepsToAcupoints(steps) {
+			const sideMap = { 1: '中心', 2: '左侧', 3: '右侧', 4: '双侧' }
+			const techniqueMap = { '2': '温和灸', '4': '回旋灸', '3': '雀啄灸' }
+			steps.forEach(step => {
+				const point = this.acupointList.find(p => p.acupointId === step.acupointId)
+				if (point) {
+					point.customSide = sideMap[step.side] || '中心'
+					point.method = techniqueMap[step.techniqueId] || '温和灸'
+					point.temperature = step.temperature || 45
+					point.duration = step.duration || 15
+					point._inPlan = true
+				}
+			})
+		},
+
+		/** 点击保存按钮 */
+		savePlan() {
+			if (this.saving) return
+			const steps = this.buildSteps()
+			if (steps.length === 0) {
+				this.$refs.ayToast.show('请至少编辑一个穴位参数')
+				return
+			}
+			if (this.pageMode === 'edit') {
+				// 编辑模式直接保存
+				this.doSavePlan()
+			} else {
+				// 新增模式弹出名称输入框
+				this.showNamePopup = true
+			}
+		},
+
+		/** 弹窗确认保存(新增模式) */
+		confirmSavePlan() {
+			if (!this.planName.trim()) {
+				this.$refs.ayToast.show('请输入方案名称')
+				return
+			}
+			this.showNamePopup = false
+			this.doSavePlan()
+		},
+
+		/** 执行保存方案 */
+		async doSavePlan() {
+			if (this.saving) return
+			if (!this.planName.trim()) {
+				this.$refs.ayToast.show('请输入方案名称')
+				return
+			}
+
+			this.saving = true
+			try {
+				const sideMap = { '中心': 1, '左侧': 2, '右侧': 3, '双侧': 4 }
+				const techniqueMap = { '温和灸': '2', '回旋灸': '4', '雀啄灸': '3' }
+				const steps = this.buildSteps()
+
+				const planData = {
+					name: this.planName.trim(),
+					status: 1,
+					steps: steps.map(point => ({
+						acupointId: point.acupointId,
+						side: sideMap[point.customSide] || 1,
+						temperature: Number(point.temperature) || 45,
+						duration: Number(point.duration) || 15,
+						techniqueId: techniqueMap[point.method] || '2'
+					}))
+				}
+
+				if (this.pageMode === 'edit' && this.planId) {
+					planData.id = this.planId
+					await updateCustomPlan(planData)
+					this.$refs.ayToast.success('修改成功')
+				} else {
+					await createCustomPlan(planData)
+					this.$refs.ayToast.success('新增成功')
+				}
+				setTimeout(() => {
+					uni.navigateBack()
+				}, 1000)
+			} catch (e) {
+				console.error('保存方案失败:', e)
+				this.$refs.ayToast.error(e.message || '保存失败')
+			} finally {
+				this.saving = false
+			}
+		},
+
+		/** 构建方案步骤(筛选已编辑的穴位) */
+		buildSteps() {
+			return this.acupointList.filter(point => point.customSide || point._inPlan)
+		},
+
 		/** 侧别标签 */
 		sideLabel(side) {
 			const map = { left: '左侧', right: '右侧', center: '中心' }
@@ -447,6 +600,50 @@ export default {
 	font-size: 12px;
 	color: rgba(255,255,255,0.8);
 }
+.nav-save-btn {
+	font-size: 14px;
+	color: #FFFFFF;
+	background: rgba(255,255,255,0.2);
+	padding: 5px 14px;
+	border-radius: 14px;
+	font-weight: 500;
+}
+
+/* 方案名称输入 */
+.plan-name-section {
+	display: flex;
+	flex-direction: row;
+	align-items: center;
+	margin: 0 15px 10px;
+	background: rgba(255,255,255,0.9);
+	border-radius: 12px;
+	padding: 10px 14px;
+}
+.plan-name-label {
+	font-size: 13px;
+	color: #545F71;
+	font-weight: 500;
+	margin-right: 10px;
+	flex-shrink: 0;
+}
+.plan-name-input {
+	flex: 1;
+	font-size: 14px;
+	color: #333333;
+	height: 32px;
+}
+
+/* 名称弹窗输入框 */
+.name-popup-input {
+	width: 100%;
+	height: 40px;
+	border: 1px solid #E8ECF0;
+	border-radius: 8px;
+	padding: 0 12px;
+	font-size: 14px;
+	color: #333333;
+	box-sizing: border-box;
+}
 
 /* 人体模型容器 */
 .body-scroll {
@@ -525,6 +722,14 @@ export default {
 	box-shadow: 0 4px 12px rgba(246, 82, 82, 0.4);
 	transform: scale(1.1);
 }
+/* 已编辑态 */
+.acupoint-edited .dot-inner {
+	width: 11px;
+	height: 11px;
+	background: #F65252;
+	border: 1.5px solid #FFFFFF;
+	box-shadow: 0 2px 8px rgba(246, 82, 82, 0.3);
+}
 
 /* 穴位列表 */
 .list-section {
@@ -578,6 +783,10 @@ export default {
 	box-shadow: 0 4px 12px rgba(56, 149, 136, 0.1);
 	transform: scale(1);
 }
+.list-item-edited {
+	background: rgba(246, 82, 82, 0.05);
+	border-color: rgba(246, 82, 82, 0.2);
+}
 .list-item-full {
 	margin-right: 50%;
 }

+ 56 - 4
code/ajyApp/pages/device/deviceInfo/deviceInfo.nvue

@@ -51,7 +51,12 @@
 
 				<!-- 方案选择 -->
 				<view class="planSection" v-if="modeType != 0 && picMode == 1">
-					<text class="planSectionTitle">选择方案</text>
+					<view class="planSectionHeader">
+						<text class="planSectionTitle">选择方案</text>
+						<view class="planAddBtn" v-if="modeType == 2" @click="goAddCustomPlan">
+							<text class="planAddBtnTxt">+ 新增</text>
+						</view>
+					</view>
 					<view class="planCard" @click="showPlanDrawer">
 						<view class="planCardLeft">
 							<text class="planCardName">{{selectedPlan ? selectedPlan.name : '点击选择方案'}}</text>
@@ -269,8 +274,8 @@
 					<text class="planEmptyTxt">加载中...</text>
 				</view>
 				<view class="planItem" :class="selectedPlan && selectedPlan.id === item.id ? 'planItemActive' : ''"
-					v-for="(item, index) in planList" :key="index" @click="selectPlan(item)">
-					<view class="planItemInfo">
+					v-for="(item, index) in planList" :key="index">
+					<view class="planItemInfo" @click="selectPlan(item)">
 						<text class="planItemName">{{item.name}}</text>
 						<text class="planItemDesc" v-if="item.description">{{item.description}}</text>
 						<view class="planItemMeta" v-if="item.steps || item.duration">
@@ -278,7 +283,11 @@
 							<text class="planMetaTxt" v-if="item.duration">约{{item.duration}}分钟</text>
 						</view>
 					</view>
-					<view class="planItemCheck">
+					<view class="planItemActions" v-if="modeType == 2">
+						<text class="planActionBtn planEditBtn" @click="goEditCustomPlan(item)">编辑</text>
+						<text class="planActionBtn planDeleteBtn" @click="confirmDeletePlan(item)">删除</text>
+					</view>
+					<view class="planItemCheck" v-else>
 						<view class="checkboxCustom">
 							<view v-if="selectedPlan && selectedPlan.id === item.id" class="checkboxCircle"></view>
 						</view>
@@ -393,6 +402,10 @@ export default {
 	onShow() {
 		// 返回页面时刷新当前用户
 		this.loadCurrentUser()
+		// 自定义模式下刷新方案列表(可能从新增/编辑页返回)
+		if (this.modeType === 2) {
+			this.loadPlanList(2)
+		}
 	},
 	onUnload() {
 		this.destroyAudio()
@@ -911,8 +924,25 @@ export default {
 .planSectionTitle {
 	font-size: 28rpx;
 	color: #9BA5B7;
+}
+.planSectionHeader {
+	flex-direction: row;
+	justify-content: space-between;
+	align-items: center;
 	margin-bottom: 16rpx;
 }
+.planAddBtn {
+	background-color: #389588;
+	border-radius: 24rpx;
+	padding-left: 20rpx;
+	padding-right: 20rpx;
+	padding-top: 8rpx;
+	padding-bottom: 8rpx;
+}
+.planAddBtnTxt {
+	font-size: 24rpx;
+	color: #FFFFFF;
+}
 .planCard {
 	flex-direction: row;
 	justify-content: space-between;
@@ -1020,6 +1050,28 @@ export default {
 	width: 50rpx;
 	align-items: center;
 }
+/* 方案操作按钮 */
+.planItemActions {
+	flex-direction: row;
+	align-items: center;
+}
+.planActionBtn {
+	font-size: 24rpx;
+	padding-left: 16rpx;
+	padding-right: 16rpx;
+	padding-top: 8rpx;
+	padding-bottom: 8rpx;
+	border-radius: 16rpx;
+	margin-left: 10rpx;
+}
+.planEditBtn {
+	color: #389588;
+	background-color: rgba(56,149,136,0.1);
+}
+.planDeleteBtn {
+	color: #F65252;
+	background-color: rgba(246,82,82,0.1);
+}
 /* 弹框样式 */
 .tip1 {
 	font-size: 28rpx;

+ 41 - 1
code/ajyApp/pages/device/deviceInfo/mixins/plan-mixin.js

@@ -2,7 +2,7 @@
  * 方案管理 mixin
  * 负责:方案列表加载、子方案选择、模式切换时刷新方案
  */
-import { getCareList, getCareDetail, getCustomPlanList, getCustomPlanDetail } from '@/utils/api/care.js'
+import { getCareList, getCareDetail, getCustomPlanList, getCustomPlanDetail, deleteCustomPlan } from '@/utils/api/care.js'
 
 export default {
 	data() {
@@ -99,6 +99,46 @@ export default {
 			this._applyPlanToCase(plan)
 		},
 
+		/** 跳转新增自定义方案 */
+		goAddCustomPlan() {
+			uni.navigateTo({
+				url: `/pages/device/acupointEdit/acupointEdit?profileId=${this.profileId}&mode=create`
+			})
+		},
+
+		/** 跳转编辑自定义方案 */
+		goEditCustomPlan(plan) {
+			this.isShowPlanDrawer = false
+			uni.navigateTo({
+				url: `/pages/device/acupointEdit/acupointEdit?profileId=${this.profileId}&mode=edit&planId=${plan.id}`
+			})
+		},
+
+		/** 删除自定义方案 */
+		confirmDeletePlan(plan) {
+			uni.showModal({
+				title: '确认删除',
+				content: `确定删除方案「${plan.name}」?删除后不可恢复。`,
+				success: async (res) => {
+					if (res.confirm) {
+						try {
+							await deleteCustomPlan(plan.id)
+							uni.showToast({ title: '删除成功', icon: 'success' })
+							// 从列表中移除
+							this.planList = this.planList.filter(p => p.id !== plan.id)
+							// 如果删除的是当前选中的方案,清空选择
+							if (this.selectedPlan && this.selectedPlan.id === plan.id) {
+								this.selectedPlan = null
+							}
+						} catch (e) {
+							console.error('删除方案失败', e)
+							uni.showToast({ title: '删除失败', icon: 'none' })
+						}
+					}
+				}
+			})
+		},
+
 		/**
 		 * 将方案数据应用到当前穴位配置
 		 */

+ 27 - 0
code/ajyApp/utils/api/care.js

@@ -45,3 +45,30 @@ export function getCustomPlanDetail(id) {
 export function getCareDetail(id) {
 	return http.get('/userCare/careDetail', { data: { id } })
 }
+
+/**
+ * 新增用户自定义方案
+ * @param {Object} data - 方案数据 { name, symptoms, applicableGender, ageMin, ageMax, description, status, steps }
+ * @returns {Promise<number>} 新方案ID
+ */
+export function createCustomPlan(data) {
+	return http.post('/app/custom-plan', data)
+}
+
+/**
+ * 修改用户自定义方案
+ * @param {Object} data - 方案数据 { id, name, symptoms, applicableGender, ageMin, ageMax, description, status, steps }
+ * @returns {Promise}
+ */
+export function updateCustomPlan(data) {
+	return http.put('/app/custom-plan', data)
+}
+
+/**
+ * 删除用户自定义方案
+ * @param {string|number} id - 方案ID
+ * @returns {Promise}
+ */
+export function deleteCustomPlan(id) {
+	return http.delete(`/app/custom-plan/${id}`)
+}