create.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="container">
  3. <view class="form">
  4. <view class="form-item">
  5. <text class="label">套餐名称</text>
  6. <input class="input" v-model="form.name" placeholder="如:21天养成学习习惯" />
  7. </view>
  8. <view class="form-item">
  9. <text class="label">详细介绍</text>
  10. <textarea class="textarea" v-model="form.description" placeholder="描述套餐的内容和目标" />
  11. </view>
  12. <view class="form-item">
  13. <text class="label">周期天数</text>
  14. <input class="input" type="number" v-model="form.cycleDays" placeholder="如:21" />
  15. </view>
  16. <view class="form-item">
  17. <text class="label">分类</text>
  18. <picker :range="categories" @change="onCategoryChange">
  19. <view class="picker">
  20. {{ form.category || '请选择分类' }}
  21. </view>
  22. </picker>
  23. </view>
  24. <view class="form-item">
  25. <text class="label">价格 (元)</text>
  26. <input class="input" type="digit" v-model="form.price" placeholder="0 表示免费" />
  27. </view>
  28. <view class="form-item">
  29. <text class="label">封面图</text>
  30. <view class="cover-upload" @click="chooseImage">
  31. <image v-if="form.coverImage" :src="form.coverImage" mode="aspectFill" class="cover-image"></image>
  32. <text v-else class="upload-icon">+</text>
  33. </view>
  34. </view>
  35. <view class="form-item">
  36. <text class="label">是否公开</text>
  37. <switch v-model="form.isPublic" :checked="form.isPublic" @change="onPublicChange" />
  38. <text class="switch-label">{{ form.isPublic ? '公开到市场' : '仅分享链接' }}</text>
  39. </view>
  40. <button class="btn-submit" @click="submit">保存</button>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { createPackage } from '@/utils/api.js'
  46. export default {
  47. data() {
  48. return {
  49. form: {
  50. name: '',
  51. description: '',
  52. cycleDays: 21,
  53. category: '',
  54. price: 0,
  55. isPublic: false,
  56. coverImage: ''
  57. },
  58. categories: ['学习', '习惯', '健康', '情绪', '其他']
  59. }
  60. },
  61. methods: {
  62. onCategoryChange(e) {
  63. this.form.category = this.categories[e.detail.value]
  64. },
  65. onPublicChange(e) {
  66. this.form.isPublic = e.detail.value
  67. },
  68. chooseImage() {
  69. uni.chooseImage({
  70. count: 1,
  71. success: (res) => {
  72. this.form.coverImage = res.tempFilePaths[0]
  73. }
  74. })
  75. },
  76. async submit() {
  77. if (!this.form.name) {
  78. uni.showToast({ title: '请输入套餐名称', icon: 'none' })
  79. return
  80. }
  81. if (!this.form.cycleDays) {
  82. uni.showToast({ title: '请输入周期天数', icon: 'none' })
  83. return
  84. }
  85. uni.showLoading({ title: '保存中...' })
  86. try {
  87. await createPackage({
  88. ...this.form,
  89. isPublic: this.form.isPublic ? 1 : 0
  90. })
  91. uni.showToast({ title: '创建成功' })
  92. setTimeout(() => {
  93. uni.navigateBack()
  94. }, 1500)
  95. } catch (e) {
  96. uni.showToast({ title: '创建失败', icon: 'none' })
  97. } finally {
  98. uni.hideLoading()
  99. }
  100. }
  101. }
  102. }
  103. </script>
  104. <style scoped>
  105. .container {
  106. min-height: 100vh;
  107. background: #f5f5f5;
  108. padding: 30rpx;
  109. }
  110. .form {
  111. background: #fff;
  112. border-radius: 16rpx;
  113. padding: 30rpx;
  114. }
  115. .form-item {
  116. margin-bottom: 30rpx;
  117. }
  118. .label {
  119. display: block;
  120. font-size: 28rpx;
  121. color: #333;
  122. margin-bottom: 16rpx;
  123. }
  124. .input, .textarea {
  125. width: 100%;
  126. padding: 20rpx;
  127. background: #f5f5f5;
  128. border-radius: 12rpx;
  129. font-size: 28rpx;
  130. }
  131. .textarea {
  132. height: 160rpx;
  133. }
  134. .picker {
  135. padding: 20rpx;
  136. background: #f5f5f5;
  137. border-radius: 12rpx;
  138. font-size: 28rpx;
  139. color: #333;
  140. }
  141. .cover-upload {
  142. width: 200rpx;
  143. height: 200rpx;
  144. background: #f5f5f5;
  145. border-radius: 12rpx;
  146. display: flex;
  147. justify-content: center;
  148. align-items: center;
  149. }
  150. .upload-icon {
  151. font-size: 60rpx;
  152. color: #ccc;
  153. }
  154. .cover-image {
  155. width: 200rpx;
  156. height: 200rpx;
  157. border-radius: 12rpx;
  158. }
  159. .switch-label {
  160. margin-left: 20rpx;
  161. font-size: 26rpx;
  162. color: #666;
  163. }
  164. .btn-submit {
  165. background: #4CAF50;
  166. color: #fff;
  167. border-radius: 40rpx;
  168. margin-top: 40rpx;
  169. }
  170. </style>