edit.vue 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. <template>
  2. <div class="price-edit-page">
  3. <!-- 顶部:标题 + 关闭 -->
  4. <div class="page-header">
  5. <div class="header-left" @click="goBack">
  6. <el-icon class="back-icon">
  7. <ArrowLeft />
  8. </el-icon>
  9. <span class="back-text">返回</span>
  10. </div>
  11. <h1 class="page-title">
  12. {{ viewMode ? "详情" : id ? "编辑" : "新建" }}
  13. </h1>
  14. <el-button v-if="viewMode" type="primary" @click="goEdit"> 编辑 </el-button>
  15. <div v-else class="header-right" @click="goBack">
  16. <el-icon class="close-icon">
  17. <Close />
  18. </el-icon>
  19. </div>
  20. </div>
  21. <div class="page-content">
  22. <el-form
  23. ref="ruleFormRef"
  24. :model="formModel"
  25. :rules="formRules"
  26. label-width="120px"
  27. class="price-form two-column-form"
  28. @submit.prevent
  29. >
  30. <!-- 左列 -->
  31. <div class="form-column form-column-left">
  32. <!-- 类型(仅美食类别显示菜品/套餐) -->
  33. <el-form-item v-if="isFoodModule" label="类型" prop="formType" required>
  34. <el-radio-group v-model="formModel.formType" :disabled="viewMode">
  35. <el-radio value="dish"> 菜品 </el-radio>
  36. <el-radio value="package"> 套餐 </el-radio>
  37. </el-radio-group>
  38. </el-form-item>
  39. <!-- 图片 -->
  40. <el-form-item label="图片" prop="images" required>
  41. <div class="image-upload-wrap">
  42. <UploadImgs
  43. v-model:file-list="imageFileList"
  44. :api="uploadImgStore"
  45. :limit="9"
  46. :file-size="5"
  47. :disabled="viewMode"
  48. class="price-list-upload"
  49. @update:file-list="onImageListChange"
  50. />
  51. <div class="image-tip">({{ (formModel.images || "").split(",").filter(Boolean).length }}/9)</div>
  52. </div>
  53. </el-form-item>
  54. <!-- 名称:美食为菜品/套餐名称,非美食为名称 -->
  55. <el-form-item
  56. :label="isFoodModule ? (formModel.formType === 'dish' ? '菜品名称' : '套餐名称') : '名称'"
  57. prop="name"
  58. required
  59. >
  60. <el-input
  61. v-model="formModel.name"
  62. placeholder="请输入"
  63. maxlength="50"
  64. show-word-limit
  65. clearable
  66. :disabled="viewMode"
  67. class="form-input"
  68. />
  69. </el-form-item>
  70. <!-- 价格 -->
  71. <el-form-item label="价格(¥)" prop="totalPrice" required>
  72. <el-input
  73. v-model="formModel.totalPrice"
  74. placeholder="请输入"
  75. maxlength="15"
  76. clearable
  77. :disabled="viewMode"
  78. class="form-input price-input"
  79. />
  80. </el-form-item>
  81. <!-- 以下仅美食类别显示:菜品原料 / 价目表内容 -->
  82. <template v-if="isFoodModule">
  83. <!-- 菜品原料(仅美食-菜品) -->
  84. <div v-if="formModel.formType === 'dish'" class="form-block">
  85. <div class="block-head">
  86. <span class="block-title">菜品原料</span>
  87. <el-link v-if="!viewMode" type="primary" class="btn-add" @click="addIngredient"> 添加项目 </el-link>
  88. <span v-if="!viewMode" class="block-tip">默认显示一个</span>
  89. </div>
  90. <div v-if="!formModel.foodIngredients.length" class="empty-tip">暂无原料,请添加项目</div>
  91. <div v-for="(item, idx) in formModel.foodIngredients" :key="'ing-' + idx" class="block-item ingredient-item">
  92. <el-form-item label="原料名称" required>
  93. <el-input
  94. v-model="item.ingredientName"
  95. placeholder="请输入"
  96. clearable
  97. :disabled="viewMode"
  98. class="form-input"
  99. />
  100. </el-form-item>
  101. <el-form-item label="所需重量(g)" required>
  102. <el-input v-model="item.weight" placeholder="请输入" clearable :disabled="viewMode" class="form-input" />
  103. </el-form-item>
  104. <el-form-item label="成本价(¥)" required>
  105. <el-input v-model="item.costPrice" placeholder="请输入" clearable :disabled="viewMode" class="form-input" />
  106. </el-form-item>
  107. <el-link v-if="!viewMode" type="primary" class="link-delete" @click="removeIngredient(idx)"> 删除 </el-link>
  108. </div>
  109. <el-link v-if="!viewMode" type="primary" class="btn-recommend" @click="calcRecommendedPrice"> 推荐价格 </el-link>
  110. </div>
  111. <!-- 价目表内容(仅套餐) -->
  112. <div v-if="formModel.formType === 'package'" class="form-block">
  113. <div class="block-head">
  114. <span class="block-title">价目表内容</span>
  115. <el-link v-if="!viewMode" type="primary" class="btn-add" @click="addPriceListGroup"> 添加项目 </el-link>
  116. <span v-if="!viewMode" class="block-tip">默认显示一个</span>
  117. </div>
  118. <div
  119. v-for="(group, gIdx) in formModel.foodPackageCategories"
  120. :key="'group-' + gIdx"
  121. class="block-item price-list-group"
  122. >
  123. <el-form-item label="类别" required>
  124. <el-input v-model="group.category" placeholder="请输入" clearable :disabled="viewMode" class="form-input" />
  125. </el-form-item>
  126. <template v-for="(dish, dIdx) in group.dishes" :key="'d-' + gIdx + '-' + dIdx">
  127. <div class="dish-row">
  128. <el-form-item label="菜品名称" required>
  129. <el-select
  130. v-model="dish.itemId"
  131. placeholder="请选择"
  132. filterable
  133. clearable
  134. :disabled="viewMode"
  135. class="form-input dish-select"
  136. @change="onPackageDishSelect(gIdx, dIdx, $event)"
  137. >
  138. <el-option v-for="opt in dishOptions" :key="opt.id" :label="opt.name" :value="String(opt.id)" />
  139. </el-select>
  140. </el-form-item>
  141. <el-form-item label="数量" required>
  142. <el-input
  143. v-model="dish.quantity"
  144. placeholder="请输入"
  145. clearable
  146. :disabled="viewMode"
  147. class="form-input qty-input"
  148. />
  149. </el-form-item>
  150. <el-form-item label="单位" required>
  151. <el-select
  152. v-model="dish.unit"
  153. placeholder="请选择单位"
  154. clearable
  155. :disabled="viewMode"
  156. class="form-input unit-select package-unit-select"
  157. style="width: 100%; min-width: 120px"
  158. >
  159. <el-option v-for="u in UNIT_OPTIONS" :key="u" :label="u" :value="u" />
  160. </el-select>
  161. </el-form-item>
  162. <el-link v-if="!viewMode" type="primary" class="link-delete" @click="removePackageDish(gIdx, dIdx)">
  163. 删除菜品
  164. </el-link>
  165. </div>
  166. </template>
  167. <div v-if="!viewMode" class="group-actions">
  168. <el-link type="primary" class="btn-add-dish" @click="addPackageDish(gIdx)"> 添加菜品 </el-link>
  169. <el-link type="primary" class="link-delete" @click="removePriceListGroup(gIdx)"> 删除项目 </el-link>
  170. </div>
  171. </div>
  172. </div>
  173. </template>
  174. <!-- 休闲娱乐/生活服务:服务项目(添加项目) -->
  175. <template v-if="!isFoodModule">
  176. <div class="form-block">
  177. <div class="block-head">
  178. <span class="block-title">服务项目</span>
  179. <el-link v-if="!viewMode" type="primary" class="btn-add" @click="addGeneralServiceItem"> 添加项目 </el-link>
  180. <span v-if="!viewMode" class="block-tip">默认显示一个</span>
  181. </div>
  182. <div v-if="!formModel.generalServiceItems.length" class="empty-tip">暂无服务项目,请添加项目</div>
  183. <div v-for="(item, idx) in formModel.generalServiceItems" :key="'svc-' + idx" class="block-item service-item">
  184. <el-form-item label="服务名称" required>
  185. <el-input v-model="item.serviceName" placeholder="请输入" clearable :disabled="viewMode" class="form-input" />
  186. </el-form-item>
  187. <el-form-item label="数量" required>
  188. <el-input
  189. v-model="item.quantity"
  190. placeholder="请输入"
  191. clearable
  192. :disabled="viewMode"
  193. class="form-input qty-input"
  194. />
  195. </el-form-item>
  196. <el-form-item label="单位" required>
  197. <el-select
  198. v-model="item.unit"
  199. placeholder="请选择单位"
  200. clearable
  201. :disabled="viewMode"
  202. class="form-input unit-select service-unit-select"
  203. style="width: 100%; min-width: 120px"
  204. >
  205. <el-option v-for="u in GENERAL_SERVICE_UNIT_OPTIONS" :key="u" :label="u" :value="u" />
  206. </el-select>
  207. </el-form-item>
  208. <el-form-item label="服务说明">
  209. <el-input
  210. v-model="item.description"
  211. type="textarea"
  212. :rows="2"
  213. placeholder="请输入"
  214. width="100%"
  215. maxlength="200"
  216. show-word-limit
  217. resize="none"
  218. :disabled="viewMode"
  219. class="form-textarea"
  220. />
  221. </el-form-item>
  222. <el-link v-if="!viewMode" type="primary" class="link-delete" @click="removeGeneralServiceItem(idx)">
  223. 删除
  224. </el-link>
  225. </div>
  226. </div>
  227. </template>
  228. <!-- 图文详情图片 -->
  229. <el-form-item label="图文详情图片">
  230. <div class="image-upload-wrap">
  231. <UploadImgs
  232. v-model:file-list="detailImageFileList"
  233. :api="uploadImgStore"
  234. :limit="9"
  235. :file-size="5"
  236. :disabled="viewMode"
  237. class="price-list-upload"
  238. @update:file-list="onDetailImageListChange"
  239. />
  240. <div class="image-tip">({{ (formModel.imageContent || "").split(",").filter(Boolean).length }}/9)</div>
  241. </div>
  242. </el-form-item>
  243. <!-- 图文详情描述 -->
  244. <el-form-item label="图文详情描述">
  245. <el-input
  246. v-model="formModel.detailContent"
  247. type="textarea"
  248. :rows="4"
  249. placeholder="请输入"
  250. maxlength="500"
  251. show-word-limit
  252. resize="none"
  253. :disabled="viewMode"
  254. class="form-textarea"
  255. />
  256. </el-form-item>
  257. </div>
  258. <!-- 右列 -->
  259. <div class="form-column form-column-right">
  260. <el-form-item label="补充说明">
  261. <el-input
  262. v-model="formModel.extraNote"
  263. type="textarea"
  264. :rows="4"
  265. placeholder="请输入"
  266. maxlength="300"
  267. show-word-limit
  268. resize="none"
  269. :disabled="viewMode"
  270. class="form-textarea"
  271. />
  272. </el-form-item>
  273. <el-form-item label="预约">
  274. <el-radio-group v-model="formModel.needReserve" :disabled="viewMode">
  275. <el-radio :value="0"> 无需预约 </el-radio>
  276. <el-radio :value="1"> 需要预约 </el-radio>
  277. </el-radio-group>
  278. </el-form-item>
  279. <el-form-item label="预约规则">
  280. <el-input
  281. v-model="formModel.reserveRule"
  282. type="textarea"
  283. :rows="3"
  284. placeholder="请输入"
  285. maxlength="200"
  286. show-word-limit
  287. resize="none"
  288. :disabled="viewMode"
  289. class="form-textarea"
  290. />
  291. </el-form-item>
  292. <el-form-item label="适用人数">
  293. <el-input v-model="formModel.peopleLimit" placeholder="请输入" clearable :disabled="viewMode" class="form-input" />
  294. </el-form-item>
  295. <el-form-item label="使用规则">
  296. <el-input
  297. v-model="formModel.usageRule"
  298. type="textarea"
  299. :rows="4"
  300. placeholder="请输入"
  301. maxlength="300"
  302. show-word-limit
  303. resize="none"
  304. :disabled="viewMode"
  305. class="form-textarea"
  306. />
  307. </el-form-item>
  308. <!-- 详情模式:提交时间、审核状态、审核时间、拒绝原因、在线状态 -->
  309. <template v-if="viewMode">
  310. <el-form-item label="提交时间">
  311. <span class="view-only-value">{{ formModel.createTime || "--" }}</span>
  312. </el-form-item>
  313. <el-form-item label="审核状态">
  314. <span :class="['view-only-value', 'audit-status', 'audit-status--' + (formModel.auditStatus ?? '')]">
  315. {{ auditStatusText(formModel.auditStatus) }}
  316. </span>
  317. </el-form-item>
  318. <el-form-item label="审核时间">
  319. <span class="view-only-value">{{ formModel.auditTime || "--" }}</span>
  320. </el-form-item>
  321. <el-form-item label="拒绝原因">
  322. <span class="view-only-value">{{ formModel.rejectionReason || "无" }}</span>
  323. </el-form-item>
  324. <el-form-item label="在线状态">
  325. <span class="view-only-value">{{ shelfStatusText(formModel.shelfStatus) }}</span>
  326. </el-form-item>
  327. </template>
  328. </div>
  329. </el-form>
  330. <!-- 底部操作:详情模式仅显示返回+编辑,编辑模式显示返回+确定 -->
  331. <div class="page-footer">
  332. <div class="footer-inner">
  333. <el-button @click="goBack" size="large"> 返回 </el-button>
  334. <template v-if="viewMode">
  335. <el-button type="primary" size="large" @click="goEdit"> 编辑 </el-button>
  336. </template>
  337. <template v-else>
  338. <el-button type="primary" size="large" :loading="submitting" @click="handleSubmit"> 确定 </el-button>
  339. </template>
  340. </div>
  341. </div>
  342. </div>
  343. </div>
  344. </template>
  345. <script setup lang="ts" name="priceListEdit">
  346. import { ref, reactive, computed, onMounted, watch } from "vue";
  347. import { ElMessage } from "element-plus";
  348. import { useRoute, useRouter } from "vue-router";
  349. import type { FormInstance, UploadUserFile } from "element-plus";
  350. import { ArrowLeft, Close } from "@element-plus/icons-vue";
  351. import {
  352. getPriceListDetail,
  353. addPriceItem,
  354. updatePriceItem,
  355. getCuisineSingleNameList,
  356. CUISINE_TYPE,
  357. getModuleTypeByBusinessSection,
  358. MODULE_TYPES
  359. } from "@/api/modules/priceList";
  360. import { localGet } from "@/utils";
  361. import UploadImgs from "@/components/Upload/Imgs.vue";
  362. import { uploadImgStore } from "@/api/modules/upload";
  363. const router = useRouter();
  364. const route = useRoute();
  365. const ruleFormRef = ref<FormInstance>();
  366. const id = ref<string>("");
  367. const businessSection = ref<string>("1");
  368. const cuisineType = ref<number | undefined>(undefined);
  369. const submitting = ref(false);
  370. const dishOptions = ref<{ id: number | string; name: string }[]>([]);
  371. // 详情模式(从列表点「详情」进入,同一编辑页只读展示)
  372. const viewMode = computed(() => route.query.mode === "detail");
  373. const UNIT_OPTIONS = ["份", "人", "元", "个", "瓶", "杯", "碗", "盘", "只", "斤", "两", "克", "千克"];
  374. // 通用套餐-服务项目单位(休闲娱乐、生活服务,与商家端一致)
  375. const GENERAL_SERVICE_UNIT_OPTIONS = [
  376. "份",
  377. "次",
  378. "人",
  379. "个",
  380. "瓶",
  381. "杯",
  382. "碗",
  383. "盘",
  384. "只",
  385. "斤",
  386. "两",
  387. "克",
  388. "千克",
  389. "天",
  390. "小时"
  391. ];
  392. // 表单数据
  393. const formModel = reactive({
  394. formType: "dish" as "dish" | "package",
  395. name: "",
  396. totalPrice: "",
  397. images: "",
  398. imageContent: "",
  399. detailContent: "",
  400. extraNote: "",
  401. needReserve: 0 as 0 | 1,
  402. reserveRule: "",
  403. peopleLimit: "",
  404. usageRule: "",
  405. // 详情只读字段(详情模式显示)
  406. createTime: "",
  407. auditTime: "",
  408. auditStatus: null as number | null,
  409. rejectionReason: "",
  410. shelfStatus: null as number | null,
  411. // 菜品原料(菜品模式)
  412. foodIngredients: [] as { ingredientName: string; weight: string; costPrice: string }[],
  413. // 价目表内容(套餐模式)
  414. foodPackageCategories: [] as {
  415. category: string;
  416. dishes: { itemId: string; dishName: string; quantity: string; unit: string }[];
  417. }[],
  418. // 通用套餐-服务项目(休闲娱乐、生活服务)
  419. generalServiceItems: [] as {
  420. serviceName: string;
  421. quantity: string;
  422. unit: string;
  423. description: string;
  424. }[]
  425. });
  426. const unitOptions = ref<string[]>(UNIT_OPTIONS);
  427. const imageFileList = ref<UploadUserFile[]>([]);
  428. const detailImageFileList = ref<UploadUserFile[]>([]);
  429. // 仅美食类别(经营板块为 1)才显示菜品/套餐分类
  430. const isFoodModule = computed(() => getModuleTypeByBusinessSection(businessSection.value) === MODULE_TYPES.FOOD);
  431. const formRules = reactive({
  432. name: [{ required: true, message: "请输入名称", trigger: "blur" }],
  433. totalPrice: [
  434. { required: true, message: "请输入价格", trigger: "blur" },
  435. {
  436. pattern: /^(?:\d+|\d+\.\d{1,2})$/,
  437. message: "请输入有效价格(最多两位小数)",
  438. trigger: "blur"
  439. }
  440. ]
  441. });
  442. // 切换类型时初始化对应块
  443. watch(
  444. () => formModel.formType,
  445. val => {
  446. if (val === "dish" && !formModel.foodIngredients.length) {
  447. formModel.foodIngredients = [{ ingredientName: "", weight: "", costPrice: "" }];
  448. }
  449. if (val === "package" && !formModel.foodPackageCategories.length) {
  450. formModel.foodPackageCategories = [{ category: "", dishes: [{ itemId: "", dishName: "", quantity: "1", unit: "份" }] }];
  451. }
  452. },
  453. { immediate: false }
  454. );
  455. function addIngredient() {
  456. formModel.foodIngredients.push({ ingredientName: "", weight: "", costPrice: "" });
  457. }
  458. function removeIngredient(idx: number) {
  459. formModel.foodIngredients.splice(idx, 1);
  460. }
  461. function addPriceListGroup() {
  462. formModel.foodPackageCategories.push({
  463. category: "",
  464. dishes: [{ itemId: "", dishName: "", quantity: "1", unit: "份" }]
  465. });
  466. }
  467. function removePriceListGroup(gIdx: number) {
  468. formModel.foodPackageCategories.splice(gIdx, 1);
  469. }
  470. function addPackageDish(gIdx: number) {
  471. formModel.foodPackageCategories[gIdx].dishes.push({
  472. itemId: "",
  473. dishName: "",
  474. quantity: "1",
  475. unit: "份"
  476. });
  477. }
  478. function removePackageDish(gIdx: number, dIdx: number) {
  479. formModel.foodPackageCategories[gIdx].dishes.splice(dIdx, 1);
  480. }
  481. function onPackageDishSelect(gIdx: number, dIdx: number, itemId: string) {
  482. const opt = dishOptions.value.find(o => String(o.id) === itemId);
  483. if (opt) formModel.foodPackageCategories[gIdx].dishes[dIdx].dishName = opt.name;
  484. }
  485. function normalizeServiceUnit(unit: string | undefined): string {
  486. const u = (unit || "份").trim();
  487. return GENERAL_SERVICE_UNIT_OPTIONS.includes(u) ? u : "份";
  488. }
  489. function normalizePackageUnit(unit: string | undefined): string {
  490. const u = (unit || "份").trim();
  491. return UNIT_OPTIONS.includes(u) ? u : "份";
  492. }
  493. function addGeneralServiceItem() {
  494. formModel.generalServiceItems.push({
  495. serviceName: "",
  496. quantity: "",
  497. unit: "份",
  498. description: ""
  499. });
  500. }
  501. function removeGeneralServiceItem(idx: number) {
  502. formModel.generalServiceItems.splice(idx, 1);
  503. }
  504. function calcRecommendedPrice() {
  505. const total = formModel.foodIngredients.reduce((sum, it) => sum + (parseFloat(it.costPrice) || 0), 0) * 1.2;
  506. if (total > 0) {
  507. formModel.totalPrice = total.toFixed(2);
  508. ElMessage.success("已根据原料成本计算推荐价格(约1.2倍)");
  509. } else {
  510. ElMessage.warning("请先填写原料成本");
  511. }
  512. }
  513. function onImageListChange(list: UploadUserFile[]) {
  514. const urls = list.map(f => (typeof f.url === "string" ? f.url : "")).filter(Boolean);
  515. formModel.images = urls.join(",");
  516. }
  517. function onDetailImageListChange(list: UploadUserFile[]) {
  518. const urls = list.map(f => (typeof f.url === "string" ? f.url : "")).filter(Boolean);
  519. formModel.imageContent = urls.join(",");
  520. }
  521. function syncImageFileListFromModel() {
  522. const str = formModel.images || "";
  523. const urls = str ? str.split(",").filter(Boolean) : [];
  524. imageFileList.value = urls.map((url, i) => ({ uid: i, name: `img-${i}`, url }));
  525. const detailStr = formModel.imageContent || "";
  526. const detailUrls = detailStr ? detailStr.split(",").filter(Boolean) : [];
  527. detailImageFileList.value = detailUrls.map((url, i) => ({ uid: 1000 + i, name: `detail-${i}`, url }));
  528. }
  529. // 详情接口返回转表单(美食接口返回 data.data.data 结构,cuisineType 1=菜品 2=套餐;非美食仅通用字段)
  530. function applyDetailToForm(data: any) {
  531. const d = data?.data?.data ?? data?.data ?? data;
  532. if (!d) return;
  533. const isFood = getModuleTypeByBusinessSection(businessSection.value) === MODULE_TYPES.FOOD;
  534. formModel.name = d.name ?? "";
  535. formModel.totalPrice = d.totalPrice !== undefined && d.totalPrice !== null ? String(d.totalPrice) : "";
  536. formModel.images = d.images ? String(d.images).trim() : "";
  537. formModel.imageContent = d.imageContent ? String(d.imageContent).trim() : "";
  538. formModel.detailContent = d.detailContent ?? "";
  539. formModel.extraNote = d.extraNote ?? "";
  540. formModel.needReserve = d.needReserve === 1 ? 1 : 0;
  541. formModel.reserveRule = d.reserveRule ?? "";
  542. formModel.peopleLimit = d.peopleLimit != null ? String(d.peopleLimit) : "";
  543. formModel.usageRule = d.usageRule ?? "";
  544. formModel.createTime = d.createdTime ?? "";
  545. formModel.auditTime = d.auditTime ?? d.updateTime ?? "";
  546. formModel.auditStatus = d.status !== undefined && d.status !== null ? d.status : d.auditStatus != null ? d.auditStatus : null;
  547. formModel.rejectionReason = d.rejectionReason ?? d.rejectReason ?? "";
  548. formModel.shelfStatus = typeof d.shelfStatus === "number" ? d.shelfStatus : d.shelfStatus != null ? d.shelfStatus : null;
  549. if (!isFood) {
  550. try {
  551. const serviceJson = d.serviceJson || "[]";
  552. const items = JSON.parse(serviceJson);
  553. formModel.generalServiceItems = Array.isArray(items)
  554. ? items.map((it: any) => ({
  555. serviceName: it.name ?? "",
  556. quantity: it.num != null ? String(it.num) : "",
  557. unit: normalizeServiceUnit(it.unit),
  558. description: it.details ?? ""
  559. }))
  560. : [];
  561. } catch {
  562. formModel.generalServiceItems = [];
  563. }
  564. if (!formModel.generalServiceItems.length) {
  565. formModel.generalServiceItems = [{ serviceName: "", quantity: "", unit: "份", description: "" }];
  566. }
  567. syncImageFileListFromModel();
  568. return;
  569. }
  570. const type = d.cuisineType;
  571. if (type === CUISINE_TYPE.SINGLE) {
  572. formModel.formType = "dish";
  573. try {
  574. const raw = d.rawJson ? JSON.parse(d.rawJson) : [];
  575. formModel.foodIngredients = Array.isArray(raw)
  576. ? raw.map((it: any) => ({
  577. ingredientName: it.name ?? "",
  578. weight: it.height ?? "",
  579. costPrice: it.cost != null ? String(it.cost) : ""
  580. }))
  581. : [{ ingredientName: "", weight: "", costPrice: "" }];
  582. } catch {
  583. formModel.foodIngredients = [{ ingredientName: "", weight: "", costPrice: "" }];
  584. }
  585. if (!formModel.foodIngredients.length) formModel.foodIngredients = [{ ingredientName: "", weight: "", costPrice: "" }];
  586. } else {
  587. formModel.formType = "package";
  588. try {
  589. const groups = d.groupJson ? JSON.parse(d.groupJson) : [];
  590. formModel.foodPackageCategories = Array.isArray(groups)
  591. ? groups.map((g: any) => ({
  592. category: g.categoryName ?? "",
  593. dishes: (g.items || []).map((it: any) => ({
  594. itemId: it.cuisineId != null ? String(it.cuisineId) : "",
  595. dishName: it.cuisineName ?? "",
  596. quantity: it.quantity != null ? String(it.quantity) : "1",
  597. unit: normalizePackageUnit(it.unit)
  598. }))
  599. }))
  600. : [];
  601. } catch {
  602. formModel.foodPackageCategories = [];
  603. }
  604. if (!formModel.foodPackageCategories.length) {
  605. formModel.foodPackageCategories = [{ category: "", dishes: [{ itemId: "", dishName: "", quantity: "1", unit: "份" }] }];
  606. }
  607. }
  608. syncImageFileListFromModel();
  609. }
  610. const fetchDetail = async () => {
  611. try {
  612. const res: any = await getPriceListDetail(id.value, businessSection.value, cuisineType.value);
  613. if (res && res.code === 200 && res.data) {
  614. applyDetailToForm(res);
  615. } else {
  616. const data = res?.data;
  617. if (data) applyDetailToForm(res);
  618. else ElMessage.error(res?.msg || "获取详情失败");
  619. }
  620. } catch (error) {
  621. console.error("获取价目表详情失败:", error);
  622. }
  623. };
  624. function buildSubmitPayload(isDraft: boolean) {
  625. const storeId = localGet("createdId") as string;
  626. const payload: any = {
  627. storeId: storeId ? parseInt(storeId, 10) : undefined,
  628. name: formModel.name.trim(),
  629. totalPrice: parseFloat(formModel.totalPrice) || 0,
  630. images: formModel.images || undefined,
  631. imageContent: formModel.imageContent || undefined,
  632. detailContent: formModel.detailContent?.trim() || undefined,
  633. extraNote: formModel.extraNote?.trim() || undefined,
  634. needReserve: formModel.needReserve,
  635. reserveRule: formModel.reserveRule?.trim() || undefined,
  636. peopleLimit: formModel.peopleLimit?.trim() || undefined,
  637. usageRule: formModel.usageRule?.trim() || undefined
  638. };
  639. const moduleType = getModuleTypeByBusinessSection(businessSection.value);
  640. if (moduleType === MODULE_TYPES.FOOD) {
  641. if (formModel.formType === "dish") {
  642. payload.cuisineType = CUISINE_TYPE.SINGLE;
  643. payload.rawJson = JSON.stringify(
  644. formModel.foodIngredients.map(it => ({
  645. name: (it.ingredientName || "").trim(),
  646. height: (it.weight || "").trim(),
  647. cost: parseFloat(it.costPrice) || 0,
  648. suggest: parseFloat(it.costPrice) || 0
  649. }))
  650. );
  651. } else {
  652. payload.cuisineType = CUISINE_TYPE.COMBO;
  653. payload.groupJson = JSON.stringify(
  654. formModel.foodPackageCategories
  655. .filter(g => (g.dishes || []).length > 0)
  656. .map(g => ({
  657. categoryName: (g.category || "").trim(),
  658. items: (g.dishes || []).map(d => ({
  659. cuisineId: d.itemId || undefined,
  660. cuisineName: (d.dishName || "").trim(),
  661. quantity: parseInt(d.quantity, 10) || 1,
  662. unit: (d.unit || "份").trim()
  663. }))
  664. }))
  665. );
  666. }
  667. } else {
  668. // 休闲娱乐、生活服务:服务项目 JSON(与商家端 generalPrice 一致)
  669. payload.serviceJson = JSON.stringify(
  670. formModel.generalServiceItems.map(it => ({
  671. name: (it.serviceName || "").trim(),
  672. num: parseInt(it.quantity, 10) || 1,
  673. unit: (it.unit || "份").trim(),
  674. details: (it.description || "").trim()
  675. }))
  676. );
  677. }
  678. if (id.value) payload.id = parseInt(id.value, 10);
  679. return payload;
  680. }
  681. async function submit(isDraft: boolean) {
  682. if (!ruleFormRef.value) return;
  683. await ruleFormRef.value.validate(async valid => {
  684. if (!valid) return;
  685. submitting.value = true;
  686. try {
  687. const payload = buildSubmitPayload(isDraft);
  688. const api = id.value ? updatePriceItem : addPriceItem;
  689. const res: any = await api(payload);
  690. if (res && res.code === 200) {
  691. ElMessage.success(id.value ? "保存成功" : "新建成功");
  692. router.back();
  693. }
  694. } catch (error) {
  695. console.error("提交失败:", error);
  696. } finally {
  697. submitting.value = false;
  698. }
  699. });
  700. }
  701. const handleSubmit = () => submit(false);
  702. const handleSaveDraft = () => submit(false); // 存草稿与确定共用提交,若后端有草稿状态可再区分
  703. const goBack = () => router.back();
  704. // 详情模式下点击「编辑」:去掉 mode=detail 进入编辑
  705. function goEdit() {
  706. const query: Record<string, string> = {
  707. id: id.value,
  708. businessSection: businessSection.value
  709. };
  710. if (cuisineType.value !== undefined && cuisineType.value !== null) {
  711. query.cuisineType = String(cuisineType.value);
  712. }
  713. router.replace({ path: "/priceList/edit", query });
  714. }
  715. function auditStatusText(status: number | null): string {
  716. if (status === null || status === undefined) return "--";
  717. const map: Record<number, string> = { 0: "审核中", 1: "已通过", 2: "已拒绝" };
  718. return map[status] ?? "--";
  719. }
  720. function shelfStatusText(status: number | null): string {
  721. if (status === 1) return "上架";
  722. if (status === 2) return "下架";
  723. return "--";
  724. }
  725. async function loadDishOptions() {
  726. const moduleType = getModuleTypeByBusinessSection(businessSection.value);
  727. if (moduleType !== MODULE_TYPES.FOOD) return;
  728. try {
  729. const res: any = await getCuisineSingleNameList();
  730. if (res?.code === 200 && Array.isArray(res?.data)) {
  731. dishOptions.value = (res.data as any[]).map((it: any) => ({
  732. id: it.id ?? it.value,
  733. name: it.name ?? it.label ?? ""
  734. }));
  735. }
  736. } catch (e) {
  737. console.error("获取单品列表失败", e);
  738. }
  739. }
  740. onMounted(() => {
  741. id.value = (route.query.id as string) || "";
  742. businessSection.value = (route.query.businessSection as string) || localGet("businessSection") || "1";
  743. const ct = route.query.cuisineType;
  744. cuisineType.value = ct !== undefined && ct !== null && ct !== "" ? Number(ct) : undefined;
  745. // 特色美食新建:从列表页「新建」选择菜品/套餐带入的 createMode
  746. const createMode = route.query.createMode as string;
  747. if (!id.value && getModuleTypeByBusinessSection(businessSection.value) === MODULE_TYPES.FOOD) {
  748. if (createMode === "package" || createMode === "dish") {
  749. formModel.formType = createMode;
  750. }
  751. }
  752. if (formModel.formType === "dish" && !formModel.foodIngredients.length) {
  753. formModel.foodIngredients = [{ ingredientName: "", weight: "", costPrice: "" }];
  754. }
  755. if (formModel.formType === "package" && !formModel.foodPackageCategories.length) {
  756. formModel.foodPackageCategories = [{ category: "", dishes: [{ itemId: "", dishName: "", quantity: "1", unit: "份" }] }];
  757. }
  758. if (id.value) {
  759. fetchDetail();
  760. } else if (getModuleTypeByBusinessSection(businessSection.value) !== MODULE_TYPES.FOOD) {
  761. // 休闲娱乐/生活服务新建时默认显示一个服务项
  762. if (!formModel.generalServiceItems.length) {
  763. formModel.generalServiceItems = [{ serviceName: "", quantity: "", unit: "份", description: "" }];
  764. }
  765. }
  766. if (getModuleTypeByBusinessSection(businessSection.value) === MODULE_TYPES.FOOD) {
  767. loadDishOptions();
  768. }
  769. });
  770. </script>
  771. <style scoped lang="scss">
  772. .price-edit-page {
  773. min-height: 100vh;
  774. padding-bottom: 80px;
  775. background-color: #f5f6f8;
  776. }
  777. .page-header {
  778. display: flex;
  779. align-items: center;
  780. height: 56px;
  781. padding: 0 24px;
  782. background: #ffffff;
  783. border-bottom: 1px solid #ebeef5;
  784. .header-left {
  785. display: flex;
  786. gap: 6px;
  787. align-items: center;
  788. font-size: 14px;
  789. color: #606266;
  790. cursor: pointer;
  791. &:hover {
  792. color: #409eff;
  793. }
  794. }
  795. .back-icon {
  796. font-size: 18px;
  797. }
  798. .page-title {
  799. flex: 1;
  800. margin: 0;
  801. margin-left: 24px;
  802. font-size: 18px;
  803. font-weight: 600;
  804. color: #303133;
  805. text-align: center;
  806. }
  807. .header-right {
  808. padding: 4px;
  809. color: #909399;
  810. cursor: pointer;
  811. &:hover {
  812. color: #303133;
  813. }
  814. }
  815. .close-icon {
  816. font-size: 20px;
  817. }
  818. }
  819. .page-content {
  820. padding: 24px;
  821. }
  822. .two-column-form {
  823. display: flex;
  824. flex-wrap: wrap;
  825. gap: 32px;
  826. padding: 24px 32px 32px;
  827. background: #ffffff;
  828. border-radius: 8px;
  829. box-shadow: 0 1px 4px rgb(0 0 0 / 6%);
  830. }
  831. .form-column {
  832. flex: 1;
  833. min-width: 320px;
  834. max-width: 80%;
  835. }
  836. .form-column-left {
  837. padding-right: 24px;
  838. border-right: 1px solid #ebeef5;
  839. }
  840. .price-form {
  841. :deep(.el-form-item) {
  842. margin-bottom: 20px;
  843. }
  844. :deep(.el-form-item__label) {
  845. font-size: 14px;
  846. color: #606266;
  847. }
  848. .form-input {
  849. width: 100%;
  850. max-width: 360px;
  851. &.price-input {
  852. max-width: 200px;
  853. }
  854. &.dish-select,
  855. &.unit-select {
  856. max-width: 200px;
  857. }
  858. &.qty-input {
  859. max-width: 100px;
  860. }
  861. }
  862. .form-textarea {
  863. width: 100%;
  864. max-width: 100%;
  865. }
  866. }
  867. .form-block {
  868. padding: 16px 0;
  869. margin-bottom: 24px;
  870. border-top: 1px solid #f0f0f0;
  871. .block-head {
  872. display: flex;
  873. gap: 12px;
  874. align-items: center;
  875. margin-bottom: 12px;
  876. }
  877. .block-title {
  878. font-size: 14px;
  879. font-weight: 500;
  880. color: #303133;
  881. }
  882. .btn-add {
  883. font-size: 14px;
  884. }
  885. .block-tip {
  886. margin-left: auto;
  887. font-size: 12px;
  888. color: #909399;
  889. }
  890. .empty-tip {
  891. padding: 12px 0;
  892. font-size: 13px;
  893. color: #909399;
  894. }
  895. .block-item {
  896. position: relative;
  897. padding: 12px 0;
  898. padding: 12px 16px;
  899. margin-bottom: 12px;
  900. background: #fafafa;
  901. border: 1px solid #ebeef5;
  902. border-radius: 6px;
  903. }
  904. .ingredient-item {
  905. display: flex;
  906. flex-flow: column wrap;
  907. gap: 0 16px;
  908. align-items: flex-start;
  909. :deep(.el-form-item) {
  910. margin-bottom: 12px;
  911. }
  912. .link-delete {
  913. position: absolute;
  914. top: 12px;
  915. right: 12px;
  916. font-size: 13px;
  917. }
  918. }
  919. .service-item {
  920. display: flex;
  921. flex-flow: column wrap;
  922. gap: 0 16px;
  923. align-items: flex-start;
  924. :deep(.el-form-item) {
  925. margin-bottom: 12px;
  926. }
  927. .form-textarea {
  928. max-width: 360px;
  929. }
  930. .service-unit-select {
  931. min-width: 120px;
  932. :deep(.el-select__wrapper),
  933. :deep(.el-input__wrapper) {
  934. min-width: 120px;
  935. }
  936. :deep(.el-input__inner) {
  937. min-width: 80px;
  938. }
  939. }
  940. .link-delete {
  941. position: absolute;
  942. top: 12px;
  943. right: 12px;
  944. font-size: 13px;
  945. }
  946. }
  947. .btn-recommend {
  948. display: inline-block;
  949. margin-top: 8px;
  950. font-size: 14px;
  951. }
  952. .price-list-group {
  953. .dish-row {
  954. display: flex;
  955. flex-flow: column wrap;
  956. gap: 0 12px;
  957. align-items: flex-start;
  958. padding: 8px 0;
  959. margin-bottom: 8px;
  960. border-bottom: 1px dashed #ebeef5;
  961. &:last-of-type {
  962. border-bottom: none;
  963. }
  964. }
  965. .package-unit-select {
  966. min-width: 120px;
  967. :deep(.el-select__wrapper),
  968. :deep(.el-input__wrapper) {
  969. min-width: 120px;
  970. }
  971. :deep(.el-input__inner) {
  972. min-width: 80px;
  973. }
  974. }
  975. .group-actions {
  976. display: flex;
  977. gap: 16px;
  978. margin-top: 12px;
  979. }
  980. .link-delete {
  981. font-size: 13px;
  982. }
  983. }
  984. }
  985. .image-upload-wrap {
  986. .price-list-upload {
  987. :deep(.el-upload-list--picture-card) {
  988. --el-upload-list-picture-card-size: 100px;
  989. }
  990. :deep(.el-upload--picture-card) {
  991. width: 100px;
  992. height: 100px;
  993. }
  994. }
  995. .image-tip {
  996. margin-top: 4px;
  997. font-size: 12px;
  998. color: #909399;
  999. }
  1000. }
  1001. .page-footer {
  1002. z-index: 100;
  1003. display: flex;
  1004. align-items: center;
  1005. justify-content: center;
  1006. height: 64px;
  1007. background: #ffffff;
  1008. border-top: 1px solid #ebeef5;
  1009. }
  1010. .footer-inner {
  1011. display: flex;
  1012. gap: 12px;
  1013. align-items: center;
  1014. justify-content: center;
  1015. width: 100%;
  1016. max-width: 1100px;
  1017. padding: 0 24px;
  1018. }
  1019. .view-only-value {
  1020. font-size: 14px;
  1021. color: #606266;
  1022. }
  1023. .audit-status {
  1024. font-weight: 500;
  1025. &.audit-status--0 {
  1026. color: #e6a23c;
  1027. }
  1028. &.audit-status--1 {
  1029. color: #67c23a;
  1030. }
  1031. &.audit-status--2 {
  1032. color: #f56c6c;
  1033. }
  1034. }
  1035. .el-form-item.el-form-item--default.asterisk-left.el-form-item--label-right {
  1036. width: 100%;
  1037. :deep(.el-form-item__label) {
  1038. width: 100%;
  1039. }
  1040. :deep(.el-form-item__content) {
  1041. width: 100%;
  1042. }
  1043. }
  1044. </style>