create.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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" :loading="submitting" :disabled="submitting" @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. submitting: false
  60. }
  61. },
  62. methods: {
  63. onCategoryChange(e) {
  64. this.form.category = this.categories[e.detail.value]
  65. },
  66. onPublicChange(e) {
  67. this.form.isPublic = e.detail.value
  68. },
  69. chooseImage() {
  70. uni.chooseImage({
  71. count: 1,
  72. success: (res) => {
  73. this.form.coverImage = res.tempFilePaths[0]
  74. }
  75. })
  76. },
  77. async submit() {
  78. if (this.submitting) return
  79. if (!this.form.name) {
  80. uni.showToast({ title: '请输入任务模板名称', icon: 'none' })
  81. return
  82. }
  83. if (!this.form.cycleDays) {
  84. uni.showToast({ title: '请输入周期天数', icon: 'none' })
  85. return
  86. }
  87. this.submitting = true
  88. uni.showLoading({ title: '保存中...' })
  89. try {
  90. await createPackage({
  91. ...this.form,
  92. isPublic: this.form.isPublic ? 1 : 0
  93. })
  94. uni.showToast({ title: '创建成功' })
  95. setTimeout(() => {
  96. uni.navigateBack()
  97. }, 1500)
  98. } catch (e) {
  99. uni.showToast({ title: '创建失败', icon: 'none' })
  100. } finally {
  101. this.submitting = false
  102. uni.hideLoading()
  103. }
  104. }
  105. }
  106. }
  107. </script>
  108. <style scoped>
  109. .container {
  110. min-height: 100vh;
  111. background: #f5f5f5;
  112. padding: 30rpx;
  113. }
  114. .form {
  115. background: #fff;
  116. border-radius: 16rpx;
  117. padding: 30rpx;
  118. }
  119. .form-item {
  120. margin-bottom: 30rpx;
  121. }
  122. .label {
  123. display: block;
  124. font-size: 28rpx;
  125. color: #333;
  126. margin-bottom: 16rpx;
  127. }
  128. .input, .textarea {
  129. width: 100%;
  130. padding: 20rpx;
  131. background: #f5f5f5;
  132. border-radius: 12rpx;
  133. font-size: 28rpx;
  134. }
  135. .textarea {
  136. height: 160rpx;
  137. }
  138. .picker {
  139. padding: 20rpx;
  140. background: #f5f5f5;
  141. border-radius: 12rpx;
  142. font-size: 28rpx;
  143. color: #333;
  144. }
  145. .cover-upload {
  146. width: 200rpx;
  147. height: 200rpx;
  148. background: #f5f5f5;
  149. border-radius: 12rpx;
  150. display: flex;
  151. justify-content: center;
  152. align-items: center;
  153. }
  154. .upload-icon {
  155. font-size: 60rpx;
  156. color: #ccc;
  157. }
  158. .cover-image {
  159. width: 200rpx;
  160. height: 200rpx;
  161. border-radius: 12rpx;
  162. }
  163. .switch-label {
  164. margin-left: 20rpx;
  165. font-size: 26rpx;
  166. color: #666;
  167. }
  168. .btn-submit {
  169. background: #4CAF50;
  170. color: #fff;
  171. border-radius: 40rpx;
  172. margin-top: 40rpx;
  173. }
  174. </style>