form-step2.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <template>
  2. <view class="page-container">
  3. <u-navbar title="新建团餐" :autoBack="true" :placeholder="true"></u-navbar>
  4. <u-form :model="form" :rules="rules" ref="formRef" :error-type="['toast', 'border-bottom']">
  5. <u-collapse ref="collapseRef" :value="activeNames" @change="onCollapseChange">
  6. <u-collapse-item :class="{
  7. active: activeNames == category.id
  8. }" v-for="(category, catIndex) in form.dishCategories" :key="category.id" :name="category.id" @click="onClick">
  9. <view slot="title" class="collapse-title">
  10. <text>{{ category.name }}</text>
  11. <text class="num" v-if="isEditedShow(category)">({{ category.items.length }})</text>
  12. <u-switch v-if="false" size="20" activeColor="#3c9cff" :value="category.isActive"
  13. @change="onSwitchChange($event, category.id)"></u-switch>
  14. </view>
  15. <view class="collapse-content">
  16. <view class="dish-item" v-for="(dish, dishIndex) in category.items" :key="dish.id">
  17. <view class="dish-header">
  18. <view class="dish-title">
  19. <u-image width="32rpx" height="32rpx" src="/static/icon/icon_edit.png"></u-image>
  20. <text>{{ category.name }}{{ dishIndex + 1 }}</text>
  21. </view>
  22. </view>
  23. <view class="dish-form">
  24. <u-form-item label="名称" :prop="`dishCategories.${catIndex}.items.${dishIndex}.name`"
  25. :required="isEdited(dish)">
  26. <u-input v-model="dish.name" height="41" placeholder="请输入" inputAlign="right"></u-input>
  27. </u-form-item>
  28. <u-form-item label="价格" :prop="`dishCategories.${catIndex}.items.${dishIndex}.price`"
  29. :required="isEdited(dish)">
  30. <u-input v-model.number="dish.price" type="digit" height="41" placeholder="输入金额" :clearable="false"
  31. inputAlign="right"></u-input>
  32. <text slot="right" style="color: #333333;">元</text>
  33. </u-form-item>
  34. <u-form-item label="描述" :prop="`dishCategories.${catIndex}.items.${dishIndex}.description`"
  35. :required="isEdited(dish)">
  36. <u-input v-model="dish.description" height="41" placeholder="请描述菜品的使用素材" inputAlign="right"></u-input>
  37. </u-form-item>
  38. </view>
  39. <view class="dish-actions">
  40. <u-button v-if="category.items.length > 1" type="error" size="mini" hover-class="none" :custom-style="{
  41. color: '#FF6067',
  42. border: '1rpx solid #FF6067',
  43. background: 'rgba(255,96,103,0.1)',
  44. }" @click="removeDish(category.id, dish.id)">
  45. <view class="text">
  46. <u-image width="32rpx" height="32rpx" src="/static/icon/icon_del.png"></u-image>
  47. <text>删除</text>
  48. </view>
  49. </u-button>
  50. <u-button v-if="dishIndex === category.items.length - 1" type="primary" size="mini" hover-class="none"
  51. :custom-style="{
  52. color: '#2D88F4',
  53. border: '1rpx solid #2D88F4',
  54. background: 'rgba(45,136,244,0.1)',
  55. }" @click="addDish(category.id)">
  56. <view class="text">
  57. <u-image width="32rpx" height="32rpx" src="/static/icon/icon_plus.png"></u-image>
  58. <text>添加</text>
  59. </view>
  60. </u-button>
  61. </view>
  62. </view>
  63. </view>
  64. </u-collapse-item>
  65. </u-collapse>
  66. </u-form>
  67. <view class="footer-bar">
  68. <u-button :custom-style="{
  69. width: '205rpx',
  70. fontWeight: '400',
  71. fontSize: '28rpx',
  72. color: '#449AFF',
  73. border: '1rpx solid #449AFF',
  74. background: '#fff',
  75. marginRight: '24rpx'
  76. }" @click="goBack">上一步</u-button>
  77. <u-button :custom-style="{
  78. fontWeight: '400',
  79. fontSize: '28rpx',
  80. color: '#FFFFFF',
  81. background: 'linear-gradient(90deg, #77B6FF 0%, #449AFF 100%)',
  82. }" @click="submit">提交审核</u-button>
  83. </view>
  84. </view>
  85. </template>
  86. <script>
  87. let dishIdCounter = 100; // 简单的唯一ID生成器
  88. export default {
  89. data() {
  90. return {
  91. // 数据
  92. form: {
  93. dishCategories: [{
  94. id: 1,
  95. name: '荤菜',
  96. isActive: false,
  97. items: [{
  98. id: dishIdCounter++,
  99. name: '',
  100. price: '',
  101. description: ''
  102. }]
  103. },
  104. {
  105. id: 2,
  106. name: '素菜',
  107. isActive: false,
  108. items: [{
  109. id: dishIdCounter++,
  110. name: '',
  111. price: '',
  112. description: ''
  113. }]
  114. },
  115. {
  116. id: 3,
  117. name: '主食',
  118. isActive: false,
  119. items: [{
  120. id: dishIdCounter++,
  121. name: '',
  122. price: '',
  123. description: ''
  124. }]
  125. },
  126. {
  127. id: 4,
  128. name: '汤',
  129. isActive: false,
  130. items: [{
  131. id: dishIdCounter++,
  132. name: '',
  133. price: '',
  134. description: ''
  135. }]
  136. },
  137. {
  138. id: 5,
  139. name: '其他',
  140. isActive: false,
  141. items: [{
  142. id: dishIdCounter++,
  143. name: '',
  144. price: '',
  145. description: ''
  146. }]
  147. }
  148. ],
  149. },
  150. rules: {},
  151. activeNames: [1], // 控制折叠面板展开的name数组
  152. };
  153. },
  154. watch: {
  155. 'form.dishCategories': {
  156. handler() {
  157. this.$nextTick(() => {
  158. this.updateValidationRules();
  159. });
  160. },
  161. deep: true
  162. }
  163. },
  164. onLoad() {
  165. const goods = uni.getStorageSync('gm_goods');
  166. if (goods.gmMerchantPackageDetails && goods.gmMerchantPackageDetails.length > 0) {
  167. this.form.dishCategories.map(a => {
  168. let items = goods.gmMerchantPackageDetails.filter(b => b.category == a.id);
  169. if (items.length > 0) a.items = items;
  170. });
  171. }
  172. },
  173. mounted() {
  174. this.$nextTick(() => {
  175. this.$refs.formRef.setRules(this.rules);
  176. });
  177. },
  178. methods: {
  179. // 判断一个菜品是否被编辑过
  180. isEdited(dish) {
  181. return !!(dish.name || dish.price || dish.description);
  182. },
  183. isEditedShow(e) {
  184. const goods = uni.getStorageSync('gm_goods');
  185. return goods.gmMerchantPackageDetails && goods.gmMerchantPackageDetails.length > 0 && goods
  186. .gmMerchantPackageDetails.some(b => b.category == e.id);;
  187. },
  188. updateValidationRules() {
  189. if (!this.$refs.formRef) return;
  190. const newRules = {};
  191. this.form.dishCategories.forEach((category, catIndex) => {
  192. category.items.forEach((dish, dishIndex) => {
  193. if (this.isEdited(dish)) {
  194. // 如果菜品的任一字段被填写,则所有字段都变为必填
  195. const nameProp = `dishCategories.${catIndex}.items.${dishIndex}.name`;
  196. const priceProp = `dishCategories.${catIndex}.items.${dishIndex}.price`;
  197. const descProp = `dishCategories.${catIndex}.items.${dishIndex}.description`;
  198. newRules[nameProp] = [{
  199. required: true,
  200. message: '请输入名称',
  201. trigger: ['blur', 'change']
  202. }];
  203. newRules[priceProp] = [{
  204. required: true,
  205. type: 'number',
  206. message: '请输入价格',
  207. trigger: ['blur', 'change']
  208. }];
  209. newRules[descProp] = [{
  210. required: true,
  211. message: '请输入描述',
  212. trigger: ['blur', 'change']
  213. }];
  214. }
  215. });
  216. });
  217. this.rules = newRules;
  218. this.$refs.formRef.setRules(this.rules);
  219. },
  220. // Switch开关状态变化
  221. onSwitchChange(isActive, categoryId) {
  222. const index = this.activeNames.indexOf(categoryId);
  223. if (isActive) {
  224. if (index === -1) {
  225. this.activeNames.push(categoryId);
  226. }
  227. } else {
  228. if (index > -1) {
  229. this.activeNames.splice(index, 1);
  230. }
  231. }
  232. },
  233. // 折叠面板状态变化
  234. onCollapseChange(names) {
  235. console.log(names)
  236. this.activeNames = names;
  237. },
  238. // 添加菜品
  239. addDish(categoryId) {
  240. const category = this.form.dishCategories.find(c => c.id === categoryId);
  241. if (category) {
  242. category.items.push({
  243. id: dishIdCounter++,
  244. name: '',
  245. price: '',
  246. description: ''
  247. });
  248. }
  249. },
  250. // 删除菜品
  251. removeDish(categoryId, dishId) {
  252. const category = this.form.dishCategories.find(c => c.id === categoryId);
  253. if (category && category.items.length > 1) {
  254. const index = category.items.findIndex(d => d.id === dishId);
  255. if (index > -1) {
  256. category.items.splice(index, 1);
  257. }
  258. }
  259. },
  260. // 上一步
  261. goBack() {
  262. uni.navigateBack({
  263. delta: 1
  264. });
  265. },
  266. // 提交审核
  267. submit() {
  268. this.$refs.formRef.validate().then(res => {
  269. const goods = uni.getStorageSync('gm_goods');
  270. const activeCategories = this.form.dishCategories.filter(c => c.items.some(this.isEdited));
  271. const details = [];
  272. // 遍历一级分类
  273. activeCategories.forEach(category => {
  274. // 检查是否有items属性且为数组
  275. if (category.items && Array.isArray(category.items)) {
  276. // 遍历二级菜品
  277. category.items.forEach(item => {
  278. // 创建新对象,只保留需要的属性
  279. if (this.isEdited(item)) {
  280. details.push({
  281. category: category.id,
  282. name: item.name,
  283. price: item.price,
  284. description: item.description
  285. });
  286. }
  287. });
  288. }
  289. });
  290. let params = {
  291. ...goods,
  292. gmMerchantPackageDetails: [...details]
  293. };
  294. console.log('表单校验成功, 提交的数据:', activeCategories, params);
  295. this.$http.post('/groupmeal/gmMerchantPackage/submit', params).then(res => {
  296. let type = res.data.code === 200 ? 'success' : 'error';
  297. this.$tip[type](res.data.message);
  298. if (res.data.code === 200) {
  299. // 清除
  300. uni.removeStorageSync('gm_goods');
  301. // 返回列表
  302. uni.navigateBack({
  303. delta: 3
  304. });
  305. }
  306. });
  307. }).catch(errors => {
  308. console.log('校验失败', errors);
  309. });
  310. }
  311. }
  312. };
  313. </script>
  314. <style lang="scss" scoped>
  315. .page-container {
  316. min-height: 100vh;
  317. background-color: #f5f5f5;
  318. padding-bottom: 160rpx;
  319. ::v-deep .u-collapse-item {
  320. margin-bottom: 16rpx;
  321. .u-collapse-head {
  322. padding: 20rpx 32rpx;
  323. background-color: #fff;
  324. }
  325. &.active {
  326. .u-collapse-body {
  327. height: auto !important;
  328. }
  329. }
  330. }
  331. .collapse-title {
  332. font-weight: 500;
  333. font-size: 32rpx;
  334. color: #333333;
  335. display: flex;
  336. align-items: center;
  337. justify-content: space-between;
  338. .num {
  339. padding: 0 10rpx;
  340. }
  341. }
  342. .collapse-content {
  343. .dish-item {
  344. margin-top: 16rpx;
  345. padding: 20rpx 32rpx;
  346. background: #FFFFFF;
  347. box-shadow: 0 2rpx 8rpx 0 rgba(214, 225, 237, 0.1);
  348. border: 1rpx solid #F0F3F5;
  349. border-radius: 16rpx;
  350. .dish-header {
  351. margin-bottom: 20rpx;
  352. display: flex;
  353. align-items: center;
  354. justify-content: space-between;
  355. .dish-title {
  356. font-size: 28rpx;
  357. color: #333333;
  358. display: flex;
  359. align-items: center;
  360. }
  361. }
  362. .dish-form {
  363. padding: 6rpx 16rpx;
  364. border-radius: 8rpx;
  365. background: #F7F8FC;
  366. }
  367. ::v-deep .u-form-item {
  368. padding: 16rpx 0;
  369. line-height: 1;
  370. .u-form-item--left__content {
  371. flex: none;
  372. }
  373. }
  374. }
  375. }
  376. }
  377. .dish-actions {
  378. width: 100%;
  379. text-align: end;
  380. margin-top: 16rpx;
  381. // display: flex;
  382. // align-items: center;
  383. // justify-content: end;
  384. // gap: 16rpx;
  385. ::v-deep .u-btn {
  386. font-weight: 400;
  387. font-size: 22rpx;
  388. margin-left: 16rpx;
  389. }
  390. .text {
  391. display: flex;
  392. align-items: center;
  393. text {
  394. line-height: 1;
  395. margin-left: 4rpx;
  396. }
  397. }
  398. }
  399. .footer-bar {
  400. ::v-deep .u-btn {
  401. width: 100%;
  402. height: 76rpx;
  403. border-radius: 54rpx;
  404. &::after {
  405. display: none;
  406. }
  407. }
  408. }
  409. </style>