create.vue 4.4 KB

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