create.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view class="create-page">
  3. <view class="form">
  4. <view class="form-item">
  5. <text class="label">食物名称</text>
  6. <input class="input" v-model="form.foodName" placeholder="请输入食物名称" />
  7. </view>
  8. <view class="form-item">
  9. <text class="label">热量(kcal)</text>
  10. <input class="input" type="number" v-model="form.calories" placeholder="请输入热量" />
  11. </view>
  12. <view class="form-item">
  13. <text class="label">餐次</text>
  14. <picker :range="mealTypes" @change="onMealChange">
  15. <view class="picker">{{ form.mealType }}</view>
  16. </picker>
  17. </view>
  18. <view class="form-item">
  19. <text class="label">进餐日期</text>
  20. <picker mode="date" :value="form.eatDate" @change="onDateChange">
  21. <view class="picker">{{ form.eatDate }}</view>
  22. </picker>
  23. </view>
  24. </view>
  25. <button class="submit-btn" @click="submit">保存</button>
  26. </view>
  27. </template>
  28. <script>
  29. import { createDietRecord } from '../../utils/api.js'
  30. import { today } from '../../utils/format.js'
  31. export default {
  32. data() {
  33. return {
  34. mealTypes: ['早餐', '午餐', '晚餐', '加餐'],
  35. form: {
  36. foodName: '',
  37. calories: '',
  38. mealType: '早餐',
  39. eatDate: today()
  40. }
  41. }
  42. },
  43. methods: {
  44. onMealChange(e) {
  45. this.form.mealType = this.mealTypes[e.detail.value]
  46. },
  47. onDateChange(e) {
  48. this.form.eatDate = e.detail.value
  49. },
  50. submit() {
  51. if (!this.form.foodName) {
  52. uni.showToast({ title: '请输入食物名称', icon: 'none' })
  53. return
  54. }
  55. if (!this.form.calories) {
  56. uni.showToast({ title: '请输入热量', icon: 'none' })
  57. return
  58. }
  59. var data = {
  60. foodName: this.form.foodName,
  61. calories: parseInt(this.form.calories),
  62. mealType: this.form.mealType,
  63. eatDate: this.form.eatDate
  64. }
  65. createDietRecord(data).then(() => {
  66. uni.showToast({ title: '保存成功', icon: 'success' })
  67. setTimeout(() => {
  68. uni.navigateBack()
  69. }, 1000)
  70. })
  71. }
  72. }
  73. }
  74. </script>
  75. <style>
  76. .create-page { padding: 30rpx; }
  77. .form { background: #fff; border-radius: 16rpx; padding: 0 30rpx; margin-bottom: 60rpx; }
  78. .form-item { display: flex; align-items: center; padding: 30rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  79. .label { width: 200rpx; color: #333; }
  80. .input { flex: 1; text-align: right; }
  81. .picker { flex: 1; text-align: right; color: #333; }
  82. .submit-btn { background: #F97316; color: #fff; border-radius: 40rpx; }
  83. </style>